12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package eta
- import (
- "eta/eta_bridge/global"
- "gorm.io/gorm/clause"
- "time"
- )
- type SysDepartment struct {
- DepartmentId int `gorm:"primaryKey;column:department_id;type:int(11);not null" json:"department_id"`
- DepartmentName string `gorm:"unique;column:department_name;type:varchar(255);default:''" json:"department_name"` // 部门名称
- OutID string `gorm:"column:out_id" json:"out_id"` // 外部id
- CreateTime time.Time `gorm:"column:create_time;type:datetime" json:"create_time"` // 创建时间
- Sort int `gorm:"column:sort;type:int(10);not null;default:0" json:"sort"` // 排序
- }
- func (m *SysDepartment) TableName() string {
- return "sys_department"
- }
- // Create 新增部门
- func (m *SysDepartment) Create() (err error) {
- err = global.MYSQL["hz_eta"].Create(m).Error
- return
- }
- // Update 更新部门
- func (m *SysDepartment) Update(cols []string) (err error) {
- err = global.MYSQL["hz_eta"].Model(m).Select(cols).Updates(m).Error
- return
- }
- // GetDepartmentById 主键获取部门
- func GetDepartmentById(departmentId int) (item *SysDepartment, err error) {
- err = global.MYSQL["hz_eta"].Where("department_id = ?", departmentId).First(&item).Error
- return
- }
- // GetAllDepartment 获取所有部门
- func GetAllDepartment() (items []*SysDepartment, err error) {
- err = global.MYSQL["hz_eta"].Find(&items).Error
- return
- }
- // DeleteDepartmentById 主键删除部门
- func DeleteDepartmentById(departmentId int) (err error) {
- sql := `DELETE FROM sys_department WHERE department_id = ? LIMIT 1`
- err = global.MYSQL["hz_eta"].Exec(sql, departmentId).Error
- return
- }
- func BatchInsertOrUpdate(departments []SysDepartment) (err error) {
- db := global.MYSQL["hz_eta"]
- OnConflictFunc := clause.OnConflict{
- Columns: []clause.Column{{Name: "department_id"}},
- DoUpdates: clause.AssignmentColumns([]string{"department_name", "sort"}),
- }
- // 执行批量插入或更新操作
- err = db.Clauses(OnConflictFunc).Create(&departments).Error
- return
- }
|