edb_info.go 74 KB

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