|
|
@@ -0,0 +1,366 @@
|
|
|
+package com.sckw.contract.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.sckw.contract.model.KwcContractProxy;
|
|
|
+import com.sckw.contract.model.KwcContractProxyGoods;
|
|
|
+import com.sckw.contract.model.KwcContractProxyUnit;
|
|
|
+import com.sckw.contract.model.enums.ProxyStatusEnum;
|
|
|
+import com.sckw.contract.model.vo.req.ProxyContractAddReq;
|
|
|
+import com.sckw.contract.model.vo.req.ProxyContractAuditReq;
|
|
|
+import com.sckw.contract.model.vo.req.ProxyContractFinishReq;
|
|
|
+import com.sckw.contract.model.vo.req.ProxyContractPageReq;
|
|
|
+import com.sckw.contract.model.vo.res.ProxyContractDetailResp;
|
|
|
+import com.sckw.contract.model.vo.res.ProxyContractListResp;
|
|
|
+import com.sckw.contract.repository.KwcContractProxyGoodsRepository;
|
|
|
+import com.sckw.contract.repository.KwcContractProxyRepository;
|
|
|
+import com.sckw.contract.repository.KwcContractProxyUnitRepository;
|
|
|
+import com.sckw.contract.service.KwcContractProxyService;
|
|
|
+import com.sckw.core.exception.BusinessException;
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
+import com.sckw.core.web.response.result.PageDataResult;
|
|
|
+import com.sckw.system.api.RemoteSystemService;
|
|
|
+import com.sckw.system.api.model.dto.res.EntCacheResDto;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author xucaiqin
|
|
|
+ * @date 2026-06-02 09:00:09
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class KwcContractProxyServiceImpl implements KwcContractProxyService {
|
|
|
+ private final KwcContractProxyRepository proxyRepository;
|
|
|
+ private final KwcContractProxyUnitRepository proxyUnitRepository;
|
|
|
+ private final KwcContractProxyGoodsRepository proxyGoodsRepository;
|
|
|
+ @DubboReference(version = "1.0.0", group = "design", check = false)
|
|
|
+ private RemoteSystemService remoteSystemService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageDataResult<ProxyContractListResp> queryListByPage(ProxyContractPageReq req) {
|
|
|
+ IPage<KwcContractProxy> page = proxyRepository.queryByPage(req.getPageNum(), req.getPageSize(), req.getContractNo(), req.getContractName(), req.getSupplyId(), req.getProxyId(), req.getStatus());
|
|
|
+
|
|
|
+ List<KwcContractProxy> records = page.getRecords();
|
|
|
+ if (CollectionUtils.isEmpty(records)) {
|
|
|
+ return PageDataResult.empty(req.getPageNum(), req.getPageSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<KwcContractProxyUnit> allUnits = proxyUnitRepository.queryByContractIds(records.stream().map(KwcContractProxy::getId).collect(Collectors.toSet()));
|
|
|
+
|
|
|
+ Map<Long, KwcContractProxyUnit> supplyUnitMap = allUnits.stream().filter(u -> Objects.equals(u.getUnitType(), 1)).collect(Collectors.toMap(KwcContractProxyUnit::getContractId, Function.identity(), (a, b) -> a));
|
|
|
+
|
|
|
+ Map<Long, KwcContractProxyUnit> proxyUnitMap = allUnits.stream().filter(u -> Objects.equals(u.getUnitType(), 2)).collect(Collectors.toMap(KwcContractProxyUnit::getContractId, Function.identity(), (a, b) -> a));
|
|
|
+
|
|
|
+ List<ProxyContractListResp> list = records.stream().map(r -> {
|
|
|
+ ProxyContractListResp resp = new ProxyContractListResp();
|
|
|
+ resp.setId(r.getId());
|
|
|
+ resp.setContractNo(r.getContractNo());
|
|
|
+ resp.setName(r.getName());
|
|
|
+ resp.setSupplyId(r.getSupplyId());
|
|
|
+ resp.setProxyId(r.getProxyId());
|
|
|
+ resp.setSignTime(r.getSignTime());
|
|
|
+ resp.setStartTime(r.getStartTime());
|
|
|
+ resp.setEndTime(r.getEndTime());
|
|
|
+ resp.setAmount(r.getAmount());
|
|
|
+ resp.setPerformedAmount(r.getPerformedAmount());
|
|
|
+ resp.setStatus(r.getStatus());
|
|
|
+ resp.setStatusDesc(ProxyStatusEnum.getLabel(r.getStatus()));
|
|
|
+ resp.setCreateBy(r.getCreateBy());
|
|
|
+ resp.setCreateTime(r.getCreateTime());
|
|
|
+
|
|
|
+ KwcContractProxyUnit supplyUnit = supplyUnitMap.get(r.getId());
|
|
|
+ if (supplyUnit != null) {
|
|
|
+ resp.setSupplyName(supplyUnit.getFirmName());
|
|
|
+ }
|
|
|
+ KwcContractProxyUnit proxyUnit = proxyUnitMap.get(r.getId());
|
|
|
+ if (proxyUnit != null) {
|
|
|
+ resp.setProxyName(proxyUnit.getFirmName());
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ return PageDataResult.of(page, list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProxyContractDetailResp queryDetail(Long id) {
|
|
|
+ KwcContractProxy proxy = proxyRepository.queryById(id);
|
|
|
+ if (proxy == null) {
|
|
|
+ throw new BusinessException("代理合同不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ ProxyContractDetailResp resp = new ProxyContractDetailResp();
|
|
|
+ resp.setId(proxy.getId());
|
|
|
+ resp.setContractNo(proxy.getContractNo());
|
|
|
+ resp.setName(proxy.getName());
|
|
|
+ resp.setSupplyId(proxy.getSupplyId());
|
|
|
+ resp.setProxyId(proxy.getProxyId());
|
|
|
+ resp.setSignTime(proxy.getSignTime());
|
|
|
+ resp.setStartTime(proxy.getStartTime());
|
|
|
+ resp.setEndTime(proxy.getEndTime());
|
|
|
+ resp.setAmount(proxy.getAmount());
|
|
|
+ resp.setPerformedAmount(proxy.getPerformedAmount());
|
|
|
+ resp.setSignFile(proxy.getSignFile());
|
|
|
+ resp.setRemark(proxy.getRemark());
|
|
|
+ resp.setStatus(proxy.getStatus());
|
|
|
+ resp.setCreateBy(proxy.getCreateBy());
|
|
|
+ resp.setCreateTime(proxy.getCreateTime());
|
|
|
+ resp.setUpdateBy(proxy.getUpdateBy());
|
|
|
+ resp.setUpdateTime(proxy.getUpdateTime());
|
|
|
+
|
|
|
+ List<KwcContractProxyGoods> goodsList = proxyGoodsRepository.queryByContractId(id);
|
|
|
+ if (CollectionUtils.isNotEmpty(goodsList)) {
|
|
|
+ resp.setGoodsInfo(goodsList.stream().map(g -> {
|
|
|
+ ProxyContractDetailResp.ProxyGoodsDetail detail = new ProxyContractDetailResp.ProxyGoodsDetail();
|
|
|
+ detail.setGoodsId(g.getGoodsId());
|
|
|
+ detail.setGoodsName(g.getGoodsName());
|
|
|
+ detail.setSkuId(g.getSkuId());
|
|
|
+ detail.setUnit(g.getUnit());
|
|
|
+ detail.setAmount(g.getAmount());
|
|
|
+ detail.setPrice(g.getPrice());
|
|
|
+ detail.setRemark(g.getRemark());
|
|
|
+ return detail;
|
|
|
+ }).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ List<KwcContractProxyUnit> unitList = proxyUnitRepository.queryByContractId(id);
|
|
|
+ if (CollectionUtils.isNotEmpty(unitList)) {
|
|
|
+ resp.setUnitInfo(unitList.stream().map(u -> {
|
|
|
+ ProxyContractDetailResp.ProxyUnitDetail detail = new ProxyContractDetailResp.ProxyUnitDetail();
|
|
|
+ detail.setUnitType(u.getUnitType());
|
|
|
+ detail.setEntId(u.getEntId());
|
|
|
+ detail.setFirmName(u.getFirmName());
|
|
|
+ detail.setContactsId(u.getContactsId());
|
|
|
+ detail.setContacts(u.getContacts());
|
|
|
+ detail.setPhone(u.getPhone());
|
|
|
+ detail.setSignPhone(u.getSignPhone());
|
|
|
+ detail.setRemark(u.getRemark());
|
|
|
+ return detail;
|
|
|
+ }).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Long add(ProxyContractAddReq req) {
|
|
|
+ if (req.getId() != null) {
|
|
|
+ throw new BusinessException("新增时合同ID必须为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ KwcContractProxy existProxy = proxyRepository.queryByContractNo(req.getContractNo());
|
|
|
+ if (existProxy != null) {
|
|
|
+ throw new BusinessException("合同编号已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ return saveOrUpdate(req, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Long update(ProxyContractAddReq req) {
|
|
|
+ if (req.getId() == null) {
|
|
|
+ throw new BusinessException("修改时合同ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ KwcContractProxy proxy = proxyRepository.queryById(req.getId());
|
|
|
+ if (proxy == null) {
|
|
|
+ throw new BusinessException("代理合同不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ KwcContractProxy existProxy = proxyRepository.queryByContractNo(req.getContractNo());
|
|
|
+ if (existProxy != null && !Objects.equals(existProxy.getId(), req.getId())) {
|
|
|
+ throw new BusinessException("合同编号已存在");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(proxy.getStatus(), ProxyStatusEnum.ISSUE.getValue())) {
|
|
|
+ throw new BusinessException("非待签约合同不允许修改");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return saveOrUpdate(req, proxy);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Boolean audit(ProxyContractAuditReq req) {
|
|
|
+ KwcContractProxy proxy = proxyRepository.queryById(req.getId());
|
|
|
+ if (proxy == null) {
|
|
|
+ throw new BusinessException("代理合同不存在");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(proxy.getStatus(), ProxyStatusEnum.ISSUE.getValue())) {
|
|
|
+ throw new BusinessException("只有待签约状态才能审核");
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer auditStatus = req.getAuditStatus();
|
|
|
+ if (!Objects.equals(auditStatus, 1) && !Objects.equals(auditStatus, 2)) {
|
|
|
+ throw new BusinessException("审核状态不合法");
|
|
|
+ }
|
|
|
+
|
|
|
+ proxy.setStatus(Objects.equals(auditStatus, 1) ? ProxyStatusEnum.SIGN.getValue() : ProxyStatusEnum.DROW.getValue());
|
|
|
+ proxy.setSignTime(Objects.equals(auditStatus, 1) ? LocalDateTime.now() : null);
|
|
|
+ proxy.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
+ proxy.setUpdateTime(LocalDateTime.now());
|
|
|
+ proxyRepository.saveOrUpdateProxy(proxy);
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long saveOrUpdate(ProxyContractAddReq req, KwcContractProxy existProxy) {
|
|
|
+ boolean isUpdate = existProxy != null;
|
|
|
+ KwcContractProxy proxy = isUpdate ? existProxy : new KwcContractProxy();
|
|
|
+
|
|
|
+ proxy.setContractNo(req.getContractNo());
|
|
|
+ proxy.setName(req.getName());
|
|
|
+ proxy.setSupplyId(req.getSupplyId());
|
|
|
+ proxy.setProxyId(req.getProxyId());
|
|
|
+ proxy.setSignTime(req.getSignTime());
|
|
|
+ proxy.setStartTime(req.getStartTime());
|
|
|
+ proxy.setEndTime(req.getEndTime());
|
|
|
+ BigDecimal totalAmount = BigDecimal.ZERO;
|
|
|
+ if (CollectionUtils.isNotEmpty(req.getGoodsInfo())) {
|
|
|
+ for (ProxyContractAddReq.ProxyGoodsInfo goodsInfo : req.getGoodsInfo()) {
|
|
|
+ if (goodsInfo != null && goodsInfo.getAmount() != null) {
|
|
|
+ totalAmount = totalAmount.add(goodsInfo.getAmount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ proxy.setAmount(totalAmount);
|
|
|
+ proxy.setSignFile(req.getSignFile());
|
|
|
+ proxy.setRemark(req.getRemark());
|
|
|
+ if (!isUpdate) {
|
|
|
+ proxy.setStatus(ProxyStatusEnum.ISSUE.getValue());
|
|
|
+ }
|
|
|
+ proxy.setEntId(LoginUserHolder.getEntId());
|
|
|
+
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ if (isUpdate) {
|
|
|
+ proxy.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
+ proxy.setUpdateTime(now);
|
|
|
+ } else {
|
|
|
+ proxy.setCreateBy(LoginUserHolder.getUserId());
|
|
|
+ proxy.setCreateTime(now);
|
|
|
+ proxy.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
+ proxy.setUpdateTime(now);
|
|
|
+ proxy.setDelFlag(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ proxyRepository.saveOrUpdateProxy(proxy);
|
|
|
+ Long contractId = proxy.getId();
|
|
|
+
|
|
|
+ proxyGoodsRepository.deleteByContractId(contractId);
|
|
|
+ if (CollectionUtils.isNotEmpty(req.getGoodsInfo())) {
|
|
|
+ List<KwcContractProxyGoods> goodsList = new ArrayList<>();
|
|
|
+ for (ProxyContractAddReq.ProxyGoodsInfo goodsInfo : req.getGoodsInfo()) {
|
|
|
+ KwcContractProxyGoods goods = new KwcContractProxyGoods();
|
|
|
+ goods.setContractId(contractId);
|
|
|
+ goods.setGoodsId(goodsInfo.getGoodsId());
|
|
|
+ goods.setGoodsName(goodsInfo.getGoodsName());
|
|
|
+ goods.setSkuId(goodsInfo.getSkuId());
|
|
|
+ goods.setUnit(goodsInfo.getUnit());
|
|
|
+ goods.setAmount(goodsInfo.getAmount());
|
|
|
+ goods.setPrice(goodsInfo.getPrice());
|
|
|
+ goods.setRemark(goodsInfo.getRemark());
|
|
|
+ goods.setStatus(0);
|
|
|
+ goods.setCreateBy(LoginUserHolder.getUserId());
|
|
|
+ goods.setCreateTime(now);
|
|
|
+ goods.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
+ goods.setUpdateTime(now);
|
|
|
+ goods.setDelFlag(0);
|
|
|
+ goodsList.add(goods);
|
|
|
+ }
|
|
|
+ proxyGoodsRepository.saveGoods(goodsList);
|
|
|
+ }
|
|
|
+
|
|
|
+ proxyUnitRepository.removeByContractId(contractId);
|
|
|
+ Long supplyId = req.getSupplyId();
|
|
|
+ EntCacheResDto entCacheResDto = remoteSystemService.queryEntCacheById(supplyId);
|
|
|
+ Long proxyId = req.getProxyId();
|
|
|
+ EntCacheResDto entCacheResDto1 = remoteSystemService.queryEntCacheById(proxyId);
|
|
|
+
|
|
|
+ if (entCacheResDto == null) {
|
|
|
+ throw new BusinessException("供应单位信息不存在");
|
|
|
+ }
|
|
|
+ if (entCacheResDto1 == null) {
|
|
|
+ throw new BusinessException("代理单位信息不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<KwcContractProxyUnit> unitList = new ArrayList<>();
|
|
|
+
|
|
|
+ KwcContractProxyUnit supplyUnit = new KwcContractProxyUnit();
|
|
|
+ supplyUnit.setContractId(contractId);
|
|
|
+ supplyUnit.setUnitType(1);
|
|
|
+ supplyUnit.setEntId(supplyId);
|
|
|
+ supplyUnit.setFirmName(entCacheResDto.getFirmName());
|
|
|
+ supplyUnit.setContactsId(entCacheResDto.getContactsId());
|
|
|
+ supplyUnit.setContacts(entCacheResDto.getContacts());
|
|
|
+ supplyUnit.setPhone(entCacheResDto.getPhone());
|
|
|
+ supplyUnit.setSignPhone(entCacheResDto.getPhone());
|
|
|
+ supplyUnit.setStatus(0);
|
|
|
+ supplyUnit.setCreateBy(LoginUserHolder.getUserId());
|
|
|
+ supplyUnit.setCreateTime(now);
|
|
|
+ supplyUnit.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
+ supplyUnit.setUpdateTime(now);
|
|
|
+ supplyUnit.setDelFlag(0);
|
|
|
+ unitList.add(supplyUnit);
|
|
|
+
|
|
|
+ KwcContractProxyUnit proxyUnit = new KwcContractProxyUnit();
|
|
|
+ proxyUnit.setContractId(contractId);
|
|
|
+ proxyUnit.setUnitType(2);
|
|
|
+ proxyUnit.setEntId(proxyId);
|
|
|
+ proxyUnit.setFirmName(entCacheResDto1.getFirmName());
|
|
|
+ proxyUnit.setContactsId(entCacheResDto1.getContactsId());
|
|
|
+ proxyUnit.setContacts(entCacheResDto1.getContacts());
|
|
|
+ proxyUnit.setPhone(entCacheResDto1.getPhone());
|
|
|
+ proxyUnit.setSignPhone(entCacheResDto1.getPhone());
|
|
|
+ proxyUnit.setStatus(0);
|
|
|
+ proxyUnit.setCreateBy(LoginUserHolder.getUserId());
|
|
|
+ proxyUnit.setCreateTime(now);
|
|
|
+ proxyUnit.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
+ proxyUnit.setUpdateTime(now);
|
|
|
+ proxyUnit.setDelFlag(0);
|
|
|
+ unitList.add(proxyUnit);
|
|
|
+
|
|
|
+ proxyUnitRepository.saveUnits(unitList);
|
|
|
+
|
|
|
+ return contractId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean delete(Long id) {
|
|
|
+ KwcContractProxy proxy = proxyRepository.queryById(id);
|
|
|
+ if (proxy == null) {
|
|
|
+ throw new BusinessException("代理合同不存在");
|
|
|
+ }
|
|
|
+ proxyGoodsRepository.removeByContractId(id);
|
|
|
+ proxyUnitRepository.deleteByContractId(id);
|
|
|
+ return proxyRepository.removeById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean finish(ProxyContractFinishReq req) {
|
|
|
+ KwcContractProxy proxy = proxyRepository.queryById(req.getId());
|
|
|
+ if (proxy == null) {
|
|
|
+ throw new BusinessException("代理合同不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ proxy.setStatus(ProxyStatusEnum.SUCCESS.getValue());
|
|
|
+ proxy.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
+ proxy.setUpdateTime(LocalDateTime.now());
|
|
|
+ proxyRepository.updateById(proxy);
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|