作者 tangxuhui

处理逻辑变更:同步数据时变更手机号的操作;同步数据是删除用户的操作。

... ... @@ -188,7 +188,7 @@ func GetUserCompanyReal(ids []int64) ([]UserCompany, error) {
func GetUserCompanyAll(companyId int64) (v []*UserCompany, err error) {
o := orm.NewOrm()
sql := `select a.*,b.nick_name from (
select id,user_id from user_company where company_id=? and enable=1
select id,user_id from user_company where company_id=? and enable=1 and delete_at=0
)a inner join user b on a.user_id = b.id`
if _, err = o.Raw(sql, companyId).QueryRows(&v); err == nil {
return v, nil
... ... @@ -200,6 +200,16 @@ func GetUserCompanyByUserId(userId int64) (v []UserCompany, err error) {
o := orm.NewOrm()
_, err = o.QueryTable(&UserCompany{}).
Filter("user_id", userId).
Filter("delete_at", 0).
All(&v)
return
}
func GetUserCompanyByPhone(phone string) (v []UserCompany, err error) {
o := orm.NewOrm()
_, err = o.QueryTable(&UserCompany{}).
Filter("phone", phone).
Filter("delete_at", 0).
All(&v)
return
}
... ...
... ... @@ -565,7 +565,26 @@ func DeleteEmployeeData(data DeleteUserData) error {
log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
return errors.New("获取公司数据失败")
}
var (
userCompanyDatas []models.UserCompany
userIdMap = make(map[int64]int64)
)
o := orm.NewOrm()
o.Begin()
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id__in", data.Ids).
Filter("company_id", companyData.Id).
Filter("delete_at", 0).
All(&userCompanyDatas)
if err != nil {
o.Rollback()
log.Error("获取用户数据失败,data=%+v", data)
return errors.New("删除user_company数据失败")
}
for i := range userCompanyDatas {
userID := userCompanyDatas[i].UserId
userIdMap[userID] = userID
}
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id__in", data.Ids).
Filter("company_id", companyData.Id).
... ... @@ -574,9 +593,36 @@ func DeleteEmployeeData(data DeleteUserData) error {
"enable": models.USERCOMPANY_ENABLE_NO,
})
if err != nil {
o.Rollback()
log.Error("更新user_company数据失败:%s", err)
return errors.New("删除user_company数据失败")
}
var deleteUserIds []int64
for _, userId := range userIdMap {
cnt, err := o.QueryTable(&models.UserCompany{}).
Filter("user_id", userId).
Filter("delete_at", 0).
Count()
if err != nil {
o.Rollback()
log.Error("查询user_company数据出现错误,err=%s", err)
return errors.New("删除user_company数据失败")
}
if cnt == 0 {
deleteUserIds = append(deleteUserIds, userId)
}
}
_, err = o.QueryTable(&models.User{}).
Filter("id__in", deleteUserIds).
Update(orm.Params{
"delete_at": time.Now().Format("2006-01-02 15:04:05"),
})
if err != nil {
o.Rollback()
log.Error("更新user数据失败:%s", err)
return errors.New("删除user_company数据失败")
}
o.Commit()
return nil
}
... ... @@ -660,41 +706,45 @@ func EmployeeChangePhone(data EmployeeChangePhoneData) error {
var (
err error
oldUser *models.User
newUser *models.User
userCompanydatas []models.UserCompany
)
//user 表查询新旧号码用户
newUserCompany, _ := models.GetUserCompanyByPhone(data.NewPhone)
if len(newUserCompany) > 0 {
log.Error("新的手机号已被使用,oldPhone=%s ,newPhone=%s", data.OldPhone, data.NewPhone)
return errors.New("新的手机号已被使用")
}
//user 表查询旧号码用户
oldUser, err = models.GetUserByPhone(data.OldPhone)
if err != nil {
log.Error("获取user数据失败:phone = %s", data.OldPhone)
return errors.New("获取user数据失败")
}
newUser, err = models.GetUserByPhone(data.NewPhone)
if err != nil {
log.Error("获取user数据失败:phone = %s", data.NewPhone)
return errors.New("获取user数据失败")
}
userCompanydatas, err = models.GetUserCompanyByUserId(oldUser.Id)
if err != nil {
log.Error("获取user_company数据失败,user_id=%d", oldUser.Id)
return errors.New("获取user_company数据失败")
}
//user_company表 以旧用户的user_id作条件变更为新用户的对应的user_id,phone,open_id
//user_company表 以旧用户的user_id作条件变更为新用户的对应的phone,open_id
o := orm.NewOrm()
o.Begin()
for i := range userCompanydatas {
temp := userCompanydatas[i]
temp.UserId = newUser.Id
temp.Phone = newUser.Phone
temp.OpenId = newUser.UserCenterId
temp.Phone = data.NewPhone
temp.UpdateAt = time.Now()
err = models.UpdateUserCompanyById(&temp, []string{"UserId", "Phone", "OpenId", "UpdateAt"}, o)
err = models.UpdateUserCompanyById(&temp, []string{"Phone", "UpdateAt"}, o)
if err != nil {
log.Error("更新user_company数据失败,id=%d", temp.Id)
o.Rollback()
return errors.New("变更用户手机号操作失败")
}
}
oldUser.Phone = data.NewPhone
err = models.UpdateUserById(oldUser, []string{"Phone"}, o)
if err != nil {
log.Error("更新user数据失败,id=%d", oldUser.Id)
o.Rollback()
return errors.New("变更用户手机号操作失败")
}
//user.user_center_id 同 user_company表.open_id
//UserAuth注销手机登录
var oldUserAuthData *models.UserAuth
... ...