edb_info.go 79 KB

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