excel_chart_edb.go 12 KB

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