edb_info.go 69 KB

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