edb_info.go 74 KB

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