edb_info.go 73 KB

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