edb_info.go 74 KB

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