edb_info.go 75 KB

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