edb_info.go 87 KB

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