作者 yangfu

feat:物料同步

... ... @@ -681,3 +681,78 @@ func (srv *PullDataK3CloudService) SyncDataMaterialGroup(ptr *pgTransaction.Tran
}
return nil
}
// SyncDataMaterial 同步物料数据
func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.TransactionContext, fromTime time.Time) error {
/*
1.获取更新时间
2.获取prd_mo_k3cloud从更新时间开始的有变化的数据
3.查询是否有重复的批次号
4.有进行更新,其他的插入
*/
var (
lastTime = utils.GetZeroTime(time.Now()).Add(-time.Hour * 24) //前一天有修改的记录
org *domain.Org
limit = 500
offset = 0
pageIndex = 0
err error
)
if !fromTime.IsZero() {
lastTime = fromTime
}
var userService = domainService.NewUserService()
org, err = userService.Organization(constant.MANUFACTURE_DEFAULT_ORGID)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var (
cid = constant.MANUFACTURE_DEFAULT_COMPANYID
productMaterialRepository, _, _ = factory.FastProductMaterial(ptr, 0)
productMaterialGroupRepository, _, _ = factory.FastProductMaterialGroup(ptr, 0)
prdMoK3cloudDao, _ = dao.NewPrdMoK3cloudDao(ptr)
materialGroups domain.ProductMaterialGroups
)
_, materialGroups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": cid})
if err != nil {
return err
}
mapMaterialGroup := materialGroups.ToMapByGroupNumber()
for {
offset += pageIndex * limit
pageIndex += 1
records, _, err := prdMoK3cloudDao.GetLatestMaterialData(lastTime, offset, limit)
if err != nil {
return err
}
for _, v := range records {
var (
material *domain.ProductMaterial
materialGroup *domain.ProductMaterialGroup
ok bool
)
if materialGroup, ok = mapMaterialGroup[v.MaterialNumber]; !ok {
continue
}
material, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": cid, "materialNumber": v.MaterialNumber})
if err == nil && material != nil {
continue
}
if err == domain.ErrorNotFound {
material = &domain.ProductMaterial{
ProductMaterialGroupId: materialGroup.ProductMaterialGroupId,
Ext: domain.NewExt(org.OrgName),
}
}
}
if len(records) == 0 || len(records) < limit {
break
}
}
return nil
}
... ...
... ... @@ -170,3 +170,18 @@ func (d *PrdMoK3cloudDao) GetLatestData(fromTime time.Time) ([]*models.PrdMoK3cl
}
return result, nil
}
// GetLatestMaterialData 增量获取物料数据
func (d *PrdMoK3cloudDao) GetLatestMaterialData(fromTime time.Time, offset, limit int) ([]*models.ProductMaterial, int, error) {
m := new(models.ProductMaterial)
result := make([]*models.ProductMaterial, 0)
query := d.transactionContext.PgTx.Model(m)
if !fromTime.IsZero() {
query.Where("modify_date>=?", fromTime)
}
err := query.Select(&result)
if err != nil {
return nil, 0, err
}
return result, len(result), nil
}
... ...