edb_info.go 62 KB

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