edb_info.go 67 KB

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