edb_info.go 66 KB

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