edb_info.go 75 KB

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