edb_info.go 74 KB

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