edb_info.go 80 KB

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