edb_info.go 68 KB

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