123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package system
- import (
- "github.com/beego/beego/v2/client/orm"
- "time"
- )
- type SysGroupAddReq struct {
- DepartmentId int `description:"部门Id"`
- GroupName string `description:"分组名称,多个用英文逗号隔开"`
- }
- type SysGroup struct {
- GroupId int `orm:"column(group_id);pk" description:"分组ID"`
- DepartmentId int `description:"部门Id"`
- ParentId int `description:"父级Id"`
- GroupName string `description:"分组名称"`
- CreateTime time.Time `description:"创建时间"`
- }
- func GetSysGroupCount(departmentId int, groupName string) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM sys_group WHERE department_id=? AND group_name=? `
- o := orm.NewOrm()
- err = o.Raw(sql, departmentId, groupName).QueryRow(&count)
- return
- }
- func AddSysGroup(item *SysGroup) (lastId int64, err error) {
- o := orm.NewOrm()
- lastId, err = o.Insert(item)
- return
- }
- type SysGroupEditReq struct {
- GroupId int `description:"分组ID"`
- GroupName string `description:"分组名称"`
- }
- func GetSysGroupById(groupId int) (item *SysGroup, err error) {
- sql := `SELECT * FROM sys_group WHERE group_id=? `
- o := orm.NewOrm()
- err = o.Raw(sql, groupId).QueryRow(&item)
- return
- }
- func GetSysGroupByName(groupName string) (item *SysGroup, err error) {
- sql := `SELECT * FROM sys_group WHERE group_name=? `
- o := orm.NewOrm()
- err = o.Raw(sql, groupName).QueryRow(&item)
- return
- }
- func ModifySysGroup(groupName string, groupId 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_group SET group_name=? WHERE group_id=? `
- _, err = to.Raw(sql, groupName, groupId).Exec()
- sql = `UPDATE admin SET group_name=? WHERE group_id=? `
- _, err = to.Raw(sql, groupName, groupId).Exec()
- return
- }
- type SysGroupDeleteReq struct {
- GroupId int `description:"分组ID"`
- }
- func DeleteSysGroup(groupId int) (err error) {
- sql := `DELETE FROM sys_group WHERE group_id=? `
- o := orm.NewOrm()
- _, err = o.Raw(sql, groupId).Exec()
- return
- }
- //因前端显示需要,TopId字段用来当做一级部门id,DepartmentId为当前分组id
- type SysGroupList struct {
- GroupId int `orm:"column(group_id);pk" json:"DepartmentId" description:"分组ID"`
- ParentId int `json:"ParentId" description:"父级ID"`
- DepartmentId int `json:"TopId" description:"部门Id"`
- GroupName string `json:"DepartmentName" description:"分组名称"`
- Child []*SysTeamList `description:"小组"`
- CreateTime time.Time `description:"创建时间"`
- IsGroup bool `description:"是否为二级部门"`
- }
- func GetSysGroupByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
- sql := `SELECT * FROM sys_group WHERE department_id=? AND parent_id=0`
- o := orm.NewOrm()
- _, err = o.Raw(sql, departmentId).QueryRows(&items)
- return
- }
- // GetSysGroupListByDepartmentId 获取该部门下的所有分组(包含大小分组,不包含 “无” 这个分组)
- func GetSysGroupListByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
- sql := `SELECT * FROM sys_group WHERE department_id=? AND group_name<>'无' `
- o := orm.NewOrm()
- _, err = o.Raw(sql, departmentId).QueryRows(&items)
- return
- }
- func ClearSysUserGroup(groupId int) (err error) {
- sql := `UPDATE admin SET group_id=0,group_name='' WHERE group_id=? `
- o := orm.NewOrm()
- _, err = o.Raw(sql, groupId).Exec()
- return
- }
- func GetSysGroupByDirectorId(directorId int) (items []*SysGroupList, err error) {
- sql := `SELECT * FROM sys_group WHERE group_id=? `
- o := orm.NewOrm()
- _, err = o.Raw(sql, directorId).QueryRows(&items)
- return
- }
- // GetSysGroupByGroupId 销售主管用,查找销售主管所在大组的名称
- func GetSysGroupByGroupId(groupId int) (items []*SysGroupList, err error) {
- sql := `SELECT * FROM sys_group WHERE department_id=2 AND parent_id=0 AND group_id=?`
- o := orm.NewOrm()
- _, err = o.Raw(sql, groupId).QueryRows(&items)
- return
- }
- // GetChildSysGroupByGroupId 通过上级分组id获取下级的分组id
- func GetChildSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
- sql := `SELECT * FROM sys_group WHERE parent_id=?`
- o := orm.NewOrm()
- _, err = o.Raw(sql, groupId).QueryRows(&items)
- return
- }
- type SysFullGroup struct {
- GroupId int `orm:"column(group_id);pk" description:"分组ID"`
- DepartmentId int `description:"部门Id"`
- ParentId int `description:"父级Id"`
- GroupName string `description:"分组名称"`
- ParentGroupName string `description:"父级分组名称"`
- DepartmentName string `description:"部门名称"`
- CreateTime time.Time `description:"创建时间"`
- }
- //GetFullGroup 获取完整的分组信息
- func GetFullGroup() (list []*SysFullGroup, err error) {
- sql := `SELECT s.*,g.group_name as parent_group_name , d.department_name
- from sys_group s
- LEFT JOIN sys_group g on s.parent_id=g.group_id
- LEFT JOIN sys_department d on s.department_id=d.department_id`
- o := orm.NewOrm()
- _, err = o.Raw(sql).QueryRows(&list)
- return
- }
|