作者 唐旭辉

修改员工编辑功能

... ... @@ -13,6 +13,7 @@ type UserCompany struct {
Id int64 `orm:"column(id);auto" description:"唯一标识"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
NickName string `orm:"column(nick_name)"`
ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"`
CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
... ... @@ -53,6 +54,15 @@ func init() {
orm.RegisterModel(new(UserCompany))
}
func GetUserCompanyById(id int64) (v *UserCompany, err error) {
o := orm.NewOrm()
v = &UserCompany{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// AddUserCompany insert a new UserCompany into database and returns
// last inserted Id on success.
func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) {
... ... @@ -66,16 +76,19 @@ func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) {
// UpdateUserCompany updates UserCompany by Id and returns error if
// the record to be updated doesn't exist
func UpdateUserCompanyById(m *UserCompany) (err error) {
o := orm.NewOrm()
v := UserCompany{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)
}
func UpdateUserCompanyById(m *UserCompany, col []string, om ...orm.Ormer) (err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
var num int64
m.UpdateAt = time.Now()
if num, err = o.Update(m, col...); err == nil {
fmt.Println("Number of records updated in database:", num)
}
return
}
... ...
... ... @@ -8,8 +8,8 @@ import (
)
type UserDepartment struct {
Id int64 `orm:"column(id);auto" description:"主键"`
UserId int64 `orm:"column(user_id)" description:"用户id"`
Id int64 `orm:"column(id);auto" description:"主键"`
//UserId int64 `orm:"column(user_id)" description:"用户id"`
CompanyId int64 `orm:"column(company_id)" description:"公司id"`
DepartmentId int64 `orm:"column(department_id)" description:"部门id"`
CreateTime time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"`
... ...
... ... @@ -8,8 +8,8 @@ import (
)
type UserPosition struct {
Id int64 `orm:"column(id)" description:"唯一键值"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
Id int64 `orm:"column(id)" description:"唯一键值"`
//UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
... ...
... ... @@ -9,7 +9,6 @@ import (
type UserRole struct {
Id int `orm:"column(id)"`
RoleId int64 `orm:"column(role_id)"`
UserId int64 `orm:"column(user_id)"`
EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
UserCompanyId int64 `orm:"column(user_company_id)"`
... ...
... ... @@ -43,7 +43,7 @@ type PositionBase struct {
//DepartUserBase 下拉选择列表-部门下的人员
type DepartUserBase struct {
UserCompanyId int64 `json:"id" orm:"column(user_company_id)"`
Name string `json:"name" orm:"-"`
NickName string `json:"name" orm:"column(nick_name)"`
}
//部门和人员混合
... ...
... ... @@ -89,7 +89,7 @@ type RequestUserAdd struct {
//RequestUserEdit 编辑用户
type RequestUserEdit struct {
ID int64 `json:"id"`
UserCompanyID int64 `json:"user_company_id"`
RequestUserAdd
}
... ...
... ... @@ -290,9 +290,9 @@ func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models
//添加部门
newUserDepartment = &models.UserDepartment{
UserCompanyId: newUserCompany.Id,
UserId: newUserCompany.UserId,
CompanyId: newUserCompany.CompanyId,
DepartmentId: newDepartment.Id,
//UserId: newUserCompany.UserId,
CompanyId: newUserCompany.CompanyId,
DepartmentId: newDepartment.Id,
}
_, err = models.AddUserDepartment(newUserDepartment, o)
if err != nil {
... ... @@ -394,7 +394,6 @@ func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o or
RoleId: newRole.Id,
UserCompanyId: newUserCompany.Id,
CompanyId: newUserCompany.CompanyId,
UserId: newUserCompany.UserId,
}
_, err = models.AddUserRole(newUserRole, o)
if err != nil {
... ...
... ... @@ -386,33 +386,16 @@ func GetDepartmentUser(companyid int64, departmentid int64) ([]protocol.DepartUs
log.Error("deparment.CompanyId err")
return nil, protocol.NewErrWithMessage("1")
}
const dataSql string = `SELECT b.user_id,b.id AS user_company_id FROM user_department AS a
JOIN user_company AS b ON a.user_company_id = b.id
WHERE a.department_id=? AND b.delete_at=0 `
type UsercompanyId struct {
UserCompanyId int64 `orm:"column(user_company_id)"`
UserId int64 `orm:"column(user_id)"`
}
const dataSql string = `SELECT b.nick_name,a.user_company_id FROM user_department AS a
JOIN user_company AS b ON a.user_company_id = b.id
WHERE a.department_id=? AND b.delete_at=0 AND a.enable_status = 1 `
var (
usercompanyData []UsercompanyId
returnData []protocol.DepartUserBase
returnData []protocol.DepartUserBase
)
err = utils.ExecuteQueryAll(&usercompanyData, dataSql, department.Id)
err = utils.ExecuteQueryAll(&returnData, dataSql, department.Id)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
for _, v := range usercompanyData {
udata, err := models.GetUserById(v.UserId)
if err != nil {
log.Error("获取user数据失败:%s", err)
continue
}
d := protocol.DepartUserBase{
UserCompanyId: v.UserCompanyId,
Name: udata.NickName,
}
returnData = append(returnData, d)
}
return returnData, nil
}
... ...
... ... @@ -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 `
... ...