edb_info.go 76 KB

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