...
|
...
|
@@ -6,6 +6,7 @@ import ( |
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
...
|
...
|
@@ -16,6 +17,11 @@ func UserAdd(param protocol.RequestUserAdd) error { |
|
|
userm *models.User
|
|
|
err error
|
|
|
)
|
|
|
//校验用户
|
|
|
err = validCompanyUser(param.CompanyId, param.Phone)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
//校验角色
|
|
|
err = validCompanyRole(param.CompanyId, param.Roles)
|
|
|
if err != nil {
|
...
|
...
|
@@ -134,6 +140,23 @@ func validCompanyPosition(companyid int64, positionid []int64) error { |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func validCompanyUser(companyid int64, phone string) error {
|
|
|
userdata, err := models.GetUserByPhone(phone)
|
|
|
if err == orm.ErrNoRows {
|
|
|
return nil
|
|
|
}
|
|
|
if err != nil {
|
|
|
log.Error("database operate fail:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
|
|
|
ok := models.ExistUserCompany(userdata.Id, companyid)
|
|
|
if ok {
|
|
|
protocol.NewErrWithMessage("10039")
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//registUser 注册用户
|
|
|
func registUser(userIn *models.User, companyid int64, o orm.Ormer) error {
|
|
|
var (
|
...
|
...
|
@@ -163,15 +186,15 @@ func registUser(userIn *models.User, companyid int64, o orm.Ormer) error { |
|
|
CompanyId: companyid,
|
|
|
UserId: userIn.Id,
|
|
|
}
|
|
|
ok := models.ExistUserCompany(usrData.Id, companyid)
|
|
|
if !ok {
|
|
|
// ok := models.ExistUserCompany(usrData.Id, companyid)
|
|
|
// if !ok {
|
|
|
_, err = models.AddUserCompany(musercompany, o)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
err = models.EnableUserCompany(usrData.Id, companyid)
|
|
|
return err
|
|
|
// }
|
|
|
// err = models.EnableUserCompany(usrData.Id, companyid)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Ormer) error {
|
...
|
...
|
@@ -257,7 +280,7 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e |
|
|
addRoleIds []int64
|
|
|
)
|
|
|
dataSql := `SELECT role_id FROM user_role WHERE enable_status=1 AND company_id=? AND user_id=?`
|
|
|
dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE company_id =? AND user_id=? AND role_id=?`
|
|
|
dataSql2 := `UPDATE user_role SET enable_status = 2 WHERE company_id =? AND user_id=? AND role_id=?`
|
|
|
datasql3 := `INSERT INTO user_role (user_id,role_id,company_id,enable_status) VALUES (?,?,?,?)`
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &oldRoleIds, dataSql, companyid, userid)
|
|
|
if err != nil {
|
...
|
...
|
@@ -291,6 +314,10 @@ func UserEdit(param protocol.RequestUserEdit) error { |
|
|
userm *models.User
|
|
|
err error
|
|
|
)
|
|
|
err = validCompanyUser(param.CompanyId, param.Phone)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
//校验角色
|
|
|
err = validCompanyRole(param.CompanyId, param.Roles)
|
|
|
if err != nil {
|
...
|
...
|
@@ -322,6 +349,7 @@ func UserEdit(param protocol.RequestUserEdit) error { |
|
|
// log.Error("registUser err:%s", err)
|
|
|
// return protocol.NewErrWithMessage("1")
|
|
|
// }
|
|
|
// TODO 用户数据更新
|
|
|
//添加角色
|
|
|
err = editUserRole(userm.Id, param.CompanyId, param.Roles, o)
|
|
|
if err != nil {
|
...
|
...
|
@@ -371,7 +399,7 @@ func UserDelete(userid, companyid int64) error { |
|
|
|
|
|
func UserList(param protocol.RequestUserList) (protocol.ResponseUserList, error) {
|
|
|
|
|
|
datasql := `SELECT a.company_id,a.user_id,a.enable, b.phone,b.nick_name
|
|
|
datasql := `SELECT a.id as user_company_id,a.company_id,a.user_id,a.enable, b.phone,b.nick_name
|
|
|
FROM user_company AS a
|
|
|
LEFT JOIN user AS b ON a.user_id = b.id
|
|
|
WHERE a.company_id=? AND a.delete_at = 0 `
|
...
|
...
|
@@ -404,20 +432,77 @@ func UserList(param protocol.RequestUserList) (protocol.ResponseUserList, error) |
|
|
log.Error(e.Error())
|
|
|
return responseData, protocol.NewErrWithMessage("0")
|
|
|
}
|
|
|
// for k, v := range result {
|
|
|
// positions, _ := models.GetUserPosition(v.UserId, param.Companyid)
|
|
|
// departments, _ := models.GetUserDepartment(v.UserId, param.Companyid)
|
|
|
// roles, _ := models.GetUserRole(v.UserId, param.Companyid)
|
|
|
// }
|
|
|
for k, v := range result {
|
|
|
var (
|
|
|
pname []string
|
|
|
dname []string
|
|
|
rname []string
|
|
|
)
|
|
|
positions := GetUserPosition(v.UserCompanydId)
|
|
|
for i := range positions {
|
|
|
pname = append(pname, positions[i].Name)
|
|
|
}
|
|
|
departments := GetUserDepartment(v.UserCompanydId)
|
|
|
for i := range departments {
|
|
|
dname = append(dname, departments[i].Name)
|
|
|
}
|
|
|
roles := GetUserRole(v.UserCompanydId)
|
|
|
for i := range roles {
|
|
|
rname = append(rname, roles[i].Name)
|
|
|
}
|
|
|
result[k].Departments = strings.Join(dname, ",")
|
|
|
result[k].Positions = strings.Join(pname, ",")
|
|
|
result[k].Roles = strings.Join(rname, ",")
|
|
|
}
|
|
|
responseData.ResponsePageInfo = pageInfo
|
|
|
responseData.List = result
|
|
|
return responseData, nil
|
|
|
}
|
|
|
|
|
|
func GetUserDepartment(user int64, companyid int64) {
|
|
|
|
|
|
func GetUserDepartment(usercompanyid int64) []protocol.DepartmentBase {
|
|
|
const datasql string = `SELECT b.name,b.parent_id,b.id FROM user_department AS a
|
|
|
JOIN department AS b ON a.department_id = b.id
|
|
|
WHERE a.enable_status =1 AND a.user_company_id =? `
|
|
|
var (
|
|
|
err error
|
|
|
data []protocol.DepartmentBase
|
|
|
)
|
|
|
err = utils.ExecuteQueryAll(&data, datasql, usercompanyid)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
}
|
|
|
return data
|
|
|
}
|
|
|
|
|
|
func GetUserPosition(user int64, companyid int64) {
|
|
|
func GetUserPosition(usercompanyid int64) []protocol.PositionBase {
|
|
|
const datasql string = `SELECT b.name,b.parent_id,b.id FROM user_position AS a
|
|
|
JOIN position AS b ON a.position_id = b.id
|
|
|
WHERE a.enable_status =1 AND a.user_company_id =? `
|
|
|
var (
|
|
|
err error
|
|
|
data []protocol.PositionBase
|
|
|
)
|
|
|
err = utils.ExecuteQueryAll(&data, datasql, usercompanyid)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
}
|
|
|
return data
|
|
|
}
|
|
|
|
|
|
func GetUserRole(usercompanyid int64) []protocol.RoleBase {
|
|
|
const datasql string = `SELECT b.name,b.pid,b.id FROM user_role AS a
|
|
|
JOIN role AS b ON a.role_id = b.id
|
|
|
WHERE a.enable_status =1 AND a.user_company_id =? `
|
|
|
var (
|
|
|
err error
|
|
|
data []protocol.RoleBase
|
|
|
)
|
|
|
err = utils.ExecuteQueryAll(&data, datasql, usercompanyid)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("EXECUTE SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
}
|
|
|
return data
|
|
|
} |
...
|
...
|
|