edb_info.go 64 KB

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