作者 陈志颖

合并分支 'dev' 到 'test'

Dev



查看合并请求 !30
@@ -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{
@@ -265,13 +265,45 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro @@ -265,13 +265,45 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro
265 } 265 }
266 for i := range datas { 266 for i := range datas {
267 data := datas[i] 267 data := datas[i]
268 - var oldUser domain.Users  
269 - oldUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{ 268 + var (
  269 + oldUser *domain.Users
  270 + userExist bool
  271 + )
  272 + oldUser, userExist, err = usersRepository.FindOneWithExist(domain.UsersFindOneQuery{
270 Id: data.Id, 273 Id: data.Id,
271 }) 274 })
272 if err != nil { 275 if err != nil {
273 return lib.ThrowError(lib.BUSINESS_ERROR, err.Error()) 276 return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
274 } 277 }
  278 + if !userExist {
  279 + //不存在可更新数据,执行添加数据的操作
  280 + newUser := domain.Users{
  281 + Id: data.Id,
  282 + CompanyId: data.CompanyId,
  283 + OpenId: data.OpenId,
  284 + Name: data.Name,
  285 + Sex: data.Sex,
  286 + JobNum: data.JobNum,
  287 + Phone: data.Phone,
  288 + PrivatePhone: data.PrivatePhone,
  289 + Email: data.Email,
  290 + ExtensionNum: data.ExtensionNum,
  291 + Workspace: data.WorkSpace,
  292 + Status: data.Status,
  293 + Avatar: data.Avatar,
  294 + Remarks: data.Remarks,
  295 + ChargeStatus: data.ChargeStatus,
  296 + Permission: []domain.AdminPermissionBase{}, //初始化权限
  297 + AccessPartners: []domain.Partner{}, //默认初始化
  298 + AdminType: data.AdminType,
  299 + IsSenior: 2,
  300 + }
  301 + newUser.EntryTime, _ = time.Parse("2006-01-02", data.EntryTime)
  302 + if err = usersRepository.Add(&newUser); err != nil {
  303 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  304 + }
  305 + continue
  306 + } else {
275 entryTime, _ := time.Parse("2006-01-02", data.EntryTime) 307 entryTime, _ := time.Parse("2006-01-02", data.EntryTime)
276 _ = oldUser.Update(map[string]interface{}{ 308 _ = oldUser.Update(map[string]interface{}{
277 "CompanyId": data.CompanyId, 309 "CompanyId": data.CompanyId,
@@ -290,10 +322,11 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro @@ -290,10 +322,11 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro
290 "EntryTime": entryTime, 322 "EntryTime": entryTime,
291 "AdminType": data.AdminType, 323 "AdminType": data.AdminType,
292 }) 324 })
293 - if err = usersRepository.Edit(&oldUser); err != nil { 325 + if err = usersRepository.Edit(oldUser); err != nil {
294 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 326 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
295 } 327 }
296 } 328 }
  329 + }
297 err = transactionContext.CommitTransaction() 330 err = transactionContext.CommitTransaction()
298 return err 331 return err
299 } 332 }
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 6
7 //PlatformAction 企业平台数据同步服务 动作接口设定 7 //PlatformAction 企业平台数据同步服务 动作接口设定
8 type SyncAction interface { 8 type SyncAction interface {
  9 + //DoAction(执行的动作,数据)
9 DoAction(string, []byte) error 10 DoAction(string, []byte) error
10 } 11 }
11 12
@@ -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 }
@@ -124,7 +124,7 @@ func (repository *ColumnSettingRepository) Find(queryOptions map[string]interfac @@ -124,7 +124,7 @@ func (repository *ColumnSettingRepository) Find(queryOptions map[string]interfac
124 columnSettings := make([]*domain.ColumnSetting, 0) 124 columnSettings := make([]*domain.ColumnSetting, 0)
125 query := tx.Model(&columnSettingModels) 125 query := tx.Model(&columnSettingModels)
126 if ids, ok := queryOptions["ids"]; ok { 126 if ids, ok := queryOptions["ids"]; ok {
127 - query = query.Where("column_setting.uid IN (?)", pg.In(ids.(int64))) 127 + query = query.Where("column_setting.uid IN (?)", pg.In(ids.([]int64)))
128 } 128 }
129 if uid, ok := queryOptions["uid"]; ok { 129 if uid, ok := queryOptions["uid"]; ok {
130 query = query.Where("column_setting.uid = ?", uid) 130 query = query.Where("column_setting.uid = ?", uid)
@@ -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{}