excel_chart_edb.go 14 KB

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