edb_info.go 73 KB

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