package supply_analysis import ( "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "fmt" "time" "gorm.io/gorm" ) // Variety variety 品种表 type Variety struct { VarietyId int `orm:"column(variety_id);pk" gorm:"primaryKey" ` VarietyName string `description:"品种名称"` LastUpdateSysUserId int `description:"最后更新人id"` LastUpdateSysUserRealName string `description:"最后更新人名称"` ProductionDay int `description:"生产天数"` SysUserId int `description:"创建人id"` SysUserRealName string `description:"创建人姓名"` ModifyTime time.Time `description:"修改时间"` CreateTime time.Time `description:"创建时间"` } // GetVarietyById 根据品种id获取品种详情 func GetVarietyById(id int) (item *Variety, err error) { sql := `SELECT * FROM variety WHERE variety_id = ?` err = global.DmSQL["data"].Raw(sql, id).First(&item).Error return } // GetVarietyByName 根据品种名称获取品种详情 func GetVarietyByName(name string) (item *Variety, err error) { sql := `SELECT * FROM variety WHERE variety_name = ?` err = global.DmSQL["data"].Raw(sql, name).First(&item).Error return } // CreateVariety 添加品种 func CreateVariety(item *Variety, adminIdList []int) (err error) { to := global.DmSQL["data"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() err = to.Create(item).Error if err != nil { return } varietyAdminPermissionList := make([]*VarietyAdminPermission, 0) for _, adminId := range adminIdList { varietyAdminPermissionList = append(varietyAdminPermissionList, &VarietyAdminPermission{ //Id: 0, VarietyId: item.VarietyId, SysUserId: adminId, CreateTime: time.Now(), }) } if len(varietyAdminPermissionList) > 0 { err = to.CreateInBatches(varietyAdminPermissionList, utils.MultiAddNum).Error } return } // EditVariety 编辑品种 func EditVariety(item *Variety, adminIdList []int) (err error) { to := global.DmSQL["data"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() err = to.Select("VarietyName", "LastUpdateSysUserId", "LastUpdateSysUserRealName", "ModifyTime").Updates(item).Error if err != nil { return } // 删除历史的权限配置 sql := `DELETE FROM variety_admin_permission where variety_id = ? ` err = to.Exec(sql, item.VarietyId).Error if err != nil { return } // 添加新的权限配置 varietyAdminPermissionList := make([]*VarietyAdminPermission, 0) for _, adminId := range adminIdList { varietyAdminPermissionList = append(varietyAdminPermissionList, &VarietyAdminPermission{ //Id: 0, VarietyId: item.VarietyId, SysUserId: adminId, CreateTime: time.Now(), }) } if len(varietyAdminPermissionList) > 0 { err = to.CreateInBatches(varietyAdminPermissionList, utils.MultiAddNum).Error } return } // VarietyItem 列表页返回的数据结构 type VarietyItem struct { VarietyId int `orm:"column(variety_id);pk" gorm:"primaryKey" ` VarietyName string `description:"品种名称"` ProductionDay int `description:"生产天数"` LastUpdateSysUserId int `description:"最后更新人id"` LastUpdateSysUserRealName string `description:"最后更新人名称"` PermissionUserId string `description:"有操作权限的用户id"` ModifyTime string `description:"修改时间"` CreateTime string `description:"创建时间"` Button VarietyButton `gorm:"-" description:"操作按钮权限"` } type VarietyButton struct { Edit bool `description:"操作权限"` Delete bool `description:"删除权限"` Analyse bool `description:"分析权限"` } func (v *VarietyItem) AfterFind(db *gorm.DB) error { tmpCreateTime, err := time.Parse(utils.FormatDateWallWithLoc, v.CreateTime) if err != nil { return err } tmpModifyTime, err := time.Parse(utils.FormatDateWallWithLoc, v.ModifyTime) if err != nil { return err } v.CreateTime = utils.TimeTransferString(utils.FormatDateTime, tmpCreateTime) v.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, tmpModifyTime) return nil } // GetListBySuperAdminPage 不区分是否有分析权限的获取分页数据 func (item Variety) GetListBySuperAdminPage(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*VarietyItem, err error) { baseSql := ` FROM ( SELECT a.variety_id,a.variety_name,a.last_update_sys_user_id, a.last_update_sys_user_real_name,a.production_day,a.sys_user_id,a.sys_user_real_name,a.modify_time,a.create_time,LISTAGG(b.sys_user_id, ',') WITHIN GROUP (ORDER BY b.sys_user_id ASC) AS permission_user_id FROM variety a LEFT JOIN variety_admin_permission b on a.variety_id=b.variety_id LEFT JOIN variety_edb_info c on a.variety_id=c.variety_id WHERE 1=1 ` if condition != "" { baseSql += condition } baseSql += ` GROUP BY a.variety_id,a.variety_name,a.last_update_sys_user_id, a.last_update_sys_user_real_name,a.production_day,a.sys_user_id,a.sys_user_real_name,a.modify_time,a.create_time ) d ` // 数据总数 totalSql := `SELECT COUNT(1) total ` + baseSql err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&total).Error if err != nil { fmt.Println("Count Err:", err) return } // 列表页数据 listSql := `SELECT * ` + baseSql + ` ORDER BY modify_time DESC,variety_id DESC LIMIT ?,?` pars = append(pars, startSize) pars = append(pars, pageSize) err = global.DmSQL["data"].Raw(listSql, pars...).Find(&items).Error return } // GetListByPage 获取分页数据 func (item Variety) GetListByPage(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*VarietyItem, err error) { baseSql := ` FROM ( SELECT a.variety_id,a."variety_name",a."production_day",a."last_update_sys_user_id",a."last_update_sys_user_real_name",a."modify_time",a."create_time", LISTAGG(DISTINCT b.sys_user_id , ',') WITHIN GROUP (ORDER BY b.sys_user_id asc) AS permission_user_id FROM variety a JOIN variety_admin_permission b on a.variety_id=b.variety_id JOIN variety_edb_info c on a.variety_id=c.variety_id WHERE 1=1 ` if condition != "" { baseSql += condition } baseSql += ` GROUP BY a.variety_id,a."variety_name",a."production_day",a."last_update_sys_user_id",a."last_update_sys_user_real_name",a."modify_time",a."create_time" ) d ` // 数据总数 totalSql := `SELECT COUNT(1) total ` + baseSql err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&total).Error if err != nil { return } // 列表页数据 listSql := `SELECT * ` + baseSql + ` ORDER BY modify_time DESC,variety_id DESC LIMIT ?,?` pars = append(pars, startSize) pars = append(pars, pageSize) err = global.DmSQL["data"].Raw(listSql, pars...).Scan(&items).Error return } // Update 更新基础信息 func (variety *Variety) Update(cols []string) (err error) { err = global.DmSQL["data"].Select(cols).Updates(variety).Error return } // Delete 删除 func (variety *Variety) Delete() (err error) { err = global.DmSQL["data"].Delete(variety).Error return } type VarietyView struct { VarietyId int `orm:"column(future_good_chart_classify_id);pk" gorm:"primaryKey" ` VarietyName string `description:"分类名称"` ParentId int `description:"父级id"` }