edb_info.go 58 KB

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