sys_department.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package eta
  2. import (
  3. "eta_gn/eta_bridge/global"
  4. "fmt"
  5. "strings"
  6. "time"
  7. )
  8. type SysDepartment struct {
  9. DepartmentId int `gorm:"primaryKey;column:department_id;type:int(11);not null" json:"department_id"`
  10. DepartmentName string `gorm:"unique;column:department_name;type:varchar(255);default:''" json:"department_name"` // 部门名称
  11. OutID string `gorm:"column:out_id" json:"out_id"` // 外部id
  12. CreateTime time.Time `gorm:"column:create_time;type:datetime" json:"create_time"` // 创建时间
  13. Sort int `gorm:"column:sort;type:int(10);not null;default:0" json:"sort"` // 排序
  14. }
  15. func (m *SysDepartment) TableName() string {
  16. return "sys_department"
  17. }
  18. // Create 新增部门
  19. func (m *SysDepartment) Create() (err error) {
  20. err = global.MYSQL["hz_eta"].Create(m).Error
  21. return
  22. }
  23. // Update 更新部门
  24. func (m *SysDepartment) Update(cols []string) (err error) {
  25. err = global.MYSQL["hz_eta"].Model(m).Select(cols).Updates(m).Error
  26. return
  27. }
  28. // GetDepartmentById 主键获取部门
  29. func GetDepartmentById(departmentId int) (item *SysDepartment, err error) {
  30. err = global.MYSQL["hz_eta"].Where("department_id = ?", departmentId).First(&item).Error
  31. return
  32. }
  33. // GetAllDepartment 获取所有部门
  34. func GetAllDepartment() (items []*SysDepartment, err error) {
  35. //sql := `SELECT * FROM sys_department WHERE out_id <> ""`
  36. sql := `SELECT * FROM sys_department `
  37. err = global.MYSQL["hz_eta"].Raw(sql).Find(&items).Error
  38. return
  39. }
  40. // DeleteDepartmentById 主键删除部门
  41. func DeleteDepartmentById(departmentId int) (err error) {
  42. sql := `DELETE FROM sys_department WHERE department_id = ? LIMIT 1`
  43. err = global.MYSQL["hz_eta"].Exec(sql, departmentId).Error
  44. return
  45. }
  46. func (m *SysDepartment) GetItemByCondition(condition string, pars []interface{}) (item *SysDepartment, err error) {
  47. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  48. err = global.MYSQL["hz_eta"].Raw(sql, pars...).First(&item).Error
  49. return
  50. }
  51. func (m *SysDepartment) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysDepartment, err error) {
  52. fields := strings.Join(fieldArr, ",")
  53. if len(fieldArr) == 0 {
  54. fields = `*`
  55. }
  56. order := ``
  57. if orderRule != "" {
  58. order = ` ORDER BY ` + orderRule
  59. }
  60. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  61. err = global.MYSQL["hz_eta"].Raw(sql, pars...).Find(&items).Error
  62. return
  63. }
  64. // GetSysDepartmentByOutId 获取部门-根据OutId
  65. func GetSysDepartmentByOutId(outId string) (item *SysDepartment, err error) {
  66. err = global.MYSQL["hz_eta"].Where("out_id = ?", outId).First(&item).Error
  67. return
  68. }