edb_info.go 74 KB

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