excel_chart_edb.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package excel
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type ExcelChartEdb struct {
  12. ExcelChartEdbId int `orm:"column(excel_chart_edb_id);pk"`
  13. ExcelInfoId int `description:"表格id"`
  14. ChartInfoId int `description:"图表id"`
  15. EdbCode string `description:"指标编码"`
  16. EdbName string `description:"指标名称"`
  17. EdbNameEn string `description:"指标英文名称"`
  18. Unit string `description:"指标单位"`
  19. UnitEn string `description:"指标单位"`
  20. DateSequence string `description:"日期序列选区"`
  21. DataSequence string `description:"数据序列选区"`
  22. SysUserId int `description:"创建人"`
  23. SysUserRealName string `description:"创建人姓名"`
  24. MaxData float64 `description:"上限"`
  25. MinData float64 `description:"下限"`
  26. IsOrder bool `description:"true:正序,false:逆序"`
  27. IsAxis int `description:"true:左轴,false:右轴"`
  28. EdbInfoType int `description:"true:标准指标,false:领先指标"`
  29. LeadValue int `description:"领先值"`
  30. LeadUnit string `description:"领先单位"`
  31. FromTag string `description:"标签"`
  32. ModifyTime time.Time `description:"修改时间"`
  33. CreateTime time.Time `description:"创建时间"`
  34. ChartWidth float64 `description:"线条大小"`
  35. }
  36. type ExcelChartEdbView struct {
  37. ExcelChartEdbId int
  38. ExcelInfoId int `description:"表格id"`
  39. ChartInfoId int `description:"图表id"`
  40. EdbCode string `description:"指标编码"`
  41. EdbName string `description:"指标名称"`
  42. DateSequenceStr string `description:"日期序列选区"`
  43. DataSequenceStr string `description:"数据序列选区"`
  44. /*MaxData float64 `description:"上限"`
  45. MinData float64 `description:"下限"`
  46. IsOrder bool `description:"true:正序,false:逆序"`
  47. IsAxis int `description:"true:左轴,false:右轴"`
  48. EdbInfoType int `description:"true:标准指标,false:领先指标"`
  49. LeadValue int `description:"领先值"`
  50. LeadUnit string `description:"领先单位"`*/
  51. FromTag string `description:"标签"`
  52. }
  53. type BalanceTableChart struct {
  54. ChartInfoId int `description:"图表id,新增时传0"`
  55. ChartName string `description:"图表名称"`
  56. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图"`
  57. Calendar string `description:"公历/农历"`
  58. LeftMin string `description:"图表左侧最小值"`
  59. LeftMax string `description:"图表左侧最大值"`
  60. RightMin string `description:"图表右侧最小值"`
  61. RightMax string `description:"图表右侧最大值"`
  62. Right2Min string `description:"图表右侧2最小值"`
  63. Right2Max string `description:"图表右侧2最大值"`
  64. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  65. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  66. ChartImage string `description:"封面图" json:"-"`
  67. SeasonExtraConfig string `description:"季节性图表中的配置,json数据"`
  68. SourcesFrom string `description:"图表来源"`
  69. // ChartEdbInfoList []ExcelChartEdbView
  70. }
  71. func (e *ExcelChartEdb) TableName() string {
  72. return "excel_chart_edb"
  73. }
  74. // 新增
  75. func (e *ExcelChartEdb) Add() (err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. _, err = o.Insert(e)
  78. return
  79. }
  80. // 修改
  81. func (e *ExcelChartEdb) Update(cols []string) (err error) {
  82. o := orm.NewOrmUsingDB("data")
  83. _, err = o.Update(e, cols...)
  84. return
  85. }
  86. // 删除
  87. func (e *ExcelChartEdb) Delete() (err error) {
  88. o := orm.NewOrmUsingDB("data")
  89. _, err = o.Delete(e)
  90. return
  91. }
  92. type AddChartEdbAndDataItem struct {
  93. ChartEdb *ExcelChartEdb
  94. DataList []*ExcelChartData `description:"数据列表"`
  95. }
  96. // 同时添加指标和指标数据
  97. func (e *ExcelChartEdb) AddChartEdbAndData(list []*AddChartEdbAndDataItem, chartInfo *data_manage.ChartInfo, deleteEdbIds []int) (err error) {
  98. o, err := orm.NewOrmUsingDB("data").Begin()
  99. if err != nil {
  100. return
  101. }
  102. defer func() {
  103. if err != nil {
  104. _ = o.Rollback()
  105. } else {
  106. _ = o.Commit()
  107. }
  108. }()
  109. // 先删除原先的绑定的指标
  110. if len(deleteEdbIds) > 0 && chartInfo.ChartInfoId > 0 {
  111. sql := `DELETE FROM excel_chart_edb WHERE chart_info_id = ? AND excel_chart_edb_id in (` + utils.GetOrmInReplace(len(deleteEdbIds)) + `)`
  112. _, err = o.Raw(sql, chartInfo.ChartInfoId, deleteEdbIds).Exec()
  113. if err != nil {
  114. err = fmt.Errorf("删除原先的指标失败:%v", err)
  115. return
  116. }
  117. }
  118. // 图表指标信息入库
  119. updateIds := make([]int, 0)
  120. var edbInfoIdArrStr []string
  121. for _, item := range list {
  122. err = addChartEdbAndData(o, item.ChartEdb, item.DataList)
  123. if err != nil {
  124. return
  125. }
  126. updateIds = append(updateIds, item.ChartEdb.ExcelChartEdbId)
  127. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(item.ChartEdb.ExcelChartEdbId))
  128. }
  129. //新增图表
  130. chartInfoId := chartInfo.ChartInfoId
  131. if chartInfo.ChartInfoId <= 0 {
  132. lastId, e := o.Insert(chartInfo)
  133. if e != nil {
  134. err = fmt.Errorf("新增图表失败,AddChartEdbAndData: %v", e)
  135. return
  136. }
  137. chartInfoId = int(lastId)
  138. } else {
  139. _, err = o.Update(chartInfo)
  140. if err != nil {
  141. err = fmt.Errorf("更新图表失败,AddChartEdbAndData: %v", e)
  142. return
  143. }
  144. }
  145. //更新图表id
  146. sql := `update excel_chart_edb set chart_info_id = ? where excel_chart_edb_id in (` + utils.GetOrmInReplace(len(updateIds)) + `) and chart_info_id=0`
  147. _, err = o.Raw(sql, chartInfoId, updateIds).Exec()
  148. if err != nil {
  149. err = fmt.Errorf("更新图表id失败,AddChartEdbAndData: %v", err)
  150. return
  151. }
  152. if len(edbInfoIdArrStr) > 0 {
  153. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  154. //更新图表关联的指标id
  155. sql = `update chart_info set edb_info_ids = ? where chart_info_id = ?`
  156. _, err = o.Raw(sql, edbInfoIdStr, chartInfoId).Exec()
  157. }
  158. if len(updateIds) > 0 {
  159. //更新图表数据
  160. sql = `update excel_chart_data set chart_info_id = ? where excel_chart_edb_id in (` + utils.GetOrmInReplace(len(updateIds)) + `) and chart_info_id=0`
  161. _, err = o.Raw(sql, chartInfoId, updateIds).Exec()
  162. if err != nil {
  163. err = fmt.Errorf("更新图表id失败,AddChartEdbAndData: %v", err)
  164. return
  165. }
  166. }
  167. return
  168. }
  169. func addChartEdbAndData(o orm.TxOrmer, chartEdb *ExcelChartEdb, dataList []*ExcelChartData) (err error) {
  170. // 图表指标信息入库
  171. excelChartEdbId := chartEdb.ExcelChartEdbId
  172. if chartEdb.ExcelChartEdbId <= 0 {
  173. lastId, e := o.Insert(chartEdb)
  174. if e != nil {
  175. err = fmt.Errorf("新增指标失败,addChartEdbAndData: %v", e)
  176. return
  177. }
  178. excelChartEdbId = int(lastId)
  179. } else {
  180. _, e := o.Update(chartEdb)
  181. if e != nil {
  182. err = fmt.Errorf("更新指标失败,addChartEdbAndData: %v", e)
  183. return
  184. }
  185. //如果有数据则删除所有的数据
  186. sql := `delete from excel_chart_data where excel_chart_edb_id = ?`
  187. _, err = o.Raw(sql, excelChartEdbId).Exec()
  188. if err != nil {
  189. return
  190. }
  191. }
  192. chartEdb.ExcelChartEdbId = excelChartEdbId
  193. // 图表指标数据入库
  194. addList := make([]*ExcelChartData, 0)
  195. if len(dataList) > 0 {
  196. for _, v := range dataList {
  197. chartData := &ExcelChartData{
  198. ExcelInfoId: chartEdb.ExcelInfoId,
  199. ExcelChartEdbId: chartEdb.ExcelChartEdbId,
  200. ChartInfoId: chartEdb.ChartInfoId,
  201. DataTime: v.DataTime,
  202. Value: v.Value,
  203. DataTimestamp: v.DataTimestamp,
  204. ModifyTime: time.Now(),
  205. CreateTime: time.Now(),
  206. }
  207. addList = append(addList, chartData)
  208. // data信息入库
  209. if len(addList) > 1000 {
  210. _, err = o.InsertMulti(len(addList), addList)
  211. if err != nil {
  212. return
  213. }
  214. addList = addList[:0]
  215. }
  216. }
  217. }
  218. // data信息入库
  219. if len(addList) > 0 {
  220. _, err = o.InsertMulti(len(addList), addList)
  221. if err != nil {
  222. return
  223. }
  224. }
  225. return
  226. }
  227. func GetExcelChartEdbMappingByExcelInfoId(excelInfoId int) (list []*ExcelChartEdb, err error) {
  228. o := orm.NewOrmUsingDB("data")
  229. sql := ` SELECT *
  230. FROM excel_chart_edb
  231. WHERE excel_info_id=?
  232. ORDER BY chart_info_id asc, excel_chart_edb_id ASC `
  233. _, err = o.Raw(sql, excelInfoId).QueryRows(&list)
  234. return
  235. }
  236. func GetExcelChartEdbMappingByExcelInfoIds(excelInfoIds []int) (list []*ExcelChartEdb, err error) {
  237. o := orm.NewOrmUsingDB("data")
  238. sql := ` SELECT *
  239. FROM excel_chart_edb
  240. WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  241. _, err = o.Raw(sql, excelInfoIds).QueryRows(&list)
  242. return
  243. }
  244. func GetExcelChartEdbById(id int) (item *ExcelChartEdb, err error) {
  245. o := orm.NewOrmUsingDB("data")
  246. sql := ` SELECT * FROM excel_chart_edb WHERE excel_chart_edb_id=? `
  247. err = o.Raw(sql, id).QueryRow(&item)
  248. return
  249. }
  250. func GetExcelChartEdbMappingByChartInfoId(chartInfoId int) (list []*ExcelChartEdb, err error) {
  251. o := orm.NewOrmUsingDB("data")
  252. sql := ` SELECT *
  253. FROM excel_chart_edb
  254. WHERE chart_info_id=?
  255. ORDER BY excel_chart_edb_id ASC `
  256. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  257. return
  258. }
  259. func GetExcelInfoByChartInfoId(chartInfoId int) (item *ExcelInfo, err error) {
  260. o := orm.NewOrmUsingDB("data")
  261. sql := ` SELECT i.*
  262. FROM excel_chart_edb e left join excel_info i on e.excel_info_id=i.excel_info_id
  263. WHERE e.chart_info_id=? limit 1`
  264. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  265. return
  266. }
  267. // 同时删除指标和指标数据
  268. func DeleteExcelChartEdbAndData(excelInfoIds []int, chartInfoIds []int) (err error) {
  269. o, err := orm.NewOrmUsingDB("data").Begin()
  270. if err != nil {
  271. return
  272. }
  273. defer func() {
  274. if err != nil {
  275. _ = o.Rollback()
  276. } else {
  277. _ = o.Commit()
  278. }
  279. }()
  280. // 把对应的表格状态改成删除状态
  281. //更新图表id
  282. sql := `update excel_info set is_delete = 1, modify_time = ? where excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  283. _, err = o.Raw(sql, time.Now(), excelInfoIds).Exec()
  284. if err != nil {
  285. err = fmt.Errorf("更新图表id失败,AddChartEdbAndData: %v", err)
  286. return
  287. }
  288. // 把删除图表状态
  289. if len(chartInfoIds) > 0 {
  290. sql := `DELETE FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(len(chartInfoIds)) + `)`
  291. _, err = o.Raw(sql, chartInfoIds).Exec()
  292. if err != nil {
  293. err = fmt.Errorf("删除原先的指标失败:%v", err)
  294. return
  295. }
  296. // todo 如果加入到我的图库中,则删除我的图库中的数据
  297. }
  298. // 删除原先的绑定的指标
  299. sql = `DELETE FROM excel_chart_edb WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  300. _, err = o.Raw(sql, excelInfoIds).Exec()
  301. if err != nil {
  302. err = fmt.Errorf("删除原先的指标失败:%v", err)
  303. return
  304. }
  305. // 删除指标数据
  306. sql = `DELETE FROM excel_chart_data WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  307. _, err = o.Raw(sql, excelInfoIds).Exec()
  308. if err != nil {
  309. err = fmt.Errorf("删除原先的指标失败:%v", err)
  310. return
  311. }
  312. return
  313. }
  314. // 删除平衡表中的指标和数据
  315. func DeleteBalanceExcelChartInfoAndData(chartInfoId int) (err error) {
  316. o := orm.NewOrmUsingDB("data")
  317. to, err := o.Begin()
  318. if err != nil {
  319. return
  320. }
  321. defer func() {
  322. if err != nil {
  323. _ = to.Rollback()
  324. } else {
  325. _ = to.Commit()
  326. }
  327. }()
  328. sql := ` DELETE FROM chart_info WHERE chart_info_id=? `
  329. _, err = to.Raw(sql, chartInfoId).Exec()
  330. if err != nil {
  331. err = fmt.Errorf("删除平衡表图表失败 %s", err.Error())
  332. return
  333. }
  334. sql = ` DELETE FROM excel_chart_edb WHERE chart_info_id=? `
  335. _, err = to.Raw(sql, chartInfoId).Exec()
  336. if err != nil {
  337. err = fmt.Errorf("删除平衡表图表指标失败 %s", err.Error())
  338. return
  339. }
  340. // 删除表格里的数据
  341. sql = ` DELETE FROM excel_chart_data WHERE chart_info_id=? `
  342. _, err = to.Raw(sql, chartInfoId).Exec()
  343. if err != nil {
  344. err = fmt.Errorf("删除平衡表图表指标失败 %s", err.Error())
  345. return
  346. }
  347. return
  348. }
  349. func EditBalanceChartBaseInfoAndEdbEnInfo(req *data_manage.EditChartInfoBaseReq, chartItem *data_manage.ChartInfo, lang string) (err error) {
  350. o := orm.NewOrmUsingDB("data")
  351. to, err := o.Begin()
  352. if err != nil {
  353. return
  354. }
  355. defer func() {
  356. if err != nil {
  357. _ = to.Rollback()
  358. } else {
  359. _ = to.Commit()
  360. }
  361. }()
  362. updateChartCols := make([]string, 0)
  363. switch lang {
  364. case utils.EnLangVersion:
  365. chartItem.ChartNameEn = req.ChartName
  366. updateChartCols = append(updateChartCols, "ChartNameEn")
  367. default:
  368. chartItem.ChartName = req.ChartName
  369. updateChartCols = append(updateChartCols, "ChartName")
  370. }
  371. if req.ExtraConfig != `` {
  372. chartItem.ExtraConfig = req.ExtraConfig
  373. updateChartCols = append(updateChartCols, "ExtraConfig")
  374. }
  375. chartItem.ModifyTime = time.Now()
  376. updateChartCols = append(updateChartCols, "ModifyTime")
  377. _, err = to.Update(chartItem, updateChartCols...)
  378. if err != nil {
  379. fmt.Println("UPDATE chart_info Err:", err.Error())
  380. return err
  381. }
  382. var edbInfoIdArr []string
  383. for _, v := range req.ChartEdbInfoList {
  384. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  385. var count int
  386. csql := `SELECT COUNT(1) AS count FROM excel_chart_edb WHERE chart_info_id=? AND excel_chart_edb_id=? `
  387. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  388. if err != nil {
  389. fmt.Println("QueryRow Err:", err.Error())
  390. return err
  391. }
  392. if count > 0 {
  393. msql := ` UPDATE excel_chart_edb SET modify_time = NOW() `
  394. pars := make([]interface{}, 0)
  395. switch lang {
  396. case utils.EnLangVersion:
  397. msql += ` ,edb_name_en = ? `
  398. pars = append(pars, v.EdbName)
  399. default:
  400. msql += ` ,edb_name = ? `
  401. pars = append(pars, v.EdbName)
  402. }
  403. msql += ` WHERE excel_chart_edb_id = ? `
  404. pars = append(pars, v.EdbInfoId)
  405. _, err = to.Raw(msql, pars...).Exec()
  406. if err != nil {
  407. fmt.Println("edb_info Err:" + err.Error())
  408. return err
  409. }
  410. }
  411. }
  412. return
  413. }
  414. func GetBalanceChartEdbByCondition(condition string, pars []interface{}) (item *ExcelChartEdb, err error) {
  415. o := orm.NewOrmUsingDB("data")
  416. sql := ` SELECT * FROM excel_chart_edb WHERE 1=1 `
  417. if condition != "" {
  418. sql += condition
  419. }
  420. err = o.Raw(sql, pars).QueryRow(&item)
  421. return
  422. }