作者 tangxuhui

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

@@ -188,7 +188,7 @@ func GetUserCompanyReal(ids []int64) ([]UserCompany, error) { @@ -188,7 +188,7 @@ func GetUserCompanyReal(ids []int64) ([]UserCompany, error) {
188 func GetUserCompanyAll(companyId int64) (v []*UserCompany, err error) { 188 func GetUserCompanyAll(companyId int64) (v []*UserCompany, err error) {
189 o := orm.NewOrm() 189 o := orm.NewOrm()
190 sql := `select a.*,b.nick_name from ( 190 sql := `select a.*,b.nick_name from (
191 - select id,user_id from user_company where company_id=? and enable=1 191 + select id,user_id from user_company where company_id=? and enable=1 and delete_at=0
192 )a inner join user b on a.user_id = b.id` 192 )a inner join user b on a.user_id = b.id`
193 if _, err = o.Raw(sql, companyId).QueryRows(&v); err == nil { 193 if _, err = o.Raw(sql, companyId).QueryRows(&v); err == nil {
194 return v, nil 194 return v, nil
@@ -200,6 +200,16 @@ func GetUserCompanyByUserId(userId int64) (v []UserCompany, err error) { @@ -200,6 +200,16 @@ func GetUserCompanyByUserId(userId int64) (v []UserCompany, err error) {
200 o := orm.NewOrm() 200 o := orm.NewOrm()
201 _, err = o.QueryTable(&UserCompany{}). 201 _, err = o.QueryTable(&UserCompany{}).
202 Filter("user_id", userId). 202 Filter("user_id", userId).
  203 + Filter("delete_at", 0).
  204 + All(&v)
  205 + return
  206 +}
  207 +
  208 +func GetUserCompanyByPhone(phone string) (v []UserCompany, err error) {
  209 + o := orm.NewOrm()
  210 + _, err = o.QueryTable(&UserCompany{}).
  211 + Filter("phone", phone).
  212 + Filter("delete_at", 0).
203 All(&v) 213 All(&v)
204 return 214 return
205 } 215 }
@@ -565,7 +565,26 @@ func DeleteEmployeeData(data DeleteUserData) error { @@ -565,7 +565,26 @@ func DeleteEmployeeData(data DeleteUserData) error {
565 log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err) 565 log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
566 return errors.New("获取公司数据失败") 566 return errors.New("获取公司数据失败")
567 } 567 }
  568 + var (
  569 + userCompanyDatas []models.UserCompany
  570 + userIdMap = make(map[int64]int64)
  571 + )
568 o := orm.NewOrm() 572 o := orm.NewOrm()
  573 + o.Begin()
  574 + _, err = o.QueryTable(&models.UserCompany{}).
  575 + Filter("id__in", data.Ids).
  576 + Filter("company_id", companyData.Id).
  577 + Filter("delete_at", 0).
  578 + All(&userCompanyDatas)
  579 + if err != nil {
  580 + o.Rollback()
  581 + log.Error("获取用户数据失败,data=%+v", data)
  582 + return errors.New("删除user_company数据失败")
  583 + }
  584 + for i := range userCompanyDatas {
  585 + userID := userCompanyDatas[i].UserId
  586 + userIdMap[userID] = userID
  587 + }
569 _, err = o.QueryTable(&models.UserCompany{}). 588 _, err = o.QueryTable(&models.UserCompany{}).
570 Filter("id__in", data.Ids). 589 Filter("id__in", data.Ids).
571 Filter("company_id", companyData.Id). 590 Filter("company_id", companyData.Id).
@@ -574,9 +593,36 @@ func DeleteEmployeeData(data DeleteUserData) error { @@ -574,9 +593,36 @@ func DeleteEmployeeData(data DeleteUserData) error {
574 "enable": models.USERCOMPANY_ENABLE_NO, 593 "enable": models.USERCOMPANY_ENABLE_NO,
575 }) 594 })
576 if err != nil { 595 if err != nil {
  596 + o.Rollback()
577 log.Error("更新user_company数据失败:%s", err) 597 log.Error("更新user_company数据失败:%s", err)
578 return errors.New("删除user_company数据失败") 598 return errors.New("删除user_company数据失败")
579 } 599 }
  600 + var deleteUserIds []int64
  601 + for _, userId := range userIdMap {
  602 + cnt, err := o.QueryTable(&models.UserCompany{}).
  603 + Filter("user_id", userId).
  604 + Filter("delete_at", 0).
  605 + Count()
  606 + if err != nil {
  607 + o.Rollback()
  608 + log.Error("查询user_company数据出现错误,err=%s", err)
  609 + return errors.New("删除user_company数据失败")
  610 + }
  611 + if cnt == 0 {
  612 + deleteUserIds = append(deleteUserIds, userId)
  613 + }
  614 + }
  615 + _, err = o.QueryTable(&models.User{}).
  616 + Filter("id__in", deleteUserIds).
  617 + Update(orm.Params{
  618 + "delete_at": time.Now().Format("2006-01-02 15:04:05"),
  619 + })
  620 + if err != nil {
  621 + o.Rollback()
  622 + log.Error("更新user数据失败:%s", err)
  623 + return errors.New("删除user_company数据失败")
  624 + }
  625 + o.Commit()
580 return nil 626 return nil
581 } 627 }
582 628
@@ -660,41 +706,45 @@ func EmployeeChangePhone(data EmployeeChangePhoneData) error { @@ -660,41 +706,45 @@ func EmployeeChangePhone(data EmployeeChangePhoneData) error {
660 var ( 706 var (
661 err error 707 err error
662 oldUser *models.User 708 oldUser *models.User
663 - newUser *models.User  
664 userCompanydatas []models.UserCompany 709 userCompanydatas []models.UserCompany
665 ) 710 )
666 - //user 表查询新旧号码用户 711 + newUserCompany, _ := models.GetUserCompanyByPhone(data.NewPhone)
  712 + if len(newUserCompany) > 0 {
  713 + log.Error("新的手机号已被使用,oldPhone=%s ,newPhone=%s", data.OldPhone, data.NewPhone)
  714 + return errors.New("新的手机号已被使用")
  715 + }
  716 + //user 表查询旧号码用户
667 oldUser, err = models.GetUserByPhone(data.OldPhone) 717 oldUser, err = models.GetUserByPhone(data.OldPhone)
668 if err != nil { 718 if err != nil {
669 log.Error("获取user数据失败:phone = %s", data.OldPhone) 719 log.Error("获取user数据失败:phone = %s", data.OldPhone)
670 return errors.New("获取user数据失败") 720 return errors.New("获取user数据失败")
671 } 721 }
672 - newUser, err = models.GetUserByPhone(data.NewPhone)  
673 - if err != nil {  
674 - log.Error("获取user数据失败:phone = %s", data.NewPhone)  
675 - return errors.New("获取user数据失败")  
676 - }  
677 userCompanydatas, err = models.GetUserCompanyByUserId(oldUser.Id) 722 userCompanydatas, err = models.GetUserCompanyByUserId(oldUser.Id)
678 if err != nil { 723 if err != nil {
679 log.Error("获取user_company数据失败,user_id=%d", oldUser.Id) 724 log.Error("获取user_company数据失败,user_id=%d", oldUser.Id)
680 return errors.New("获取user_company数据失败") 725 return errors.New("获取user_company数据失败")
681 } 726 }
682 - //user_company表 以旧用户的user_id作条件变更为新用户的对应的user_id,phone,open_id 727 + //user_company表 以旧用户的user_id作条件变更为新用户的对应的phone,open_id
683 o := orm.NewOrm() 728 o := orm.NewOrm()
684 o.Begin() 729 o.Begin()
685 for i := range userCompanydatas { 730 for i := range userCompanydatas {
686 temp := userCompanydatas[i] 731 temp := userCompanydatas[i]
687 - temp.UserId = newUser.Id  
688 - temp.Phone = newUser.Phone  
689 - temp.OpenId = newUser.UserCenterId 732 + temp.Phone = data.NewPhone
690 temp.UpdateAt = time.Now() 733 temp.UpdateAt = time.Now()
691 - err = models.UpdateUserCompanyById(&temp, []string{"UserId", "Phone", "OpenId", "UpdateAt"}, o) 734 + err = models.UpdateUserCompanyById(&temp, []string{"Phone", "UpdateAt"}, o)
692 if err != nil { 735 if err != nil {
693 log.Error("更新user_company数据失败,id=%d", temp.Id) 736 log.Error("更新user_company数据失败,id=%d", temp.Id)
694 o.Rollback() 737 o.Rollback()
695 return errors.New("变更用户手机号操作失败") 738 return errors.New("变更用户手机号操作失败")
696 } 739 }
697 } 740 }
  741 + oldUser.Phone = data.NewPhone
  742 + err = models.UpdateUserById(oldUser, []string{"Phone"}, o)
  743 + if err != nil {
  744 + log.Error("更新user数据失败,id=%d", oldUser.Id)
  745 + o.Rollback()
  746 + return errors.New("变更用户手机号操作失败")
  747 + }
698 //user.user_center_id 同 user_company表.open_id 748 //user.user_center_id 同 user_company表.open_id
699 //UserAuth注销手机登录 749 //UserAuth注销手机登录
700 var oldUserAuthData *models.UserAuth 750 var oldUserAuthData *models.UserAuth