edb_info.go 62 KB

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