123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package system
- import (
- "github.com/beego/beego/v2/client/orm"
- "time"
- )
- type SysDepartmentAddReq struct {
- DepartmentName string `description:"部门名称"`
- }
- type SysDepartment struct {
- DepartmentId int `orm:"column(department_id);pk" description:"部门Id"`
- DepartmentName string `description:"部门名称"`
- Sort int `description:"排序"`
- CreateTime time.Time `description:"创建时间"`
- }
- func GetSysDepartmentCount(departmentName string) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM sys_department WHERE department_name=? `
- o := orm.NewOrm()
- err = o.Raw(sql, departmentName).QueryRow(&count)
- return
- }
- func AddSysDepartment(item *SysDepartment) (lastId int64, err error) {
- o := orm.NewOrm()
- lastId, err = o.Insert(item)
- return
- }
- type SysDepartmentEditReq struct {
- DepartmentId int `description:"部门Id"`
- DepartmentName string `description:"部门名称"`
- }
- func GetSysDepartmentById(departmentId int) (item *SysDepartment, err error) {
- sql := `SELECT * FROM sys_department WHERE department_id=? `
- o := orm.NewOrm()
- err = o.Raw(sql, departmentId).QueryRow(&item)
- return
- }
- func GetSysDepartmentByName(departmentName string) (item *SysDepartment, err error) {
- sql := `SELECT * FROM sys_department WHERE department_name=? `
- o := orm.NewOrm()
- err = o.Raw(sql, departmentName).QueryRow(&item)
- return
- }
- func ModifySysDepartment(departmentName string, departmentId int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- sql := `UPDATE sys_department SET department_name=? WHERE department_id=? `
- _, err = to.Raw(sql, departmentName, departmentId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE admin SET department_name=? WHERE department_id=? `
- _, err = to.Raw(sql, departmentName, departmentId).Exec()
- return
- }
- type SysDepartmentDeleteReq struct {
- DepartmentId int `description:"部门Id"`
- }
- func DeleteSysDepartment(departmentId int) (err error) {
- sql := `DELETE FROM sys_department WHERE department_id=? `
- o := orm.NewOrm()
- _, err = o.Raw(sql, departmentId).Exec()
- return
- }
- type SysDepartmentList struct {
- DepartmentId int `orm:"column(department_id);pk" description:"部门Id"`
- DepartmentName string `description:"部门名称"`
- CreateTime time.Time `description:"创建时间"`
- Child []*SysGroupList `description:"分组"`
- IsDepartment bool `description:"true:部门,false:分组"`
- }
- func GetDepartmentList() (items []*SysDepartmentList, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM sys_department ORDER BY sort ASC, create_time ASC `
- _, err = o.Raw(sql).QueryRows(&items)
- return
- }
- // GetDepartmentListByIds 根据部门id集合获取部门列表信息
- func GetDepartmentListByIds(departmentIds string) (items []*SysDepartmentList, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM sys_department where 1 = 1 `
- if departmentIds != "" {
- sql += `and department_id in(` + departmentIds + `) `
- }
- sql += ` ORDER BY sort ASC, create_time ASC `
- _, err = o.Raw(sql).QueryRows(&items)
- return
- }
- type SysDepartmentListResp struct {
- List []*SysDepartmentList
- }
- func GetSysDepartmentAll() (item []*SysDepartment, err error) {
- sql := `SELECT * FROM sys_department ORDER BY sort ASC, department_id ASC `
- o := orm.NewOrm()
- _, err = o.Raw(sql).QueryRows(&item)
- return
- }
- type DepartmentSort struct {
- DepartmentId int
- Sort int
- }
- func MultiUpdateDepartmentSort(items []*DepartmentSort) (err error) {
- if len(items) == 0 {
- return
- }
- o := orm.NewOrm()
- p, err := o.Raw("UPDATE sys_department SET sort = ? WHERE department_id = ?").Prepare()
- if err != nil {
- return
- }
- defer func() {
- _ = p.Close()
- }()
- for _, v := range items {
- _, err = p.Exec(v.Sort, v.DepartmentId)
- if err != nil {
- return
- }
- }
- return
- }
|