excel_chart_edb.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. DateList []string `description:"日期列表"`
  92. ValList []float64 `description:"数据列表"`
  93. }
  94. // 同时添加指标和指标数据
  95. func (e *ExcelChartEdb) AddChartEdbAndData(list []*AddChartEdbAndDataItem, chartInfo *data_manage.ChartInfo, deleteEdbIds []int) (err error) {
  96. o, err := orm.NewOrmUsingDB("data").Begin()
  97. if err != nil {
  98. return
  99. }
  100. defer func() {
  101. if err != nil {
  102. _ = o.Rollback()
  103. } else {
  104. _ = o.Commit()
  105. }
  106. }()
  107. // 先删除原先的绑定的指标
  108. if len(deleteEdbIds) > 0 && chartInfo.ChartInfoId > 0 {
  109. sql := `DELETE FROM excel_chart_edb WHERE excel_chart_info_id = ? AND excel_chart_edb_id in (` + utils.GetOrmInReplace(len(deleteEdbIds)) + `)`
  110. _, err = o.Raw(sql, chartInfo.ChartInfoId, deleteEdbIds).Exec()
  111. if err != nil {
  112. err = fmt.Errorf("删除原先的指标失败:%v", err)
  113. return
  114. }
  115. }
  116. // 图表指标信息入库
  117. updateIds := make([]int, 0)
  118. var edbInfoIdArrStr []string
  119. for _, item := range list {
  120. err = addChartEdbAndData(o, item.ChartEdb, item.DateList, item.ValList)
  121. if err != nil {
  122. return
  123. }
  124. updateIds = append(updateIds, item.ChartEdb.ExcelChartEdbId)
  125. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(item.ChartEdb.ExcelChartEdbId))
  126. }
  127. //新增图表
  128. chartInfoId := chartInfo.ChartInfoId
  129. if chartInfo.ChartInfoId <= 0 {
  130. lastId, e := o.Insert(chartInfo)
  131. if e != nil {
  132. err = fmt.Errorf("新增图表失败,AddChartEdbAndData: %v", e)
  133. return
  134. }
  135. chartInfoId = int(lastId)
  136. } else {
  137. _, err = o.Update(chartInfo)
  138. if err != nil {
  139. err = fmt.Errorf("更新图表失败,AddChartEdbAndData: %v", e)
  140. return
  141. }
  142. }
  143. //更新图表id
  144. sql := `update excel_chart_edb set chart_info_id = ? where excel_chart_edb_id in (` + utils.GetOrmInReplace(len(updateIds)) + `)`
  145. _, err = o.Raw(sql, chartInfoId, updateIds).Exec()
  146. if err != nil {
  147. err = fmt.Errorf("更新图表id失败,AddChartEdbAndData: %v", err)
  148. return
  149. }
  150. if len(edbInfoIdArrStr) > 0 {
  151. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  152. //更新图表关联的指标id
  153. sql = `update chart_info set edb_info_ids = ? where chart_info_id = ?`
  154. _, err = o.Raw(sql, edbInfoIdStr, chartInfoId).Exec()
  155. }
  156. return
  157. }
  158. func addChartEdbAndData(o orm.TxOrmer, chartEdb *ExcelChartEdb, dateList []string, valList []float64) (err error) {
  159. // 图表指标信息入库
  160. excelChartEdbId := chartEdb.ExcelChartEdbId
  161. if chartEdb.ExcelChartEdbId <= 0 {
  162. lastId, e := o.Insert(chartEdb)
  163. if e != nil {
  164. err = fmt.Errorf("新增指标失败,addChartEdbAndData: %v", e)
  165. return
  166. }
  167. excelChartEdbId = int(lastId)
  168. } else {
  169. _, e := o.Update(chartEdb)
  170. if e != nil {
  171. err = fmt.Errorf("更新指标失败,addChartEdbAndData: %v", e)
  172. return
  173. }
  174. //如果有数据则删除所有的数据
  175. sql := `delete from excel_chart_data where excel_chart_edb_id = ?`
  176. _, err = o.Raw(sql, excelChartEdbId).Exec()
  177. if err != nil {
  178. return
  179. }
  180. }
  181. chartEdb.ExcelChartEdbId = excelChartEdbId
  182. // 图表指标数据入库
  183. addList := make([]*ExcelChartData, 0)
  184. if len(dateList) > 0 && len(valList) > 0 {
  185. for k, v := range dateList {
  186. chartData := &ExcelChartData{
  187. ExcelInfoId: chartEdb.ExcelInfoId,
  188. ExcelChartEdbId: chartEdb.ExcelChartEdbId,
  189. DataTime: v,
  190. Value: valList[k],
  191. ModifyTime: time.Now(),
  192. CreateTime: time.Now(),
  193. }
  194. //时间戳
  195. currentDate, er := time.Parse(utils.FormatDate, v)
  196. if er != nil {
  197. err = fmt.Errorf("时间格式化失败 err:%v", er)
  198. return
  199. }
  200. timestamp := currentDate.UnixNano() / 1e6
  201. chartData.DataTimestamp = timestamp
  202. addList = append(addList, chartData)
  203. // data信息入库
  204. if len(addList) > 1000 {
  205. _, err = o.InsertMulti(len(addList), len(addList))
  206. if err != nil {
  207. return
  208. }
  209. addList = addList[:0]
  210. }
  211. }
  212. }
  213. // data信息入库
  214. if len(addList) > 0 {
  215. _, err = o.InsertMulti(len(addList), len(addList))
  216. if err != nil {
  217. return
  218. }
  219. }
  220. return
  221. }
  222. func GetExcelChartEdbMappingByExcelInfoId(excelInfoId int) (list []*ExcelChartEdb, err error) {
  223. o := orm.NewOrmUsingDB("data")
  224. sql := ` SELECT *
  225. FROM excel_chart_edb
  226. WHERE excel_info_id=?
  227. ORDER BY excel_chart_edb_id ASC `
  228. _, err = o.Raw(sql, excelInfoId).QueryRows(&list)
  229. return
  230. }
  231. func GetExcelChartEdbMappingByExcelInfoIds(excelInfoIds []int) (list []*ExcelChartEdb, err error) {
  232. o := orm.NewOrmUsingDB("data")
  233. sql := ` SELECT *
  234. FROM excel_chart_edb
  235. WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  236. _, err = o.Raw(sql, excelInfoIds).QueryRows(&list)
  237. return
  238. }
  239. func GetExcelChartEdbById(id int) (item *ExcelChartEdb, err error) {
  240. o := orm.NewOrmUsingDB("data")
  241. sql := ` SELECT * FROM excel_chart_edb WHERE excel_chart_edb_id=? `
  242. err = o.Raw(sql, id).QueryRow(&item)
  243. return
  244. }
  245. func GetExcelChartEdbMappingByChartInfoId(chartInfoId int) (list []*ExcelChartEdb, err error) {
  246. o := orm.NewOrmUsingDB("data")
  247. sql := ` SELECT *
  248. FROM excel_chart_edb
  249. WHERE chart_info_id=?
  250. ORDER BY excel_chart_edb_id ASC `
  251. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  252. return
  253. }
  254. func GetExcelInfoByChartInfoId(chartInfoId int) (item *ExcelInfo, err error) {
  255. o := orm.NewOrmUsingDB("data")
  256. sql := ` SELECT i.*
  257. FROM excel_chart_edb e left join excel_info i on e.excel_info_id=i.excel_info_id
  258. WHERE e.chart_info_id=? limit 1`
  259. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  260. return
  261. }
  262. // 同时删除指标和指标数据
  263. func DeleteExcelChartEdbAndData(excelInfoIds []int, chartInfoIds []int) (err error) {
  264. o, err := orm.NewOrmUsingDB("data").Begin()
  265. if err != nil {
  266. return
  267. }
  268. defer func() {
  269. if err != nil {
  270. _ = o.Rollback()
  271. } else {
  272. _ = o.Commit()
  273. }
  274. }()
  275. // 把对应的表格状态改成删除状态
  276. //更新图表id
  277. sql := `update excel_info set is_delete = 1, modify_time = ? where excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  278. _, err = o.Raw(sql, time.Now(), excelInfoIds).Exec()
  279. if err != nil {
  280. err = fmt.Errorf("更新图表id失败,AddChartEdbAndData: %v", err)
  281. return
  282. }
  283. // 把删除图表状态
  284. if len(chartInfoIds) > 0 {
  285. sql := `DELETE FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(len(chartInfoIds)) + `)`
  286. _, err = o.Raw(sql, chartInfoIds).Exec()
  287. if err != nil {
  288. err = fmt.Errorf("删除原先的指标失败:%v", err)
  289. return
  290. }
  291. // todo 如果加入到我的图库中,则删除我的图库中的数据
  292. }
  293. // 删除原先的绑定的指标
  294. sql = `DELETE FROM excel_chart_edb WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  295. _, err = o.Raw(sql, excelInfoIds).Exec()
  296. if err != nil {
  297. err = fmt.Errorf("删除原先的指标失败:%v", err)
  298. return
  299. }
  300. // 删除指标数据
  301. sql = `DELETE FROM excel_chart_data WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  302. _, err = o.Raw(sql, excelInfoIds).Exec()
  303. if err != nil {
  304. err = fmt.Errorf("删除原先的指标失败:%v", err)
  305. return
  306. }
  307. return
  308. }