edb_info.go 74 KB

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