edb_info.go 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. package data_manage
  2. import (
  3. "eta/eta_api/services/alarm_msg"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfo struct {
  13. EdbInfoId int `orm:"column(edb_info_id);pk"`
  14. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  15. SourceName string `description:"来源名称"`
  16. Source int `description:"来源id"`
  17. EdbCode string `description:"指标编码"`
  18. EdbName string `description:"指标名称"`
  19. EdbNameEn string `description:"英文指标名称"`
  20. EdbNameSource string `description:"指标名称来源"`
  21. Frequency string `description:"频率"`
  22. Unit string `description:"单位"`
  23. UnitEn string `description:"英文单位"`
  24. StartDate string `description:"起始日期"`
  25. EndDate string `description:"终止日期"`
  26. ClassifyId int `description:"分类id"`
  27. SysUserId int
  28. SysUserRealName string
  29. UniqueCode string `description:"指标唯一编码"`
  30. CreateTime time.Time
  31. ModifyTime time.Time
  32. MinValue float64 `description:"指标最小值"`
  33. MaxValue float64 `description:"指标最大值"`
  34. CalculateFormula string `description:"计算公式"`
  35. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  36. Sort int `description:"排序字段"`
  37. LatestDate string `description:"数据最新日期"`
  38. LatestValue float64 `description:"数据最新值"`
  39. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  40. MoveFrequency string `description:"移动频度"`
  41. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  42. ServerUrl string `description:"服务器地址"`
  43. ChartImage string `description:"图表图片"`
  44. Calendar string `description:"公历/农历" orm:"default(公历);"`
  45. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  46. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  47. }
  48. type EdbInfoFullClassify struct {
  49. *EdbInfo
  50. ClassifyList []*EdbClassifyIdItems
  51. }
  52. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  53. o := orm.NewOrmUsingDB("data")
  54. lastId, err = o.Insert(item)
  55. return
  56. }
  57. type EdbInfoItem struct {
  58. EdbInfoId int `description:"指标id"`
  59. EdbName string `description:"指标名称"`
  60. ClassifyId int `description:"指标分类"`
  61. }
  62. // GetEdbInfoAll 用于分类展示
  63. func GetEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  64. o := orm.NewOrmUsingDB("data")
  65. sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
  66. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  67. return
  68. }
  69. // GetPredictEdbInfoAll 用于分类展示(预测指标)
  70. func GetPredictEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  71. o := orm.NewOrmUsingDB("data")
  72. sql := ` SELECT edb_info_id,classify_id,edb_name AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
  73. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  74. return
  75. }
  76. // 用于分类展示
  77. func GetEdbInfoAllList() (items []*EdbInfo, err error) {
  78. o := orm.NewOrmUsingDB("data")
  79. sql := ` SELECT * FROM edb_info `
  80. _, err = o.Raw(sql).QueryRows(&items)
  81. return
  82. }
  83. // 指标检索数据
  84. type EdbInfoSearch struct {
  85. EdbCode string `description:"指标编码"`
  86. StartDate string `description:"起始日期"`
  87. EndDate string `description:"终止日期"`
  88. EdbName string `description:"指标名称"`
  89. Unit string `description:"单位"`
  90. Frequency string `description:"频率"`
  91. DataList []*EdbInfoSearchData
  92. }
  93. type EdbInfoSearchData struct {
  94. DataTime string `description:"数据日期"`
  95. Value float64 `description:"数据"`
  96. }
  97. type EdbInfoSearchResp struct {
  98. SearchItem *EdbInfoSearch `description:"指标分类"`
  99. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  100. ClassifyList []*EdbClassifySimplify
  101. }
  102. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  103. o := orm.NewOrmUsingDB("data")
  104. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  105. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  106. return
  107. }
  108. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  111. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  112. return
  113. }
  114. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  115. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  116. num := len(edbInfoIdList)
  117. if num <= 0 {
  118. return
  119. }
  120. o := orm.NewOrmUsingDB("data")
  121. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  122. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  123. return
  124. }
  125. type AddEdbInfoReq struct {
  126. Source int `description:"来源id"`
  127. EdbCode string `description:"指标编码"`
  128. EdbName string `description:"指标名称"`
  129. Frequency string `description:"频率"`
  130. Unit string `description:"单位"`
  131. ClassifyId int `description:"分类id"`
  132. StartDate string `description:"起始日期"`
  133. EndDate string `description:"终止日期"`
  134. }
  135. func DeleteEdbInfoAndData(edbInfoId, source int) (err error) {
  136. o := orm.NewOrmUsingDB("data")
  137. to, err := o.Begin()
  138. if err != nil {
  139. return
  140. }
  141. defer func() {
  142. if err != nil {
  143. _ = to.Rollback()
  144. } else {
  145. _ = to.Commit()
  146. }
  147. }()
  148. // 删除指标信息
  149. sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
  150. _, err = to.Raw(sql, edbInfoId).Exec()
  151. if err != nil {
  152. return
  153. }
  154. // 删除指标数据
  155. tableName := GetEdbDataTableName(source)
  156. if tableName != "" {
  157. sql = ` DELETE FROM %s WHERE edb_info_id=? `
  158. sql = fmt.Sprintf(sql, tableName)
  159. _, err = to.Raw(sql, edbInfoId).Exec()
  160. if err != nil {
  161. return
  162. }
  163. }
  164. //calculateTableName := GetEdbInfoCalculateTableName(source)
  165. //if calculateTableName != "" {
  166. // sql = ` DELETE FROM %s WHERE edb_info_id=? `
  167. // sql = fmt.Sprintf(sql, calculateTableName)
  168. // _, err = to.Raw(sql, edbInfoId).Exec()
  169. // return
  170. //}
  171. //return
  172. // 删除计算指标的关系
  173. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  174. _, err = to.Raw(sql, edbInfoId).Exec()
  175. // 删除预测指标的配置
  176. sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id=? `
  177. _, err = to.Raw(sql, edbInfoId).Exec()
  178. return
  179. }
  180. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  181. o := orm.NewOrmUsingDB("data")
  182. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  183. if condition != "" {
  184. sql += condition
  185. }
  186. err = o.Raw(sql, pars).QueryRow(&count)
  187. return
  188. }
  189. type EditEdbInfoReq struct {
  190. EdbInfoId int `description:"指标ID"`
  191. EdbName string `description:"指标名称"`
  192. Frequency string `description:"频率"`
  193. Unit string `description:"单位"`
  194. ClassifyId int `description:"分类id"`
  195. CalculateFormula string `description:"计算公式"`
  196. }
  197. type EditEdbEnInfoReq struct {
  198. EdbInfoId int `description:"指标ID"`
  199. EdbNameEn string `description:"英文指标名称"`
  200. UnitEn string `description:"英文单位"`
  201. }
  202. func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
  203. o := orm.NewOrmUsingDB("data")
  204. sql := ` UPDATE edb_info
  205. SET
  206. edb_name =?,
  207. edb_name_source =?,
  208. frequency = ?,
  209. unit = ?,
  210. classify_id = ?,
  211. modify_time = NOW()
  212. WHERE edb_info_id = ?`
  213. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Exec()
  214. return
  215. }
  216. // ModifyEdbEnInfo 修改指标英文信息
  217. func ModifyEdbEnInfo(item *EditEdbEnInfoReq) (err error) {
  218. o := orm.NewOrmUsingDB("data")
  219. sql := ` UPDATE edb_info
  220. SET
  221. edb_name_en =?,
  222. unit_en = ?,
  223. modify_time = NOW()
  224. WHERE edb_info_id = ?`
  225. _, err = o.Raw(sql, item.EdbNameEn, item.UnitEn, item.EdbInfoId).Exec()
  226. return
  227. }
  228. type EdbInfoList struct {
  229. EdbInfoId int `orm:"column(edb_info_id);pk"`
  230. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  231. SourceName string `description:"来源名称"`
  232. Source int `description:"来源id"`
  233. EdbCode string `description:"指标编码"`
  234. EdbNameEn string `description:"英文指标名称"`
  235. EdbName string `description:"指标名称"`
  236. Frequency string `description:"频率"`
  237. FrequencyEn string `description:"英文频率"`
  238. Unit string `description:"单位"`
  239. UnitEn string `description:"英文单位"`
  240. StartDate string `description:"起始日期"`
  241. EndDate string `description:"终止日期"`
  242. LatestDate string `description:"数据最新日期"`
  243. LatestValue float64 `description:"数据最新值"`
  244. ClassifyId int `description:"分类id"`
  245. UniqueCode string `description:"指标唯一编码"`
  246. SysUserId int `description:"创建人id"`
  247. SysUserRealName string `description:"创建人姓名"`
  248. ModifyTime string `description:"最新修改时间"`
  249. CreateTime string `description:"创建时间"`
  250. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  251. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  252. ChartImage string `description:"图表图片"`
  253. RuleType int `description:"预测规则,1:最新,2:固定值"`
  254. FixedValue float64 `description:"固定值"`
  255. DataList []*EdbData `description:"实际指标数据"`
  256. PredictDataList []*EdbData `description:"预测指标数据"`
  257. Button EdbClassifyItemsButton `description:"操作权限"`
  258. IsEnEdb bool `description:"是否展示英文标识"`
  259. DataInsertConfig EdbDataInsertConfigItem `description:"指标数据插入配置"`
  260. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  261. }
  262. type EdbDataInsertConfigItem struct {
  263. Date string `description:"插入的日期"`
  264. RealDate string `description:"实际最晚的日期"`
  265. Value string `description:"插入的值"`
  266. }
  267. type EdbData struct {
  268. EdbDataId int `orm:"column(edb_data_id);pk"`
  269. EdbInfoId int
  270. DataTime string
  271. Value float64
  272. }
  273. type EdbInfoListResp struct {
  274. Paging *paging.PagingItem
  275. Item *EdbInfoList
  276. ClassifyList []*EdbClassifyIdItems
  277. }
  278. func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  279. o := orm.NewOrmUsingDB("data")
  280. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  281. if condition != "" {
  282. sql += condition
  283. }
  284. err = o.Raw(sql, pars).QueryRow(&item)
  285. return
  286. }
  287. func GetEdbDataCountByCondition(condition string, pars []interface{}, source int) (count int, err error) {
  288. o := orm.NewOrmUsingDB("data")
  289. tableName := GetEdbDataTableName(source)
  290. sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  291. sql = fmt.Sprintf(sql, tableName)
  292. if condition != "" {
  293. sql += condition
  294. }
  295. err = o.Raw(sql, pars).QueryRow(&count)
  296. return
  297. }
  298. func GetEdbDataListByCondition(condition string, pars []interface{}, source, pageSize, startSize int) (item []*EdbData, err error) {
  299. o := orm.NewOrmUsingDB("data")
  300. tableName := GetEdbDataTableName(source)
  301. sql := ` SELECT * FROM %s WHERE 1=1 `
  302. sql = fmt.Sprintf(sql, tableName)
  303. if condition != "" {
  304. sql += condition
  305. }
  306. sql += ` ORDER BY data_time DESC `
  307. sql += ` LIMIT ?,? `
  308. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  309. return
  310. }
  311. // GetAllEdbDataListByCondition 根据条件获取所有的数据
  312. func GetAllEdbDataListByCondition(condition string, pars []interface{}, source int) (item []*EdbData, err error) {
  313. o := orm.NewOrmUsingDB("data")
  314. tableName := GetEdbDataTableName(source)
  315. sql := ` SELECT * FROM %s WHERE 1=1 `
  316. sql = fmt.Sprintf(sql, tableName)
  317. if condition != "" {
  318. sql += condition
  319. }
  320. sql += ` ORDER BY data_time DESC `
  321. _, err = o.Raw(sql, pars).QueryRows(&item)
  322. return
  323. }
  324. func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
  325. o := orm.NewOrmUsingDB("data")
  326. sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  327. err = o.Raw(sql).QueryRow(&item)
  328. return
  329. }
  330. func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
  331. o := orm.NewOrmUsingDB("data")
  332. sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
  333. _, err = o.Raw(sql, edbInfoId).Exec()
  334. return
  335. }
  336. type MoveEdbInfoReq struct {
  337. EdbInfoId int `description:"指标ID"`
  338. PrevEdbInfoId int `description:"上一个指标ID"`
  339. NextEdbInfoId int `description:"下一个指标ID"`
  340. ClassifyId int `description:"分类id"`
  341. }
  342. func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
  343. o := orm.NewOrmUsingDB("data")
  344. sql := ` UPDATE edb_info
  345. SET
  346. classify_id = ?
  347. WHERE edb_info_id = ?`
  348. _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
  349. return
  350. }
  351. // GetEdbInfoByName 根据名称获取普通的指标
  352. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  353. o := orm.NewOrmUsingDB("data")
  354. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 0 `
  355. _, err = o.Raw(sql, edbName).QueryRows(&items)
  356. return
  357. }
  358. // GetPredictEdbInfoByName 根据指标名称获取预测指标
  359. func GetPredictEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  360. o := orm.NewOrmUsingDB("data")
  361. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 1 `
  362. _, err = o.Raw(sql, edbName).QueryRows(&items)
  363. return
  364. }
  365. func GetEdbInfoByEnName(edbNameEn string) (items []*EdbInfoList, err error) {
  366. o := orm.NewOrmUsingDB("data")
  367. sql := ` SELECT * FROM edb_info WHERE edb_name_en=? AND edb_info_type = 0 `
  368. _, err = o.Raw(sql, edbNameEn).QueryRows(&items)
  369. return
  370. }
  371. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  372. o := orm.NewOrmUsingDB("data")
  373. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  374. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  375. return
  376. }
  377. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  378. o := orm.NewOrmUsingDB("data")
  379. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  380. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  381. return
  382. }
  383. type ChartEdbInfo struct {
  384. EdbInfoId int `description:"指标id"`
  385. EdbName string `description:"指标名称"`
  386. SourceName string `json:"-"`
  387. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  388. }
  389. func EdbInfoSearchByKeyWord(KeyWord string) (searchList []*ChartEdbInfo, err error) {
  390. o := orm.NewOrmUsingDB("data")
  391. sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  392. if KeyWord != "" {
  393. sql += ` AND (edb_name LIKE '%` + KeyWord + `%' OR edb_code LIKE '%` + KeyWord + `%' ) `
  394. }
  395. sql += ` ORDER BY create_time DESC `
  396. _, err = o.Raw(sql).QueryRows(&searchList)
  397. return
  398. }
  399. type EdbInfoMaxAndMinInfo struct {
  400. MinDate string `description:"最小日期"`
  401. MaxDate string `description:"最大日期"`
  402. MinValue float64 `description:"最小值"`
  403. MaxValue float64 `description:"最大值"`
  404. LatestValue float64 `description:"最新值"`
  405. }
  406. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  407. o := orm.NewOrmUsingDB("data")
  408. sql := ``
  409. tableName := GetEdbDataTableName(source)
  410. sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM %s WHERE edb_code=? `
  411. sql = fmt.Sprintf(sql, tableName)
  412. err = o.Raw(sql, edbCode).QueryRow(&item)
  413. if err != nil {
  414. return
  415. }
  416. var latest_value float64
  417. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  418. sql = fmt.Sprintf(sql, tableName)
  419. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  420. item.LatestValue = latest_value
  421. return
  422. }
  423. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  424. o := orm.NewOrmUsingDB("data")
  425. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
  426. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  427. return
  428. }
  429. func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  430. o := orm.NewOrmUsingDB("data")
  431. sql := ` SELECT * FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  432. if condition != "" {
  433. sql += condition
  434. }
  435. sql += ` ORDER BY create_time DESC `
  436. _, err = o.Raw(sql, pars).QueryRows(&list)
  437. return
  438. }
  439. //order:1升序,其余值为降序
  440. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  441. o := orm.NewOrmUsingDB("data")
  442. sql := ``
  443. tableName := GetEdbDataTableName(source)
  444. sql = ` SELECT * FROM %s WHERE 1=1 `
  445. sql = fmt.Sprintf(sql, tableName)
  446. if condition != "" {
  447. sql += condition
  448. }
  449. if order == 1 {
  450. sql += ` ORDER BY data_time ASC `
  451. } else {
  452. sql += ` ORDER BY data_time DESC `
  453. }
  454. _, err = o.Raw(sql, pars).QueryRows(&item)
  455. return
  456. }
  457. // GetLastEdbData 获取最近的一条指标数据
  458. func GetLastEdbData(condition string, pars []interface{}, source int) (item *EdbInfoSearchData, err error) {
  459. o := orm.NewOrmUsingDB("data")
  460. sql := ``
  461. tableName := GetEdbDataTableName(source)
  462. sql = ` SELECT * FROM %s WHERE 1=1 `
  463. sql = fmt.Sprintf(sql, tableName)
  464. if condition != "" {
  465. sql += condition
  466. }
  467. sql += ` ORDER BY data_time DESC `
  468. err = o.Raw(sql, pars).QueryRow(&item)
  469. return
  470. }
  471. //order:1升序,其余值为降序
  472. func GetEdbDataCount(condition string, pars []interface{}, source int) (count int, err error) {
  473. o := orm.NewOrmUsingDB("data")
  474. sql := ``
  475. tableName := GetEdbDataTableName(source)
  476. sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  477. sql = fmt.Sprintf(sql, tableName)
  478. if condition != "" {
  479. sql += condition
  480. }
  481. err = o.Raw(sql, pars).QueryRow(&count)
  482. return
  483. }
  484. func ModifyCalculateEdbInfo(item *EditEdbInfoReq) (err error) {
  485. o := orm.NewOrmUsingDB("data")
  486. sql := ` UPDATE edb_info
  487. SET
  488. edb_name =?,
  489. edb_name_source =?,
  490. frequency = ?,
  491. unit = ?,
  492. classify_id = ?,
  493. calculate_formula=?,
  494. modify_time = NOW()
  495. WHERE edb_info_id = ?`
  496. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.CalculateFormula, item.EdbInfoId).Exec()
  497. return
  498. }
  499. type AddEdbInfoResp struct {
  500. EdbInfoId int `description:"指标ID"`
  501. UniqueCode string `description:"指标唯一编码"`
  502. ClassifyId int `description:"分类id"`
  503. }
  504. // 根据基础指标获取所有关联指标
  505. func GetNeedRefreshCalculateEdbInfoFromBase(fromEdbInfo int) (list []*EdbInfo, err error) {
  506. o := orm.NewOrmUsingDB("data")
  507. sql := ` SELECT b.*
  508. FROM edb_info_calculate AS a
  509. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  510. WHERE from_edb_info_id=? `
  511. _, err = o.Raw(sql, fromEdbInfo).QueryRows(&list)
  512. return
  513. }
  514. func GetEdbInfoCalculateCount(edbInfoId int) (count int, err error) {
  515. o := orm.NewOrmUsingDB("data")
  516. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate WHERE from_edb_info_id=? `
  517. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  518. return
  519. }
  520. func GetEdbInfoCalculateLjzzyCount(edbInfoId int) (count int, err error) {
  521. o := orm.NewOrmUsingDB("data")
  522. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_ljzzy WHERE from_edb_info_id=? `
  523. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  524. return
  525. }
  526. func GetEdbInfoCalculateTbzCount(edbInfoId int) (count int, err error) {
  527. o := orm.NewOrmUsingDB("data")
  528. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tbz WHERE from_edb_info_id=? `
  529. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  530. return
  531. }
  532. func GetEdbInfoCalculateTczCount(edbInfoId int) (count int, err error) {
  533. o := orm.NewOrmUsingDB("data")
  534. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tcz WHERE from_edb_info_id=? `
  535. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  536. return
  537. }
  538. func GetEdbInfoCalculateNszydpjjsCount(edbInfoId int) (count int, err error) {
  539. o := orm.NewOrmUsingDB("data")
  540. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_nszydpjjs WHERE from_edb_info_id=? `
  541. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  542. return
  543. }
  544. func GetEdbInfoCalculateCountByCondition(source int, condition string, pars []interface{}) (count int, err error) {
  545. o := orm.NewOrmUsingDB("data")
  546. //calculateTableName := GetEdbInfoCalculateTableName(source)
  547. //if calculateTableName == "" {
  548. // err = errors.New("无效的表名")
  549. // return
  550. //}
  551. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
  552. //sql = fmt.Sprintf(sql, calculateTableName)
  553. if condition != "" {
  554. sql += condition
  555. }
  556. err = o.Raw(sql, pars).QueryRow(&count)
  557. return
  558. }
  559. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  560. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  561. o := orm.NewOrmUsingDB("data")
  562. //calculateTableName := GetEdbInfoCalculateTableName(source)
  563. //if calculateTableName == "" {
  564. // err = errors.New("无效的表名")
  565. // return
  566. //}
  567. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  568. //sql = fmt.Sprintf(sql, calculateTableName)
  569. if condition != "" {
  570. sql += condition
  571. }
  572. _, err = o.Raw(sql, pars).QueryRows(&items)
  573. return
  574. }
  575. func GetRefreshEdbInfoFromCalculate(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  576. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  577. if err != nil && err.Error() != utils.ErrNoRow() {
  578. return
  579. }
  580. for _, item := range calculateList {
  581. if item.EdbType == 1 {
  582. baseEdbInfoArr = append(baseEdbInfoArr, item)
  583. } else {
  584. calculateInfoArr = append(calculateInfoArr, item)
  585. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromCalculate(item.EdbInfoId, item.Source)
  586. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  587. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  588. }
  589. }
  590. return
  591. }
  592. // 获取基础指标对应的所有计算指标
  593. func GetEdbInfoAllCalculate(edbInfoId int) (list []*EdbInfo, err error) {
  594. o := orm.NewOrmUsingDB("data")
  595. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  596. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  597. WHERE a.from_edb_info_id=?
  598. GROUP BY a.edb_info_id
  599. ORDER BY a.edb_info_id ASC `
  600. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  601. return
  602. }
  603. // GetEdbInfoAllCalculateByEdbInfoIdList 根据指标id集合 获取基础指标对应的所有计算指标
  604. func GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList []int) (list []*EdbInfo, err error) {
  605. num := len(edbInfoIdList)
  606. if num <= 0 {
  607. return
  608. }
  609. o := orm.NewOrmUsingDB("data")
  610. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  611. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  612. WHERE a.from_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  613. GROUP BY a.edb_info_id
  614. ORDER BY a.edb_info_id ASC `
  615. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  616. return
  617. }
  618. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  619. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  620. if err != nil && err.Error() != utils.ErrNoRow() {
  621. return
  622. }
  623. for _, item := range calculateList {
  624. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  625. continue
  626. }
  627. if item.EdbType == 1 {
  628. baseEdbInfoArr = append(baseEdbInfoArr, item)
  629. } else {
  630. calculateInfoArr = append(calculateInfoArr, item)
  631. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  632. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  633. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  634. }
  635. }
  636. return
  637. }
  638. func ModifyEdbInfoDataStatus(edbInfoId int64, source int, edbCode string) (err error) {
  639. o := orm.NewOrmUsingDB("data")
  640. sql := ``
  641. tableName := GetEdbDataTableName(source)
  642. sql = ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
  643. sql = fmt.Sprintf(sql, tableName)
  644. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  645. return
  646. }
  647. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  648. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  649. o := orm.NewOrmUsingDB("data")
  650. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  651. err = o.Raw(sql, classifyId).QueryRow(&item)
  652. return
  653. }
  654. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  655. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  656. o := orm.NewOrmUsingDB("data")
  657. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=? AND `
  658. if prevEdbInfoId > 0 {
  659. sql += ` ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  660. }
  661. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  662. return
  663. }
  664. // Update 更新指标基础信息
  665. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  666. o := orm.NewOrmUsingDB("data")
  667. _, err = o.Update(edbInfo, cols...)
  668. return
  669. }
  670. type EdbInfoDataResp struct {
  671. EdbInfo *EdbInfo
  672. DataList []*EdbDataList
  673. }
  674. type EdbInfoDataFullClassifyResp struct {
  675. EdbInfo *EdbInfoFullClassify
  676. DataList []*EdbDataList
  677. }
  678. type EdbInfoReplaceReq struct {
  679. OldEdbInfoId int `description:"原指标ID"`
  680. NewEdbInfoId int `description:"替换为指标ID"`
  681. }
  682. /*
  683. EdbInfoReplace 替换指标初始版本(已废弃,废弃时间:2022年01月05日15:33:42)
  684. 1、替换图库中的指标
  685. 2、替换计算指标中的指标,完了计算指标中的指标,全部重新计算
  686. */
  687. func EdbInfoReplace(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
  688. var errmsg string
  689. o := orm.NewOrmUsingDB("data")
  690. to, err := o.Begin()
  691. if err != nil {
  692. return
  693. }
  694. defer func() {
  695. if errmsg != "" {
  696. fmt.Println("errmsg:" + errmsg)
  697. }
  698. }()
  699. //替换图表
  700. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  701. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  702. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  703. if err != nil {
  704. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  705. return
  706. }
  707. replaceChartTotal = len(chartEdbMappingList)
  708. for _, mv := range chartEdbMappingList {
  709. //获取图表所有指标信息
  710. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  711. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  712. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  713. if err != nil {
  714. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  715. return
  716. }
  717. var minData, maxData float64
  718. minData = newEdbInfo.MinValue
  719. maxData = newEdbInfo.MaxValue
  720. for _, cv := range chartEdbList {
  721. if mv.IsAxis == cv.IsAxis {
  722. if minData > cv.MinData {
  723. minData = cv.MinData
  724. }
  725. if maxData < cv.MaxData {
  726. maxData = cv.MaxData
  727. }
  728. }
  729. }
  730. //修改图表关联指标
  731. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  732. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  733. if err != nil {
  734. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  735. return
  736. }
  737. }
  738. //替换计算指标
  739. //获取所有包含的计算指标
  740. mappingList := make([]*EdbInfoCalculateMapping, 0)
  741. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  742. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  743. if err != nil {
  744. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  745. return
  746. }
  747. replaceCalculateTotal = len(mappingList)
  748. //计算指标
  749. for _, mv := range mappingList {
  750. //替换原指标
  751. msql := `UPDATE edb_info_calculate_mapping SET from_edb_info_id=?,from_edb_code=?,from_edb_name=?,from_source=?,from_source_name=? WHERE edb_info_calculate_mapping_id=? `
  752. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  753. if err != nil {
  754. return
  755. }
  756. }
  757. if err != nil && errmsg != "" {
  758. go alarm_msg.SendAlarmMsg("替换指标失败提醒,errmsg:"+errmsg, 3)
  759. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  760. _ = to.Rollback()
  761. } else {
  762. _ = to.Commit()
  763. }
  764. //获取计算指标关联指标
  765. allMappingList := make([]*EdbInfoCalculateMapping, 0)
  766. allMapping := make(map[int]int)
  767. tmpMapping := make(map[int]int)
  768. for _, v := range mappingList {
  769. if _, ok := allMapping[v.EdbInfoId]; !ok {
  770. allMappingList = append(allMappingList, v)
  771. }
  772. newList, _ := GetAllCalculate(v.EdbInfoId, tmpMapping)
  773. for _, nv := range newList {
  774. if _, ok := allMapping[v.EdbInfoId]; !ok {
  775. allMappingList = append(allMappingList, nv)
  776. }
  777. allMapping[v.EdbInfoId] = v.EdbInfoId
  778. }
  779. allMapping[v.EdbInfoId] = v.EdbInfoId
  780. }
  781. for mk, mv := range allMappingList { //原指标关联的所有计算指标
  782. fmt.Println("mk/mv", mk, mv)
  783. //重新计算计算指标
  784. {
  785. edbInfo, err := GetEdbInfoById(mv.EdbInfoId) //计算指标
  786. if err != nil {
  787. if err.Error() == utils.ErrNoRow() {
  788. errmsg = "计算指标已被删除"
  789. return replaceChartTotal, replaceCalculateTotal, err
  790. }
  791. errmsg = "获取计算指标失败:err:" + err.Error()
  792. return replaceChartTotal, replaceCalculateTotal, err
  793. }
  794. fromEdbInfoList, err := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
  795. if err != nil {
  796. errmsg = "获取计算指标失败:err:" + err.Error()
  797. return replaceChartTotal, replaceCalculateTotal, err
  798. }
  799. if len(fromEdbInfoList) <= 0 {
  800. errmsg = "计算指标所依赖指标不存在"
  801. return replaceChartTotal, replaceCalculateTotal, err
  802. }
  803. fromEdbInfoItem := fromEdbInfoList[0]
  804. if fromEdbInfoItem == nil {
  805. return replaceChartTotal, replaceCalculateTotal, err
  806. }
  807. fromEdbInfo, err := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
  808. if err != nil {
  809. errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
  810. return replaceChartTotal, replaceCalculateTotal, err
  811. }
  812. edbCode := edbInfo.EdbCode
  813. uniqueCode := edbInfo.UniqueCode
  814. sourName := edbInfo.SourceName
  815. source := edbInfo.Source
  816. edbInfoId := edbInfo.EdbInfoId
  817. req := new(EdbInfoCalculateBatchSaveReq)
  818. req.EdbInfoId = edbInfoId
  819. req.EdbName = edbInfo.EdbName
  820. req.Frequency = edbInfo.Frequency
  821. req.Unit = edbInfo.Unit
  822. req.ClassifyId = edbInfo.ClassifyId
  823. req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  824. if source == utils.DATA_SOURCE_CALCULATE {
  825. //检验公式
  826. var formulaStr string
  827. var edbInfoIdBytes []string
  828. for _, v := range fromEdbInfoList {
  829. formulaStr += v.FromTag + ","
  830. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  831. }
  832. formulaStr = strings.Trim(formulaStr, ",")
  833. formulaMap := CheckFormula(edbInfo.CalculateFormula)
  834. for _, v := range formulaMap {
  835. if !strings.Contains(formulaStr, v) {
  836. errmsg = "公式错误,请重新填写"
  837. return replaceChartTotal, replaceCalculateTotal, err
  838. }
  839. }
  840. edbInfoList := make([]*EdbInfo, 0)
  841. for _, v := range fromEdbInfoList {
  842. edbInfo, err := GetEdbInfoById(v.FromEdbInfoId)
  843. if err != nil {
  844. if err.Error() == utils.ErrNoRow() {
  845. errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  846. return replaceChartTotal, replaceCalculateTotal, err
  847. }
  848. errmsg = "获取指标失败:Err:" + err.Error()
  849. return replaceChartTotal, replaceCalculateTotal, err
  850. }
  851. edbInfoList = append(edbInfoList, edbInfo)
  852. }
  853. //清除历史数据
  854. err = DeleteCalculateData(edbInfoId)
  855. if err != nil {
  856. errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
  857. return replaceChartTotal, replaceCalculateTotal, err
  858. }
  859. err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  860. if err != nil {
  861. errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
  862. return replaceChartTotal, replaceCalculateTotal, err
  863. }
  864. } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  865. _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  866. } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  867. sourName = "同比值"
  868. edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  869. } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  870. sourName = "同差值"
  871. edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  872. } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  873. sourName = "N数值移动平均计算"
  874. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  875. edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  876. } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  877. sourName = "环比值"
  878. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  879. edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  880. } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  881. sourName = "环差值"
  882. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  883. edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  884. } else if source == utils.DATA_SOURCE_CALCULATE_BP {
  885. sourName = "变频"
  886. fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  887. edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  888. } else {
  889. errmsg = "无效计算方式,source:" + strconv.Itoa(source)
  890. return replaceChartTotal, replaceCalculateTotal, err
  891. }
  892. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(source, edbCode)
  893. if err != nil && err.Error() != utils.ErrNoRow() {
  894. errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  895. return replaceChartTotal, replaceCalculateTotal, err
  896. }
  897. if maxAndMinItem != nil {
  898. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  899. }
  900. }
  901. }
  902. return
  903. }
  904. // EdbInfoReplaceV2 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  905. //func EdbInfoReplaceV2(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (mappingList,replaceChartTotal, replaceCalculateTotal int, err error) {
  906. // defer func() {
  907. // if err != nil {
  908. // fmt.Println("ERR:", err.Error())
  909. // }
  910. // }()
  911. //
  912. // // 替换关联表中的source_edb_info_id
  913. // mappingList, replaceChartTotal, replaceCalculateTotal, err := replaceChartEdb(oldEdbInfo, newEdbInfo)
  914. // if err != nil {
  915. // return
  916. // }
  917. // err = replaceRelationEdbInfo(mappingList, oldEdbInfo, newEdbInfo, sysAdminId, sysAdminRealName)
  918. // return
  919. //}
  920. // ReplaceChartEdb 替换图表中的指标
  921. func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
  922. var errmsg string
  923. relationEdbInfoIdList = make([]int, 0) // 关联的指标id
  924. logMsg := `` // 记录替换的日志
  925. o := orm.NewOrmUsingDB("data")
  926. to, err := o.Begin()
  927. if err != nil {
  928. return
  929. }
  930. defer func() {
  931. if err != nil {
  932. _ = to.Rollback()
  933. } else {
  934. _ = to.Commit()
  935. if logMsg != `` {
  936. utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  937. }
  938. }
  939. if errmsg != "" {
  940. fmt.Println("errmsg:" + errmsg)
  941. }
  942. if err != nil && errmsg != "" {
  943. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  944. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  945. }
  946. }()
  947. //替换图表
  948. {
  949. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  950. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  951. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  952. if err != nil {
  953. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  954. return
  955. }
  956. replaceChartTotal = len(chartEdbMappingList)
  957. if len(chartEdbMappingList) > 0 {
  958. chartInfoIdList := make([]string, 0)
  959. for _, mv := range chartEdbMappingList {
  960. //获取图表所有指标信息
  961. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  962. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  963. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  964. if err != nil {
  965. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  966. return
  967. }
  968. var minData, maxData float64
  969. minData = newEdbInfo.MinValue
  970. maxData = newEdbInfo.MaxValue
  971. for _, cv := range chartEdbList {
  972. if mv.IsAxis == cv.IsAxis {
  973. if minData > cv.MinData {
  974. minData = cv.MinData
  975. }
  976. if maxData < cv.MaxData {
  977. maxData = cv.MaxData
  978. }
  979. }
  980. }
  981. //修改图表关联指标
  982. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  983. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  984. if err != nil {
  985. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  986. return
  987. }
  988. chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
  989. }
  990. logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
  991. }
  992. }
  993. //替换计算指标
  994. {
  995. //获取所有包含的计算指标
  996. mappingList := make([]*EdbInfoCalculateMapping, 0)
  997. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  998. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  999. if err != nil {
  1000. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  1001. return
  1002. }
  1003. replaceCalculateTotal = len(mappingList)
  1004. //计算指标
  1005. if len(mappingList) > 0 {
  1006. edbInfoIdList := make([]string, 0)
  1007. for _, mv := range mappingList {
  1008. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  1009. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  1010. continue
  1011. }
  1012. //替换原指标
  1013. msql := `UPDATE edb_info_calculate_mapping SET from_edb_info_id=?,from_edb_code=?,from_edb_name=?,from_source=?,from_source_name=? WHERE edb_info_calculate_mapping_id=? `
  1014. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  1015. if err != nil {
  1016. return
  1017. }
  1018. relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
  1019. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
  1020. }
  1021. logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1022. }
  1023. }
  1024. // 替换预测指标中的来源指标
  1025. {
  1026. //获取所有包含的计算指标
  1027. predictEdbConfList := make([]*PredictEdbConf, 0)
  1028. msql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? GROUP BY predict_edb_info_id `
  1029. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&predictEdbConfList)
  1030. if err != nil {
  1031. errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
  1032. return
  1033. }
  1034. replaceCalculateTotal += len(predictEdbConfList)
  1035. //替换预测指标的来源指标
  1036. msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE source_edb_info_id=? `
  1037. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Exec()
  1038. if err != nil {
  1039. return
  1040. }
  1041. if len(predictEdbConfList) > 0 {
  1042. edbInfoIdList := make([]string, 0)
  1043. for _, v := range predictEdbConfList {
  1044. relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
  1045. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
  1046. }
  1047. logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1048. }
  1049. }
  1050. return
  1051. }
  1052. // replaceRelationEdbInfo 替换关联指标
  1053. func replaceRelationEdbInfo(mappingList []*EdbInfoCalculateMapping, oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (err error) {
  1054. errmsg := ``
  1055. defer func() {
  1056. if errmsg != "" {
  1057. fmt.Println("errmsg:" + errmsg)
  1058. }
  1059. if err != nil && errmsg != "" {
  1060. go alarm_msg.SendAlarmMsg("替换关联指标失败提醒,errmsg:"+errmsg, 3)
  1061. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  1062. }
  1063. }()
  1064. //获取计算指标关联指标
  1065. allMappingList := make([]*EdbInfoCalculateMapping, 0)
  1066. allMapping := make(map[int]int)
  1067. tmpMapping := make(map[int]int)
  1068. for _, v := range mappingList {
  1069. if _, ok := allMapping[v.EdbInfoId]; !ok {
  1070. allMappingList = append(allMappingList, v)
  1071. }
  1072. newList, _ := GetAllCalculateV2(v.EdbInfoId, newEdbInfo.EdbInfoId, tmpMapping)
  1073. for _, nv := range newList {
  1074. if _, ok := allMapping[v.EdbInfoId]; !ok {
  1075. allMappingList = append(allMappingList, nv)
  1076. }
  1077. allMapping[v.EdbInfoId] = v.EdbInfoId
  1078. }
  1079. allMapping[v.EdbInfoId] = v.EdbInfoId
  1080. }
  1081. for mk, mv := range allMappingList { //原指标关联的所有计算指标
  1082. fmt.Println("mk/mv", mk, mv)
  1083. //重新计算计算指标
  1084. {
  1085. edbInfo, tmpErr := GetEdbInfoById(mv.EdbInfoId) //计算指标
  1086. err = tmpErr
  1087. if err != nil {
  1088. if err.Error() == utils.ErrNoRow() {
  1089. errmsg = "计算指标已被删除"
  1090. } else {
  1091. errmsg = "获取计算指标失败:err:" + err.Error()
  1092. }
  1093. return
  1094. }
  1095. fromEdbInfoList, tmpErr := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
  1096. err = tmpErr
  1097. if err != nil {
  1098. errmsg = "获取计算指标失败:err:" + err.Error()
  1099. return
  1100. }
  1101. if len(fromEdbInfoList) <= 0 {
  1102. errmsg = "计算指标所依赖指标不存在"
  1103. err = fmt.Errorf("计算指标所依赖指标不存在")
  1104. return
  1105. }
  1106. fromEdbInfoItem := fromEdbInfoList[0]
  1107. if fromEdbInfoItem == nil {
  1108. return
  1109. }
  1110. fromEdbInfo, tmpErr := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
  1111. err = tmpErr
  1112. if err != nil {
  1113. errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
  1114. return
  1115. }
  1116. edbCode := edbInfo.EdbCode
  1117. uniqueCode := edbInfo.UniqueCode
  1118. sourName := edbInfo.SourceName
  1119. source := edbInfo.Source
  1120. edbInfoId := edbInfo.EdbInfoId
  1121. req := new(EdbInfoCalculateBatchSaveReq)
  1122. req.EdbInfoId = edbInfoId
  1123. req.EdbName = edbInfo.EdbName
  1124. req.Frequency = edbInfo.Frequency
  1125. req.Unit = edbInfo.Unit
  1126. req.ClassifyId = edbInfo.ClassifyId
  1127. req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1128. if source == utils.DATA_SOURCE_CALCULATE {
  1129. //检验公式
  1130. var formulaStr string
  1131. var edbInfoIdBytes []string
  1132. for _, v := range fromEdbInfoList {
  1133. formulaStr += v.FromTag + ","
  1134. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  1135. }
  1136. formulaStr = strings.Trim(formulaStr, ",")
  1137. formulaMap := CheckFormula(edbInfo.CalculateFormula)
  1138. for _, v := range formulaMap {
  1139. if !strings.Contains(formulaStr, v) {
  1140. errmsg = "公式错误,请重新填写"
  1141. err = fmt.Errorf("公式错误,请重新填写")
  1142. return
  1143. }
  1144. }
  1145. edbInfoList := make([]*EdbInfo, 0)
  1146. for _, v := range fromEdbInfoList {
  1147. edbInfo, tmpErr := GetEdbInfoById(v.FromEdbInfoId)
  1148. err = tmpErr
  1149. if err != nil {
  1150. if err.Error() == utils.ErrNoRow() {
  1151. errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  1152. } else {
  1153. errmsg = "获取指标失败:Err:" + err.Error()
  1154. }
  1155. return
  1156. }
  1157. edbInfoList = append(edbInfoList, edbInfo)
  1158. }
  1159. //清除历史数据
  1160. err = DeleteCalculateData(edbInfoId)
  1161. if err != nil {
  1162. errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
  1163. return
  1164. }
  1165. err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  1166. if err != nil {
  1167. errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
  1168. return
  1169. }
  1170. } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  1171. _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1172. } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  1173. sourName = "同比值"
  1174. edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1175. } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  1176. sourName = "同差值"
  1177. edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1178. } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  1179. sourName = "N数值移动平均计算"
  1180. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1181. edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1182. } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  1183. sourName = "环比值"
  1184. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1185. edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1186. } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  1187. sourName = "环差值"
  1188. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1189. edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1190. } else if source == utils.DATA_SOURCE_CALCULATE_BP {
  1191. sourName = "变频"
  1192. fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1193. edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1194. } else if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
  1195. req2 := &EdbInfoCalculateBatchEditReq{
  1196. EdbName: edbInfo.EdbName,
  1197. Frequency: edbInfo.Frequency,
  1198. Unit: edbInfo.Unit,
  1199. ClassifyId: edbInfo.ClassifyId,
  1200. Formula: edbInfo.CalculateFormula,
  1201. EdbInfoId: edbInfo.EdbInfoId,
  1202. FromEdbInfoId: fromEdbInfoList[0].FromEdbInfoId,
  1203. Source: edbInfo.Source,
  1204. MoveType: edbInfo.MoveType,
  1205. MoveFrequency: edbInfo.MoveFrequency,
  1206. }
  1207. sourName = "时间移位"
  1208. edbInfoId, err = EditCalculateTimeShift(req2, fromEdbInfo, edbCode, edbInfo)
  1209. } else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ {
  1210. edbInfoIdArr := make([]EdbInfoFromTag, 0)
  1211. //A指标
  1212. aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
  1213. for _, v := range fromEdbInfoList {
  1214. if v.FromTag == "A" {
  1215. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1216. aFromEdbInfoId = newEdbInfo.EdbInfoId
  1217. } else {
  1218. aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1219. }
  1220. }
  1221. if v.FromTag == "B" {
  1222. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1223. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1224. EdbInfoId: newEdbInfo.EdbInfoId,
  1225. FromTag: v.FromTag,
  1226. MoveValue: v.MoveValue,
  1227. })
  1228. } else {
  1229. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1230. EdbInfoId: v.FromEdbInfoId,
  1231. FromTag: v.FromTag,
  1232. MoveValue: v.MoveValue,
  1233. })
  1234. }
  1235. }
  1236. }
  1237. req2 := &EdbInfoCalculateBatchEditReq{
  1238. EdbName: edbInfo.EdbName,
  1239. Frequency: edbInfo.Frequency,
  1240. Unit: edbInfo.Unit,
  1241. ClassifyId: edbInfo.ClassifyId,
  1242. Formula: edbInfo.CalculateFormula,
  1243. EdbInfoId: edbInfo.EdbInfoId,
  1244. FromEdbInfoId: aFromEdbInfoId,
  1245. Source: edbInfo.Source,
  1246. MoveType: edbInfo.MoveType,
  1247. MoveFrequency: edbInfo.MoveFrequency,
  1248. EdbInfoIdArr: edbInfoIdArr,
  1249. }
  1250. sourName = "直接拼接"
  1251. if len(req2.EdbInfoIdArr) != 1 {
  1252. errmsg = "请传入拼接日期之后的指标"
  1253. err = fmt.Errorf("请传入拼接日期之后的指标")
  1254. return
  1255. }
  1256. secondEdbInfoReq := req2.EdbInfoIdArr[0]
  1257. secondEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1258. err = tmpErr
  1259. if err != nil {
  1260. errmsg = "获取拼接日期之后的指标信息失败:Err:" + err.Error()
  1261. err = fmt.Errorf("获取拼接日期之后的指标信息失败:Err:" + err.Error())
  1262. return
  1263. }
  1264. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1265. errmsg = "两个指标不允许为同一个"
  1266. err = fmt.Errorf("两个指标不允许为同一个")
  1267. return
  1268. }
  1269. tmpEdbInfoId, tmpErr := EditCalculateZjpj(req2, edbInfo, fromEdbInfo, secondEdbInfo)
  1270. err = tmpErr
  1271. edbInfoId = tmpEdbInfoId
  1272. } else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ { //累计值同比拼接
  1273. edbInfoIdArr := make([]EdbInfoFromTag, 0)
  1274. //A指标
  1275. aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
  1276. for _, v := range fromEdbInfoList {
  1277. if v.FromTag == "A" {
  1278. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1279. aFromEdbInfoId = newEdbInfo.EdbInfoId
  1280. } else {
  1281. aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1282. }
  1283. }
  1284. if v.FromTag == "B" {
  1285. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1286. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1287. EdbInfoId: newEdbInfo.EdbInfoId,
  1288. FromTag: v.FromTag,
  1289. MoveValue: v.MoveValue,
  1290. })
  1291. } else {
  1292. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1293. EdbInfoId: v.FromEdbInfoId,
  1294. FromTag: v.FromTag,
  1295. MoveValue: v.MoveValue,
  1296. })
  1297. }
  1298. }
  1299. }
  1300. req2 := &EdbInfoCalculateBatchEditReq{
  1301. EdbName: edbInfo.EdbName,
  1302. Frequency: edbInfo.Frequency,
  1303. Unit: edbInfo.Unit,
  1304. ClassifyId: edbInfo.ClassifyId,
  1305. Formula: edbInfo.CalculateFormula,
  1306. EdbInfoId: edbInfo.EdbInfoId,
  1307. FromEdbInfoId: aFromEdbInfoId,
  1308. Source: edbInfo.Source,
  1309. MoveType: edbInfo.MoveType,
  1310. MoveFrequency: edbInfo.MoveFrequency,
  1311. EdbInfoIdArr: edbInfoIdArr,
  1312. }
  1313. sourName = "累计值同比拼接"
  1314. if fromEdbInfo.Frequency != "月度" {
  1315. errmsg = "累计值同比拼接,待拼接指标只能筛选月度指标"
  1316. err = fmt.Errorf("累计值同比拼接,待拼接指标只能筛选月度指标")
  1317. return
  1318. }
  1319. if len(req2.EdbInfoIdArr) != 1 {
  1320. errmsg = "请传入同比值指标"
  1321. err = fmt.Errorf("请传入同比值指标")
  1322. return
  1323. }
  1324. secondEdbInfoReq := req2.EdbInfoIdArr[0]
  1325. tbzEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1326. err = tmpErr
  1327. if err != nil {
  1328. errmsg = "获取同比值指标信息失败:Err:" + err.Error()
  1329. err = fmt.Errorf("获取同比值指标信息失败:Err:" + err.Error())
  1330. return
  1331. }
  1332. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  1333. errmsg = "指标必须是传入同比值指标类型"
  1334. err = fmt.Errorf("指标必须是传入同比值指标类型")
  1335. return
  1336. }
  1337. if tbzEdbInfo.Frequency != "月度" {
  1338. errmsg = "同比值指标只能筛选月度指标"
  1339. err = fmt.Errorf("同比值指标只能筛选月度指标")
  1340. return
  1341. }
  1342. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  1343. errmsg = "两个指标不允许为同一个"
  1344. err = fmt.Errorf("两个指标不允许为同一个")
  1345. return
  1346. }
  1347. tmpEdbInfoId, tmpErr := EditCalculateLjztbpj(req2, edbInfo, fromEdbInfo, tbzEdbInfo)
  1348. err = tmpErr
  1349. edbInfoId = tmpEdbInfoId
  1350. } else if source == utils.DATA_SOURCE_PYTHON {
  1351. //python代码运算的过滤
  1352. continue
  1353. } else {
  1354. errmsg = "无效计算方式,source:" + strconv.Itoa(source)
  1355. err = fmt.Errorf("无效计算方式,source:" + strconv.Itoa(source))
  1356. return
  1357. }
  1358. maxAndMinItem, tmpErr := GetEdbInfoMaxAndMinInfo(source, edbCode)
  1359. err = tmpErr
  1360. if err != nil && err.Error() != utils.ErrNoRow() {
  1361. errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  1362. err = fmt.Errorf("生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error())
  1363. return
  1364. }
  1365. if maxAndMinItem != nil {
  1366. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  1367. }
  1368. }
  1369. }
  1370. return
  1371. }
  1372. type EdbInfoView struct {
  1373. EdbInfoId int `orm:"column(edb_info_id);pk"`
  1374. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  1375. SourceName string `description:"来源名称"`
  1376. Source int `description:"来源id"`
  1377. EdbCode string `description:"指标编码"`
  1378. EdbName string `description:"指标名称"`
  1379. EdbNameSource string `description:"指标名称来源"`
  1380. Frequency string `description:"频率"`
  1381. Unit string `description:"单位"`
  1382. StartDate string `description:"起始日期"`
  1383. EndDate string `description:"终止日期"`
  1384. ClassifyId int `description:"分类id"`
  1385. SysUserId int
  1386. SysUserRealName string
  1387. UniqueCode string `description:"指标唯一编码"`
  1388. CreateTime string
  1389. ModifyTime string
  1390. MinValue float64 `description:"指标最小值"`
  1391. MaxValue float64 `description:"指标最大值"`
  1392. CalculateFormula string `description:"计算公式"`
  1393. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  1394. Sort int `description:"排序字段"`
  1395. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  1396. LatestDate string `description:"数据最新日期"`
  1397. LatestValue float64 `description:"数据最新值"`
  1398. }
  1399. // 获取指标的所有计算指标,以及计算指标所依赖计算指标
  1400. func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1401. o := orm.NewOrmUsingDB("data")
  1402. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  1403. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1404. if err != nil {
  1405. return
  1406. }
  1407. return
  1408. }
  1409. func GetAllCalculate(edbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1410. calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
  1411. if err != nil && err.Error() != utils.ErrNoRow() {
  1412. return
  1413. }
  1414. for _, item := range calculateList {
  1415. _, ok := edbInfoMap[item.EdbInfoId]
  1416. if !ok {
  1417. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1418. mappingList = append(mappingList, item)
  1419. newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId, edbInfoMap)
  1420. mappingList = append(mappingList, newCalculateInfoArr...)
  1421. }
  1422. }
  1423. return
  1424. }
  1425. // GetAllCalculateByEdbInfoIdV2 获取计算指标的所有所依赖的指标(基础指标+计算指标)
  1426. func GetAllCalculateByEdbInfoIdV2(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1427. o := orm.NewOrmUsingDB("data")
  1428. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_info_id=? GROUP BY from_edb_info_id `
  1429. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1430. if err != nil {
  1431. return
  1432. }
  1433. return
  1434. }
  1435. // GetAllCalculateV2 获取所有的指标(如果下层指标包含了待替换指标,那么就不加入)
  1436. func GetAllCalculateV2(oldEdbInfoId, replaceEdbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1437. calculateList, err := GetAllCalculateByEdbInfoId(oldEdbInfoId)
  1438. if err != nil && err.Error() != utils.ErrNoRow() {
  1439. return
  1440. }
  1441. tmpMappingList := make([]*EdbInfoCalculateMapping, 0)
  1442. for _, item := range calculateList {
  1443. //如果计算指标里面存在即将替换的指标id,那么退出当前循环,进入下一循环
  1444. if item.EdbInfoId == replaceEdbInfoId {
  1445. err = fmt.Errorf("指标重复1")
  1446. return
  1447. }
  1448. //获取该指标的依赖指标列表,如果依赖指标列表中存在即将替换的指标,那么过滤该指标
  1449. childCalculateList, tmpErr := GetAllCalculateByEdbInfoIdV2(item.EdbInfoId)
  1450. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1451. err = tmpErr
  1452. return
  1453. }
  1454. for _, childCalculate := range childCalculateList {
  1455. if childCalculate.FromEdbInfoId == replaceEdbInfoId {
  1456. err = fmt.Errorf("指标重复2")
  1457. return
  1458. }
  1459. }
  1460. _, ok := edbInfoMap[item.EdbInfoId]
  1461. if !ok {
  1462. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1463. newCalculateInfoArr, tmpErr := GetAllCalculateV2(item.EdbInfoId, replaceEdbInfoId, edbInfoMap)
  1464. if tmpErr != nil {
  1465. err = tmpErr
  1466. continue
  1467. } else {
  1468. mappingList = append(mappingList, item)
  1469. tmpMappingList = append(tmpMappingList, newCalculateInfoArr...)
  1470. }
  1471. }
  1472. }
  1473. mappingList = append(mappingList, tmpMappingList...)
  1474. return
  1475. }
  1476. // EdbInfoFilterDataResp 搜索指标列表数据返回
  1477. type EdbInfoFilterDataResp struct {
  1478. Paging *paging.PagingItem
  1479. List []*EdbInfoList
  1480. }
  1481. // GetEdbInfoFilterList 获取指指标列表数据接口
  1482. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  1483. o := orm.NewOrmUsingDB("data")
  1484. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  1485. err = o.Raw(sql, pars).QueryRow(&total)
  1486. if err != nil {
  1487. return
  1488. }
  1489. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1490. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  1491. return
  1492. }
  1493. // SetEdbInfoImageReq 设置指标图片
  1494. type SetEdbInfoImageReq struct {
  1495. EdbInfoId int `description:"指标ID"`
  1496. ImageUrl string `description:"指标图片地址"`
  1497. }
  1498. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  1499. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  1500. o := orm.NewOrmUsingDB("data")
  1501. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1502. if condition != "" {
  1503. sql += condition
  1504. }
  1505. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  1506. err = o.Raw(sql, pars).QueryRow(&item)
  1507. return
  1508. }
  1509. // GetNextEdbInfo 根据分类id获取下一个 指标
  1510. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  1511. o := orm.NewOrmUsingDB("data")
  1512. sql := ` SELECT b.* FROM edb_classify AS a
  1513. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  1514. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  1515. AND a.classify_type=? AND b.edb_info_type=?
  1516. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  1517. LIMIT 1 `
  1518. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).QueryRow(&item)
  1519. return
  1520. }
  1521. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  1522. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1523. num := len(edbCodeList)
  1524. if num <= 0 {
  1525. return
  1526. }
  1527. o := orm.NewOrmUsingDB("data")
  1528. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  1529. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  1530. return
  1531. }
  1532. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  1533. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  1534. o := orm.NewOrmUsingDB("data")
  1535. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  1536. if condition != "" {
  1537. sql += condition
  1538. }
  1539. err = o.Raw(sql, pars).QueryRow(&count)
  1540. return
  1541. }
  1542. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  1543. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfoList, err error) {
  1544. o := orm.NewOrmUsingDB("data")
  1545. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1546. if condition != "" {
  1547. sql += condition
  1548. }
  1549. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1550. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1551. return
  1552. }
  1553. // GetEdbInfoListV2ByCondition 根据条件获取指标列表数据(EdbInfo原始数据结构)
  1554. func GetEdbInfoListV2ByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfo, err error) {
  1555. o := orm.NewOrmUsingDB("data")
  1556. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1557. if condition != "" {
  1558. sql += condition
  1559. }
  1560. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1561. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1562. return
  1563. }
  1564. // GetEdbInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  1565. func GetEdbInfoListGroupByUserId(edbIdList []string) (items []*EdbInfo, err error) {
  1566. num := len(edbIdList)
  1567. if num <= 0 {
  1568. return
  1569. }
  1570. o := orm.NewOrmUsingDB("data")
  1571. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  1572. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1573. return
  1574. }
  1575. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  1576. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1577. num := len(edbIdList)
  1578. if num <= 0 {
  1579. return
  1580. }
  1581. o := orm.NewOrmUsingDB("data")
  1582. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1583. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  1584. return
  1585. }
  1586. // MoveEdbChartList 图表/指标信息
  1587. type MoveEdbChartList struct {
  1588. DataId string `description:"指标/图表唯一id"`
  1589. Code string `description:"指标code"`
  1590. Name string `description:"指标/图表名称"`
  1591. ClassifyName string `description:"分类名称"`
  1592. CreateUserId int `description:"创建人id"`
  1593. CreateUserName string `description:"创建人名称"`
  1594. }
  1595. // MoveEdbChartListResp 更改创建人返回数据
  1596. type MoveEdbChartListResp struct {
  1597. Paging *paging.PagingItem
  1598. List []MoveEdbChartList
  1599. }
  1600. // MoveEdbChartReq 转移指标/图表的请求
  1601. type MoveEdbChartReq struct {
  1602. Source int `description:"来源id"`
  1603. DataIdList []string `description:"指标/图表唯一id列表"`
  1604. NewUserId int `description:"新的创建人id"`
  1605. }
  1606. type EdbChartClassifyResp struct {
  1607. List []*EdbChartClassify
  1608. }
  1609. type EdbChartClassify struct {
  1610. ClassifyId int
  1611. ClassifyName string
  1612. ParentId int
  1613. Child []*EdbChartClassify
  1614. }
  1615. // EdbInfoDataSeasonalResp 指标季节性数据返回
  1616. type EdbInfoDataSeasonalResp struct {
  1617. EdbInfo *EdbInfo
  1618. DataList interface{}
  1619. }
  1620. // SetEdbDataInsertConfigReq 设置插入规则的请求
  1621. type SetEdbDataInsertConfigReq struct {
  1622. EdbInfoId int `description:"指标ID"`
  1623. Date string `description:"日期"`
  1624. Value float64 `description:"值"`
  1625. }
  1626. // GetEdbInfoByClassifyId 用于分类展示
  1627. func GetEdbInfoByClassifyId(classifyId, edbInfoType, adminId int) (items []*EdbClassifyItems, err error) {
  1628. o := orm.NewOrmUsingDB("data")
  1629. sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE classify_id = ? AND edb_info_type = ?`
  1630. pars := []interface{}{classifyId, edbInfoType}
  1631. // 如果筛选了用户id
  1632. if adminId > 0 {
  1633. sql += ` AND sys_user_id = ? `
  1634. pars = append(pars, adminId)
  1635. }
  1636. sql += ` order by sort asc,edb_info_id asc `
  1637. _, err = o.Raw(sql, pars).QueryRows(&items)
  1638. return
  1639. }
  1640. type EdbAndClassify struct {
  1641. EdbInfoId int `description:"指标id"`
  1642. EdbCode string `description:"指标code"`
  1643. UniqueCode string `description:"唯一code"`
  1644. EdbName string `description:"指标名称"`
  1645. FirstClassifyId int `description:"第1级的分类id"`
  1646. FirstClassifyName string `description:"第1级的分类名称"`
  1647. SecondClassifyId int `description:"第2级的分类id"`
  1648. SecondClassifyName string `description:"第2级的分类名称"`
  1649. }
  1650. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1651. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1652. num := len(edbIdList)
  1653. if num <= 0 {
  1654. return
  1655. }
  1656. o := orm.NewOrmUsingDB("data")
  1657. sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,b.classify_id as second_classify_id,b.classify_name as second_classify_name,c.classify_id as first_classify_id,c.classify_name as first_classify_name FROM edb_info AS a
  1658. JOIN edb_classify b on a.classify_id=b.classify_id
  1659. JOIN edb_classify c on b.parent_id=c.classify_id
  1660. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1661. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1662. return
  1663. }
  1664. // TraceEdbInfoResp 指标追溯数据返回
  1665. type TraceEdbInfoResp struct {
  1666. EdbInfoId int `description:"指标id"`
  1667. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  1668. EdbName string `description:"指标名称"`
  1669. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  1670. RuleTitle string `description:"指标规则"`
  1671. UniqueCode string `description:"唯一编码"`
  1672. ClassifyId int `description:"分类ID"`
  1673. Child []TraceEdbInfoResp `description:"下级来源"`
  1674. }
  1675. // BeforeAndAfterDateDataResp 前后几期数据
  1676. type BeforeAndAfterDateDataResp struct {
  1677. List []*EdbDataList `description:"list"`
  1678. Date string `description:"实际日期"`
  1679. }
  1680. // GetEdbInfoAdminList
  1681. func GetEdbInfoAdminList(edbType int) (list []int, err error) {
  1682. o := orm.NewOrmUsingDB("data")
  1683. sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
  1684. _, err = o.Raw(sql, edbType).QueryRows(&list)
  1685. return
  1686. }