作者 唐旭辉

修复:同步公司数据时更新公司主管数据

@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "fmt" 6 "fmt"
7 "time" 7 "time"
8 8
  9 + "github.com/astaxie/beego/logs"
9 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory" 10 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
10 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" 11 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
11 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" 12 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
@@ -162,7 +163,8 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error { @@ -162,7 +163,8 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error {
162 companyRespository domain.CompanyRepository 163 companyRespository domain.CompanyRepository
163 userRespository domain.UsersRepository 164 userRespository domain.UsersRepository
164 oldCompany domain.Company 165 oldCompany domain.Company
165 - oldUser domain.Users 166 + oldCompanyAdmins []domain.Users
  167 + newUser domain.Users
166 ) 168 )
167 if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{ 169 if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
168 "transactionContext": transactionContext, 170 "transactionContext": transactionContext,
@@ -177,7 +179,11 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error { @@ -177,7 +179,11 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error {
177 oldCompany, err = companyRespository.FindOne(domain.CompanyFindOneOptions{ 179 oldCompany, err = companyRespository.FindOne(domain.CompanyFindOneOptions{
178 Id: data.Company.Id, 180 Id: data.Company.Id,
179 }) 181 })
180 - oldUser, err = userRespository.FindOne(domain.UsersFindOneQuery{ 182 + _, oldCompanyAdmins, err = userRespository.Find(domain.UsersFindQuery{
  183 + CompanyId: data.Company.Id,
  184 + AdminType: domain.UserIsAdmin,
  185 + })
  186 + newUser, err = userRespository.FindOne(domain.UsersFindOneQuery{
181 Id: data.User.Id, 187 Id: data.User.Id,
182 }) 188 })
183 oldCompany.Update(map[string]interface{}{ 189 oldCompany.Update(map[string]interface{}{
@@ -187,8 +193,29 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error { @@ -187,8 +193,29 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error {
187 "AdminCompanyId": data.Company.AdminCompanyId, 193 "AdminCompanyId": data.Company.AdminCompanyId,
188 "Abbreviation": data.Company.Abbreviation, 194 "Abbreviation": data.Company.Abbreviation,
189 }) 195 })
190 - entryTime, _ := time.Parse("2006-01-02", data.User.EntryTime)  
191 - oldUser.Update(map[string]interface{}{ 196 + for i := range oldCompanyAdmins {
  197 + oldCompanyAdmins[i].Update(map[string]interface{}{
  198 + "AdminType": domain.UserIsNotAdmin,
  199 + })
  200 + err = userRespository.Edit(&oldCompanyAdmins[i])
  201 + if err != nil {
  202 + e := fmt.Sprintf("更新公司主管user数据(id=%d)失败:%s",
  203 + oldCompanyAdmins[i].Id, err)
  204 + return lib.ThrowError(lib.BUSINESS_ERROR, e)
  205 + }
  206 + }
  207 + err = companyRespository.Edit(&oldCompany)
  208 + if err != nil {
  209 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  210 + }
  211 + var entryTime time.Time
  212 + if len(data.User.EntryTime) > 0 {
  213 + entryTime, err = time.Parse("2006-01-02", data.User.EntryTime)
  214 + if err != nil {
  215 + logs.Error("Parse User.EntryTime 错误:%s", err)
  216 + }
  217 + }
  218 + newUser.Update(map[string]interface{}{
192 "CompanyId": data.User.CompanyId, 219 "CompanyId": data.User.CompanyId,
193 "OpenId": data.User.OpenId, 220 "OpenId": data.User.OpenId,
194 "Name": data.User.Name, 221 "Name": data.User.Name,
@@ -206,11 +233,7 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error { @@ -206,11 +233,7 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error {
206 "EntryTime": entryTime, 233 "EntryTime": entryTime,
207 "AdminType": data.User.AdminType, 234 "AdminType": data.User.AdminType,
208 }) 235 })
209 - err = companyRespository.Edit(&oldCompany)  
210 - if err != nil {  
211 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
212 - }  
213 - err = userRespository.Edit(&oldUser) 236 + err = userRespository.Edit(&newUser)
214 if err != nil { 237 if err != nil {
215 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 238 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
216 } 239 }