作者 yangfu

feat:物料同步

@@ -681,3 +681,78 @@ func (srv *PullDataK3CloudService) SyncDataMaterialGroup(ptr *pgTransaction.Tran @@ -681,3 +681,78 @@ func (srv *PullDataK3CloudService) SyncDataMaterialGroup(ptr *pgTransaction.Tran
681 } 681 }
682 return nil 682 return nil
683 } 683 }
  684 +
  685 +// SyncDataMaterial 同步物料数据
  686 +func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.TransactionContext, fromTime time.Time) error {
  687 + /*
  688 + 1.获取更新时间
  689 + 2.获取prd_mo_k3cloud从更新时间开始的有变化的数据
  690 + 3.查询是否有重复的批次号
  691 + 4.有进行更新,其他的插入
  692 + */
  693 + var (
  694 + lastTime = utils.GetZeroTime(time.Now()).Add(-time.Hour * 24) //前一天有修改的记录
  695 + org *domain.Org
  696 + limit = 500
  697 + offset = 0
  698 + pageIndex = 0
  699 + err error
  700 + )
  701 + if !fromTime.IsZero() {
  702 + lastTime = fromTime
  703 + }
  704 +
  705 + var userService = domainService.NewUserService()
  706 + org, err = userService.Organization(constant.MANUFACTURE_DEFAULT_ORGID)
  707 + if err != nil {
  708 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  709 + }
  710 +
  711 + var (
  712 + cid = constant.MANUFACTURE_DEFAULT_COMPANYID
  713 + productMaterialRepository, _, _ = factory.FastProductMaterial(ptr, 0)
  714 + productMaterialGroupRepository, _, _ = factory.FastProductMaterialGroup(ptr, 0)
  715 + prdMoK3cloudDao, _ = dao.NewPrdMoK3cloudDao(ptr)
  716 + materialGroups domain.ProductMaterialGroups
  717 + )
  718 +
  719 + _, materialGroups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": cid})
  720 + if err != nil {
  721 + return err
  722 + }
  723 + mapMaterialGroup := materialGroups.ToMapByGroupNumber()
  724 + for {
  725 + offset += pageIndex * limit
  726 + pageIndex += 1
  727 +
  728 + records, _, err := prdMoK3cloudDao.GetLatestMaterialData(lastTime, offset, limit)
  729 + if err != nil {
  730 + return err
  731 + }
  732 +
  733 + for _, v := range records {
  734 + var (
  735 + material *domain.ProductMaterial
  736 + materialGroup *domain.ProductMaterialGroup
  737 + ok bool
  738 + )
  739 + if materialGroup, ok = mapMaterialGroup[v.MaterialNumber]; !ok {
  740 + continue
  741 + }
  742 + material, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": cid, "materialNumber": v.MaterialNumber})
  743 + if err == nil && material != nil {
  744 + continue
  745 + }
  746 + if err == domain.ErrorNotFound {
  747 + material = &domain.ProductMaterial{
  748 + ProductMaterialGroupId: materialGroup.ProductMaterialGroupId,
  749 + Ext: domain.NewExt(org.OrgName),
  750 + }
  751 + }
  752 + }
  753 + if len(records) == 0 || len(records) < limit {
  754 + break
  755 + }
  756 + }
  757 + return nil
  758 +}
@@ -170,3 +170,18 @@ func (d *PrdMoK3cloudDao) GetLatestData(fromTime time.Time) ([]*models.PrdMoK3cl @@ -170,3 +170,18 @@ func (d *PrdMoK3cloudDao) GetLatestData(fromTime time.Time) ([]*models.PrdMoK3cl
170 } 170 }
171 return result, nil 171 return result, nil
172 } 172 }
  173 +
  174 +// GetLatestMaterialData 增量获取物料数据
  175 +func (d *PrdMoK3cloudDao) GetLatestMaterialData(fromTime time.Time, offset, limit int) ([]*models.ProductMaterial, int, error) {
  176 + m := new(models.ProductMaterial)
  177 + result := make([]*models.ProductMaterial, 0)
  178 + query := d.transactionContext.PgTx.Model(m)
  179 + if !fromTime.IsZero() {
  180 + query.Where("modify_date>=?", fromTime)
  181 + }
  182 + err := query.Select(&result)
  183 + if err != nil {
  184 + return nil, 0, err
  185 + }
  186 + return result, len(result), nil
  187 +}