edb_info.go 76 KB

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