edb_info.go 74 KB

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