edb_info.go 77 KB

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