edb_info.go 68 KB

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