edb_info.go 30 KB

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