...
|
...
|
@@ -15,11 +15,10 @@ import ( |
|
|
|
|
|
func UserAdd(param protocol.RequestUserAdd) error {
|
|
|
var (
|
|
|
userm *models.User
|
|
|
err error
|
|
|
err error
|
|
|
)
|
|
|
//校验用户
|
|
|
err = validCompanyUser(param.CompanyId, param.Phone)
|
|
|
//校验用户是否存在
|
|
|
err = existCompanyUser(param.CompanyId, param.Phone)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
...
|
...
|
@@ -41,34 +40,41 @@ func UserAdd(param protocol.RequestUserAdd) error { |
|
|
log.Error(err.Error())
|
|
|
return err
|
|
|
}
|
|
|
userm = &models.User{
|
|
|
NickName: param.Name,
|
|
|
Phone: param.Phone,
|
|
|
}
|
|
|
var (
|
|
|
userId int64
|
|
|
userCompanyid int64
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
err = registUser(userm, param.CompanyId, o)
|
|
|
//添加用户
|
|
|
userId, err = addNewUser(param.Name, param.Phone, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("registUser err:%s", err)
|
|
|
log.Error("addNewUser err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加用户公司关系
|
|
|
userCompanyid, err = addUserCompany(userId, param.CompanyId, param.Name, o)
|
|
|
if err != nil {
|
|
|
log.Error("addUserCompany err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加角色
|
|
|
err = editUserRole(userm.Id, param.CompanyId, param.Roles, o)
|
|
|
err = editUserRole(userCompanyid, userId, param.CompanyId, param.Roles, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserRole err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加部门
|
|
|
err = editUserDepart(userm.Id, param.CompanyId, param.Departments, o)
|
|
|
err = editUserDepart(userCompanyid, userId, param.CompanyId, param.Departments, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加职位
|
|
|
err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o)
|
|
|
err = editUserPosition(userCompanyid, userId, param.CompanyId, param.Positions, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
...
|
...
|
@@ -141,7 +147,7 @@ func validCompanyPosition(companyid int64, positionid []int64) error { |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func validCompanyUser(companyid int64, phone string) error {
|
|
|
func existCompanyUser(companyid int64, phone string) error {
|
|
|
userdata, err := models.GetUserByPhone(phone)
|
|
|
if err == orm.ErrNoRows {
|
|
|
return nil
|
...
|
...
|
@@ -159,56 +165,93 @@ func validCompanyUser(companyid int64, phone string) error { |
|
|
}
|
|
|
|
|
|
//registUser 注册用户
|
|
|
func registUser(userIn *models.User, companyid int64, o orm.Ormer) error {
|
|
|
// func registUser(userIn *models.User, companyid int64, o orm.Ormer) error {
|
|
|
// var (
|
|
|
// err error
|
|
|
// usrData *models.User
|
|
|
// )
|
|
|
// usrData, err = models.GetUserByPhone(userIn.Phone)
|
|
|
// if err != nil && err != orm.ErrNoRows {
|
|
|
// return err
|
|
|
// }
|
|
|
// if err == orm.ErrNoRows {
|
|
|
// //用户不存在 添加用户
|
|
|
// _, err := models.AddUser(userIn, o)
|
|
|
// if err != nil {
|
|
|
// return err
|
|
|
// }
|
|
|
// }
|
|
|
// if err == nil {
|
|
|
// // 用户存在,更新用户
|
|
|
// usrData.NickName = userIn.NickName
|
|
|
// err = models.UpdateUserById(usrData, []string{"NickName"}, o)
|
|
|
// if err != nil {
|
|
|
// return err
|
|
|
// }
|
|
|
// }
|
|
|
// musercompany := &models.UserCompany{
|
|
|
// CompanyId: companyid,
|
|
|
// UserId: userIn.Id,
|
|
|
// }
|
|
|
|
|
|
// _, err = models.AddUserCompany(musercompany, o)
|
|
|
// if err != nil {
|
|
|
// return err
|
|
|
// }
|
|
|
// return nil
|
|
|
// }
|
|
|
|
|
|
func addNewUser(name string, phone string, o orm.Ormer) (userid int64, err error) {
|
|
|
var (
|
|
|
err error
|
|
|
usrData *models.User
|
|
|
)
|
|
|
usrData, err = models.GetUserByPhone(userIn.Phone)
|
|
|
usrData, err = models.GetUserByPhone(phone)
|
|
|
if err != nil && err != orm.ErrNoRows {
|
|
|
return err
|
|
|
return 0, err
|
|
|
}
|
|
|
if err == orm.ErrNoRows {
|
|
|
//用户不存在 添加用户
|
|
|
_, err := models.AddUser(userIn, o)
|
|
|
usrData = &models.User{
|
|
|
Phone: phone,
|
|
|
NickName: name,
|
|
|
}
|
|
|
_, err := models.AddUser(usrData, o)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return 0, err
|
|
|
}
|
|
|
return usrData.Id, nil
|
|
|
}
|
|
|
if err == nil {
|
|
|
// 用户存在,更新用户
|
|
|
usrData.NickName = userIn.NickName
|
|
|
err = models.UpdateUserById(usrData, []string{"NickName"}, o)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
// 用户存在,
|
|
|
return usrData.Id, nil
|
|
|
}
|
|
|
musercompany := &models.UserCompany{
|
|
|
CompanyId: companyid,
|
|
|
UserId: userIn.Id,
|
|
|
return 0, err
|
|
|
}
|
|
|
|
|
|
func addUserCompany(userId int64, companyId int64, nickname string, o orm.Ormer) (id int64, err error) {
|
|
|
m := &models.UserCompany{
|
|
|
UserId: userId,
|
|
|
CompanyId: companyId,
|
|
|
NickName: nickname,
|
|
|
}
|
|
|
// ok := models.ExistUserCompany(usrData.Id, companyid)
|
|
|
// if !ok {
|
|
|
_, err = models.AddUserCompany(musercompany, o)
|
|
|
id, err = models.AddUserCompany(m, o)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return 0, err
|
|
|
}
|
|
|
// }
|
|
|
// err = models.EnableUserCompany(usrData.Id, companyid)
|
|
|
return nil
|
|
|
return id, err
|
|
|
}
|
|
|
|
|
|
func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Ormer) error {
|
|
|
func editUserDepart(usercompanyid int64, userid int64, companyid int64, departids []int64, o orm.Ormer) error {
|
|
|
var (
|
|
|
oldDepartIds []int64
|
|
|
err error
|
|
|
delDepartIds []int64
|
|
|
addDepartIds []int64
|
|
|
)
|
|
|
dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND company_id=? AND user_id=?`
|
|
|
dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE company_id = ? AND user_id=? AND department_id=?`
|
|
|
datasql3 := `INSERT INTO user_department (user_id,department_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, companyid, userid)
|
|
|
dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND user_company_id=?`
|
|
|
dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE user_company_id=? AND department_id=?`
|
|
|
datasql3 := `INSERT INTO user_department (user_company_id,department_id,company_id,enable_status,create_time) VALUES (?,?,?,?,?)`
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, usercompanyid)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -217,7 +260,7 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme |
|
|
delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids)
|
|
|
addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds)
|
|
|
for _, v := range delDepartIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -226,7 +269,7 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme |
|
|
}
|
|
|
nowTime := time.Now().String()
|
|
|
for _, v := range addDepartIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -236,17 +279,17 @@ func editUserDepart(userid int64, companyid int64, departids []int64, o orm.Orme |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func editUserPosition(userid int64, companyid int64, positionids []int64, o orm.Ormer) error {
|
|
|
func editUserPosition(usercompanyid int64, userid int64, companyid int64, positionids []int64, o orm.Ormer) error {
|
|
|
var (
|
|
|
oldPositionIds []int64
|
|
|
err error
|
|
|
delPositionIds []int64
|
|
|
addPositionIds []int64
|
|
|
)
|
|
|
dataSql := `SELECT position_id FROM user_position 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 position_id=?`
|
|
|
datasql3 := `INSERT INTO user_position (user_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, companyid, userid)
|
|
|
dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND user_company_id=?`
|
|
|
dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE user_company_id=? AND position_id=?`
|
|
|
datasql3 := `INSERT INTO user_position (user_company_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, usercompanyid)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -255,7 +298,7 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm. |
|
|
delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids)
|
|
|
addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds)
|
|
|
for _, v := range delPositionIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -264,7 +307,7 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm. |
|
|
}
|
|
|
nowTime := time.Now().String()
|
|
|
for _, v := range addPositionIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -273,17 +316,17 @@ func editUserPosition(userid int64, companyid int64, positionids []int64, o orm. |
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) error {
|
|
|
func editUserRole(usercompanyid int64, userid int64, companyid int64, roleids []int64, o orm.Ormer) error {
|
|
|
var (
|
|
|
oldRoleIds []int64
|
|
|
err error
|
|
|
delRoleIds []int64
|
|
|
addRoleIds []int64
|
|
|
)
|
|
|
dataSql := `SELECT role_id FROM user_role WHERE enable_status=1 AND company_id=? AND user_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)
|
|
|
dataSql := `SELECT role_id FROM user_role WHERE enable_status=1 AND user_company_id=?`
|
|
|
dataSql2 := `UPDATE user_role SET enable_status = 2 WHERE user_company_id=? AND role_id=?`
|
|
|
datasql3 := `INSERT INTO user_role (user_company_id,role_id,company_id,enable_status) VALUES (?,?,?,?)`
|
|
|
err = utils.ExecuteQueryAllWithOrmer(o, &oldRoleIds, dataSql, usercompanyid)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -292,7 +335,7 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e |
|
|
delRoleIds = utils.ArrayInt64Diff(oldRoleIds, roleids)
|
|
|
addRoleIds = utils.ArrayInt64Diff(roleids, oldRoleIds)
|
|
|
for _, v := range delRoleIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -300,7 +343,7 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e |
|
|
}
|
|
|
}
|
|
|
for _, v := range addRoleIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES)
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
...
|
...
|
@@ -310,12 +353,38 @@ func editUserRole(userid int64, companyid int64, roleids []int64, o orm.Ormer) e |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func editUserCompany(userCompanyid int64, userid int64, nickname string, o orm.Ormer) error {
|
|
|
m := &models.UserCompany{
|
|
|
Id: userCompanyid,
|
|
|
NickName: nickname,
|
|
|
UserId: userid,
|
|
|
}
|
|
|
|
|
|
err := models.UpdateUserCompanyById(m, []string{"NickName", "UserId"}, o)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
//校验user和公司的关系是否正确
|
|
|
func validUserCompany(userCompanyid int64, companyid int64) (*models.UserCompany, error) {
|
|
|
ucompany, err := models.GetUserCompanyById(userCompanyid)
|
|
|
if err != nil {
|
|
|
log.Error("GetUserCompanyById err;%s", err)
|
|
|
return nil, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if ucompany.CompanyId != companyid {
|
|
|
log.Error("用户与公司不关联")
|
|
|
return nil, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
return ucompany, nil
|
|
|
}
|
|
|
|
|
|
func UserEdit(param protocol.RequestUserEdit) error {
|
|
|
var (
|
|
|
userm *models.User
|
|
|
err error
|
|
|
err error
|
|
|
ucompany *models.UserCompany
|
|
|
)
|
|
|
err = validCompanyUser(param.CompanyId, param.Phone)
|
|
|
//校验公司和用户的关联
|
|
|
ucompany, err = validUserCompany(param.UserCompanyID, param.CompanyId)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
...
|
...
|
@@ -323,44 +392,64 @@ func UserEdit(param protocol.RequestUserEdit) error { |
|
|
err = validCompanyRole(param.CompanyId, param.Roles)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10031")
|
|
|
return err
|
|
|
}
|
|
|
//校验部门
|
|
|
err = validCompanyDepart(param.CompanyId, param.Departments)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10032")
|
|
|
return err
|
|
|
}
|
|
|
//校验职位
|
|
|
err = validCompanyPosition(param.CompanyId, param.Positions)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10033")
|
|
|
return err
|
|
|
}
|
|
|
userm = &models.User{
|
|
|
Id: param.ID,
|
|
|
NickName: param.Name,
|
|
|
Phone: param.Phone,
|
|
|
//检查用户手机号是否生变更
|
|
|
var (
|
|
|
usrData *models.User
|
|
|
)
|
|
|
usrData, err = models.GetUserById(ucompany.UserId)
|
|
|
if err != nil {
|
|
|
log.Error("获取用户数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
// TODO 用户数据更新
|
|
|
if usrData.Phone != param.Phone {
|
|
|
//手机号变更
|
|
|
uid, err := addNewUser(param.Name, param.Phone, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("手机号变更,新增用户失败;%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
ucompany.UserId = uid
|
|
|
}
|
|
|
//用户数据更新
|
|
|
err = editUserCompany(param.UserCompanyID, ucompany.UserId, param.Name, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserCompany err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加角色
|
|
|
err = editUserRole(userm.Id, param.CompanyId, param.Roles, o)
|
|
|
err = editUserRole(param.UserCompanyID, ucompany.UserId, param.CompanyId, param.Roles, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserRole err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加部门
|
|
|
err = editUserDepart(userm.Id, param.CompanyId, param.Departments, o)
|
|
|
err = editUserDepart(param.UserCompanyID, ucompany.UserId, param.CompanyId, param.Departments, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加职位
|
|
|
err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o)
|
|
|
err = editUserPosition(param.UserCompanyID, ucompany.UserId, param.CompanyId, param.Positions, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
...
|
...
|
@@ -415,7 +504,7 @@ func UserDelete(userCompanyids []int64, companyid int64) error { |
|
|
|
|
|
func UserList(param protocol.RequestUserList) (protocol.ResponseUserList, error) {
|
|
|
|
|
|
datasql := `SELECT a.id as user_company_id,a.company_id,a.user_id,a.enable, b.phone,b.nick_name
|
|
|
datasql := `SELECT a.id as user_company_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 `
|
...
|
...
|
|