edb_info.go 75 KB

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