edb_info.go 74 KB

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