edb_info.go 68 KB

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