edb_info.go 75 KB

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