|
|
|
|
@ -1,14 +1,27 @@ |
|
|
|
|
package org.springblade.desk.basic.service.impl; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import jakarta.annotation.Resource; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.springblade.core.mp.base.BaseServiceImpl; |
|
|
|
|
import org.springblade.core.tool.api.R; |
|
|
|
|
import org.springblade.desk.basic.mapper.OemCustomerMapper; |
|
|
|
|
import org.springblade.desk.basic.pojo.entity.OemCustomer; |
|
|
|
|
import org.springblade.desk.basic.service.IOemCustomerService; |
|
|
|
|
import org.springblade.erpdata.feign.IErpMesRbSplyClient; |
|
|
|
|
import org.springblade.erpdata.pojo.dto.view.MesRbSply; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.function.Function; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* [外协厂家-客户] 服务实现类 |
|
|
|
|
* |
|
|
|
|
@ -19,6 +32,8 @@ import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
@Service |
|
|
|
|
public class OemCustomerServiceImpl extends BaseServiceImpl<OemCustomerMapper, OemCustomer> implements IOemCustomerService { |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
IErpMesRbSplyClient erpMesRbSplyClient; |
|
|
|
|
/** |
|
|
|
|
* 从ERP系统同步供应商/客户数据到本地数据库 |
|
|
|
|
*/ |
|
|
|
|
@ -27,9 +42,81 @@ public class OemCustomerServiceImpl extends BaseServiceImpl<OemCustomerMapper, O |
|
|
|
|
public R syncCustomerFromErp() { |
|
|
|
|
log.info("开始同步外协厂家管理数据..."); |
|
|
|
|
|
|
|
|
|
baseMapper.syncCustomerFromErp(); |
|
|
|
|
try { |
|
|
|
|
List<MesRbSply> splyList = erpMesRbSplyClient.getSplyList(); |
|
|
|
|
if (CollUtil.isEmpty(splyList)) { |
|
|
|
|
return R.fail("没有获取到供应商数据"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.info("从ERP获取到 {} 条供应商数据", splyList.size()); |
|
|
|
|
|
|
|
|
|
List<String> ocCodes = splyList.stream() |
|
|
|
|
.map(MesRbSply::getSplycode) |
|
|
|
|
.filter(code -> code != null && !code.isEmpty()) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
LambdaQueryWrapper<OemCustomer> queryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
queryWrapper.in(OemCustomer::getOcCode, ocCodes); |
|
|
|
|
Map<String, OemCustomer> existingCustomerMap = list(Wrappers.lambdaQuery(OemCustomer.class) |
|
|
|
|
.in(OemCustomer::getOcCode, ocCodes)) |
|
|
|
|
.stream() |
|
|
|
|
.collect(Collectors.toMap(OemCustomer::getOcCode, Function.identity())); |
|
|
|
|
|
|
|
|
|
// 新增和更新
|
|
|
|
|
Date now = new Date(); |
|
|
|
|
List<OemCustomer> toInsert = splyList.stream() |
|
|
|
|
.filter(sply -> !existingCustomerMap.containsKey(sply.getSplycode())) |
|
|
|
|
.map(sply -> convertToCustomer(sply, null, now)) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
List<OemCustomer> toUpdate = splyList.stream() |
|
|
|
|
.filter(sply -> existingCustomerMap.containsKey(sply.getSplycode())) |
|
|
|
|
.map(sply -> convertToCustomer(sply, existingCustomerMap.get(sply.getSplycode()), now)) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
if (!toInsert.isEmpty()) { |
|
|
|
|
saveBatch(toInsert); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!toUpdate.isEmpty()) { |
|
|
|
|
updateBatchById(toUpdate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.info("外协厂家管理数据同步完成 - 新增: {}, 更新: {}", toInsert.size(), toUpdate.size()); |
|
|
|
|
return R.success(String.format("同步成功 - 新增: %d, 更新: %d", toInsert.size(), toUpdate.size())); |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("同步外协厂家管理数据失败", e); |
|
|
|
|
return R.fail("同步失败: " + e.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private OemCustomer convertToCustomer(MesRbSply sply, OemCustomer existing, Date updateTime) { |
|
|
|
|
OemCustomer customer = BeanUtil.isNotEmpty(existing) ? existing : new OemCustomer(); |
|
|
|
|
|
|
|
|
|
customer.setOcCode(sply.getSplycode()); |
|
|
|
|
customer.setOcName(sply.getSplyname()); |
|
|
|
|
customer.setAbbreviation(sply.getSplyname()); |
|
|
|
|
customer.setRegion(sply.getProvince()); |
|
|
|
|
customer.setAddress(sply.getSplyadd()); |
|
|
|
|
customer.setQualification(sply.getProducttype()); |
|
|
|
|
customer.setContactMan(sply.getLinkman()); |
|
|
|
|
customer.setEmail(sply.getEmail()); |
|
|
|
|
customer.setContactPhone(sply.getCtactqumd()); |
|
|
|
|
customer.setUpdateTime(updateTime); |
|
|
|
|
|
|
|
|
|
// 存储过程在INSERT时会设置cur_status,但UPDATE时不更新cur_status ???
|
|
|
|
|
// if (sply.getSplystat() != null) {
|
|
|
|
|
// customer.setCurStatus(sply.getSplystat());
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (existing == null) { |
|
|
|
|
customer.setIsDeleted(0); |
|
|
|
|
if (sply.getSplystat() != null) { |
|
|
|
|
customer.setCurStatus(sply.getSplystat()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.info("外协厂家管理数据同步完成"); |
|
|
|
|
return R.success("同步成功"); |
|
|
|
|
return customer; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|