edb_info.go 62 KB

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