edb_info.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/services/alarm_msg"
  4. "eta/eta_mobile/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "time"
  9. )
  10. type EdbInfo struct {
  11. EdbInfoId int `orm:"column(edb_info_id);pk"`
  12. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  13. SourceName string `description:"来源名称"`
  14. Source int `description:"来源id"`
  15. EdbCode string `description:"指标编码"`
  16. EdbName string `description:"指标名称"`
  17. EdbNameEn string `description:"英文指标名称"`
  18. EdbNameSource string `description:"指标名称来源"`
  19. Frequency string `description:"频率"`
  20. Unit string `description:"单位"`
  21. UnitEn string `description:"英文单位"`
  22. StartDate string `description:"起始日期"`
  23. EndDate string `description:"终止日期"`
  24. ClassifyId int `description:"分类id"`
  25. SysUserId int
  26. SysUserRealName string
  27. UniqueCode string `description:"指标唯一编码"`
  28. CreateTime time.Time
  29. ModifyTime time.Time
  30. MinValue float64 `description:"指标最小值"`
  31. MaxValue float64 `description:"指标最大值"`
  32. CalculateFormula string `description:"计算公式"`
  33. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  34. Sort int `description:"排序字段"`
  35. LatestDate string `description:"数据最新日期"`
  36. LatestValue float64 `description:"数据最新值"`
  37. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  38. MoveFrequency string `description:"移动频度"`
  39. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  40. ServerUrl string `description:"服务器地址"`
  41. ChartImage string `description:"图表图片"`
  42. Calendar string `description:"公历/农历" orm:"default(公历);"`
  43. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  44. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  45. }
  46. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  47. o := orm.NewOrmUsingDB("data")
  48. lastId, err = o.Insert(item)
  49. return
  50. }
  51. type EdbInfoItem struct {
  52. EdbInfoId int `description:"指标id"`
  53. EdbName string `description:"指标名称"`
  54. ClassifyId int `description:"指标分类"`
  55. }
  56. // GetEdbInfoAll 用于分类展示
  57. func GetEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,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`
  60. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  61. return
  62. }
  63. // GetPredictEdbInfoAll 用于分类展示(预测指标)
  64. func GetPredictEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := ` SELECT edb_info_id,classify_id,edb_name AS classify_name,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`
  67. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  68. return
  69. }
  70. // 用于分类展示
  71. func GetEdbInfoAllList() (items []*EdbInfo, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. sql := ` SELECT * FROM edb_info `
  74. _, err = o.Raw(sql).QueryRows(&items)
  75. return
  76. }
  77. // 指标检索数据
  78. type EdbInfoSearch struct {
  79. EdbCode string `description:"指标编码"`
  80. StartDate string `description:"起始日期"`
  81. EndDate string `description:"终止日期"`
  82. EdbName string `description:"指标名称"`
  83. Unit string `description:"单位"`
  84. Frequency string `description:"频率"`
  85. DataList []*EdbInfoSearchData
  86. }
  87. type EdbInfoSearchData struct {
  88. DataTime string `description:"数据日期"`
  89. Value float64 `description:"数据"`
  90. }
  91. type EdbInfoSearchResp struct {
  92. SearchItem *EdbInfoSearch `description:"指标分类"`
  93. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  94. ClassifyList []*EdbClassifySimplify
  95. }
  96. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  97. o := orm.NewOrmUsingDB("data")
  98. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  99. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  100. return
  101. }
  102. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  103. o := orm.NewOrmUsingDB("data")
  104. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  105. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  106. return
  107. }
  108. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  109. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  110. num := len(edbInfoIdList)
  111. if num <= 0 {
  112. return
  113. }
  114. o := orm.NewOrmUsingDB("data")
  115. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  116. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  117. return
  118. }
  119. type AddEdbInfoReq struct {
  120. Source int `description:"来源id"`
  121. EdbCode string `description:"指标编码"`
  122. EdbName string `description:"指标名称"`
  123. Frequency string `description:"频率"`
  124. Unit string `description:"单位"`
  125. ClassifyId int `description:"分类id"`
  126. StartDate string `description:"起始日期"`
  127. EndDate string `description:"终止日期"`
  128. }
  129. func DeleteEdbInfoAndData(edbInfoId, source int) (err error) {
  130. o := orm.NewOrmUsingDB("data")
  131. to, err := o.Begin()
  132. if err != nil {
  133. return
  134. }
  135. defer func() {
  136. if err != nil {
  137. _ = to.Rollback()
  138. } else {
  139. _ = to.Commit()
  140. }
  141. }()
  142. // 删除指标信息
  143. sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
  144. _, err = to.Raw(sql, edbInfoId).Exec()
  145. if err != nil {
  146. return
  147. }
  148. // 删除指标数据
  149. tableName := GetEdbDataTableName(source)
  150. if tableName != "" {
  151. sql = ` DELETE FROM %s WHERE edb_info_id=? `
  152. sql = fmt.Sprintf(sql, tableName)
  153. _, err = to.Raw(sql, edbInfoId).Exec()
  154. if err != nil {
  155. return
  156. }
  157. }
  158. //calculateTableName := GetEdbInfoCalculateTableName(source)
  159. //if calculateTableName != "" {
  160. // sql = ` DELETE FROM %s WHERE edb_info_id=? `
  161. // sql = fmt.Sprintf(sql, calculateTableName)
  162. // _, err = to.Raw(sql, edbInfoId).Exec()
  163. // return
  164. //}
  165. //return
  166. // 删除计算指标的关系
  167. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  168. _, err = to.Raw(sql, edbInfoId).Exec()
  169. // 删除预测指标的配置
  170. sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id=? `
  171. _, err = to.Raw(sql, edbInfoId).Exec()
  172. return
  173. }
  174. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  175. o := orm.NewOrmUsingDB("data")
  176. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  177. if condition != "" {
  178. sql += condition
  179. }
  180. err = o.Raw(sql, pars).QueryRow(&count)
  181. return
  182. }
  183. type EditEdbInfoReq struct {
  184. EdbInfoId int `description:"指标ID"`
  185. EdbName string `description:"指标名称"`
  186. Frequency string `description:"频率"`
  187. Unit string `description:"单位"`
  188. ClassifyId int `description:"分类id"`
  189. CalculateFormula string `description:"计算公式"`
  190. }
  191. type EditEdbEnInfoReq struct {
  192. EdbInfoId int `description:"指标ID"`
  193. EdbNameEn string `description:"英文指标名称"`
  194. UnitEn string `description:"英文单位"`
  195. }
  196. func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
  197. o := orm.NewOrmUsingDB("data")
  198. sql := ` UPDATE edb_info
  199. SET
  200. edb_name =?,
  201. edb_name_source =?,
  202. frequency = ?,
  203. unit = ?,
  204. classify_id = ?,
  205. modify_time = NOW()
  206. WHERE edb_info_id = ?`
  207. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Exec()
  208. return
  209. }
  210. // ModifyEdbEnInfo 修改指标英文信息
  211. func ModifyEdbEnInfo(item *EditEdbEnInfoReq) (err error) {
  212. o := orm.NewOrmUsingDB("data")
  213. sql := ` UPDATE edb_info
  214. SET
  215. edb_name_en =?,
  216. unit_en = ?,
  217. modify_time = NOW()
  218. WHERE edb_info_id = ?`
  219. _, err = o.Raw(sql, item.EdbNameEn, item.UnitEn, item.EdbInfoId).Exec()
  220. return
  221. }
  222. type EdbInfoList struct {
  223. EdbInfoId int `orm:"column(edb_info_id);pk"`
  224. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  225. SourceName string `description:"来源名称"`
  226. Source int `description:"来源id"`
  227. EdbCode string `description:"指标编码"`
  228. EdbNameEn string `description:"英文指标名称"`
  229. EdbName string `description:"指标名称"`
  230. Frequency string `description:"频率"`
  231. FrequencyEn string `description:"英文频率"`
  232. Unit string `description:"单位"`
  233. UnitEn string `description:"英文单位"`
  234. StartDate string `description:"起始日期"`
  235. EndDate string `description:"终止日期"`
  236. LatestDate string `description:"数据最新日期"`
  237. LatestValue float64 `description:"数据最新值"`
  238. ClassifyId int `description:"分类id"`
  239. UniqueCode string `description:"指标唯一编码"`
  240. SysUserId int `description:"创建人id"`
  241. SysUserRealName string `description:"创建人姓名"`
  242. ModifyTime string `description:"最新修改时间"`
  243. CreateTime string `description:"创建时间"`
  244. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  245. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  246. ChartImage string `description:"图表图片"`
  247. RuleType int `description:"预测规则,1:最新,2:固定值"`
  248. FixedValue float64 `description:"固定值"`
  249. DataList []*EdbData `description:"实际指标数据"`
  250. PredictDataList []*EdbData `description:"预测指标数据"`
  251. Button EdbClassifyItemsButton `description:"操作权限"`
  252. IsEnEdb bool `description:"是否展示英文标识"`
  253. DataInsertConfig EdbDataInsertConfigItem `description:"指标数据插入配置"`
  254. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  255. }
  256. type EdbDataInsertConfigItem struct {
  257. Date string `description:"插入的日期"`
  258. RealDate string `description:"实际最晚的日期"`
  259. Value string `description:"插入的值"`
  260. }
  261. type EdbData struct {
  262. EdbDataId int `orm:"column(edb_data_id);pk"`
  263. EdbInfoId int
  264. DataTime string
  265. Value float64
  266. }
  267. type EdbInfoListResp struct {
  268. Paging *paging.PagingItem
  269. Item *EdbInfoList
  270. ClassifyList []*EdbClassifySimplify
  271. }
  272. func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  273. o := orm.NewOrmUsingDB("data")
  274. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  275. if condition != "" {
  276. sql += condition
  277. }
  278. err = o.Raw(sql, pars).QueryRow(&item)
  279. return
  280. }
  281. func GetEdbDataCountByCondition(condition string, pars []interface{}, source int) (count int, err error) {
  282. o := orm.NewOrmUsingDB("data")
  283. tableName := GetEdbDataTableName(source)
  284. sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  285. sql = fmt.Sprintf(sql, tableName)
  286. if condition != "" {
  287. sql += condition
  288. }
  289. err = o.Raw(sql, pars).QueryRow(&count)
  290. return
  291. }
  292. func GetEdbDataListByCondition(condition string, pars []interface{}, source, pageSize, startSize int) (item []*EdbData, err error) {
  293. o := orm.NewOrmUsingDB("data")
  294. tableName := GetEdbDataTableName(source)
  295. sql := ` SELECT * FROM %s WHERE 1=1 `
  296. sql = fmt.Sprintf(sql, tableName)
  297. if condition != "" {
  298. sql += condition
  299. }
  300. sql += ` ORDER BY data_time DESC `
  301. sql += ` LIMIT ?,? `
  302. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  303. return
  304. }
  305. // GetAllEdbDataListByCondition 根据条件获取所有的数据
  306. func GetAllEdbDataListByCondition(condition string, pars []interface{}, source int) (item []*EdbData, err error) {
  307. o := orm.NewOrmUsingDB("data")
  308. tableName := GetEdbDataTableName(source)
  309. sql := ` SELECT * FROM %s WHERE 1=1 `
  310. sql = fmt.Sprintf(sql, tableName)
  311. if condition != "" {
  312. sql += condition
  313. }
  314. sql += ` ORDER BY data_time DESC `
  315. _, err = o.Raw(sql, pars).QueryRows(&item)
  316. return
  317. }
  318. func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
  319. o := orm.NewOrmUsingDB("data")
  320. sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  321. err = o.Raw(sql).QueryRow(&item)
  322. return
  323. }
  324. func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
  325. o := orm.NewOrmUsingDB("data")
  326. sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
  327. _, err = o.Raw(sql, edbInfoId).Exec()
  328. return
  329. }
  330. type MoveEdbInfoReq struct {
  331. EdbInfoId int `description:"指标ID"`
  332. PrevEdbInfoId int `description:"上一个指标ID"`
  333. NextEdbInfoId int `description:"下一个指标ID"`
  334. ClassifyId int `description:"分类id"`
  335. }
  336. func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
  337. o := orm.NewOrmUsingDB("data")
  338. sql := ` UPDATE edb_info
  339. SET
  340. classify_id = ?
  341. WHERE edb_info_id = ?`
  342. _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
  343. return
  344. }
  345. // GetEdbInfoByName 根据名称获取普通的指标
  346. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  347. o := orm.NewOrmUsingDB("data")
  348. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 0 `
  349. _, err = o.Raw(sql, edbName).QueryRows(&items)
  350. return
  351. }
  352. // GetPredictEdbInfoByName 根据指标名称获取预测指标
  353. func GetPredictEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  354. o := orm.NewOrmUsingDB("data")
  355. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 1 `
  356. _, err = o.Raw(sql, edbName).QueryRows(&items)
  357. return
  358. }
  359. func GetEdbInfoByEnName(edbNameEn string) (items []*EdbInfoList, err error) {
  360. o := orm.NewOrmUsingDB("data")
  361. sql := ` SELECT * FROM edb_info WHERE edb_name_en=? AND edb_info_type = 0 `
  362. _, err = o.Raw(sql, edbNameEn).QueryRows(&items)
  363. return
  364. }
  365. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  366. o := orm.NewOrmUsingDB("data")
  367. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  368. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  369. return
  370. }
  371. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  372. o := orm.NewOrmUsingDB("data")
  373. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  374. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  375. return
  376. }
  377. type ChartEdbInfo struct {
  378. EdbInfoId int `description:"指标id"`
  379. EdbName string `description:"指标名称"`
  380. SourceName string `json:"-"`
  381. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  382. }
  383. func EdbInfoSearchByKeyWord(KeyWord string) (searchList []*ChartEdbInfo, err error) {
  384. o := orm.NewOrmUsingDB("data")
  385. sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  386. if KeyWord != "" {
  387. sql += ` AND (edb_name LIKE '%` + KeyWord + `%' OR edb_code LIKE '%` + KeyWord + `%' ) `
  388. }
  389. sql += ` ORDER BY create_time DESC `
  390. _, err = o.Raw(sql).QueryRows(&searchList)
  391. return
  392. }
  393. type EdbInfoMaxAndMinInfo struct {
  394. MinDate string `description:"最小日期"`
  395. MaxDate string `description:"最大日期"`
  396. MinValue float64 `description:"最小值"`
  397. MaxValue float64 `description:"最大值"`
  398. LatestValue float64 `description:"最新值"`
  399. }
  400. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  401. o := orm.NewOrmUsingDB("data")
  402. sql := ``
  403. tableName := GetEdbDataTableName(source)
  404. 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=? `
  405. sql = fmt.Sprintf(sql, tableName)
  406. err = o.Raw(sql, edbCode).QueryRow(&item)
  407. if err != nil {
  408. return
  409. }
  410. var latest_value float64
  411. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  412. sql = fmt.Sprintf(sql, tableName)
  413. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  414. item.LatestValue = latest_value
  415. return
  416. }
  417. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  418. o := orm.NewOrmUsingDB("data")
  419. 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=? `
  420. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  421. return
  422. }
  423. func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  424. o := orm.NewOrmUsingDB("data")
  425. sql := ` SELECT * FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  426. if condition != "" {
  427. sql += condition
  428. }
  429. sql += ` ORDER BY create_time DESC `
  430. _, err = o.Raw(sql, pars).QueryRows(&list)
  431. return
  432. }
  433. //order:1升序,其余值为降序
  434. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  435. o := orm.NewOrmUsingDB("data")
  436. sql := ``
  437. tableName := GetEdbDataTableName(source)
  438. sql = ` SELECT * FROM %s WHERE 1=1 `
  439. sql = fmt.Sprintf(sql, tableName)
  440. if condition != "" {
  441. sql += condition
  442. }
  443. if order == 1 {
  444. sql += ` ORDER BY data_time ASC `
  445. } else {
  446. sql += ` ORDER BY data_time DESC `
  447. }
  448. _, err = o.Raw(sql, pars).QueryRows(&item)
  449. return
  450. }
  451. // GetLastEdbData 获取最近的一条指标数据
  452. func GetLastEdbData(condition string, pars []interface{}, source int) (item *EdbInfoSearchData, err error) {
  453. o := orm.NewOrmUsingDB("data")
  454. sql := ``
  455. tableName := GetEdbDataTableName(source)
  456. sql = ` SELECT * FROM %s WHERE 1=1 `
  457. sql = fmt.Sprintf(sql, tableName)
  458. if condition != "" {
  459. sql += condition
  460. }
  461. sql += ` ORDER BY data_time DESC `
  462. err = o.Raw(sql, pars).QueryRow(&item)
  463. return
  464. }
  465. //order:1升序,其余值为降序
  466. func GetEdbDataCount(condition string, pars []interface{}, source int) (count int, err error) {
  467. o := orm.NewOrmUsingDB("data")
  468. sql := ``
  469. tableName := GetEdbDataTableName(source)
  470. sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  471. sql = fmt.Sprintf(sql, tableName)
  472. if condition != "" {
  473. sql += condition
  474. }
  475. err = o.Raw(sql, pars).QueryRow(&count)
  476. return
  477. }
  478. func ModifyCalculateEdbInfo(item *EditEdbInfoReq) (err error) {
  479. o := orm.NewOrmUsingDB("data")
  480. sql := ` UPDATE edb_info
  481. SET
  482. edb_name =?,
  483. edb_name_source =?,
  484. frequency = ?,
  485. unit = ?,
  486. classify_id = ?,
  487. calculate_formula=?,
  488. modify_time = NOW()
  489. WHERE edb_info_id = ?`
  490. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.CalculateFormula, item.EdbInfoId).Exec()
  491. return
  492. }
  493. type AddEdbInfoResp struct {
  494. EdbInfoId int `description:"指标ID"`
  495. UniqueCode string `description:"指标唯一编码"`
  496. }
  497. // 根据基础指标获取所有关联指标
  498. func GetNeedRefreshCalculateEdbInfoFromBase(fromEdbInfo int) (list []*EdbInfo, err error) {
  499. o := orm.NewOrmUsingDB("data")
  500. sql := ` SELECT b.*
  501. FROM edb_info_calculate AS a
  502. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  503. WHERE from_edb_info_id=? `
  504. _, err = o.Raw(sql, fromEdbInfo).QueryRows(&list)
  505. return
  506. }
  507. func GetEdbInfoCalculateCount(edbInfoId int) (count int, err error) {
  508. o := orm.NewOrmUsingDB("data")
  509. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate WHERE from_edb_info_id=? `
  510. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  511. return
  512. }
  513. func GetEdbInfoCalculateLjzzyCount(edbInfoId int) (count int, err error) {
  514. o := orm.NewOrmUsingDB("data")
  515. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_ljzzy WHERE from_edb_info_id=? `
  516. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  517. return
  518. }
  519. func GetEdbInfoCalculateTbzCount(edbInfoId int) (count int, err error) {
  520. o := orm.NewOrmUsingDB("data")
  521. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tbz WHERE from_edb_info_id=? `
  522. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  523. return
  524. }
  525. func GetEdbInfoCalculateTczCount(edbInfoId int) (count int, err error) {
  526. o := orm.NewOrmUsingDB("data")
  527. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tcz WHERE from_edb_info_id=? `
  528. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  529. return
  530. }
  531. func GetEdbInfoCalculateNszydpjjsCount(edbInfoId int) (count int, err error) {
  532. o := orm.NewOrmUsingDB("data")
  533. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_nszydpjjs WHERE from_edb_info_id=? `
  534. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  535. return
  536. }
  537. func GetEdbInfoCalculateCountByCondition(source int, condition string, pars []interface{}) (count int, err error) {
  538. o := orm.NewOrmUsingDB("data")
  539. //calculateTableName := GetEdbInfoCalculateTableName(source)
  540. //if calculateTableName == "" {
  541. // err = errors.New("无效的表名")
  542. // return
  543. //}
  544. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
  545. //sql = fmt.Sprintf(sql, calculateTableName)
  546. if condition != "" {
  547. sql += condition
  548. }
  549. err = o.Raw(sql, pars).QueryRow(&count)
  550. return
  551. }
  552. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  553. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  554. o := orm.NewOrmUsingDB("data")
  555. //calculateTableName := GetEdbInfoCalculateTableName(source)
  556. //if calculateTableName == "" {
  557. // err = errors.New("无效的表名")
  558. // return
  559. //}
  560. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  561. //sql = fmt.Sprintf(sql, calculateTableName)
  562. if condition != "" {
  563. sql += condition
  564. }
  565. _, err = o.Raw(sql, pars).QueryRows(&items)
  566. return
  567. }
  568. func GetRefreshEdbInfoFromCalculate(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  569. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  570. if err != nil && err.Error() != utils.ErrNoRow() {
  571. return
  572. }
  573. for _, item := range calculateList {
  574. if item.EdbType == 1 {
  575. baseEdbInfoArr = append(baseEdbInfoArr, item)
  576. } else {
  577. calculateInfoArr = append(calculateInfoArr, item)
  578. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromCalculate(item.EdbInfoId, item.Source)
  579. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  580. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  581. }
  582. }
  583. return
  584. }
  585. // 获取基础指标对应的所有计算指标
  586. func GetEdbInfoAllCalculate(edbInfoId int) (list []*EdbInfo, err error) {
  587. o := orm.NewOrmUsingDB("data")
  588. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  589. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  590. WHERE a.from_edb_info_id=?
  591. GROUP BY a.edb_info_id
  592. ORDER BY a.edb_info_id ASC `
  593. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  594. return
  595. }
  596. // GetEdbInfoAllCalculateByEdbInfoIdList 根据指标id集合 获取基础指标对应的所有计算指标
  597. func GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList []int) (list []*EdbInfo, err error) {
  598. num := len(edbInfoIdList)
  599. if num <= 0 {
  600. return
  601. }
  602. o := orm.NewOrmUsingDB("data")
  603. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  604. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  605. WHERE a.from_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  606. GROUP BY a.edb_info_id
  607. ORDER BY a.edb_info_id ASC `
  608. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  609. return
  610. }
  611. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  612. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  613. if err != nil && err.Error() != utils.ErrNoRow() {
  614. return
  615. }
  616. for _, item := range calculateList {
  617. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  618. continue
  619. }
  620. if item.EdbType == 1 {
  621. baseEdbInfoArr = append(baseEdbInfoArr, item)
  622. } else {
  623. calculateInfoArr = append(calculateInfoArr, item)
  624. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  625. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  626. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  627. }
  628. }
  629. return
  630. }
  631. func ModifyEdbInfoDataStatus(edbInfoId int64, source int, edbCode string) (err error) {
  632. o := orm.NewOrmUsingDB("data")
  633. sql := ``
  634. tableName := GetEdbDataTableName(source)
  635. sql = ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
  636. sql = fmt.Sprintf(sql, tableName)
  637. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  638. return
  639. }
  640. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  641. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  642. o := orm.NewOrmUsingDB("data")
  643. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  644. err = o.Raw(sql, classifyId).QueryRow(&item)
  645. return
  646. }
  647. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  648. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  649. o := orm.NewOrmUsingDB("data")
  650. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=? AND `
  651. if prevEdbInfoId > 0 {
  652. sql += ` ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  653. }
  654. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  655. return
  656. }
  657. // Update 更新指标基础信息
  658. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  659. o := orm.NewOrmUsingDB("data")
  660. _, err = o.Update(edbInfo, cols...)
  661. return
  662. }
  663. type EdbInfoDataResp struct {
  664. EdbInfo *EdbInfo
  665. DataList []*EdbDataList
  666. }
  667. type EdbInfoReplaceReq struct {
  668. OldEdbInfoId int `description:"原指标ID"`
  669. NewEdbInfoId int `description:"替换为指标ID"`
  670. }
  671. // ReplaceChartEdb 替换图表中的指标
  672. func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (mappingList []*EdbInfoCalculateMapping, replaceChartTotal, replaceCalculateTotal int, err error) {
  673. return replaceChartEdb(oldEdbInfo, newEdbInfo)
  674. }
  675. // replaceChartEdb 替换图表中的指标
  676. func replaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (mappingList []*EdbInfoCalculateMapping, replaceChartTotal, replaceCalculateTotal int, err error) {
  677. var errmsg string
  678. o := orm.NewOrmUsingDB("data")
  679. to, err := o.Begin()
  680. if err != nil {
  681. return
  682. }
  683. defer func() {
  684. if err != nil {
  685. _ = to.Rollback()
  686. } else {
  687. _ = to.Commit()
  688. }
  689. if errmsg != "" {
  690. fmt.Println("errmsg:" + errmsg)
  691. }
  692. if err != nil && errmsg != "" {
  693. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  694. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  695. }
  696. }()
  697. //替换图表
  698. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  699. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  700. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  701. if err != nil {
  702. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  703. return
  704. }
  705. replaceChartTotal = len(chartEdbMappingList)
  706. for _, mv := range chartEdbMappingList {
  707. //获取图表所有指标信息
  708. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  709. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  710. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  711. if err != nil {
  712. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  713. return
  714. }
  715. var minData, maxData float64
  716. minData = newEdbInfo.MinValue
  717. maxData = newEdbInfo.MaxValue
  718. for _, cv := range chartEdbList {
  719. if mv.IsAxis == cv.IsAxis {
  720. if minData > cv.MinData {
  721. minData = cv.MinData
  722. }
  723. if maxData < cv.MaxData {
  724. maxData = cv.MaxData
  725. }
  726. }
  727. }
  728. //修改图表关联指标
  729. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  730. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  731. if err != nil {
  732. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  733. return
  734. }
  735. }
  736. //替换计算指标
  737. //获取所有包含的计算指标
  738. mappingList = make([]*EdbInfoCalculateMapping, 0)
  739. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  740. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  741. if err != nil {
  742. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  743. return
  744. }
  745. replaceCalculateTotal = len(mappingList)
  746. //计算指标
  747. for _, mv := range mappingList {
  748. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  749. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  750. continue
  751. }
  752. //替换原指标
  753. 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=? `
  754. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  755. if err != nil {
  756. return
  757. }
  758. }
  759. return
  760. }
  761. type EdbInfoView struct {
  762. EdbInfoId int `orm:"column(edb_info_id);pk"`
  763. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  764. SourceName string `description:"来源名称"`
  765. Source int `description:"来源id"`
  766. EdbCode string `description:"指标编码"`
  767. EdbName string `description:"指标名称"`
  768. EdbNameSource string `description:"指标名称来源"`
  769. Frequency string `description:"频率"`
  770. Unit string `description:"单位"`
  771. StartDate string `description:"起始日期"`
  772. EndDate string `description:"终止日期"`
  773. ClassifyId int `description:"分类id"`
  774. SysUserId int
  775. SysUserRealName string
  776. UniqueCode string `description:"指标唯一编码"`
  777. CreateTime string
  778. ModifyTime string
  779. MinValue float64 `description:"指标最小值"`
  780. MaxValue float64 `description:"指标最大值"`
  781. CalculateFormula string `description:"计算公式"`
  782. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  783. Sort int `description:"排序字段"`
  784. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  785. LatestDate string `description:"数据最新日期"`
  786. LatestValue float64 `description:"数据最新值"`
  787. }
  788. // 获取指标的所有计算指标,以及计算指标所依赖计算指标
  789. func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  790. o := orm.NewOrmUsingDB("data")
  791. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  792. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  793. if err != nil {
  794. return
  795. }
  796. return
  797. }
  798. func GetAllCalculate(edbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  799. calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
  800. if err != nil && err.Error() != utils.ErrNoRow() {
  801. return
  802. }
  803. for _, item := range calculateList {
  804. _, ok := edbInfoMap[item.EdbInfoId]
  805. if !ok {
  806. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  807. mappingList = append(mappingList, item)
  808. newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId, edbInfoMap)
  809. mappingList = append(mappingList, newCalculateInfoArr...)
  810. }
  811. }
  812. return
  813. }
  814. // GetAllCalculateByEdbInfoIdV2 获取计算指标的所有所依赖的指标(基础指标+计算指标)
  815. func GetAllCalculateByEdbInfoIdV2(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  816. o := orm.NewOrmUsingDB("data")
  817. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_info_id=? GROUP BY from_edb_info_id `
  818. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  819. if err != nil {
  820. return
  821. }
  822. return
  823. }
  824. // GetAllCalculateV2 获取所有的指标(如果下层指标包含了待替换指标,那么就不加入)
  825. func GetAllCalculateV2(oldEdbInfoId, replaceEdbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  826. calculateList, err := GetAllCalculateByEdbInfoId(oldEdbInfoId)
  827. if err != nil && err.Error() != utils.ErrNoRow() {
  828. return
  829. }
  830. tmpMappingList := make([]*EdbInfoCalculateMapping, 0)
  831. for _, item := range calculateList {
  832. //如果计算指标里面存在即将替换的指标id,那么退出当前循环,进入下一循环
  833. if item.EdbInfoId == replaceEdbInfoId {
  834. err = fmt.Errorf("指标重复1")
  835. return
  836. }
  837. //获取该指标的依赖指标列表,如果依赖指标列表中存在即将替换的指标,那么过滤该指标
  838. childCalculateList, tmpErr := GetAllCalculateByEdbInfoIdV2(item.EdbInfoId)
  839. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  840. err = tmpErr
  841. return
  842. }
  843. for _, childCalculate := range childCalculateList {
  844. if childCalculate.FromEdbInfoId == replaceEdbInfoId {
  845. err = fmt.Errorf("指标重复2")
  846. return
  847. }
  848. }
  849. _, ok := edbInfoMap[item.EdbInfoId]
  850. if !ok {
  851. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  852. newCalculateInfoArr, tmpErr := GetAllCalculateV2(item.EdbInfoId, replaceEdbInfoId, edbInfoMap)
  853. if tmpErr != nil {
  854. err = tmpErr
  855. continue
  856. } else {
  857. mappingList = append(mappingList, item)
  858. tmpMappingList = append(tmpMappingList, newCalculateInfoArr...)
  859. }
  860. }
  861. }
  862. mappingList = append(mappingList, tmpMappingList...)
  863. return
  864. }
  865. // EdbInfoFilterDataResp 搜索指标列表数据返回
  866. type EdbInfoFilterDataResp struct {
  867. Paging *paging.PagingItem
  868. List []*EdbInfoList
  869. }
  870. // GetEdbInfoFilterList 获取指指标列表数据接口
  871. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  872. o := orm.NewOrmUsingDB("data")
  873. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  874. err = o.Raw(sql, pars).QueryRow(&total)
  875. if err != nil {
  876. return
  877. }
  878. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  879. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  880. return
  881. }
  882. // SetEdbInfoImageReq 设置指标图片
  883. type SetEdbInfoImageReq struct {
  884. EdbInfoId int `description:"指标ID"`
  885. ImageUrl string `description:"指标图片地址"`
  886. }
  887. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  888. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  889. o := orm.NewOrmUsingDB("data")
  890. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  891. if condition != "" {
  892. sql += condition
  893. }
  894. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  895. err = o.Raw(sql, pars).QueryRow(&item)
  896. return
  897. }
  898. // GetNextEdbInfo 根据分类id获取下一个 指标
  899. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  900. o := orm.NewOrmUsingDB("data")
  901. sql := ` SELECT b.* FROM edb_classify AS a
  902. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  903. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  904. AND a.classify_type=? AND b.edb_info_type=?
  905. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  906. LIMIT 1 `
  907. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).QueryRow(&item)
  908. return
  909. }
  910. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  911. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  912. num := len(edbCodeList)
  913. if num <= 0 {
  914. return
  915. }
  916. o := orm.NewOrmUsingDB("data")
  917. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  918. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  919. return
  920. }
  921. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  922. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  923. o := orm.NewOrmUsingDB("data")
  924. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  925. if condition != "" {
  926. sql += condition
  927. }
  928. err = o.Raw(sql, pars).QueryRow(&count)
  929. return
  930. }
  931. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  932. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfoList, err error) {
  933. o := orm.NewOrmUsingDB("data")
  934. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  935. if condition != "" {
  936. sql += condition
  937. }
  938. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  939. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  940. return
  941. }
  942. // GetEdbInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  943. func GetEdbInfoListGroupByUserId(edbIdList []string) (items []*EdbInfo, err error) {
  944. num := len(edbIdList)
  945. if num <= 0 {
  946. return
  947. }
  948. o := orm.NewOrmUsingDB("data")
  949. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  950. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  951. return
  952. }
  953. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  954. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  955. num := len(edbIdList)
  956. if num <= 0 {
  957. return
  958. }
  959. o := orm.NewOrmUsingDB("data")
  960. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  961. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  962. return
  963. }
  964. // MoveEdbChartList 图表/指标信息
  965. type MoveEdbChartList struct {
  966. DataId string `description:"指标/图表唯一id"`
  967. Code string `description:"指标code"`
  968. Name string `description:"指标/图表名称"`
  969. ClassifyName string `description:"分类名称"`
  970. CreateUserId int `description:"创建人id"`
  971. CreateUserName string `description:"创建人名称"`
  972. }
  973. // MoveEdbChartListResp 更改创建人返回数据
  974. type MoveEdbChartListResp struct {
  975. Paging *paging.PagingItem
  976. List []MoveEdbChartList
  977. }
  978. // MoveEdbChartReq 转移指标/图表的请求
  979. type MoveEdbChartReq struct {
  980. Source int `description:"来源id"`
  981. DataIdList []string `description:"指标/图表唯一id列表"`
  982. NewUserId int `description:"新的创建人id"`
  983. }
  984. type EdbChartClassifyResp struct {
  985. List []*EdbChartClassify
  986. }
  987. type EdbChartClassify struct {
  988. ClassifyId int
  989. ClassifyName string
  990. ParentId int
  991. Child []*EdbChartClassify
  992. }
  993. // EdbInfoDataSeasonalResp 指标季节性数据返回
  994. type EdbInfoDataSeasonalResp struct {
  995. EdbInfo *EdbInfo
  996. DataList interface{}
  997. }
  998. // SetEdbDataInsertConfigReq 设置插入规则的请求
  999. type SetEdbDataInsertConfigReq struct {
  1000. EdbInfoId int `description:"指标ID"`
  1001. Date string `description:"日期"`
  1002. Value float64 `description:"值"`
  1003. }
  1004. // GetEdbInfoAll 用于分类展示
  1005. func GetEdbInfoByClassifyId(classifyId, edbInfoType int) (items []*EdbClassifyItems, err error) {
  1006. o := orm.NewOrmUsingDB("data")
  1007. sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE classify_id = ? AND edb_info_type = ? order by sort asc,edb_info_id asc`
  1008. _, err = o.Raw(sql, classifyId, edbInfoType).QueryRows(&items)
  1009. return
  1010. }
  1011. type EdbAndClassify struct {
  1012. EdbInfoId int `description:"指标id"`
  1013. EdbCode string `description:"指标code"`
  1014. UniqueCode string `description:"唯一code"`
  1015. EdbName string `description:"指标名称"`
  1016. FirstClassifyId int `description:"第1级的分类id"`
  1017. FirstClassifyName string `description:"第1级的分类名称"`
  1018. SecondClassifyId int `description:"第2级的分类id"`
  1019. SecondClassifyName string `description:"第2级的分类名称"`
  1020. }
  1021. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1022. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1023. num := len(edbIdList)
  1024. if num <= 0 {
  1025. return
  1026. }
  1027. o := orm.NewOrmUsingDB("data")
  1028. sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,b.classify_id as second_classify_id,b.classify_name as second_classify_name,c.classify_id as first_classify_id,c.classify_name as first_classify_name FROM edb_info AS a
  1029. JOIN edb_classify b on a.classify_id=b.classify_id
  1030. JOIN edb_classify c on b.parent_id=c.classify_id
  1031. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1032. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1033. return
  1034. }
  1035. // TraceEdbInfoResp 指标追溯数据返回
  1036. type TraceEdbInfoResp struct {
  1037. //EdbInfo *EdbInfo
  1038. EdbInfoId int `description:"指标id"`
  1039. EdbName string `description:"指标名称"`
  1040. RuleTitle string `description:"指标规则"`
  1041. Child []TraceEdbInfoResp `description:"下级来源"`
  1042. }