edb_info.go 75 KB

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