edb_info.go 76 KB

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