edb_info.go 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  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=? AND `
  659. if prevEdbInfoId > 0 {
  660. sql += ` ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  661. }
  662. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  663. return
  664. }
  665. // Update 更新指标基础信息
  666. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  667. o := orm.NewOrmUsingDB("data")
  668. _, err = o.Update(edbInfo, cols...)
  669. return
  670. }
  671. type EdbInfoDataResp struct {
  672. EdbInfo *EdbInfo
  673. DataList []*EdbDataList
  674. }
  675. type EdbInfoDataFullClassifyResp struct {
  676. EdbInfo *EdbInfoFullClassify
  677. DataList []*EdbDataList
  678. }
  679. type EdbInfoReplaceReq struct {
  680. OldEdbInfoId int `description:"原指标ID"`
  681. NewEdbInfoId int `description:"替换为指标ID"`
  682. }
  683. /*
  684. EdbInfoReplace 替换指标初始版本(已废弃,废弃时间:2022年01月05日15:33:42)
  685. 1、替换图库中的指标
  686. 2、替换计算指标中的指标,完了计算指标中的指标,全部重新计算
  687. */
  688. func EdbInfoReplace(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
  689. var errmsg string
  690. o := orm.NewOrmUsingDB("data")
  691. to, err := o.Begin()
  692. if err != nil {
  693. return
  694. }
  695. defer func() {
  696. if errmsg != "" {
  697. fmt.Println("errmsg:" + errmsg)
  698. }
  699. }()
  700. //替换图表
  701. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  702. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  703. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  704. if err != nil {
  705. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  706. return
  707. }
  708. replaceChartTotal = len(chartEdbMappingList)
  709. for _, mv := range chartEdbMappingList {
  710. //获取图表所有指标信息
  711. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  712. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  713. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  714. if err != nil {
  715. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  716. return
  717. }
  718. var minData, maxData float64
  719. minData = newEdbInfo.MinValue
  720. maxData = newEdbInfo.MaxValue
  721. for _, cv := range chartEdbList {
  722. if mv.IsAxis == cv.IsAxis {
  723. if minData > cv.MinData {
  724. minData = cv.MinData
  725. }
  726. if maxData < cv.MaxData {
  727. maxData = cv.MaxData
  728. }
  729. }
  730. }
  731. //修改图表关联指标
  732. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  733. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  734. if err != nil {
  735. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  736. return
  737. }
  738. }
  739. //替换计算指标
  740. //获取所有包含的计算指标
  741. mappingList := make([]*EdbInfoCalculateMapping, 0)
  742. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  743. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  744. if err != nil {
  745. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  746. return
  747. }
  748. replaceCalculateTotal = len(mappingList)
  749. //计算指标
  750. for _, mv := range mappingList {
  751. //替换原指标
  752. 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=? `
  753. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  754. if err != nil {
  755. return
  756. }
  757. }
  758. if err != nil && errmsg != "" {
  759. go alarm_msg.SendAlarmMsg("替换指标失败提醒,errmsg:"+errmsg, 3)
  760. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  761. _ = to.Rollback()
  762. } else {
  763. _ = to.Commit()
  764. }
  765. //获取计算指标关联指标
  766. allMappingList := make([]*EdbInfoCalculateMapping, 0)
  767. allMapping := make(map[int]int)
  768. tmpMapping := make(map[int]int)
  769. for _, v := range mappingList {
  770. if _, ok := allMapping[v.EdbInfoId]; !ok {
  771. allMappingList = append(allMappingList, v)
  772. }
  773. newList, _ := GetAllCalculate(v.EdbInfoId, tmpMapping)
  774. for _, nv := range newList {
  775. if _, ok := allMapping[v.EdbInfoId]; !ok {
  776. allMappingList = append(allMappingList, nv)
  777. }
  778. allMapping[v.EdbInfoId] = v.EdbInfoId
  779. }
  780. allMapping[v.EdbInfoId] = v.EdbInfoId
  781. }
  782. for mk, mv := range allMappingList { //原指标关联的所有计算指标
  783. fmt.Println("mk/mv", mk, mv)
  784. //重新计算计算指标
  785. {
  786. edbInfo, err := GetEdbInfoById(mv.EdbInfoId) //计算指标
  787. if err != nil {
  788. if err.Error() == utils.ErrNoRow() {
  789. errmsg = "计算指标已被删除"
  790. return replaceChartTotal, replaceCalculateTotal, err
  791. }
  792. errmsg = "获取计算指标失败:err:" + err.Error()
  793. return replaceChartTotal, replaceCalculateTotal, err
  794. }
  795. fromEdbInfoList, err := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
  796. if err != nil {
  797. errmsg = "获取计算指标失败:err:" + err.Error()
  798. return replaceChartTotal, replaceCalculateTotal, err
  799. }
  800. if len(fromEdbInfoList) <= 0 {
  801. errmsg = "计算指标所依赖指标不存在"
  802. return replaceChartTotal, replaceCalculateTotal, err
  803. }
  804. fromEdbInfoItem := fromEdbInfoList[0]
  805. if fromEdbInfoItem == nil {
  806. return replaceChartTotal, replaceCalculateTotal, err
  807. }
  808. fromEdbInfo, err := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
  809. if err != nil {
  810. errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
  811. return replaceChartTotal, replaceCalculateTotal, err
  812. }
  813. edbCode := edbInfo.EdbCode
  814. uniqueCode := edbInfo.UniqueCode
  815. sourName := edbInfo.SourceName
  816. source := edbInfo.Source
  817. edbInfoId := edbInfo.EdbInfoId
  818. req := new(EdbInfoCalculateBatchSaveReq)
  819. req.EdbInfoId = edbInfoId
  820. req.EdbName = edbInfo.EdbName
  821. req.Frequency = edbInfo.Frequency
  822. req.Unit = edbInfo.Unit
  823. req.ClassifyId = edbInfo.ClassifyId
  824. req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  825. if source == utils.DATA_SOURCE_CALCULATE {
  826. //检验公式
  827. var formulaStr string
  828. var edbInfoIdBytes []string
  829. for _, v := range fromEdbInfoList {
  830. formulaStr += v.FromTag + ","
  831. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  832. }
  833. formulaStr = strings.Trim(formulaStr, ",")
  834. formulaMap := CheckFormula(edbInfo.CalculateFormula)
  835. for _, v := range formulaMap {
  836. if !strings.Contains(formulaStr, v) {
  837. errmsg = "公式错误,请重新填写"
  838. return replaceChartTotal, replaceCalculateTotal, err
  839. }
  840. }
  841. edbInfoList := make([]*EdbInfo, 0)
  842. for _, v := range fromEdbInfoList {
  843. edbInfo, err := GetEdbInfoById(v.FromEdbInfoId)
  844. if err != nil {
  845. if err.Error() == utils.ErrNoRow() {
  846. errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  847. return replaceChartTotal, replaceCalculateTotal, err
  848. }
  849. errmsg = "获取指标失败:Err:" + err.Error()
  850. return replaceChartTotal, replaceCalculateTotal, err
  851. }
  852. edbInfoList = append(edbInfoList, edbInfo)
  853. }
  854. //清除历史数据
  855. err = DeleteCalculateData(edbInfoId)
  856. if err != nil {
  857. errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
  858. return replaceChartTotal, replaceCalculateTotal, err
  859. }
  860. err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  861. if err != nil {
  862. errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
  863. return replaceChartTotal, replaceCalculateTotal, err
  864. }
  865. } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  866. _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  867. } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  868. sourName = "同比值"
  869. edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  870. } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  871. sourName = "同差值"
  872. edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  873. } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  874. sourName = "N数值移动平均计算"
  875. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  876. edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  877. } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  878. sourName = "环比值"
  879. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  880. edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  881. } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  882. sourName = "环差值"
  883. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  884. edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  885. } else if source == utils.DATA_SOURCE_CALCULATE_BP {
  886. sourName = "变频"
  887. fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  888. edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  889. } else {
  890. errmsg = "无效计算方式,source:" + strconv.Itoa(source)
  891. return replaceChartTotal, replaceCalculateTotal, err
  892. }
  893. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(source, edbCode)
  894. if err != nil && err.Error() != utils.ErrNoRow() {
  895. errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  896. return replaceChartTotal, replaceCalculateTotal, err
  897. }
  898. if maxAndMinItem != nil {
  899. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  900. }
  901. }
  902. }
  903. return
  904. }
  905. // EdbInfoReplaceV2 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  906. //func EdbInfoReplaceV2(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (mappingList,replaceChartTotal, replaceCalculateTotal int, err error) {
  907. // defer func() {
  908. // if err != nil {
  909. // fmt.Println("ERR:", err.Error())
  910. // }
  911. // }()
  912. //
  913. // // 替换关联表中的source_edb_info_id
  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) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
  923. var errmsg string
  924. relationEdbInfoIdList = make([]int, 0) // 关联的指标id
  925. logMsg := `` // 记录替换的日志
  926. o := orm.NewOrmUsingDB("data")
  927. to, err := o.Begin()
  928. if err != nil {
  929. return
  930. }
  931. defer func() {
  932. if err != nil {
  933. _ = to.Rollback()
  934. } else {
  935. _ = to.Commit()
  936. if logMsg != `` {
  937. utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  938. }
  939. }
  940. if errmsg != "" {
  941. fmt.Println("errmsg:" + errmsg)
  942. }
  943. if err != nil && errmsg != "" {
  944. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  945. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  946. }
  947. }()
  948. //替换图表
  949. {
  950. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  951. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  952. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  953. if err != nil {
  954. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  955. return
  956. }
  957. replaceChartTotal = len(chartEdbMappingList)
  958. if len(chartEdbMappingList) > 0 {
  959. chartInfoIdList := make([]string, 0)
  960. for _, mv := range chartEdbMappingList {
  961. //获取图表所有指标信息
  962. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  963. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  964. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  965. if err != nil {
  966. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  967. return
  968. }
  969. var minData, maxData float64
  970. minData = newEdbInfo.MinValue
  971. maxData = newEdbInfo.MaxValue
  972. for _, cv := range chartEdbList {
  973. if mv.IsAxis == cv.IsAxis {
  974. if minData > cv.MinData {
  975. minData = cv.MinData
  976. }
  977. if maxData < cv.MaxData {
  978. maxData = cv.MaxData
  979. }
  980. }
  981. }
  982. //修改图表关联指标
  983. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  984. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  985. if err != nil {
  986. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  987. return
  988. }
  989. chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
  990. }
  991. logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
  992. }
  993. }
  994. //替换计算指标
  995. {
  996. //获取所有包含的计算指标
  997. mappingList := make([]*EdbInfoCalculateMapping, 0)
  998. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  999. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  1000. if err != nil {
  1001. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  1002. return
  1003. }
  1004. replaceCalculateTotal = len(mappingList)
  1005. //计算指标
  1006. if len(mappingList) > 0 {
  1007. edbInfoIdList := make([]string, 0)
  1008. for _, mv := range mappingList {
  1009. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  1010. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  1011. continue
  1012. }
  1013. //替换原指标
  1014. 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=? `
  1015. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  1016. if err != nil {
  1017. return
  1018. }
  1019. relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
  1020. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
  1021. }
  1022. logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1023. }
  1024. }
  1025. // 替换预测指标中的来源指标
  1026. {
  1027. //获取所有包含的计算指标
  1028. predictEdbConfList := make([]*PredictEdbConf, 0)
  1029. msql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? GROUP BY predict_edb_info_id `
  1030. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&predictEdbConfList)
  1031. if err != nil {
  1032. errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
  1033. return
  1034. }
  1035. replaceCalculateTotal += len(predictEdbConfList)
  1036. //替换预测指标的来源指标
  1037. msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE source_edb_info_id=? `
  1038. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Exec()
  1039. if err != nil {
  1040. return
  1041. }
  1042. if len(predictEdbConfList) > 0 {
  1043. edbInfoIdList := make([]string, 0)
  1044. for _, v := range predictEdbConfList {
  1045. relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
  1046. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
  1047. }
  1048. logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1049. }
  1050. }
  1051. return
  1052. }
  1053. // replaceRelationEdbInfo 替换关联指标
  1054. func replaceRelationEdbInfo(mappingList []*EdbInfoCalculateMapping, oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (err error) {
  1055. errmsg := ``
  1056. defer func() {
  1057. if errmsg != "" {
  1058. fmt.Println("errmsg:" + errmsg)
  1059. }
  1060. if err != nil && errmsg != "" {
  1061. go alarm_msg.SendAlarmMsg("替换关联指标失败提醒,errmsg:"+errmsg, 3)
  1062. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  1063. }
  1064. }()
  1065. //获取计算指标关联指标
  1066. allMappingList := make([]*EdbInfoCalculateMapping, 0)
  1067. allMapping := make(map[int]int)
  1068. tmpMapping := make(map[int]int)
  1069. for _, v := range mappingList {
  1070. if _, ok := allMapping[v.EdbInfoId]; !ok {
  1071. allMappingList = append(allMappingList, v)
  1072. }
  1073. newList, _ := GetAllCalculateV2(v.EdbInfoId, newEdbInfo.EdbInfoId, tmpMapping)
  1074. for _, nv := range newList {
  1075. if _, ok := allMapping[v.EdbInfoId]; !ok {
  1076. allMappingList = append(allMappingList, nv)
  1077. }
  1078. allMapping[v.EdbInfoId] = v.EdbInfoId
  1079. }
  1080. allMapping[v.EdbInfoId] = v.EdbInfoId
  1081. }
  1082. for mk, mv := range allMappingList { //原指标关联的所有计算指标
  1083. fmt.Println("mk/mv", mk, mv)
  1084. //重新计算计算指标
  1085. {
  1086. edbInfo, tmpErr := GetEdbInfoById(mv.EdbInfoId) //计算指标
  1087. err = tmpErr
  1088. if err != nil {
  1089. if err.Error() == utils.ErrNoRow() {
  1090. errmsg = "计算指标已被删除"
  1091. } else {
  1092. errmsg = "获取计算指标失败:err:" + err.Error()
  1093. }
  1094. return
  1095. }
  1096. fromEdbInfoList, tmpErr := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
  1097. err = tmpErr
  1098. if err != nil {
  1099. errmsg = "获取计算指标失败:err:" + err.Error()
  1100. return
  1101. }
  1102. if len(fromEdbInfoList) <= 0 {
  1103. errmsg = "计算指标所依赖指标不存在"
  1104. err = fmt.Errorf("计算指标所依赖指标不存在")
  1105. return
  1106. }
  1107. fromEdbInfoItem := fromEdbInfoList[0]
  1108. if fromEdbInfoItem == nil {
  1109. return
  1110. }
  1111. fromEdbInfo, tmpErr := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
  1112. err = tmpErr
  1113. if err != nil {
  1114. errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
  1115. return
  1116. }
  1117. edbCode := edbInfo.EdbCode
  1118. uniqueCode := edbInfo.UniqueCode
  1119. sourName := edbInfo.SourceName
  1120. source := edbInfo.Source
  1121. edbInfoId := edbInfo.EdbInfoId
  1122. req := new(EdbInfoCalculateBatchSaveReq)
  1123. req.EdbInfoId = edbInfoId
  1124. req.EdbName = edbInfo.EdbName
  1125. req.Frequency = edbInfo.Frequency
  1126. req.Unit = edbInfo.Unit
  1127. req.ClassifyId = edbInfo.ClassifyId
  1128. req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1129. if source == utils.DATA_SOURCE_CALCULATE {
  1130. //检验公式
  1131. var formulaStr string
  1132. var edbInfoIdBytes []string
  1133. for _, v := range fromEdbInfoList {
  1134. formulaStr += v.FromTag + ","
  1135. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  1136. }
  1137. formulaStr = strings.Trim(formulaStr, ",")
  1138. formulaMap := CheckFormula(edbInfo.CalculateFormula)
  1139. for _, v := range formulaMap {
  1140. if !strings.Contains(formulaStr, v) {
  1141. errmsg = "公式错误,请重新填写"
  1142. err = fmt.Errorf("公式错误,请重新填写")
  1143. return
  1144. }
  1145. }
  1146. edbInfoList := make([]*EdbInfo, 0)
  1147. for _, v := range fromEdbInfoList {
  1148. edbInfo, tmpErr := GetEdbInfoById(v.FromEdbInfoId)
  1149. err = tmpErr
  1150. if err != nil {
  1151. if err.Error() == utils.ErrNoRow() {
  1152. errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  1153. } else {
  1154. errmsg = "获取指标失败:Err:" + err.Error()
  1155. }
  1156. return
  1157. }
  1158. edbInfoList = append(edbInfoList, edbInfo)
  1159. }
  1160. //清除历史数据
  1161. err = DeleteCalculateData(edbInfoId)
  1162. if err != nil {
  1163. errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
  1164. return
  1165. }
  1166. err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  1167. if err != nil {
  1168. errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
  1169. return
  1170. }
  1171. } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  1172. _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1173. } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  1174. sourName = "同比值"
  1175. edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1176. } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  1177. sourName = "同差值"
  1178. edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1179. } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  1180. sourName = "N数值移动平均计算"
  1181. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1182. edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1183. } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  1184. sourName = "环比值"
  1185. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1186. edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1187. } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  1188. sourName = "环差值"
  1189. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1190. edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1191. } else if source == utils.DATA_SOURCE_CALCULATE_BP {
  1192. sourName = "变频"
  1193. fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1194. edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1195. } else if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
  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: fromEdbInfoList[0].FromEdbInfoId,
  1204. Source: edbInfo.Source,
  1205. MoveType: edbInfo.MoveType,
  1206. MoveFrequency: edbInfo.MoveFrequency,
  1207. }
  1208. sourName = "时间移位"
  1209. edbInfoId, err = EditCalculateTimeShift(req2, fromEdbInfo, edbCode, edbInfo)
  1210. } else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ {
  1211. edbInfoIdArr := make([]EdbInfoFromTag, 0)
  1212. //A指标
  1213. aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
  1214. for _, v := range fromEdbInfoList {
  1215. if v.FromTag == "A" {
  1216. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1217. aFromEdbInfoId = newEdbInfo.EdbInfoId
  1218. } else {
  1219. aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1220. }
  1221. }
  1222. if v.FromTag == "B" {
  1223. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1224. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1225. EdbInfoId: newEdbInfo.EdbInfoId,
  1226. FromTag: v.FromTag,
  1227. MoveValue: v.MoveValue,
  1228. })
  1229. } else {
  1230. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1231. EdbInfoId: v.FromEdbInfoId,
  1232. FromTag: v.FromTag,
  1233. MoveValue: v.MoveValue,
  1234. })
  1235. }
  1236. }
  1237. }
  1238. req2 := &EdbInfoCalculateBatchEditReq{
  1239. EdbName: edbInfo.EdbName,
  1240. Frequency: edbInfo.Frequency,
  1241. Unit: edbInfo.Unit,
  1242. ClassifyId: edbInfo.ClassifyId,
  1243. Formula: edbInfo.CalculateFormula,
  1244. EdbInfoId: edbInfo.EdbInfoId,
  1245. FromEdbInfoId: aFromEdbInfoId,
  1246. Source: edbInfo.Source,
  1247. MoveType: edbInfo.MoveType,
  1248. MoveFrequency: edbInfo.MoveFrequency,
  1249. EdbInfoIdArr: edbInfoIdArr,
  1250. }
  1251. sourName = "直接拼接"
  1252. if len(req2.EdbInfoIdArr) != 1 {
  1253. errmsg = "请传入拼接日期之后的指标"
  1254. err = fmt.Errorf("请传入拼接日期之后的指标")
  1255. return
  1256. }
  1257. secondEdbInfoReq := req2.EdbInfoIdArr[0]
  1258. secondEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1259. err = tmpErr
  1260. if err != nil {
  1261. errmsg = "获取拼接日期之后的指标信息失败:Err:" + err.Error()
  1262. err = fmt.Errorf("获取拼接日期之后的指标信息失败:Err:" + err.Error())
  1263. return
  1264. }
  1265. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1266. errmsg = "两个指标不允许为同一个"
  1267. err = fmt.Errorf("两个指标不允许为同一个")
  1268. return
  1269. }
  1270. tmpEdbInfoId, tmpErr := EditCalculateZjpj(req2, edbInfo, fromEdbInfo, secondEdbInfo)
  1271. err = tmpErr
  1272. edbInfoId = tmpEdbInfoId
  1273. } else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ { //累计值同比拼接
  1274. edbInfoIdArr := make([]EdbInfoFromTag, 0)
  1275. //A指标
  1276. aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
  1277. for _, v := range fromEdbInfoList {
  1278. if v.FromTag == "A" {
  1279. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1280. aFromEdbInfoId = newEdbInfo.EdbInfoId
  1281. } else {
  1282. aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1283. }
  1284. }
  1285. if v.FromTag == "B" {
  1286. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1287. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1288. EdbInfoId: newEdbInfo.EdbInfoId,
  1289. FromTag: v.FromTag,
  1290. MoveValue: v.MoveValue,
  1291. })
  1292. } else {
  1293. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1294. EdbInfoId: v.FromEdbInfoId,
  1295. FromTag: v.FromTag,
  1296. MoveValue: v.MoveValue,
  1297. })
  1298. }
  1299. }
  1300. }
  1301. req2 := &EdbInfoCalculateBatchEditReq{
  1302. EdbName: edbInfo.EdbName,
  1303. Frequency: edbInfo.Frequency,
  1304. Unit: edbInfo.Unit,
  1305. ClassifyId: edbInfo.ClassifyId,
  1306. Formula: edbInfo.CalculateFormula,
  1307. EdbInfoId: edbInfo.EdbInfoId,
  1308. FromEdbInfoId: aFromEdbInfoId,
  1309. Source: edbInfo.Source,
  1310. MoveType: edbInfo.MoveType,
  1311. MoveFrequency: edbInfo.MoveFrequency,
  1312. EdbInfoIdArr: edbInfoIdArr,
  1313. }
  1314. sourName = "累计值同比拼接"
  1315. if fromEdbInfo.Frequency != "月度" {
  1316. errmsg = "累计值同比拼接,待拼接指标只能筛选月度指标"
  1317. err = fmt.Errorf("累计值同比拼接,待拼接指标只能筛选月度指标")
  1318. return
  1319. }
  1320. if len(req2.EdbInfoIdArr) != 1 {
  1321. errmsg = "请传入同比值指标"
  1322. err = fmt.Errorf("请传入同比值指标")
  1323. return
  1324. }
  1325. secondEdbInfoReq := req2.EdbInfoIdArr[0]
  1326. tbzEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1327. err = tmpErr
  1328. if err != nil {
  1329. errmsg = "获取同比值指标信息失败:Err:" + err.Error()
  1330. err = fmt.Errorf("获取同比值指标信息失败:Err:" + err.Error())
  1331. return
  1332. }
  1333. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  1334. errmsg = "指标必须是传入同比值指标类型"
  1335. err = fmt.Errorf("指标必须是传入同比值指标类型")
  1336. return
  1337. }
  1338. if tbzEdbInfo.Frequency != "月度" {
  1339. errmsg = "同比值指标只能筛选月度指标"
  1340. err = fmt.Errorf("同比值指标只能筛选月度指标")
  1341. return
  1342. }
  1343. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  1344. errmsg = "两个指标不允许为同一个"
  1345. err = fmt.Errorf("两个指标不允许为同一个")
  1346. return
  1347. }
  1348. tmpEdbInfoId, tmpErr := EditCalculateLjztbpj(req2, edbInfo, fromEdbInfo, tbzEdbInfo)
  1349. err = tmpErr
  1350. edbInfoId = tmpEdbInfoId
  1351. } else if source == utils.DATA_SOURCE_PYTHON {
  1352. //python代码运算的过滤
  1353. continue
  1354. } else {
  1355. errmsg = "无效计算方式,source:" + strconv.Itoa(source)
  1356. err = fmt.Errorf("无效计算方式,source:" + strconv.Itoa(source))
  1357. return
  1358. }
  1359. maxAndMinItem, tmpErr := GetEdbInfoMaxAndMinInfo(source, edbCode)
  1360. err = tmpErr
  1361. if err != nil && err.Error() != utils.ErrNoRow() {
  1362. errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  1363. err = fmt.Errorf("生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error())
  1364. return
  1365. }
  1366. if maxAndMinItem != nil {
  1367. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  1368. }
  1369. }
  1370. }
  1371. return
  1372. }
  1373. type EdbInfoView struct {
  1374. EdbInfoId int `orm:"column(edb_info_id);pk"`
  1375. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  1376. SourceName string `description:"来源名称"`
  1377. Source int `description:"来源id"`
  1378. EdbCode string `description:"指标编码"`
  1379. EdbName string `description:"指标名称"`
  1380. EdbNameSource string `description:"指标名称来源"`
  1381. Frequency string `description:"频率"`
  1382. Unit string `description:"单位"`
  1383. StartDate string `description:"起始日期"`
  1384. EndDate string `description:"终止日期"`
  1385. ClassifyId int `description:"分类id"`
  1386. SysUserId int
  1387. SysUserRealName string
  1388. UniqueCode string `description:"指标唯一编码"`
  1389. CreateTime string
  1390. ModifyTime string
  1391. MinValue float64 `description:"指标最小值"`
  1392. MaxValue float64 `description:"指标最大值"`
  1393. CalculateFormula string `description:"计算公式"`
  1394. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  1395. Sort int `description:"排序字段"`
  1396. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  1397. LatestDate string `description:"数据最新日期"`
  1398. LatestValue float64 `description:"数据最新值"`
  1399. }
  1400. // 获取指标的所有计算指标,以及计算指标所依赖计算指标
  1401. func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1402. o := orm.NewOrmUsingDB("data")
  1403. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  1404. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1405. if err != nil {
  1406. return
  1407. }
  1408. return
  1409. }
  1410. func GetAllCalculate(edbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1411. calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
  1412. if err != nil && err.Error() != utils.ErrNoRow() {
  1413. return
  1414. }
  1415. for _, item := range calculateList {
  1416. _, ok := edbInfoMap[item.EdbInfoId]
  1417. if !ok {
  1418. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1419. mappingList = append(mappingList, item)
  1420. newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId, edbInfoMap)
  1421. mappingList = append(mappingList, newCalculateInfoArr...)
  1422. }
  1423. }
  1424. return
  1425. }
  1426. // GetAllCalculateByEdbInfoIdV2 获取计算指标的所有所依赖的指标(基础指标+计算指标)
  1427. func GetAllCalculateByEdbInfoIdV2(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1428. o := orm.NewOrmUsingDB("data")
  1429. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_info_id=? GROUP BY from_edb_info_id `
  1430. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1431. if err != nil {
  1432. return
  1433. }
  1434. return
  1435. }
  1436. // GetAllCalculateV2 获取所有的指标(如果下层指标包含了待替换指标,那么就不加入)
  1437. func GetAllCalculateV2(oldEdbInfoId, replaceEdbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1438. calculateList, err := GetAllCalculateByEdbInfoId(oldEdbInfoId)
  1439. if err != nil && err.Error() != utils.ErrNoRow() {
  1440. return
  1441. }
  1442. tmpMappingList := make([]*EdbInfoCalculateMapping, 0)
  1443. for _, item := range calculateList {
  1444. //如果计算指标里面存在即将替换的指标id,那么退出当前循环,进入下一循环
  1445. if item.EdbInfoId == replaceEdbInfoId {
  1446. err = fmt.Errorf("指标重复1")
  1447. return
  1448. }
  1449. //获取该指标的依赖指标列表,如果依赖指标列表中存在即将替换的指标,那么过滤该指标
  1450. childCalculateList, tmpErr := GetAllCalculateByEdbInfoIdV2(item.EdbInfoId)
  1451. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1452. err = tmpErr
  1453. return
  1454. }
  1455. for _, childCalculate := range childCalculateList {
  1456. if childCalculate.FromEdbInfoId == replaceEdbInfoId {
  1457. err = fmt.Errorf("指标重复2")
  1458. return
  1459. }
  1460. }
  1461. _, ok := edbInfoMap[item.EdbInfoId]
  1462. if !ok {
  1463. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1464. newCalculateInfoArr, tmpErr := GetAllCalculateV2(item.EdbInfoId, replaceEdbInfoId, edbInfoMap)
  1465. if tmpErr != nil {
  1466. err = tmpErr
  1467. continue
  1468. } else {
  1469. mappingList = append(mappingList, item)
  1470. tmpMappingList = append(tmpMappingList, newCalculateInfoArr...)
  1471. }
  1472. }
  1473. }
  1474. mappingList = append(mappingList, tmpMappingList...)
  1475. return
  1476. }
  1477. // EdbInfoFilterDataResp 搜索指标列表数据返回
  1478. type EdbInfoFilterDataResp struct {
  1479. Paging *paging.PagingItem
  1480. List []*EdbInfoList
  1481. }
  1482. // GetEdbInfoFilterList 获取指指标列表数据接口
  1483. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  1484. o := orm.NewOrmUsingDB("data")
  1485. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  1486. err = o.Raw(sql, pars).QueryRow(&total)
  1487. if err != nil {
  1488. return
  1489. }
  1490. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1491. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  1492. return
  1493. }
  1494. // SetEdbInfoImageReq 设置指标图片
  1495. type SetEdbInfoImageReq struct {
  1496. EdbInfoId int `description:"指标ID"`
  1497. ImageUrl string `description:"指标图片地址"`
  1498. }
  1499. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  1500. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  1501. o := orm.NewOrmUsingDB("data")
  1502. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1503. if condition != "" {
  1504. sql += condition
  1505. }
  1506. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  1507. err = o.Raw(sql, pars).QueryRow(&item)
  1508. return
  1509. }
  1510. // GetNextEdbInfo 根据分类id获取下一个 指标
  1511. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  1512. o := orm.NewOrmUsingDB("data")
  1513. sql := ` SELECT b.* FROM edb_classify AS a
  1514. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  1515. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  1516. AND a.classify_type=? AND b.edb_info_type=?
  1517. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  1518. LIMIT 1 `
  1519. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).QueryRow(&item)
  1520. return
  1521. }
  1522. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  1523. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1524. num := len(edbCodeList)
  1525. if num <= 0 {
  1526. return
  1527. }
  1528. o := orm.NewOrmUsingDB("data")
  1529. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  1530. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  1531. return
  1532. }
  1533. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  1534. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  1535. o := orm.NewOrmUsingDB("data")
  1536. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  1537. if condition != "" {
  1538. sql += condition
  1539. }
  1540. err = o.Raw(sql, pars).QueryRow(&count)
  1541. return
  1542. }
  1543. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  1544. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfoList, err error) {
  1545. o := orm.NewOrmUsingDB("data")
  1546. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1547. if condition != "" {
  1548. sql += condition
  1549. }
  1550. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1551. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1552. return
  1553. }
  1554. // GetEdbInfoListV2ByCondition 根据条件获取指标列表数据(EdbInfo原始数据结构)
  1555. func GetEdbInfoListV2ByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfo, err error) {
  1556. o := orm.NewOrmUsingDB("data")
  1557. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1558. if condition != "" {
  1559. sql += condition
  1560. }
  1561. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1562. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1563. return
  1564. }
  1565. // GetEdbInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  1566. func GetEdbInfoListGroupByUserId(edbIdList []string) (items []*EdbInfo, err error) {
  1567. num := len(edbIdList)
  1568. if num <= 0 {
  1569. return
  1570. }
  1571. o := orm.NewOrmUsingDB("data")
  1572. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  1573. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1574. return
  1575. }
  1576. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  1577. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1578. num := len(edbIdList)
  1579. if num <= 0 {
  1580. return
  1581. }
  1582. o := orm.NewOrmUsingDB("data")
  1583. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1584. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  1585. return
  1586. }
  1587. // MoveEdbChartList 图表/指标信息
  1588. type MoveEdbChartList struct {
  1589. DataId string `description:"指标/图表唯一id"`
  1590. Code string `description:"指标code"`
  1591. Name string `description:"指标/图表名称"`
  1592. ClassifyName string `description:"分类名称"`
  1593. CreateUserId int `description:"创建人id"`
  1594. CreateUserName string `description:"创建人名称"`
  1595. }
  1596. // MoveEdbChartListResp 更改创建人返回数据
  1597. type MoveEdbChartListResp struct {
  1598. Paging *paging.PagingItem
  1599. List []MoveEdbChartList
  1600. }
  1601. // MoveEdbChartReq 转移指标/图表的请求
  1602. type MoveEdbChartReq struct {
  1603. Source int `description:"来源id"`
  1604. DataIdList []string `description:"指标/图表唯一id列表"`
  1605. NewUserId int `description:"新的创建人id"`
  1606. }
  1607. type EdbChartClassifyResp struct {
  1608. List []*EdbChartClassify
  1609. }
  1610. type EdbChartClassify struct {
  1611. ClassifyId int
  1612. ClassifyName string
  1613. ParentId int
  1614. Child []*EdbChartClassify
  1615. }
  1616. // EdbInfoDataSeasonalResp 指标季节性数据返回
  1617. type EdbInfoDataSeasonalResp struct {
  1618. EdbInfo *EdbInfo
  1619. DataList interface{}
  1620. }
  1621. // SetEdbDataInsertConfigReq 设置插入规则的请求
  1622. type SetEdbDataInsertConfigReq struct {
  1623. EdbInfoId int `description:"指标ID"`
  1624. Date string `description:"日期"`
  1625. Value float64 `description:"值"`
  1626. }
  1627. // GetEdbInfoByClassifyId 用于分类展示
  1628. func GetEdbInfoByClassifyId(classifyId, edbInfoType, adminId int) (items []*EdbClassifyItems, err error) {
  1629. o := orm.NewOrmUsingDB("data")
  1630. 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 = ?`
  1631. pars := []interface{}{classifyId, edbInfoType}
  1632. // 如果筛选了用户id
  1633. if adminId > 0 {
  1634. sql += ` AND sys_user_id = ? `
  1635. pars = append(pars, adminId)
  1636. }
  1637. sql += ` order by sort asc,edb_info_id asc `
  1638. _, err = o.Raw(sql, pars).QueryRows(&items)
  1639. return
  1640. }
  1641. type EdbAndClassify struct {
  1642. EdbInfoId int `description:"指标id"`
  1643. EdbCode string `description:"指标code"`
  1644. UniqueCode string `description:"唯一code"`
  1645. EdbName string `description:"指标名称"`
  1646. FirstClassifyId int `description:"第1级的分类id"`
  1647. FirstClassifyName string `description:"第1级的分类名称"`
  1648. SecondClassifyId int `description:"第2级的分类id"`
  1649. SecondClassifyName string `description:"第2级的分类名称"`
  1650. }
  1651. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1652. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1653. num := len(edbIdList)
  1654. if num <= 0 {
  1655. return
  1656. }
  1657. o := orm.NewOrmUsingDB("data")
  1658. 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
  1659. JOIN edb_classify b on a.classify_id=b.classify_id
  1660. JOIN edb_classify c on b.parent_id=c.classify_id
  1661. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1662. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1663. return
  1664. }
  1665. // TraceEdbInfoResp 指标追溯数据返回
  1666. type TraceEdbInfoResp struct {
  1667. EdbInfoId int `description:"指标id"`
  1668. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  1669. EdbName string `description:"指标名称"`
  1670. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  1671. RuleTitle string `description:"指标规则"`
  1672. UniqueCode string `description:"唯一编码"`
  1673. ClassifyId int `description:"分类ID"`
  1674. Child []TraceEdbInfoResp `description:"下级来源"`
  1675. }
  1676. // BeforeAndAfterDateDataResp 前后几期数据
  1677. type BeforeAndAfterDateDataResp struct {
  1678. List []*EdbDataList `description:"list"`
  1679. Date string `description:"实际日期"`
  1680. }
  1681. // GetEdbInfoAdminList
  1682. func GetEdbInfoAdminList(edbType int) (list []int, err error) {
  1683. o := orm.NewOrmUsingDB("data")
  1684. sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
  1685. _, err = o.Raw(sql, edbType).QueryRows(&list)
  1686. return
  1687. }