edb_info.go 75 KB

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