...
|
...
|
@@ -5,83 +5,343 @@ import ( |
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
|
|
|
|
|
func UserAdd(param protocol.RequestUserAdd) error {
|
|
|
var (
|
|
|
// user models.User
|
|
|
// userRoles []models.RoleRelated
|
|
|
// userPositions []models.UserPosition
|
|
|
// userDeparts []models.UserPosition
|
|
|
userm *models.User
|
|
|
err error
|
|
|
)
|
|
|
//校验角色
|
|
|
validCompanyRole(param.CompanyId, param.Roles)
|
|
|
err = validCompanyRole(param.CompanyId, param.Roles)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10031")
|
|
|
}
|
|
|
//校验部门
|
|
|
validCompanyDepart(param.CompanyId, param.Departments)
|
|
|
err = validCompanyDepart(param.CompanyId, param.Departments)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10032")
|
|
|
}
|
|
|
//校验职位
|
|
|
validCompanyPositions(param.CompanyId, param.Positions)
|
|
|
//添加用户
|
|
|
err = validCompanyPosition(param.CompanyId, param.Positions)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10033")
|
|
|
}
|
|
|
userm = &models.User{
|
|
|
NickName: param.Name,
|
|
|
Phone: param.Phone,
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
err = registUser(userm, param.CompanyId, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("registUser err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加角色
|
|
|
err = editUserRole(userm.Id, 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)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加职位
|
|
|
err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
func validCompanyRole(companyid int64, roleid []int64) error {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
for _, v := range roleid {
|
|
|
ur := models.UserRole{
|
|
|
CompanyId: companyid,
|
|
|
RoleId: v,
|
|
|
EnableStatus: models.USER_ROLE_ENABLE_YES,
|
|
|
}
|
|
|
err = ur.ValidCompanyRole()
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("ValidCompanyRole err:%s [company:%d,role:%d]", err, companyid, v)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("10031")
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func validCompanyRole(companyId int64, roleIds []int64) error {
|
|
|
for _, v := range roleIds {
|
|
|
role, err := models.GetRoleById(v)
|
|
|
func validCompanyDepart(companyid int64, departid []int64) error {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
for _, v := range departid {
|
|
|
ud := models.UserDepartment{
|
|
|
CompanyId: companyid,
|
|
|
DepartmentId: v,
|
|
|
EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
|
|
|
}
|
|
|
err = ud.ValidCompanyDepart()
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetRoleById(%d) err:%s", v, err)
|
|
|
e := fmt.Errorf("ValidCompanyDepart err:%s [company:%d,department:%d]", err, companyid, v)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return protocol.NewErrWithMessage("10032")
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func validCompanyPosition(companyid int64, positionid []int64) error {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
for _, v := range positionid {
|
|
|
ud := models.UserPosition{
|
|
|
|
|
|
CompanyId: companyid,
|
|
|
PositionId: v,
|
|
|
EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
|
|
|
}
|
|
|
if role.CompanyId != companyId {
|
|
|
e := fmt.Errorf(" role.CompanyId != param.CompanyId ")
|
|
|
err = ud.ValidCompanyPosition()
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("ValidCompanyPosition err:%s [company:%d,department:%d]", err, companyid, v)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return protocol.NewErrWithMessage("10033")
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func validCompanyDepart(companyId int64, departIds []int64) error {
|
|
|
for _, v := range departIds {
|
|
|
depart, err := models.GetDepartmentById(v)
|
|
|
//registUser 注册用户
|
|
|
func registUser(userIn *models.User, companyid int64, o orm.Ormer) error {
|
|
|
var (
|
|
|
err error
|
|
|
// usrData *models.User
|
|
|
)
|
|
|
_, err = models.GetUserByPhone(userIn.Phone)
|
|
|
if err == nil {
|
|
|
return nil
|
|
|
}
|
|
|
if err != nil && err != orm.ErrNoRows {
|
|
|
return err
|
|
|
}
|
|
|
if err == orm.ErrNoRows {
|
|
|
_, err := models.AddUser(userIn, o)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", v, err)
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
musercompany := &models.UserCompany{
|
|
|
CompanyId: companyid,
|
|
|
UserId: userIn.Id,
|
|
|
}
|
|
|
_, err = models.AddUserCompany(musercompany, o)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
//更新
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func editUserDepart(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)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return e
|
|
|
}
|
|
|
if depart.CompanyId != companyId {
|
|
|
e := fmt.Errorf(" depart.CompanyId != param.CompanyId ")
|
|
|
delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids)
|
|
|
addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds)
|
|
|
for _, v := range delDepartIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return e
|
|
|
}
|
|
|
}
|
|
|
nowTime := time.Now().String()
|
|
|
for _, v := range addDepartIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return e
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func validCompanyPositions(companyId int64, positionIds []int64) error {
|
|
|
for _, v := range positionIds {
|
|
|
pos, err := models.GetPositionById(v)
|
|
|
func editUserPosition(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)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetDepartmentById(%d) err:%s", v, err)
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return e
|
|
|
}
|
|
|
if pos.CompanyId != companyId {
|
|
|
e := fmt.Errorf("pos.CompanyId != pos.CompanyId")
|
|
|
delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids)
|
|
|
addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds)
|
|
|
for _, v := range delPositionIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return e
|
|
|
}
|
|
|
}
|
|
|
nowTime := time.Now().String()
|
|
|
for _, v := range addPositionIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return e
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
func editUserRole(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_position 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 {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return e
|
|
|
}
|
|
|
delRoleIds = utils.ArrayInt64Diff(oldRoleIds, roleids)
|
|
|
addRoleIds = utils.ArrayInt64Diff(roleids, oldRoleIds)
|
|
|
for _, v := range delRoleIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, dataSql2, companyid, userid, v)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return e
|
|
|
}
|
|
|
}
|
|
|
for _, v := range addRoleIds {
|
|
|
err = utils.ExecuteSQLWithOrmer(o, datasql3, userid, v, companyid, models.USER_POSITION_ENABLE_YES)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("Execute SQL err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return e
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func UserEdit(param protocol.RequestUserEdit) error {
|
|
|
var (
|
|
|
userm *models.User
|
|
|
err error
|
|
|
)
|
|
|
//校验角色
|
|
|
err = validCompanyRole(param.CompanyId, param.Roles)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10031")
|
|
|
}
|
|
|
//校验部门
|
|
|
err = validCompanyDepart(param.CompanyId, param.Departments)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10032")
|
|
|
}
|
|
|
//校验职位
|
|
|
err = validCompanyPosition(param.CompanyId, param.Positions)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("10033")
|
|
|
}
|
|
|
userm = &models.User{
|
|
|
Id: param.ID,
|
|
|
NickName: param.Name,
|
|
|
Phone: param.Phone,
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
// err = registUser(userm, param.CompanyId, o)
|
|
|
// if err != nil {
|
|
|
// o.Rollback()
|
|
|
// log.Error("registUser err:%s", err)
|
|
|
// return protocol.NewErrWithMessage("1")
|
|
|
// }
|
|
|
//添加角色
|
|
|
err = editUserRole(userm.Id, 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)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//添加职位
|
|
|
err = editUserDepart(userm.Id, param.CompanyId, param.Positions, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("editUserDepart err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func UserDelete(param protocol.RequestUserAdd) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func UserList() error {
|
|
|
return nil
|
|
|
} |
...
|
...
|
|