edb_info.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. package data_manage
  2. import (
  3. "fmt"
  4. "hongze/hongze_chart_lib/utils"
  5. "github.com/rdlucklib/rdluck_tools/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type EdbInfo struct {
  12. EdbInfoId int `orm:"column(edb_info_id);pk"`
  13. SourceName string `description:"来源名称"`
  14. Source int `description:"来源id"`
  15. EdbCode string `description:"指标编码"`
  16. EdbName string `description:"指标名称"`
  17. EdbNameSource string `description:"指标名称来源"`
  18. Frequency string `description:"频率"`
  19. Unit string `description:"单位"`
  20. StartDate string `description:"起始日期"`
  21. EndDate string `description:"终止日期"`
  22. ClassifyId int `description:"分类id"`
  23. SysUserId int
  24. SysUserRealName string
  25. UniqueCode string `description:"指标唯一编码"`
  26. CreateTime time.Time
  27. ModifyTime time.Time
  28. MinValue float64 `description:"指标最小值"`
  29. MaxValue float64 `description:"指标最大值"`
  30. CalculateFormula string `description:"计算公式"`
  31. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  32. Sort int `description:"排序字段"`
  33. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  34. MoveFrequency string `description:"移动频度"`
  35. }
  36. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  37. o := orm.NewOrm()
  38. o.Using("data")
  39. lastId, err = o.Insert(item)
  40. return
  41. }
  42. type EdbInfoItem struct {
  43. EdbInfoId int `description:"指标id"`
  44. EdbName string `description:"指标名称"`
  45. ClassifyId int `description:"指标分类"`
  46. }
  47. //用于分类展示
  48. func GetEdbInfoAll() (items []*EdbClassifyItems, err error) {
  49. o := orm.NewOrm()
  50. o.Using("data")
  51. 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 order by sort asc,edb_info_id asc`
  52. _, err = o.Raw(sql).QueryRows(&items)
  53. return
  54. }
  55. //用于分类展示
  56. func GetEdbInfoAllList() (items []*EdbInfo, err error) {
  57. o := orm.NewOrm()
  58. o.Using("data")
  59. sql := ` SELECT * FROM edb_info `
  60. _, err = o.Raw(sql).QueryRows(&items)
  61. return
  62. }
  63. //指标检索数据
  64. type EdbInfoSearch struct {
  65. EdbCode string `description:"指标编码"`
  66. StartDate string `description:"起始日期"`
  67. EndDate string `description:"终止日期"`
  68. EdbName string `description:"指标名称"`
  69. Unit string `description:"单位"`
  70. Frequency string `description:"频率"`
  71. DataList []*EdbInfoSearchData
  72. }
  73. type EdbInfoSearchData struct {
  74. DataTime string `description:"数据日期"`
  75. Value float64 `description:"数据"`
  76. }
  77. type EdbInfoSearchResp struct {
  78. SearchItem *EdbInfoSearch `description:"指标分类"`
  79. Status int `description:"1:数据已存在于弘则数据库,2:新数据"`
  80. ClassifyList []*EdbClassifySimplify
  81. }
  82. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  83. o := orm.NewOrm()
  84. o.Using("data")
  85. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  86. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  87. return
  88. }
  89. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  90. o := orm.NewOrm()
  91. o.Using("data")
  92. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  93. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  94. return
  95. }
  96. type AddEdbInfoReq struct {
  97. Source int `description:"来源id"`
  98. EdbCode string `description:"指标编码"`
  99. EdbName string `description:"指标名称"`
  100. Frequency string `description:"频率"`
  101. Unit string `description:"单位"`
  102. ClassifyId int `description:"分类id"`
  103. StartDate string `description:"起始日期"`
  104. EndDate string `description:"终止日期"`
  105. }
  106. func DeleteEdbInfoAndData(edbInfoId, source int) (err error) {
  107. o := orm.NewOrm()
  108. o.Using("data")
  109. o.Begin()
  110. defer func() {
  111. if err != nil {
  112. o.Rollback()
  113. } else {
  114. o.Commit()
  115. }
  116. }()
  117. sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
  118. _, err = o.Raw(sql, edbInfoId).Exec()
  119. if err != nil {
  120. return
  121. }
  122. tableName := GetEdbDataTableName(source)
  123. if tableName != "" {
  124. sql = ` DELETE FROM %s WHERE edb_info_id=? `
  125. sql = fmt.Sprintf(sql, tableName)
  126. _, err = o.Raw(sql, edbInfoId).Exec()
  127. if err != nil {
  128. return
  129. }
  130. }
  131. //calculateTableName := GetEdbInfoCalculateTableName(source)
  132. //if calculateTableName != "" {
  133. // sql = ` DELETE FROM %s WHERE edb_info_id=? `
  134. // sql = fmt.Sprintf(sql, calculateTableName)
  135. // _, err = o.Raw(sql, edbInfoId).Exec()
  136. // return
  137. //}
  138. //return
  139. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  140. _, err = o.Raw(sql, edbInfoId).Exec()
  141. return
  142. }
  143. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  144. o := orm.NewOrm()
  145. o.Using("data")
  146. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  147. if condition != "" {
  148. sql += condition
  149. }
  150. err = o.Raw(sql, pars).QueryRow(&count)
  151. return
  152. }
  153. type EditEdbInfoReq struct {
  154. EdbInfoId int `description:"指标ID"`
  155. EdbName string `description:"指标名称"`
  156. Frequency string `description:"频率"`
  157. Unit string `description:"单位"`
  158. ClassifyId int `description:"分类id"`
  159. CalculateFormula string `description:"计算公式"`
  160. }
  161. func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
  162. o := orm.NewOrm()
  163. o.Using("data")
  164. sql := ` UPDATE edb_info
  165. SET
  166. edb_name =?,
  167. edb_name_source =?,
  168. frequency = ?,
  169. unit = ?,
  170. classify_id = ?,
  171. modify_time = NOW()
  172. WHERE edb_info_id = ?`
  173. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Exec()
  174. return
  175. }
  176. type EdbInfoList struct {
  177. EdbInfoId int `orm:"column(edb_info_id);pk"`
  178. SourceName string `description:"来源名称"`
  179. Source int `description:"来源id"`
  180. EdbCode string `description:"指标编码"`
  181. EdbName string `description:"指标名称"`
  182. Frequency string `description:"频率"`
  183. Unit string `description:"单位"`
  184. StartDate string `description:"起始日期"`
  185. EndDate string `description:"终止日期"`
  186. ClassifyId int `description:"分类id"`
  187. UniqueCode string `description:"指标唯一编码"`
  188. SysUserId int `description:"创建人id"`
  189. SysUserRealName string `description:"创建人姓名"`
  190. ModifyTime string `description:"最新修改时间"`
  191. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  192. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  193. DataList []*EdbData
  194. }
  195. type EdbData struct {
  196. EdbDataId int `orm:"column(edb_data_id);pk"`
  197. EdbInfoId int
  198. DataTime string
  199. Value float64
  200. }
  201. type EdbInfoListResp struct {
  202. Paging *paging.PagingItem
  203. Item *EdbInfoList
  204. ClassifyList []*EdbClassifySimplify
  205. }
  206. func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  207. o := orm.NewOrm()
  208. o.Using("data")
  209. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  210. if condition != "" {
  211. sql += condition
  212. }
  213. err = o.Raw(sql, pars).QueryRow(&item)
  214. return
  215. }
  216. func GetEdbDataCountByCondition(condition string, pars []interface{}, source int) (count int, err error) {
  217. o := orm.NewOrm()
  218. o.Using("data")
  219. tableName := GetEdbDataTableName(source)
  220. sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 AND status=1 `
  221. sql = fmt.Sprintf(sql, tableName)
  222. if condition != "" {
  223. sql += condition
  224. }
  225. err = o.Raw(sql, pars).QueryRow(&count)
  226. return
  227. }
  228. func GetEdbDataListByCondition(condition string, pars []interface{}, source, pageSize, startSize int) (item []*EdbData, err error) {
  229. o := orm.NewOrm()
  230. o.Using("data")
  231. tableName := GetEdbDataTableName(source)
  232. sql := ` SELECT * FROM %s WHERE 1=1 AND status=1 `
  233. sql = fmt.Sprintf(sql, tableName)
  234. if condition != "" {
  235. sql += condition
  236. }
  237. sql += ` ORDER BY data_time DESC `
  238. sql += ` LIMIT ?,? `
  239. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  240. return
  241. }
  242. func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
  243. o := orm.NewOrm()
  244. o.Using("data")
  245. sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  246. err = o.Raw(sql).QueryRow(&item)
  247. return
  248. }
  249. func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
  250. o := orm.NewOrm()
  251. o.Using("data")
  252. sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
  253. _, err = o.Raw(sql, edbInfoId).Exec()
  254. return
  255. }
  256. type MoveEdbInfoReq struct {
  257. EdbInfoId int `description:"指标ID"`
  258. PrevEdbInfoId int `description:"上一个指标ID"`
  259. NextEdbInfoId int `description:"下一个指标ID"`
  260. ClassifyId int `description:"分类id"`
  261. }
  262. func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
  263. o := orm.NewOrm()
  264. o.Using("data")
  265. sql := ` UPDATE edb_info
  266. SET
  267. classify_id = ?
  268. WHERE edb_info_id = ?`
  269. _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
  270. return
  271. }
  272. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  273. o := orm.NewOrm()
  274. o.Using("data")
  275. sql := ` SELECT * FROM edb_info WHERE edb_name=? `
  276. _, err = o.Raw(sql, edbName).QueryRows(&items)
  277. return
  278. }
  279. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  280. o := orm.NewOrm()
  281. o.Using("data")
  282. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  283. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  284. return
  285. }
  286. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  287. o := orm.NewOrm()
  288. o.Using("data")
  289. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  290. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  291. return
  292. }
  293. type ChartEdbInfo struct {
  294. EdbInfoId int `description:"指标id"`
  295. EdbName string `description:"指标名称"`
  296. SourceName string `json:"-"`
  297. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  298. }
  299. func EdbInfoSearchByKeyWord(KeyWord string) (searchList []*ChartEdbInfo, err error) {
  300. o := orm.NewOrm()
  301. o.Using("data")
  302. sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info `
  303. if KeyWord != "" {
  304. sql += `WHERE (edb_name LIKE '%` + KeyWord + `%' OR edb_code LIKE '%` + KeyWord + `%' ) `
  305. }
  306. sql += ` ORDER BY create_time DESC `
  307. _, err = o.Raw(sql).QueryRows(&searchList)
  308. return
  309. }
  310. type EdbInfoMaxAndMinInfo struct {
  311. MinDate string `description:"最小日期"`
  312. MaxDate string `description:"最大日期"`
  313. MinValue float64 `description:"最小值"`
  314. MaxValue float64 `description:"最大值"`
  315. LatestValue float64 `description:"最新值"`
  316. }
  317. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  318. o := orm.NewOrm()
  319. o.Using("data")
  320. sql := ``
  321. tableName := GetEdbDataTableName(source)
  322. 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=? `
  323. sql = fmt.Sprintf(sql, tableName)
  324. err = o.Raw(sql, edbCode).QueryRow(&item)
  325. var latest_value float64
  326. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  327. sql = fmt.Sprintf(sql, tableName)
  328. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  329. item.LatestValue = latest_value
  330. return
  331. }
  332. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  333. o := orm.NewOrm()
  334. o.Using("data")
  335. 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=? `
  336. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  337. return
  338. }
  339. func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  340. o := orm.NewOrm()
  341. o.Using("data")
  342. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  343. if condition != "" {
  344. sql += condition
  345. }
  346. sql += ` ORDER BY create_time DESC `
  347. _, err = o.Raw(sql, pars).QueryRows(&list)
  348. return
  349. }
  350. //order:1升序,其余值为降序
  351. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  352. o := orm.NewOrm()
  353. o.Using("data")
  354. sql := ``
  355. tableName := GetEdbDataTableName(source)
  356. sql = ` SELECT * FROM %s WHERE 1=1 `
  357. sql = fmt.Sprintf(sql, tableName)
  358. if condition != "" {
  359. sql += condition
  360. }
  361. if order == 1 {
  362. sql += ` ORDER BY data_time ASC `
  363. } else {
  364. sql += ` ORDER BY data_time DESC `
  365. }
  366. _, err = o.Raw(sql, pars).QueryRows(&item)
  367. return
  368. }
  369. // GetLastEdbData 获取最近的一条指标数据
  370. func GetLastEdbData(condition string, pars []interface{}, source int) (item *EdbInfoSearchData, err error) {
  371. o := orm.NewOrm()
  372. o.Using("data")
  373. sql := ``
  374. tableName := GetEdbDataTableName(source)
  375. sql = ` SELECT * FROM %s WHERE 1=1 `
  376. sql = fmt.Sprintf(sql, tableName)
  377. if condition != "" {
  378. sql += condition
  379. }
  380. sql += ` ORDER BY data_time DESC `
  381. err = o.Raw(sql, pars).QueryRow(&item)
  382. return
  383. }
  384. //order:1升序,其余值为降序
  385. func GetEdbDataCount(condition string, pars []interface{}, source int) (count int, err error) {
  386. o := orm.NewOrm()
  387. o.Using("data")
  388. sql := ``
  389. tableName := GetEdbDataTableName(source)
  390. sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  391. sql = fmt.Sprintf(sql, tableName)
  392. if condition != "" {
  393. sql += condition
  394. }
  395. err = o.Raw(sql, pars).QueryRow(&count)
  396. return
  397. }
  398. func ModifyCalculateEdbInfo(item *EditEdbInfoReq) (err error) {
  399. o := orm.NewOrm()
  400. o.Using("data")
  401. sql := ` UPDATE edb_info
  402. SET
  403. edb_name =?,
  404. edb_name_source =?,
  405. frequency = ?,
  406. unit = ?,
  407. classify_id = ?,
  408. calculate_formula=?,
  409. modify_time = NOW()
  410. WHERE edb_info_id = ?`
  411. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.CalculateFormula, item.EdbInfoId).Exec()
  412. return
  413. }
  414. type AddEdbInfoResp struct {
  415. EdbInfoId int `description:"指标ID"`
  416. UniqueCode string `description:"指标唯一编码"`
  417. }
  418. //根据基础指标获取所有关联指标
  419. func GetNeedRefreshCalculateEdbInfoFromBase(fromEdbInfo int) (list []*EdbInfo, err error) {
  420. o := orm.NewOrm()
  421. o.Using("data")
  422. sql := ` SELECT b.*
  423. FROM edb_info_calculate AS a
  424. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  425. WHERE from_edb_info_id=? `
  426. _, err = o.Raw(sql, fromEdbInfo).QueryRows(&list)
  427. return
  428. }
  429. func GetEdbInfoCalculateCount(edbInfoId int) (count int, err error) {
  430. o := orm.NewOrm()
  431. o.Using("data")
  432. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate WHERE from_edb_info_id=? `
  433. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  434. return
  435. }
  436. func GetEdbInfoCalculateLjzzyCount(edbInfoId int) (count int, err error) {
  437. o := orm.NewOrm()
  438. o.Using("data")
  439. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_ljzzy WHERE from_edb_info_id=? `
  440. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  441. return
  442. }
  443. func GetEdbInfoCalculateTbzCount(edbInfoId int) (count int, err error) {
  444. o := orm.NewOrm()
  445. o.Using("data")
  446. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tbz WHERE from_edb_info_id=? `
  447. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  448. return
  449. }
  450. func GetEdbInfoCalculateTczCount(edbInfoId int) (count int, err error) {
  451. o := orm.NewOrm()
  452. o.Using("data")
  453. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tcz WHERE from_edb_info_id=? `
  454. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  455. return
  456. }
  457. func GetEdbInfoCalculateNszydpjjsCount(edbInfoId int) (count int, err error) {
  458. o := orm.NewOrm()
  459. o.Using("data")
  460. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_nszydpjjs WHERE from_edb_info_id=? `
  461. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  462. return
  463. }
  464. func GetEdbInfoCalculateCountByCondition(source int, condition string, pars []interface{}) (count int, err error) {
  465. o := orm.NewOrm()
  466. o.Using("data")
  467. //calculateTableName := GetEdbInfoCalculateTableName(source)
  468. //if calculateTableName == "" {
  469. // err = errors.New("无效的表名")
  470. // return
  471. //}
  472. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
  473. //sql = fmt.Sprintf(sql, calculateTableName)
  474. if condition != "" {
  475. sql += condition
  476. }
  477. err = o.Raw(sql, pars).QueryRow(&count)
  478. return
  479. }
  480. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  481. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  482. o := orm.NewOrm()
  483. o.Using("data")
  484. //calculateTableName := GetEdbInfoCalculateTableName(source)
  485. //if calculateTableName == "" {
  486. // err = errors.New("无效的表名")
  487. // return
  488. //}
  489. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  490. //sql = fmt.Sprintf(sql, calculateTableName)
  491. if condition != "" {
  492. sql += condition
  493. }
  494. _, err = o.Raw(sql, pars).QueryRows(&items)
  495. return
  496. }
  497. func GetRefreshEdbInfoFromCalculate(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  498. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  499. if err != nil && err.Error() != utils.ErrNoRow() {
  500. return
  501. }
  502. for _, item := range calculateList {
  503. if item.EdbType == 1 {
  504. baseEdbInfoArr = append(baseEdbInfoArr, item)
  505. } else {
  506. calculateInfoArr = append(calculateInfoArr, item)
  507. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromCalculate(item.EdbInfoId, item.Source)
  508. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  509. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  510. }
  511. }
  512. return
  513. }
  514. //获取基础指标对应的所有计算指标
  515. func GetEdbInfoAllCalculate(edbInfoId int) (list []*EdbInfo, err error) {
  516. o := orm.NewOrm()
  517. o.Using("data")
  518. sql := ` SELECT a.*,b.start_date,b.end_date,b.frequency FROM edb_info_calculate_mapping AS a
  519. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  520. WHERE a.from_edb_info_id=?
  521. GROUP BY a.edb_info_id
  522. ORDER BY a.edb_info_id ASC `
  523. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  524. return
  525. }
  526. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  527. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  528. if err != nil && err.Error() != utils.ErrNoRow() {
  529. return
  530. }
  531. for _, item := range calculateList {
  532. if item.EdbType == 1 {
  533. baseEdbInfoArr = append(baseEdbInfoArr, item)
  534. } else {
  535. calculateInfoArr = append(calculateInfoArr, item)
  536. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  537. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  538. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  539. }
  540. }
  541. return
  542. }
  543. func ModifyEdbInfoDataStatus(edbInfoId int64, source int, edbCode string) (err error) {
  544. o := orm.NewOrm()
  545. o.Using("data")
  546. sql := ``
  547. tableName := GetEdbDataTableName(source)
  548. sql = ` UPDATE %s SET edb_info_id=?, status=1,modify_time=NOW() WHERE edb_code=? `
  549. sql = fmt.Sprintf(sql, tableName)
  550. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  551. return
  552. }
  553. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  554. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  555. o := orm.NewOrm()
  556. o.Using("data")
  557. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  558. err = o.Raw(sql, classifyId).QueryRow(&item)
  559. return
  560. }
  561. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  562. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  563. o := orm.NewOrm()
  564. o.Using("data")
  565. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=? and sort > ?`
  566. if prevEdbInfoId > 0 {
  567. sql += ` or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` ) `
  568. }
  569. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  570. return
  571. }
  572. // Update 更新指标基础信息
  573. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  574. o := orm.NewOrm()
  575. o.Using("data")
  576. _, err = o.Update(edbInfo, cols...)
  577. return
  578. }
  579. type EdbInfoDataResp struct {
  580. EdbInfo *EdbInfo
  581. DataList []*EdbDataList
  582. }
  583. type EdbInfoReplaceReq struct {
  584. OldEdbInfoId int `description:"原指标ID"`
  585. NewEdbInfoId int `description:"替换为指标ID"`
  586. }
  587. /*
  588. 1、替换图库中的指标
  589. 2、替换计算指标中的指标,完了计算指标中的指标,全部重新计算
  590. */
  591. func EdbInfoReplace(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
  592. var errmsg string
  593. o := orm.NewOrm()
  594. o.Using("data")
  595. o.Begin()
  596. defer func() {
  597. if errmsg != "" {
  598. fmt.Println("errmsg:" + errmsg)
  599. }
  600. }()
  601. //替换图表
  602. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  603. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  604. _, err = o.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  605. if err != nil {
  606. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  607. return
  608. }
  609. replaceChartTotal = len(chartEdbMappingList)
  610. for _, mv := range chartEdbMappingList {
  611. //获取图表所有指标信息
  612. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  613. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  614. _, err = o.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  615. if err != nil {
  616. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  617. return
  618. }
  619. var minData, maxData float64
  620. minData = newEdbInfo.MinValue
  621. maxData = newEdbInfo.MaxValue
  622. for _, cv := range chartEdbList {
  623. if mv.IsAxis == cv.IsAxis {
  624. if minData > cv.MinData {
  625. minData = cv.MinData
  626. }
  627. if maxData < cv.MaxData {
  628. maxData = cv.MaxData
  629. }
  630. }
  631. }
  632. //修改图表关联指标
  633. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  634. _, err = o.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  635. if err != nil {
  636. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  637. return
  638. }
  639. }
  640. //替换计算指标
  641. //获取所有包含的计算指标
  642. mappingList := make([]*EdbInfoCalculateMapping, 0)
  643. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  644. _, err = o.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  645. if err != nil {
  646. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  647. return
  648. }
  649. replaceCalculateTotal = len(mappingList)
  650. //计算指标
  651. for _, mv := range mappingList {
  652. //替换原指标
  653. 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=? `
  654. _, err = o.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  655. if err != nil {
  656. return
  657. }
  658. }
  659. if err != nil && errmsg != "" {
  660. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  661. o.Rollback()
  662. } else {
  663. o.Commit()
  664. }
  665. //获取计算指标关联指标
  666. allMappingList := make([]*EdbInfoCalculateMapping, 0)
  667. allMapping := make(map[int]int)
  668. for _, v := range mappingList {
  669. if _, ok := allMapping[v.EdbInfoId]; !ok {
  670. allMappingList = append(allMappingList, v)
  671. }
  672. newList, _ := GetAllCalculate(v.EdbInfoId)
  673. for _, nv := range newList {
  674. if _, ok := allMapping[v.EdbInfoId]; !ok {
  675. allMappingList = append(allMappingList, nv)
  676. }
  677. allMapping[v.EdbInfoId] = v.EdbInfoId
  678. }
  679. allMapping[v.EdbInfoId] = v.EdbInfoId
  680. }
  681. for mk, mv := range allMappingList { //原指标关联的所有计算指标
  682. fmt.Println("mk/mv", mk, mv)
  683. //重新计算计算指标
  684. {
  685. edbInfo, err := GetEdbInfoById(mv.EdbInfoId) //计算指标
  686. if err != nil {
  687. if err.Error() == utils.ErrNoRow() {
  688. errmsg = "计算指标已被删除"
  689. return replaceChartTotal, replaceCalculateTotal, err
  690. }
  691. errmsg = "获取计算指标失败:err:" + err.Error()
  692. return replaceChartTotal, replaceCalculateTotal, err
  693. }
  694. fromEdbInfoList, err := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
  695. if err != nil {
  696. errmsg = "获取计算指标失败:err:" + err.Error()
  697. return replaceChartTotal, replaceCalculateTotal, err
  698. }
  699. if len(fromEdbInfoList) <= 0 {
  700. errmsg = "计算指标所依赖指标不存在"
  701. return replaceChartTotal, replaceCalculateTotal, err
  702. }
  703. fromEdbInfoItem := fromEdbInfoList[0]
  704. if fromEdbInfoItem == nil {
  705. return replaceChartTotal, replaceCalculateTotal, err
  706. }
  707. fromEdbInfo, err := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
  708. if err != nil {
  709. errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
  710. return replaceChartTotal, replaceCalculateTotal, err
  711. }
  712. edbCode := edbInfo.EdbCode
  713. uniqueCode := edbInfo.UniqueCode
  714. sourName := edbInfo.SourceName
  715. source := edbInfo.Source
  716. edbInfoId := edbInfo.EdbInfoId
  717. req := new(EdbInfoCalculateBatchSaveReq)
  718. req.EdbInfoId = edbInfoId
  719. req.EdbName = edbInfo.EdbName
  720. req.Frequency = edbInfo.Frequency
  721. req.Unit = edbInfo.Unit
  722. req.ClassifyId = edbInfo.ClassifyId
  723. req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  724. if source == utils.DATA_SOURCE_CALCULATE {
  725. //检验公式
  726. var formulaStr string
  727. var edbInfoIdBytes []string
  728. for _, v := range fromEdbInfoList {
  729. formulaStr += v.FromTag + ","
  730. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  731. }
  732. formulaStr = strings.Trim(formulaStr, ",")
  733. formulaMap := CheckFormula(edbInfo.CalculateFormula)
  734. for _, v := range formulaMap {
  735. if !strings.Contains(formulaStr, v) {
  736. errmsg = "公式错误,请重新填写"
  737. return replaceChartTotal, replaceCalculateTotal, err
  738. }
  739. }
  740. edbInfoList := make([]*EdbInfo, 0)
  741. for _, v := range fromEdbInfoList {
  742. edbInfo, err := GetEdbInfoById(v.FromEdbInfoId)
  743. if err != nil {
  744. if err.Error() == utils.ErrNoRow() {
  745. errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  746. return replaceChartTotal, replaceCalculateTotal, err
  747. }
  748. errmsg = "获取指标失败:Err:" + err.Error()
  749. return replaceChartTotal, replaceCalculateTotal, err
  750. }
  751. edbInfoList = append(edbInfoList, edbInfo)
  752. }
  753. //清除历史数据
  754. err = DeleteCalculateData(edbInfoId)
  755. if err != nil {
  756. errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
  757. return replaceChartTotal, replaceCalculateTotal, err
  758. }
  759. err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  760. if err != nil {
  761. errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
  762. return replaceChartTotal, replaceCalculateTotal, err
  763. }
  764. } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  765. _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  766. } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  767. sourName = "同比值"
  768. edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  769. } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  770. sourName = "同差值"
  771. edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  772. } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  773. sourName = "N数值移动平均计算"
  774. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  775. edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  776. } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  777. sourName = "环比值"
  778. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  779. edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  780. } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  781. sourName = "环差值"
  782. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  783. edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  784. } else if source == utils.DATA_SOURCE_CALCULATE_BP {
  785. sourName = "变频"
  786. fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  787. edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  788. } else {
  789. errmsg = "无效计算方式,source:" + strconv.Itoa(source)
  790. return replaceChartTotal, replaceCalculateTotal, err
  791. }
  792. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(source, edbCode)
  793. if err != nil && err.Error() != utils.ErrNoRow() {
  794. errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  795. return replaceChartTotal, replaceCalculateTotal, err
  796. }
  797. if maxAndMinItem != nil {
  798. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  799. }
  800. }
  801. }
  802. return
  803. }
  804. type EdbInfoView struct {
  805. EdbInfoId int `orm:"column(edb_info_id);pk"`
  806. SourceName string `description:"来源名称"`
  807. Source int `description:"来源id"`
  808. EdbCode string `description:"指标编码"`
  809. EdbName string `description:"指标名称"`
  810. EdbNameSource string `description:"指标名称来源"`
  811. Frequency string `description:"频率"`
  812. Unit string `description:"单位"`
  813. StartDate string `description:"起始日期"`
  814. EndDate string `description:"终止日期"`
  815. ClassifyId int `description:"分类id"`
  816. SysUserId int
  817. SysUserRealName string
  818. UniqueCode string `description:"指标唯一编码"`
  819. CreateTime string
  820. ModifyTime string
  821. MinValue float64 `description:"指标最小值"`
  822. MaxValue float64 `description:"指标最大值"`
  823. CalculateFormula string `description:"计算公式"`
  824. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  825. Sort int `description:"排序字段"`
  826. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  827. LatestDate string `description:"数据最新日期"`
  828. LatestValue float64 `description:"数据最新值"`
  829. }
  830. //获取指标的所有计算指标,以及计算指标所依赖计算指标
  831. func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  832. o := orm.NewOrm()
  833. o.Using("data")
  834. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  835. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  836. if err != nil {
  837. return
  838. }
  839. return
  840. }
  841. func GetAllCalculate(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  842. calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
  843. if err != nil && err.Error() != utils.ErrNoRow() {
  844. return
  845. }
  846. for _, item := range calculateList {
  847. mappingList = append(mappingList, item)
  848. newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId)
  849. mappingList = append(mappingList, newCalculateInfoArr...)
  850. }
  851. return
  852. }