edb_info.go 63 KB

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