edb_info.go 63 KB

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