edb_info.go 61 KB

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