edb_info.go 79 KB

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