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