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