edb_info.go 58 KB

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