123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package models
- import (
- "eta/eta_forum_hub/utils"
- "time"
- "github.com/beego/beego/v2/client/orm"
- )
- type ChartDescription struct {
- Id int `gorm:"column:id;primaryKey"`
- Description string `gorm:"column:description"`
- ChartInfoId int `gorm:"column:chart_info_id"` // 图表id
- SysUserId int `gorm:"column:sys_user_id"` // 创建人id
- SysUserRealName string `gorm:"column:sys_user_real_name"` // 创建人姓名
- ModifyTime time.Time `gorm:"column:modify_time"` // 变更时间
- CreateTime time.Time `gorm:"column:create_time"` // 关系建立时间
- }
- type ChartDescriptionReq struct {
- Id int
- Description string // 图表id
- ChartInfoId int
- SysUserId int // 创建人id
- SysUserRealName string // 创建人姓名
- AdminName string // 创建人姓名
- ModifyTime time.Time // 变更时间
- CreateTime time.Time // 关系建立时间
- }
- // GetChartDescriptionByChartInfoId 根据图表ID获取图表简介
- func GetChartDescriptionByChartInfoId(chartInfoId int) (item []*ChartDescription, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM chart_description WHERE chart_info_id = ? order by create_time desc, id desc`
- _, err = o.Raw(sql, chartInfoId).QueryRows(&item)
- return
- }
- // 批量更新
- func BatchUpdateChartDescription(chartInfoId int, descriptionList []*ChartDescription) (err error) {
- o := orm.NewOrm()
- // 用事务
- tx, err := o.Begin()
- if err != nil {
- return err
- }
- defer func () {
- if err != nil {
- tx.Rollback()
- }else{
- tx.Commit()
- }
- }()
-
- // 批量更新
- if len(descriptionList) == 0 {
- // 删除所有记录
- sql := `DELETE FROM chart_description WHERE chart_info_id = ?`
- _, err = tx.Raw(sql, chartInfoId).Exec()
- return
- }
- // 删除数组
- deleteIdMap := make(map[int]bool)
- // 查询现有记录
- existingRecords := make(map[int]*ChartDescription)
- var existing []*ChartDescription
- sql := `SELECT * FROM chart_description WHERE chart_info_id = ?`
- _, err = o.Raw(sql, chartInfoId).QueryRows(&existing)
- if err != nil {
- return err
- }
- for _, record := range existing {
- existingRecords[record.Id] = record
- deleteIdMap[record.Id] = true
- }
- // 分离需要更新和插入的记录
- var toInsert []*ChartDescription
- for _, desc := range descriptionList {
- delete(deleteIdMap, desc.Id)
- if _, exists := existingRecords[desc.Id]; exists {
- // 更新
- sql := "UPDATE chart_description SET description = ?, sys_user_id = ?, sys_user_real_name = ?, modify_time = ? WHERE id = ?"
- _, err = tx.Raw(sql, desc.Description, desc.SysUserId, desc.SysUserRealName, desc.ModifyTime, desc.Id).Exec()
- if err != nil {
- return err
- }
- } else {
- toInsert = append(toInsert, desc)
- }
- }
- // 批量插入
- if len(toInsert) > 0 {
- _, err := tx.InsertMulti(len(toInsert), toInsert)
- if err != nil {
- return err
- }
- }
-
- // 删除数组中为true的记录
- deleteIds := make([]int, 0)
- for id, _ := range deleteIdMap {
- deleteIds = append(deleteIds, id)
- }
- if len(deleteIds) > 0 {
- sql := `DELETE FROM chart_description WHERE id IN (`+utils.GetOrmInReplace(len(deleteIds))+`) AND chart_info_id = ?`
- _, err = tx.Raw(sql, deleteIds, chartInfoId).Exec()
- }
-
- return
- }
|