edb_info.go 74 KB

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