edb_info.go 40 KB

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