edb_info.go 74 KB

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