edb_info.go 75 KB

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