正在显示
6 个修改的文件
包含
114 行增加
和
10 行删除
| @@ -2,6 +2,7 @@ package models | @@ -2,6 +2,7 @@ package models | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | + "errors" | ||
| 5 | "fmt" | 6 | "fmt" |
| 6 | "oppmg/common/log" | 7 | "oppmg/common/log" |
| 7 | "time" | 8 | "time" |
| @@ -44,6 +45,18 @@ func (t *Department) ParseManagesIds(v []int64) string { | @@ -44,6 +45,18 @@ func (t *Department) ParseManagesIds(v []int64) string { | ||
| 44 | return string(bt) | 45 | return string(bt) |
| 45 | } | 46 | } |
| 46 | 47 | ||
| 48 | +func (t *Department) SetRelation(parent *Department) error { | ||
| 49 | + if t.Id == 0 { | ||
| 50 | + return errors.New("Id==0") | ||
| 51 | + } | ||
| 52 | + if parent == nil { | ||
| 53 | + t.Relation = fmt.Sprintf("%d", t.Id) | ||
| 54 | + } else { | ||
| 55 | + t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id) | ||
| 56 | + } | ||
| 57 | + return nil | ||
| 58 | +} | ||
| 59 | + | ||
| 47 | // AddDepartment insert a new Department into database and returns | 60 | // AddDepartment insert a new Department into database and returns |
| 48 | // last inserted Id on success. | 61 | // last inserted Id on success. |
| 49 | func AddDepartment(m *Department) (id int64, err error) { | 62 | func AddDepartment(m *Department) (id int64, err error) { |
| 1 | package models | 1 | package models |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "errors" | ||
| 4 | "oppmg/common/log" | 5 | "oppmg/common/log" |
| 5 | "time" | 6 | "time" |
| 6 | 7 | ||
| @@ -26,6 +27,35 @@ func init() { | @@ -26,6 +27,35 @@ func init() { | ||
| 26 | orm.RegisterModel(new(Role)) | 27 | orm.RegisterModel(new(Role)) |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 30 | +const ( | ||
| 31 | + ROLETYPES_GROUP int8 = 1 | ||
| 32 | + ROLETYPES_ROLE int8 = 2 | ||
| 33 | +) | ||
| 34 | + | ||
| 35 | +func (t *Role) ValidateTypes() bool { | ||
| 36 | + switch i { | ||
| 37 | + case ROLETYPES_GROUP: | ||
| 38 | + return true | ||
| 39 | + case ROLETYPES_ROLE: | ||
| 40 | + return true | ||
| 41 | + } | ||
| 42 | + return false | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +func (t *Role) ValidatePid() error { | ||
| 46 | + roledata, err := GetRoleById(t.Pid) | ||
| 47 | + if err != nil { | ||
| 48 | + return err | ||
| 49 | + } | ||
| 50 | + if roledata.Types != ROLETYPES_GROUP { | ||
| 51 | + return errors.New("roledata.Types != ROLETYPES_GROUP") | ||
| 52 | + } | ||
| 53 | + if roledata.CompanyId != t.CompanyId { | ||
| 54 | + return errors.New("validate companyId err") | ||
| 55 | + } | ||
| 56 | + return nil | ||
| 57 | +} | ||
| 58 | + | ||
| 29 | // AddRole insert a new Role into database and returns | 59 | // AddRole insert a new Role into database and returns |
| 30 | // last inserted Id on success. | 60 | // last inserted Id on success. |
| 31 | func AddRole(m *Role) (id int64, err error) { | 61 | func AddRole(m *Role) (id int64, err error) { |
| @@ -47,13 +77,13 @@ func GetRoleById(id int) (v *Role, err error) { | @@ -47,13 +77,13 @@ func GetRoleById(id int) (v *Role, err error) { | ||
| 47 | 77 | ||
| 48 | // UpdateRole updates Role by Id and returns error if | 78 | // UpdateRole updates Role by Id and returns error if |
| 49 | // the record to be updated doesn't exist | 79 | // the record to be updated doesn't exist |
| 50 | -func UpdateRoleById(m *Role) (err error) { | 80 | +func UpdateRoleById(m *Role, col []string) (err error) { |
| 51 | o := orm.NewOrm() | 81 | o := orm.NewOrm() |
| 52 | v := Role{Id: m.Id} | 82 | v := Role{Id: m.Id} |
| 53 | // ascertain id exists in the database | 83 | // ascertain id exists in the database |
| 54 | if err = o.Read(&v); err == nil { | 84 | if err = o.Read(&v); err == nil { |
| 55 | var num int64 | 85 | var num int64 |
| 56 | - if num, err = o.Update(m); err == nil { | 86 | + if num, err = o.Update(m, col...); err == nil { |
| 57 | log.Debug("Number of records updated in database:%d", num) | 87 | log.Debug("Number of records updated in database:%d", num) |
| 58 | } | 88 | } |
| 59 | } | 89 | } |
| @@ -11,8 +11,6 @@ type UserCompany struct { | @@ -11,8 +11,6 @@ type UserCompany struct { | ||
| 11 | Id int64 `orm:"column(id);auto" description:"唯一标识"` | 11 | Id int64 `orm:"column(id);auto" description:"唯一标识"` |
| 12 | CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` | 12 | CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` |
| 13 | UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"` | 13 | UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"` |
| 14 | - DepartmentId int `orm:"column(department_id)" description:"部门id"` | ||
| 15 | - PositionId int `orm:"column(position_id)" description:"职位id"` | ||
| 16 | ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"` | 14 | ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"` |
| 17 | CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"` | 15 | CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"` |
| 18 | CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` | 16 | CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` |
| @@ -3,7 +3,9 @@ package protocol | @@ -3,7 +3,9 @@ package protocol | ||
| 3 | //RequestRoleAdd 添加角色信息操作入参 | 3 | //RequestRoleAdd 添加角色信息操作入参 |
| 4 | type RequestRoleAdd struct { | 4 | type RequestRoleAdd struct { |
| 5 | CompanyID int `json:"company"` | 5 | CompanyID int `json:"company"` |
| 6 | + Pid int `json:"pid"` | ||
| 6 | Name string `json:"name"` | 7 | Name string `json:"name"` |
| 8 | + Types int8 `json:"types"` | ||
| 7 | Descript string `json:"descript"` | 9 | Descript string `json:"descript"` |
| 8 | } | 10 | } |
| 9 | 11 | ||
| @@ -28,8 +30,10 @@ type RequestRoleOne struct { | @@ -28,8 +30,10 @@ type RequestRoleOne struct { | ||
| 28 | //ResponseRoleInfo 响应数据 | 30 | //ResponseRoleInfo 响应数据 |
| 29 | type ResponseRoleInfo struct { | 31 | type ResponseRoleInfo struct { |
| 30 | ID int `json:"id"` | 32 | ID int `json:"id"` |
| 33 | + Pid int `json:"pid"` | ||
| 31 | Name string `json:"name"` | 34 | Name string `json:"name"` |
| 32 | Descript string `json:"descript"` | 35 | Descript string `json:"descript"` |
| 36 | + Types int `json:"types"` | ||
| 33 | CreateTime int64 `json:"create_time` | 37 | CreateTime int64 `json:"create_time` |
| 34 | UpdateTime int64 `json:"update_time"` | 38 | UpdateTime int64 `json:"update_time"` |
| 35 | } | 39 | } |
| @@ -12,10 +12,47 @@ import ( | @@ -12,10 +12,47 @@ import ( | ||
| 12 | "github.com/astaxie/beego/orm" | 12 | "github.com/astaxie/beego/orm" |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | -func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepartmentInfo, error) { | ||
| 16 | - | ||
| 17 | - r := protocol.ResponseDepartmentInfo{} | ||
| 18 | - return r, nil | 15 | +func DepartmentAdd(param protocol.RequestDepartmentAdd) error { |
| 16 | + var ( | ||
| 17 | + parentDepart *models.Department | ||
| 18 | + err error | ||
| 19 | + ) | ||
| 20 | + if param.ParantID > 0 { | ||
| 21 | + parentDepart, err = models.GetDepartmentById(param.ParantID) | ||
| 22 | + if err != nil { | ||
| 23 | + e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParantID, err) | ||
| 24 | + log.Error(e.Error()) | ||
| 25 | + return protocol.NewErrWithMessage("1", e) | ||
| 26 | + } | ||
| 27 | + if parentDepart.CompanyId != param.CompanyID { | ||
| 28 | + e := fmt.Errorf("parentDepart.CompanyId != param.CompanyID") | ||
| 29 | + log.Error(e.Error()) | ||
| 30 | + return protocol.NewErrWithMessage("1", e) | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + for _, v := range param.Managers { | ||
| 34 | + _, err = models.GetUserCompanyBy(v, param.CompanyID) | ||
| 35 | + if err != nil { | ||
| 36 | + e := fmt.Errorf("GetUserCompanyBy(%d, %d)", v, param.CompanyID) | ||
| 37 | + log.Error(e.Error()) | ||
| 38 | + return protocol.NewErrWithMessage("1", e) | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + departmentAdd := &models.Department{ | ||
| 42 | + CompanyId: param.CompanyID, | ||
| 43 | + Name: param.Name, | ||
| 44 | + CreateAt: time.Now(), | ||
| 45 | + ParentId: param.ParantID, | ||
| 46 | + Member: 0, | ||
| 47 | + } | ||
| 48 | + departmentAdd.SetRelation(parentDepart) | ||
| 49 | + _, err = models.AddDepartment(departmentAdd) | ||
| 50 | + if err != nil { | ||
| 51 | + e := fmt.Errorf("AddDepartment err:%s", err) | ||
| 52 | + log.Error(e.Error()) | ||
| 53 | + return protocol.NewErrWithMessage("1", e) | ||
| 54 | + } | ||
| 55 | + return nil | ||
| 19 | 56 | ||
| 20 | } | 57 | } |
| 21 | 58 | ||
| @@ -74,7 +111,7 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error { | @@ -74,7 +111,7 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error { | ||
| 74 | //更新部门关系数据 | 111 | //更新部门关系数据 |
| 75 | err = departmentRelationUpdate(departUpdate, newparent) | 112 | err = departmentRelationUpdate(departUpdate, newparent) |
| 76 | if err != nil { | 113 | if err != nil { |
| 77 | - e := fmt.Errorf(" departmentRelationUpdate err:%s", err) | 114 | + e := fmt.Errorf("departmentRelationUpdate err:%s", err) |
| 78 | log.Error(e.Error()) | 115 | log.Error(e.Error()) |
| 79 | return protocol.NewErrWithMessage("1", e) | 116 | return protocol.NewErrWithMessage("1", e) |
| 80 | } | 117 | } |
| @@ -107,7 +144,16 @@ func departmentRelationUpdate(old *models.Department, newparent *models.Departme | @@ -107,7 +144,16 @@ func departmentRelationUpdate(old *models.Department, newparent *models.Departme | ||
| 107 | log.Error(e.Error()) | 144 | log.Error(e.Error()) |
| 108 | return protocol.NewErrWithMessage("1", e) | 145 | return protocol.NewErrWithMessage("1", e) |
| 109 | } | 146 | } |
| 147 | + | ||
| 110 | for i := range departSubset { | 148 | for i := range departSubset { |
| 149 | + if departSubset[i].Id == newparent.Id { | ||
| 150 | + //确认新的父级id是否合法 | ||
| 151 | + o.Rollback() | ||
| 152 | + e := fmt.Errorf("departSubset[i].Id == newparent.Id") | ||
| 153 | + log.Error(e.Error()) | ||
| 154 | + return protocol.NewErrWithMessage("1", e) | ||
| 155 | + } | ||
| 156 | + //重建关系树 | ||
| 111 | s := strings.TrimPrefix(departSubset[i].Relation, oldrelation) | 157 | s := strings.TrimPrefix(departSubset[i].Relation, oldrelation) |
| 112 | departSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s)) | 158 | departSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s)) |
| 113 | err := utils.ExcuteSQLWithOrmer(o, dataSql2, departSubset[i].Relation, departSubset[i].Id) | 159 | err := utils.ExcuteSQLWithOrmer(o, dataSql2, departSubset[i].Relation, departSubset[i].Id) |
| @@ -14,8 +14,21 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) | @@ -14,8 +14,21 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) | ||
| 14 | CompanyId: param.CompanyID, | 14 | CompanyId: param.CompanyID, |
| 15 | Name: param.Name, | 15 | Name: param.Name, |
| 16 | CreateAt: time.Now(), | 16 | CreateAt: time.Now(), |
| 17 | + Pid: param.Pid, | ||
| 18 | + Types: param.Types, | ||
| 17 | Descript: param.Descript, | 19 | Descript: param.Descript, |
| 18 | } | 20 | } |
| 21 | + if ok := role.ValidateTypes(); !ok { | ||
| 22 | + e := fmt.Errorf("ValidateTypes err") | ||
| 23 | + log.Error(e.Error()) | ||
| 24 | + return nil, protocol.NewErrWithMessage("1", e) | ||
| 25 | + } | ||
| 26 | + err := role.ValidatePid() | ||
| 27 | + if err != nil { | ||
| 28 | + e := fmt.Errorf("ValidatePid err:%s", err) | ||
| 29 | + log.Error(e.Error()) | ||
| 30 | + return nil, protocol.NewErrWithMessage("1", e) | ||
| 31 | + } | ||
| 19 | roleid, err := models.AddRole(&role) | 32 | roleid, err := models.AddRole(&role) |
| 20 | if err != nil { | 33 | if err != nil { |
| 21 | log.Error("AddRole err:%s", err) | 34 | log.Error("AddRole err:%s", err) |
| @@ -68,7 +81,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error | @@ -68,7 +81,7 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error | ||
| 68 | } | 81 | } |
| 69 | role.Descript = param.Descript | 82 | role.Descript = param.Descript |
| 70 | role.Name = param.Name | 83 | role.Name = param.Name |
| 71 | - if err = models.UpdateRoleById(role); err != nil { | 84 | + if err = models.UpdateRoleById(role, []string{"Descript", "Name"}); err != nil { |
| 72 | e := fmt.Errorf("UpdateRoleById err:%s", err) | 85 | e := fmt.Errorf("UpdateRoleById err:%s", err) |
| 73 | log.Error(e.Error()) | 86 | log.Error(e.Error()) |
| 74 | return nil, protocol.NewErrWithMessage("1", e) | 87 | return nil, protocol.NewErrWithMessage("1", e) |
-
请 注册 或 登录 后发表评论