edb_info.go 67 KB

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