作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/oppmg into dev

... ... @@ -28,8 +28,8 @@ log_level = "${LOG_LEVEL||error}"
##统一用户中心相关配置
ucenter_check_alt = "rsF0pL!6DwjBO735"
ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_app_key = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
ucenter_secret = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
# 审批流程修改 消息发布
message_publish = "audit_change"
#---自定义配置 结束----
\ No newline at end of file
... ...
... ... @@ -28,8 +28,8 @@ log_level = "${LOG_LEVEL||error}"
##统一用户中心相关配置
ucenter_check_alt = "rsF0pL!6DwjBO735"
ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_app_key = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
ucenter_secret = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
# 审批流程修改 消息发布
message_publish = "audit_change"
#---自定义配置 结束----
\ No newline at end of file
... ...
... ... @@ -21,7 +21,7 @@ func (c *CompanyController) URLMapping() {
}
// DepartmentList 部门列表
// @router /department [get]
// @router /department [post]
func (c *CompanyController) DepartmentList() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -39,7 +39,7 @@ func (c *CompanyController) DepartmentList() {
msg = protocol.NewReturnResponse(listdata, err)
}
// DepartmentOne 部门信息
// DepartmentOne 部门信息 TODO
// @router /department/:id [get]
func (c *CompanyController) DepartmentOne() {
log.Debug("DepartmentOne param:%v", c.Ctx.Input.Param(":id"))
... ... @@ -78,6 +78,19 @@ func (c *CompanyController) DepartmentAdd() {
msg = protocol.BadRequestParam("1")
return
}
s := []rune(param.Name)
if len(s) == 0 {
msg = protocol.BadRequestParam("10044")
return
}
if len(s) > 20 {
msg = protocol.BadRequestParam("10043")
return
}
if param.ParentID <= 0 {
msg = protocol.BadRequestParam("10042")
return
}
param.CompanyID = c.GetCompanyId()
if param.CompanyID <= 0 {
log.Error("param.CompanyID <= 0")
... ... @@ -103,6 +116,15 @@ func (c *CompanyController) DepartmentUpdate() {
msg = protocol.BadRequestParam("1")
return
}
s := []rune(param.Name)
if len(s) == 0 {
msg = protocol.BadRequestParam("10044")
return
}
if len(s) > 20 {
msg = protocol.BadRequestParam("10043")
return
}
param.CompanyID = c.GetCompanyId()
if param.ID <= 0 {
log.Error(" param.ID <= 0 ")
... ...
... ... @@ -40,9 +40,7 @@ var AuthToken = func(ctx *context.Context) {
accesstoken := ctx.Input.Header(protocol.HeaderAccessToken)
refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken)
mtoken, err = serveauth.ValidJWTToken(accesstoken)
if beego.BConfig.RunMode != "prod" {
return
}
if err == nil {
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
if err != nil {
... ...
... ... @@ -20,7 +20,8 @@ type Department struct {
Relation string `orm:"column(relation);size(1024)" description:"父子级关系树"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"`
Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"` //存user_company_id
IsTop int8 `orm:"column(is_top)" `
}
func (t *Department) TableName() string {
... ... @@ -41,6 +42,9 @@ func (t *Department) GetManagesIds() []int64 {
}
func (t *Department) SetManages(v []int64) {
if len(v) == 0 {
v = []int64{}
}
bt, _ := json.Marshal(v)
t.Manages = string(bt)
return
... ... @@ -58,17 +62,23 @@ func (t *Department) SetRelation(parent *Department) error {
return nil
}
//fix:变更主管获取方式
func (t *Department) GetManages() []protocol.DepartmentManager {
ids := t.GetManagesIds()
users, err := getUserNameByIds(ids)
usercompany, err := GetUserCompanyReal(ids)
if err != nil {
log.Error("GetUserNameByIds err :%s", err)
return nil
}
managesdata := []protocol.DepartmentManager{}
for _, v := range users {
for _, v := range usercompany {
u, err := GetUserById(v.UserId)
if err != nil {
log.Error("GetUserById(%d) err:%s", v.UserId, err)
continue
}
m := protocol.DepartmentManager{
Id: v.Id, Name: v.NickName,
Id: v.Id, Name: u.NickName,
}
managesdata = append(managesdata, m)
}
... ... @@ -96,6 +106,13 @@ func (t *Department) GetMembers() []protocol.DepartmentMember {
return managesdata
}
func (t *Department) IsTopDepartment() bool {
if t.IsTop == 1 {
return true
}
return false
}
// AddDepartment insert a new Department into database and returns
// last inserted Id on success.
func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) {
... ...
... ... @@ -110,9 +110,27 @@ func EnableUserCompany(userid int64, companyid int64) error {
o := orm.NewOrm()
_, err := o.QueryTable(&UserCompany{}).
Filter("user_id", userid).
Filter("company_id", companyid).Update(orm.Params{
Filter("company_id", companyid).
Update(orm.Params{
"enable": USERCOMPANY_ENABLE_YES,
"delete_at": 0,
})
return err
}
func GetUserCompanyReal(ids []int64) ([]UserCompany, error) {
var (
err error
data []UserCompany
)
if len(ids) == 0 {
return data, nil
}
o := orm.NewOrm()
_, err = o.QueryTable(&UserCompany{}).
Filter("id__in", ids).
Filter("delete_at", 0).
All(&data)
return data, err
}
... ...
... ... @@ -63,42 +63,43 @@ func AddUserDepartment(m *UserDepartment) (id int64, err error) {
// GetUserDepartmentById retrieves UserDepartment by Id. Returns error if
// Id doesn't exist
func GetUserDepartmentById(id int64) (v *UserDepartment, err error) {
o := orm.NewOrm()
v = &UserDepartment{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// func GetUserDepartmentById(id int64) (v *UserDepartment, err error) {
// o := orm.NewOrm()
// v = &UserDepartment{Id: id}
// if err = o.Read(v); err == nil {
// return v, nil
// }
// return nil, err
// }
// UpdateUserDepartment updates UserDepartment by Id and returns error if
// the record to be updated doesn't exist
func UpdateUserDepartmentById(m *UserDepartment) (err error) {
o := orm.NewOrm()
v := UserDepartment{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
func GetUserDepartment(userid, companyid int64) ([]*UserDepartment, error) {
o := orm.NewOrm()
var (
err error
result []*UserDepartment
)
_, err = o.QueryTable(&UserDepartment{}).
Filter("user_id", userid).
Filter("company_id", companyid).
Filter("enable_status", 1).
All(&result)
return result, err
}
// func UpdateUserDepartmentById(m *UserDepartment) (err error) {
// o := orm.NewOrm()
// v := UserDepartment{Id: m.Id}
// // ascertain id exists in the database
// if err = o.Read(&v); err == nil {
// var num int64
// if num, err = o.Update(m); err == nil {
// fmt.Println("Number of records updated in database:", num)
// }
// }
// return
// }
// func GetUserDepartment(userid, companyid int64) ([]*UserDepartment, error) {
// o := orm.NewOrm()
// var (
// err error
// result []*UserDepartment
// )
// _, err = o.QueryTable(&UserDepartment{}).
// Filter("user_id", userid).
// Filter("company_id", companyid).
// Filter("enable_status", 1).
// All(&result)
// return result, err
// }
func CountUserDepartByDepart(departid int64) (int64, error) {
var (
... ... @@ -125,3 +126,16 @@ func GetUserDepartmentIds(companyId, dId int) (v []int64, err error) {
}
return
}
func ExistUserDepart(departid int64, usercompanyid int64) bool {
var (
ok bool
)
o := orm.NewOrm()
ok = o.QueryTable(&UserDepartment{}).
Filter("department_id", departid).
Filter("user_company_id", usercompanyid).
Filter("delete_at", 0).
Exist()
return ok
}
... ...
... ... @@ -5,7 +5,7 @@ type RequestDepartmentAdd struct {
CompanyID int64 `json:"company_id"` //公司
Name string `json:"name"` //部门名字
ParentID int64 `json:"parent_id"` //父级部门Id
Managers []int64 `json:"manages"` //主管userid
}
type ResponseDepartmenrAdd struct {
... ... @@ -13,7 +13,7 @@ type ResponseDepartmenrAdd struct {
}
type DepartmentManager struct {
Id int64 `json:"id"`
Id int64 `json:"id"` //user_company表的id
Name string `json:"name"`
}
... ... @@ -25,6 +25,7 @@ type DepartmentMember struct {
//RequestDepartmentEdit 编辑
type RequestDepartmentEdit struct {
ID int64 `json:"id"`
Managers []int64 `json:"manages"` //主管user_company_id
RequestDepartmentAdd
}
... ...
... ... @@ -27,6 +27,11 @@ var errmessge ErrorMap = map[string]string{
"10037": "用户的部门必填",
"10038": "用户的角色必填",
"10039": "用户已存在",
//部门相关
"10041": "无效的主管设置",
"10042": "无效的上级部门",
"10043": "部门名称限制不超过20个字符",
"10044": "部门名称必填",
}
//错误码转换 ,兼容需要
... ...
... ... @@ -12,7 +12,7 @@ func init() {
nsV1 := beego.NewNamespace("v1",
beego.NSBefore(middleware.AllowOption),
beego.NSBefore(middleware.LogRequestData),
//beego.NSBefore(middleware.AuthToken),
beego.NSBefore(middleware.AuthToken),
beego.NSNamespace("/department",
beego.NSRouter("/list", &controllers.CompanyController{}, "post:DepartmentList"),
beego.NSRouter("/add", &controllers.CompanyController{}, "post:DepartmentAdd"),
... ...
... ... @@ -226,7 +226,7 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
var (
err error
logintoken protocol.LoginAuthToken
uclientReturn ucenter.ResponseLogin
companys []companybase
companyid int64
userdata *models.User
... ... @@ -253,6 +253,7 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
return logintoken, protocol.NewErrWithMessage("10021")
}
companyid = companys[0].Id
var uclientReturn ucenter.ResponseLogin
param := ucenter.RequesLogin{
Phone: account,
Password: password,
... ... @@ -283,27 +284,28 @@ type companybase struct {
}
//getUserCompanyReal 获取用户有效的公司id
func getUserCompanyReal(userid int64) (companyids []companybase, err error) {
func getUserCompanyReal(userid int64) ([]companybase, error) {
const (
datasql0 string = `SELECT b.company_id FROM user_company AS b
WHERE b.delete_at=0 AND b.enable = 1 AND b.user_id=?`
datasql2 string = `SELECT b.id,a.name,a.logo FROM company AS b
datasql2 string = `SELECT b.id,b.name,b.logo FROM company AS b
WHERE b.delete_at=0 AND b.enable = 1 AND b.id in (%s) `
)
var (
companys []companybase
ids []string
err error
)
err = utils.ExecuteQueryAll(&ids, datasql0, userid)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, err
}
if len(companys) == 0 {
if len(ids) == 0 {
return nil, nil
}
datasql3 := fmt.Sprintf(datasql2, strings.Join(ids, ","))
err = utils.ExecuteQueryAll(&companyids, datasql3)
err = utils.ExecuteQueryAll(&companys, datasql3)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, err
... ...
... ... @@ -18,6 +18,10 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart
returndata protocol.ResponseDepartmenrAdd
err error
)
if param.ParentID == 0 {
//部门必定有上级部门 ,至少是公司一级
return returndata, protocol.NewErrWithMessage("10042")
}
if param.ParentID > 0 {
parentDepart, err = models.GetDepartmentById(param.ParentID)
if err != nil {
... ... @@ -31,14 +35,21 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart
return returndata, protocol.NewErrWithMessage("1", e)
}
}
for _, v := range param.Managers {
_, err = models.GetUserCompanyBy(v, param.CompanyID)
if err != nil {
e := fmt.Errorf("GetUserCompanyBy(userid,companyid)[%d, %d] err:%s", v, param.CompanyID, err)
log.Error(e.Error())
return returndata, protocol.NewErrWithMessage("1", e)
}
}
// for _, v := range param.Managers {
// uc, err := models.GetUserCompanyReal([]int64{v})
// if err != nil {
// e := fmt.Errorf("GetUserCompanyReal([]int64{%d}) err:%s", v, err)
// log.Error(e.Error())
// return returndata, protocol.NewErrWithMessage("1", e)
// }
// if len(uc) > 0 {
// if uc[0].CompanyId != param.CompanyID {
// e := fmt.Errorf("managers err")
// log.Error(e.Error())
// return returndata, protocol.NewErrWithMessage("1", e)
// }
// }
// }
departmentAdd := &models.Department{
CompanyId: param.CompanyID,
Name: param.Name,
... ... @@ -46,7 +57,7 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart
UpdateAt: time.Now(),
ParentId: param.ParentID,
}
departmentAdd.SetManages(param.Managers)
departmentAdd.SetManages(nil)
o := orm.NewOrm()
o.Begin()
_, err = models.AddDepartment(departmentAdd, o)
... ... @@ -93,22 +104,46 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error {
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
//校验数据
if ok := departUpdate.IsTopDepartment(); ok {
//公司一级不能修改上级,不能修改名称
param.Name = departUpdate.Name
param.ParentID = departUpdate.ParentId
} else {
if param.ParentID <= 0 {
return protocol.NewErrWithMessage("10042")
}
}
//确认部门主管变更情况
var (
oldmanage []int64
newManage []int64
)
oldmanage = departUpdate.GetManagesIds()
diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
for i := range diffmanage {
_, err = models.GetUserCompanyBy(diffmanage[i], param.CompanyID)
//oldmanage = departUpdate.GetManagesIds()
//diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
uc, err := models.GetUserCompanyReal(param.Managers)
if err != nil {
e := fmt.Errorf("GetUserCompanyBy(userid,companyid)[%d,%d] err:%s", diffmanage[i], param.CompanyID, err)
e := fmt.Errorf("GetUserCompanyReal err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
return protocol.NewErrWithMessage("10041")
}
for i := range uc {
if uc[i].CompanyId != param.CompanyID {
e := fmt.Errorf("managers err")
log.Error(e.Error())
return protocol.NewErrWithMessage("10041")
}
//检查设置的主管是否是部门下的人员
ok := models.ExistUserDepart(param.ID, uc[i].Id)
if !ok {
log.Error("false=aExistUserDepart(departid,usercompanyid) [%d,%d]", param.ID, uc[i].Id)
return protocol.NewErrWithMessage("10041")
}
departUpdate.SetManages(param.Managers)
newManage = append(newManage, uc[i].Id)
}
departUpdate.SetManages(newManage)
departUpdate.Name = param.Name
departUpdate.UpdateAt = time.Now()
err = models.UpdateDepartmentById(departUpdate, []string{"Manages", "Name", "UpdateAt"})
if err != nil {
... ... @@ -120,7 +155,9 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error {
var (
newparent *models.Department
)
if departUpdate.ParentId != param.ParentID {
//检查上级部门是否合法
newparent, err = models.GetDepartmentById(param.ParentID)
if err != nil {
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
... ... @@ -188,7 +225,7 @@ func departmentRelationUpdate(departUpdate *models.Department, newparent *models
o.Rollback()
e := fmt.Errorf("departSubset[i].Id == newparent.Id")
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
return protocol.NewErrWithMessage("10042")
}
//重建关系树
s := strings.TrimPrefix(departSubset[i].Relation, oldRelation)
... ... @@ -227,6 +264,10 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
//一级部门不能删除
if ok := p.IsTopDepartment(); ok {
continue
}
if p.CompanyId != param.CompanyID {
e := fmt.Errorf("CompanyID err")
log.Error(e.Error())
... ... @@ -286,7 +327,7 @@ func DepartmentListAll(companyId int64) ([]protocol.ResponseDepartmentInfo, erro
err error
)
const (
datasql0 string = `SELECT id, company_id,name,parent_id,managers,delete_at ` +
datasql0 string = `SELECT id, company_id,name,parent_id,member,managers ` +
` FROM department WHERE company_id = ? AND delete_at = 0`
)
err = utils.ExecuteQueryAll(&departmodels, datasql0, companyId)
... ...