edb_info.go 75 KB

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