edb_info.go 74 KB

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