edb_info.go 78 KB

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