excel_chart_edb.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package excel
  2. import (
  3. "eta/eta_mobile/models/data_manage"
  4. "eta/eta_mobile/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. return
  159. }
  160. func addChartEdbAndData(o orm.TxOrmer, chartEdb *ExcelChartEdb, dataList []*ExcelChartData) (err error) {
  161. // 图表指标信息入库
  162. excelChartEdbId := chartEdb.ExcelChartEdbId
  163. if chartEdb.ExcelChartEdbId <= 0 {
  164. lastId, e := o.Insert(chartEdb)
  165. if e != nil {
  166. err = fmt.Errorf("新增指标失败,addChartEdbAndData: %v", e)
  167. return
  168. }
  169. excelChartEdbId = int(lastId)
  170. } else {
  171. _, e := o.Update(chartEdb)
  172. if e != nil {
  173. err = fmt.Errorf("更新指标失败,addChartEdbAndData: %v", e)
  174. return
  175. }
  176. //如果有数据则删除所有的数据
  177. sql := `delete from excel_chart_data where excel_chart_edb_id = ?`
  178. _, err = o.Raw(sql, excelChartEdbId).Exec()
  179. if err != nil {
  180. return
  181. }
  182. }
  183. chartEdb.ExcelChartEdbId = excelChartEdbId
  184. // 图表指标数据入库
  185. addList := make([]*ExcelChartData, 0)
  186. if len(dataList) > 0 {
  187. for _, v := range dataList {
  188. chartData := &ExcelChartData{
  189. ExcelInfoId: chartEdb.ExcelInfoId,
  190. ExcelChartEdbId: chartEdb.ExcelChartEdbId,
  191. ChartInfoId: chartEdb.ChartInfoId,
  192. DataTime: v.DataTime,
  193. Value: v.Value,
  194. DataTimestamp: v.DataTimestamp,
  195. ModifyTime: time.Now(),
  196. CreateTime: time.Now(),
  197. }
  198. addList = append(addList, chartData)
  199. // data信息入库
  200. if len(addList) > 1000 {
  201. _, err = o.InsertMulti(len(addList), addList)
  202. if err != nil {
  203. return
  204. }
  205. addList = addList[:0]
  206. }
  207. }
  208. }
  209. // data信息入库
  210. if len(addList) > 0 {
  211. _, err = o.InsertMulti(len(addList), addList)
  212. if err != nil {
  213. return
  214. }
  215. }
  216. return
  217. }
  218. func GetExcelChartEdbMappingByExcelInfoId(excelInfoId int) (list []*ExcelChartEdb, err error) {
  219. o := orm.NewOrmUsingDB("data")
  220. sql := ` SELECT *
  221. FROM excel_chart_edb
  222. WHERE excel_info_id=?
  223. ORDER BY chart_info_id asc, excel_chart_edb_id ASC `
  224. _, err = o.Raw(sql, excelInfoId).QueryRows(&list)
  225. return
  226. }
  227. func GetExcelChartEdbMappingByExcelInfoIds(excelInfoIds []int) (list []*ExcelChartEdb, err error) {
  228. o := orm.NewOrmUsingDB("data")
  229. sql := ` SELECT *
  230. FROM excel_chart_edb
  231. WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  232. _, err = o.Raw(sql, excelInfoIds).QueryRows(&list)
  233. return
  234. }
  235. func GetExcelChartEdbById(id int) (item *ExcelChartEdb, err error) {
  236. o := orm.NewOrmUsingDB("data")
  237. sql := ` SELECT * FROM excel_chart_edb WHERE excel_chart_edb_id=? `
  238. err = o.Raw(sql, id).QueryRow(&item)
  239. return
  240. }
  241. func GetExcelChartEdbMappingByChartInfoId(chartInfoId int) (list []*ExcelChartEdb, err error) {
  242. o := orm.NewOrmUsingDB("data")
  243. sql := ` SELECT *
  244. FROM excel_chart_edb
  245. WHERE chart_info_id=?
  246. ORDER BY excel_chart_edb_id ASC `
  247. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  248. return
  249. }
  250. func GetExcelInfoByChartInfoId(chartInfoId int) (item *ExcelInfo, err error) {
  251. o := orm.NewOrmUsingDB("data")
  252. sql := ` SELECT i.*
  253. FROM excel_chart_edb e left join excel_info i on e.excel_info_id=i.excel_info_id
  254. WHERE e.chart_info_id=? limit 1`
  255. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  256. return
  257. }
  258. // 同时删除指标和指标数据
  259. func DeleteExcelChartEdbAndData(excelInfoIds []int, chartInfoIds []int) (err error) {
  260. o, err := orm.NewOrmUsingDB("data").Begin()
  261. if err != nil {
  262. return
  263. }
  264. defer func() {
  265. if err != nil {
  266. _ = o.Rollback()
  267. } else {
  268. _ = o.Commit()
  269. }
  270. }()
  271. // 把对应的表格状态改成删除状态
  272. //更新图表id
  273. sql := `update excel_info set is_delete = 1, modify_time = ? where excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  274. _, err = o.Raw(sql, time.Now(), excelInfoIds).Exec()
  275. if err != nil {
  276. err = fmt.Errorf("更新图表id失败,AddChartEdbAndData: %v", err)
  277. return
  278. }
  279. // 把删除图表状态
  280. if len(chartInfoIds) > 0 {
  281. sql := `DELETE FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(len(chartInfoIds)) + `)`
  282. _, err = o.Raw(sql, chartInfoIds).Exec()
  283. if err != nil {
  284. err = fmt.Errorf("删除原先的指标失败:%v", err)
  285. return
  286. }
  287. // todo 如果加入到我的图库中,则删除我的图库中的数据
  288. }
  289. // 删除原先的绑定的指标
  290. sql = `DELETE FROM excel_chart_edb WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  291. _, err = o.Raw(sql, excelInfoIds).Exec()
  292. if err != nil {
  293. err = fmt.Errorf("删除原先的指标失败:%v", err)
  294. return
  295. }
  296. // 删除指标数据
  297. sql = `DELETE FROM excel_chart_data WHERE excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `)`
  298. _, err = o.Raw(sql, excelInfoIds).Exec()
  299. if err != nil {
  300. err = fmt.Errorf("删除原先的指标失败:%v", err)
  301. return
  302. }
  303. return
  304. }
  305. // 删除平衡表中的指标和数据
  306. func DeleteBalanceExcelChartInfoAndData(chartInfoId int) (err error) {
  307. o := orm.NewOrmUsingDB("data")
  308. to, err := o.Begin()
  309. if err != nil {
  310. return
  311. }
  312. defer func() {
  313. if err != nil {
  314. _ = to.Rollback()
  315. } else {
  316. _ = to.Commit()
  317. }
  318. }()
  319. sql := ` DELETE FROM chart_info WHERE chart_info_id=? `
  320. _, err = to.Raw(sql, chartInfoId).Exec()
  321. if err != nil {
  322. err = fmt.Errorf("删除平衡表图表失败 %s", err.Error())
  323. return
  324. }
  325. sql = ` DELETE FROM excel_chart_edb 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. // 删除表格里的数据
  332. sql = ` DELETE FROM excel_chart_data WHERE chart_info_id=? `
  333. _, err = to.Raw(sql, chartInfoId).Exec()
  334. if err != nil {
  335. err = fmt.Errorf("删除平衡表图表指标失败 %s", err.Error())
  336. return
  337. }
  338. return
  339. }