edb_info.go 68 KB

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