edb_info.go 40 KB

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