更新。添加处理“接收企业平台同步用户数据时,没有找到可更新数据,就进行添加数据 ”
正在显示
5 个修改的文件
包含
91 行增加
和
3 行删除
@@ -199,13 +199,22 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error { | @@ -199,13 +199,22 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error { | ||
199 | oldCompany, err = companyRespository.FindOne(domain.CompanyFindOneOptions{ | 199 | oldCompany, err = companyRespository.FindOne(domain.CompanyFindOneOptions{ |
200 | Id: data.Company.Id, | 200 | Id: data.Company.Id, |
201 | }) | 201 | }) |
202 | + if err != nil { | ||
203 | + return fmt.Errorf("获取公司数据失败,%s", err) | ||
204 | + } | ||
202 | _, oldCompanyAdmins, err = userRespository.Find(domain.UsersFindQuery{ | 205 | _, oldCompanyAdmins, err = userRespository.Find(domain.UsersFindQuery{ |
203 | CompanyId: data.Company.Id, | 206 | CompanyId: data.Company.Id, |
204 | AdminType: domain.UserIsAdmin, | 207 | AdminType: domain.UserIsAdmin, |
205 | }) | 208 | }) |
209 | + if err != nil { | ||
210 | + return fmt.Errorf("获取公司admin数据失败,%s", err) | ||
211 | + } | ||
206 | newUser, err = userRespository.FindOne(domain.UsersFindOneQuery{ | 212 | newUser, err = userRespository.FindOne(domain.UsersFindOneQuery{ |
207 | Id: data.User.Id, | 213 | Id: data.User.Id, |
208 | }) | 214 | }) |
215 | + if err != nil { | ||
216 | + return fmt.Errorf("获取用户数据失败,%s,data.user.id=%d", err, data.User.Id) | ||
217 | + } | ||
209 | applets := []domain.CompanyApplets{} | 218 | applets := []domain.CompanyApplets{} |
210 | for _, v := range data.Company.Applets { | 219 | for _, v := range data.Company.Applets { |
211 | app := domain.CompanyApplets{ | 220 | app := domain.CompanyApplets{ |
@@ -227,13 +227,45 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro | @@ -227,13 +227,45 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro | ||
227 | } | 227 | } |
228 | for i := range datas { | 228 | for i := range datas { |
229 | data := datas[i] | 229 | data := datas[i] |
230 | - var oldUser domain.Users | ||
231 | - oldUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{ | 230 | + var ( |
231 | + oldUser *domain.Users | ||
232 | + userExist bool | ||
233 | + ) | ||
234 | + oldUser, userExist, err = usersRepository.FindOneWithExist(domain.UsersFindOneQuery{ | ||
232 | Id: data.Id, | 235 | Id: data.Id, |
233 | }) | 236 | }) |
234 | if err != nil { | 237 | if err != nil { |
235 | return lib.ThrowError(lib.BUSINESS_ERROR, err.Error()) | 238 | return lib.ThrowError(lib.BUSINESS_ERROR, err.Error()) |
236 | } | 239 | } |
240 | + if !userExist { | ||
241 | + //不存在可更新数据,执行添加数据的操作 | ||
242 | + newUser := domain.Users{ | ||
243 | + Id: data.Id, | ||
244 | + CompanyId: data.CompanyId, | ||
245 | + OpenId: data.OpenId, | ||
246 | + Name: data.Name, | ||
247 | + Sex: data.Sex, | ||
248 | + JobNum: data.JobNum, | ||
249 | + Phone: data.Phone, | ||
250 | + PrivatePhone: data.PrivatePhone, | ||
251 | + Email: data.Email, | ||
252 | + ExtensionNum: data.ExtensionNum, | ||
253 | + Workspace: data.WorkSpace, | ||
254 | + Status: data.Status, | ||
255 | + Avatar: data.Avatar, | ||
256 | + Remarks: data.Remarks, | ||
257 | + ChargeStatus: data.ChargeStatus, | ||
258 | + Permission: []domain.AdminPermissionBase{}, //初始化权限 | ||
259 | + AccessPartners: []domain.Partner{}, //默认初始化 | ||
260 | + AdminType: data.AdminType, | ||
261 | + IsSenior: 2, | ||
262 | + } | ||
263 | + newUser.EntryTime, _ = time.Parse("2006-01-02", data.EntryTime) | ||
264 | + if err = usersRepository.Add(&newUser); err != nil { | ||
265 | + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
266 | + } | ||
267 | + continue | ||
268 | + } else { | ||
237 | entryTime, _ := time.Parse("2006-01-02", data.EntryTime) | 269 | entryTime, _ := time.Parse("2006-01-02", data.EntryTime) |
238 | _ = oldUser.Update(map[string]interface{}{ | 270 | _ = oldUser.Update(map[string]interface{}{ |
239 | "CompanyId": data.CompanyId, | 271 | "CompanyId": data.CompanyId, |
@@ -252,10 +284,11 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro | @@ -252,10 +284,11 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro | ||
252 | "EntryTime": entryTime, | 284 | "EntryTime": entryTime, |
253 | "AdminType": data.AdminType, | 285 | "AdminType": data.AdminType, |
254 | }) | 286 | }) |
255 | - if err = usersRepository.Edit(&oldUser); err != nil { | 287 | + if err = usersRepository.Edit(oldUser); err != nil { |
256 | return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | 288 | return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) |
257 | } | 289 | } |
258 | } | 290 | } |
291 | + } | ||
259 | err = transactionContext.CommitTransaction() | 292 | err = transactionContext.CommitTransaction() |
260 | return err | 293 | return err |
261 | } | 294 | } |
@@ -172,5 +172,8 @@ type UsersRepository interface { | @@ -172,5 +172,8 @@ type UsersRepository interface { | ||
172 | Edit(*Users) error | 172 | Edit(*Users) error |
173 | Remove([]int64) error | 173 | Remove([]int64) error |
174 | FindOne(queryOptions UsersFindOneQuery) (Users, error) | 174 | FindOne(queryOptions UsersFindOneQuery) (Users, error) |
175 | + //FindOneWithExist 查询一个用户, | ||
176 | + //当数据不存在时不返回错误,替代的是返回bool,true表示数据存在,false表示数据不存在 | ||
177 | + FindOneWithExist(queryOptions UsersFindOneQuery) (*Users, bool, error) | ||
175 | Find(queryOptions UsersFindQuery) (int, []Users, error) | 178 | Find(queryOptions UsersFindQuery) (int, []Users, error) |
176 | } | 179 | } |
@@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
5 | "fmt" | 5 | "fmt" |
6 | "time" | 6 | "time" |
7 | 7 | ||
8 | + "github.com/go-pg/pg/v10" | ||
8 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | 9 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" |
9 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" | 10 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" |
10 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | 11 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" |
@@ -153,6 +154,47 @@ func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery | @@ -153,6 +154,47 @@ func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery | ||
153 | return reponsitory.transformPgModelToDomainModel(&m) | 154 | return reponsitory.transformPgModelToDomainModel(&m) |
154 | } | 155 | } |
155 | 156 | ||
157 | +//FindOneWithExist 查询一个用户, | ||
158 | +//当数据不存在时不返回错误,替代的是返回bool,true表示数据存在,false表示数据不存在 | ||
159 | +func (reponsitory UsersRepository) FindOneWithExist(queryOptions domain.UsersFindOneQuery) (*domain.Users, bool, error) { | ||
160 | + var ( | ||
161 | + err error | ||
162 | + tx = reponsitory.transactionContext.PgTx | ||
163 | + m models.Users | ||
164 | + hasCondition bool | ||
165 | + ) | ||
166 | + query := tx.Model(&m) | ||
167 | + query = query.Where("delete_at ISNULL") | ||
168 | + if queryOptions.Id > 0 { | ||
169 | + hasCondition = true | ||
170 | + query = query.Where("id=?", queryOptions.Id) | ||
171 | + } | ||
172 | + if len(queryOptions.Phone) > 0 { | ||
173 | + hasCondition = true | ||
174 | + query = query.Where("phone=?", queryOptions.Phone) | ||
175 | + } | ||
176 | + if queryOptions.CompanyId > 0 { | ||
177 | + hasCondition = true | ||
178 | + query = query.Where("company_id=?", queryOptions.CompanyId) | ||
179 | + } | ||
180 | + if queryOptions.OpenId > 0 { | ||
181 | + hasCondition = true | ||
182 | + query = query.Where("open_id=?", queryOptions.OpenId) | ||
183 | + } | ||
184 | + if !hasCondition { | ||
185 | + return nil, false, errors.New("UsersRepository.FindOne 必须要有查询条件") | ||
186 | + } | ||
187 | + err = query.First() | ||
188 | + if err != nil && err == pg.ErrNoRows { | ||
189 | + return nil, false, nil | ||
190 | + } | ||
191 | + if err != nil { | ||
192 | + return nil, false, err | ||
193 | + } | ||
194 | + searchResult, err := reponsitory.transformPgModelToDomainModel(&m) | ||
195 | + return &searchResult, true, err | ||
196 | +} | ||
197 | + | ||
156 | func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) { | 198 | func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) { |
157 | db := reponsitory.transactionContext.PgTx | 199 | db := reponsitory.transactionContext.PgTx |
158 | usersModels := []models.Users{} | 200 | usersModels := []models.Users{} |
-
请 注册 或 登录 后发表评论