|
|
@@ -0,0 +1,285 @@
|
|
|
+package com.ydtech.modules.fnc.service.contract.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ydtech.exception.SystemException;
|
|
|
+import com.ydtech.modules.base.controller.BaseController;
|
|
|
+import com.ydtech.modules.base.model.vo.PageVo;
|
|
|
+import com.ydtech.modules.fnc.dao.contract.ContractMapper;
|
|
|
+import com.ydtech.modules.fnc.dao.contract.ContractSupplierMapper;
|
|
|
+import com.ydtech.modules.fnc.dto.contract.ContractDto;
|
|
|
+import com.ydtech.modules.fnc.dto.contract.ContractSupplierDto;
|
|
|
+import com.ydtech.modules.fnc.entity.ContractEntity;
|
|
|
+import com.ydtech.modules.fnc.entity.ContractFileEntity;
|
|
|
+import com.ydtech.modules.fnc.entity.ContractSupplierEntity;
|
|
|
+import com.ydtech.modules.fnc.service.contract.ContractFileService;
|
|
|
+import com.ydtech.modules.fnc.service.contract.ContractService;
|
|
|
+import com.ydtech.modules.fnc.service.contract.ContractSupplierService;
|
|
|
+import com.ydtech.modules.fnc.vo.ContractVo;
|
|
|
+import com.ydtech.modules.inv.dao.InvAccountMapper;
|
|
|
+import com.ydtech.modules.inv.model.InvAccount;
|
|
|
+import com.ydtech.utils.StringUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.sourceforge.pinyin4j.PinyinHelper;
|
|
|
+import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
|
|
+import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
|
|
+import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
|
|
+import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
|
|
|
+import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class contractServiceImpl extends ServiceImpl<ContractMapper, ContractEntity> implements ContractService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ContractMapper contractMapper;
|
|
|
+ @Autowired
|
|
|
+ private InvAccountMapper invAccountMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ContractSupplierMapper contractSupplierMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ContractSupplierService contractSupplierService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ContractFileService contractFileService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param contractDto
|
|
|
+ * @return 新增合同
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void saveAndUpdate(ContractDto contractDto) {
|
|
|
+ if (StringUtils.isEmpty(contractDto.getId())) {
|
|
|
+ ContractEntity contractEntity = new ContractEntity();
|
|
|
+ BeanUtils.copyProperties(contractDto, contractEntity);
|
|
|
+ String userId = BaseController.getUserId();
|
|
|
+ String deptId = BaseController.getDeptId();
|
|
|
+ contractEntity.setDeptId(deptId);
|
|
|
+ contractEntity.setUserId(userId);
|
|
|
+ contractEntity.setCreateTime(LocalDateTime.now());
|
|
|
+// 合同编码生成
|
|
|
+ String contractCode = this.generateCode(contractDto);
|
|
|
+ contractEntity.setContractCode(contractCode);
|
|
|
+// 添加关联表 供应商与合同
|
|
|
+ List<String> supplierIdList = contractDto.getSupplierIdList();
|
|
|
+ contractEntity.setContractStatus("0");
|
|
|
+ baseMapper.insert(contractEntity);
|
|
|
+ if (contractDto.getSupplierIdList() != null) {
|
|
|
+ contractSupplierService.saveAndUpdate(supplierIdList, contractEntity.getId());
|
|
|
+ }
|
|
|
+ if (contractDto.getFilePathIdList() != null) {
|
|
|
+ contractFileService.saveAndUpdate(contractDto.getFilePathIdList(), contractEntity.getId());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ContractEntity contractEntity = new ContractEntity();
|
|
|
+ BeanUtils.copyProperties(contractDto, contractEntity);
|
|
|
+ String contractCode = this.generateCode(contractDto);
|
|
|
+ contractEntity.setContractCode(contractCode);
|
|
|
+ baseMapper.updateById(contractEntity);
|
|
|
+ if (!contractDto.getSupplierIdList().isEmpty()) {
|
|
|
+ contractSupplierService.saveAndUpdate(contractDto.getSupplierIdList(), contractEntity.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (contractDto.getFilePathIdList() != null) {
|
|
|
+ contractFileService.saveAndUpdate(contractDto.getFilePathIdList(), contractEntity.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param contractDto
|
|
|
+ * @return 合同编码规则 甲方简称 首字母 大写 + 当前年份 + 000
|
|
|
+ * @throws BadHanyuPinyinOutputFormatCombination
|
|
|
+ */
|
|
|
+ private String generateCode(ContractDto contractDto) {
|
|
|
+ HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
|
|
|
+ // 获取首字母大写
|
|
|
+ format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
|
|
|
+ format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
|
|
+ format.setVCharType(HanyuPinyinVCharType.WITH_V);
|
|
|
+ if (StringUtils.isEmpty(contractDto.getId())) {
|
|
|
+ StringBuilder stringBuffer = new StringBuilder();
|
|
|
+ //获取最大编码
|
|
|
+ HashMap<String, Object> maxCode = baseMapper.getMaxCode();
|
|
|
+ String Number = "";
|
|
|
+ if (maxCode == null) {
|
|
|
+ Number = "001";
|
|
|
+ } else {
|
|
|
+ Object contractCode = maxCode.get("contractCode");
|
|
|
+
|
|
|
+ int i = Integer.parseInt(contractCode.toString());
|
|
|
+
|
|
|
+ Number = String.format("%03d", i + 1);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 查询甲方简称
|
|
|
+ */
|
|
|
+ String simpleName = this.getSimpleName(contractDto.getExternalCompaniesId());
|
|
|
+ if (StringUtils.isEmpty(simpleName)) {
|
|
|
+ throw new SystemException("甲方简称为空不能新增 生成不了合同编号");
|
|
|
+ }
|
|
|
+
|
|
|
+ char[] chars = simpleName.toCharArray();
|
|
|
+ for (char aChar : chars) {
|
|
|
+ String[] pinyinArray = new String[0];
|
|
|
+ try {
|
|
|
+ pinyinArray = PinyinHelper.toHanyuPinyinStringArray(aChar, format);
|
|
|
+ } catch (BadHanyuPinyinOutputFormatCombination e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ if (pinyinArray != null) {
|
|
|
+ stringBuffer.append(pinyinArray[0].charAt(0)); // 取首字母
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalDate currentDate = LocalDate.now();
|
|
|
+ int currentYear = currentDate.getYear();
|
|
|
+ stringBuffer.append(currentYear).append(Number);
|
|
|
+ return stringBuffer.toString();
|
|
|
+ } else {
|
|
|
+ String firtLetter = "";
|
|
|
+ String contractCode = contractDto.getContractCode();
|
|
|
+ String simpleName = this.getSimpleName(contractDto.getExternalCompaniesId());
|
|
|
+ if (simpleName.isEmpty()) {
|
|
|
+ return contractCode;
|
|
|
+ }
|
|
|
+ char c = contractCode.toCharArray()[0];
|
|
|
+ char[] chars = simpleName.toCharArray();
|
|
|
+ for (char aChar : chars) {
|
|
|
+ String[] pinyinArray = new String[0];
|
|
|
+ try {
|
|
|
+ pinyinArray = PinyinHelper.toHanyuPinyinStringArray(aChar, format);
|
|
|
+ } catch (BadHanyuPinyinOutputFormatCombination e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ if (pinyinArray != null) {
|
|
|
+ firtLetter = String.valueOf(pinyinArray[0].charAt(0)); // 取首字母
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String contractCodeNew = contractCode.replaceFirst(String.valueOf(c), firtLetter);
|
|
|
+ return contractCodeNew;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getSimpleName(String externalCompaniesId) {
|
|
|
+ QueryWrapper<InvAccount> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("account_id", externalCompaniesId);
|
|
|
+ InvAccount invAccount = invAccountMapper.selectOne(queryWrapper);
|
|
|
+
|
|
|
+ return invAccount.getSimpleName();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<ContractEntity> queryPage(ContractVo contractVO) {
|
|
|
+
|
|
|
+ PageVo<ContractVo> pageVo = new PageVo<>();
|
|
|
+ Page page = new Page(contractVO.getPageNum(), contractVO.getPageSize());
|
|
|
+ pageVo.setPageNum(contractVO.getPageNum());
|
|
|
+ pageVo.setPageSize(contractVO.getPageSize());
|
|
|
+
|
|
|
+ if (contractVO.getSupplierIdList() != null && !contractVO.getSupplierIdList().isEmpty()) { //查关联表获取合同id
|
|
|
+ ContractSupplierDto contractSupplierDto = new ContractSupplierDto();
|
|
|
+ contractSupplierDto.setSupplierIdList(contractVO.getSupplierIdList());
|
|
|
+ List<ContractSupplierEntity> contractSupplierEntityList = contractSupplierService.selecSupplierList(contractSupplierDto);
|
|
|
+ List<String> contractIdList = contractSupplierEntityList.stream().map(ContractSupplierEntity::getContractId).collect(Collectors.toList());
|
|
|
+ contractVO.setContractIdList(contractIdList);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ IPage<ContractEntity> queriedPage = contractMapper.queryPage(page, contractVO);
|
|
|
+ List<ContractEntity> records = queriedPage.getRecords();
|
|
|
+ if (!records.isEmpty()) {
|
|
|
+ for (ContractEntity record : records) {
|
|
|
+ String id = record.getId();
|
|
|
+ List<ContractSupplierEntity> contractSupplierEntityList = contractSupplierMapper.selectByContractId(id);
|
|
|
+ List<String> nameList = contractSupplierEntityList.stream().map(ContractSupplierEntity::getNamesimple).collect(Collectors.toList());
|
|
|
+ record.setNamesimpleList(nameList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return queriedPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteById(String id) {
|
|
|
+ baseMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ContractEntity getSelectById(String id) {
|
|
|
+ ContractEntity contractEntity = baseMapper.selectById(id);
|
|
|
+
|
|
|
+// 供应商
|
|
|
+ List<ContractSupplierEntity> contractSupplierEntityList = contractSupplierService.getByContractById(id);
|
|
|
+ if (contractSupplierEntityList != null && !contractSupplierEntityList.isEmpty()) {
|
|
|
+ List<String> supplierIdList = contractSupplierEntityList.stream().map(ContractSupplierEntity::getSupplierId).collect(Collectors.toList());
|
|
|
+ contractEntity.setSupplierIdList(supplierIdList);
|
|
|
+ }
|
|
|
+// 附件
|
|
|
+ List<ContractFileEntity> contractFileEntityList = contractFileService.getByContractById(id);
|
|
|
+ if (!contractFileEntityList.isEmpty()) {
|
|
|
+ contractEntity.setContractFileEntityList(contractFileEntityList);
|
|
|
+ }
|
|
|
+ return contractEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param contractDto
|
|
|
+ * @return 合同协议新增
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void copyContract(ContractDto contractDto) {
|
|
|
+// 备案编码
|
|
|
+ String conrtactCodeNew = this.contractCopyCode(contractDto.getContractCode(), contractDto.getId());
|
|
|
+ ContractEntity contractEntity = new ContractEntity();
|
|
|
+ BeanUtils.copyProperties(contractDto, contractEntity);
|
|
|
+ String userId = BaseController.getUserId();
|
|
|
+ String deptId = BaseController.getDeptId();
|
|
|
+ contractEntity.setDeptId(deptId);
|
|
|
+ contractEntity.setUserId(userId);
|
|
|
+ contractEntity.setCreateTime(LocalDateTime.now());
|
|
|
+// 合同编码生成
|
|
|
+ contractEntity.setContractCode(conrtactCodeNew);
|
|
|
+// 添加关联表 供应商与合同
|
|
|
+ List<String> supplierIdList = contractDto.getSupplierIdList();
|
|
|
+ contractEntity.setContractStatus("0");
|
|
|
+ contractEntity.setId(null);
|
|
|
+ contractEntity.setParentId(contractDto.getId());
|
|
|
+
|
|
|
+ baseMapper.insert(contractEntity);
|
|
|
+ if (contractDto.getSupplierIdList() != null) {
|
|
|
+ contractSupplierService.saveAndUpdate(supplierIdList, contractEntity.getId());
|
|
|
+ }
|
|
|
+ if (contractDto.getFilePathIdList() != null) {
|
|
|
+ contractFileService.saveAndUpdate(contractDto.getFilePathIdList(), contractEntity.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String contractCopyCode(String contractCode, String id) {
|
|
|
+ HashMap<String, Object> contractCodeCopy = baseMapper.copyContractCode(id);
|
|
|
+ if (contractCodeCopy == null) {
|
|
|
+ return contractCode + "-1";
|
|
|
+ } else {
|
|
|
+ int i = Integer.parseInt(contractCodeCopy.get("contractCode").toString());
|
|
|
+ i=i+1;
|
|
|
+ return contractCode+i;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|