edb_info.go 69 KB

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