| | |
| | | package com.ruoyi.basic.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | 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.ruoyi.basic.dto.SupplierManageDto; |
| | | import com.ruoyi.basic.dto.SupplierArchiveDetailVo; |
| | | import com.ruoyi.basic.dto.SupplierArchiveSaveDto; |
| | | import com.ruoyi.basic.excel.SupplierManageExcelDto; |
| | | import com.ruoyi.basic.mapper.SupplierManageMapper; |
| | | import com.ruoyi.basic.mapper.SupplierQualificationMapper; |
| | | import com.ruoyi.basic.mapper.SupplierProductMapper; |
| | | import com.ruoyi.basic.mapper.SupplierCooperationRecordMapper; |
| | | import com.ruoyi.basic.pojo.SupplierQualification; |
| | | import com.ruoyi.basic.pojo.SupplierProduct; |
| | | import com.ruoyi.basic.pojo.SupplierCooperationRecord; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | | import com.ruoyi.basic.pojo.SupplierManage; |
| | | import com.ruoyi.basic.service.ISupplierService; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.purchase.mapper.PurchaseLedgerMapper; |
| | | import com.ruoyi.purchase.pojo.PurchaseLedger; |
| | | import com.ruoyi.purchase.mapper.TicketRegistrationMapper; |
| | | import com.ruoyi.purchase.pojo.TicketRegistration; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.math.BigDecimal; |
| | | import java.util.stream.Collectors; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class SupplierServiceImpl extends ServiceImpl<SupplierManageMapper,SupplierManage> implements ISupplierService { |
| | | |
| | | @Autowired |
| | | private SupplierManageMapper supplierMapper; |
| | | @Autowired |
| | | private PurchaseLedgerMapper purchaseLedgerMapper; |
| | | @Autowired private TicketRegistrationMapper ticketRegistrationMapper; |
| | | @Autowired private SupplierQualificationMapper qualificationMapper; |
| | | @Autowired private SupplierProductMapper productMapper; |
| | | @Autowired private SupplierCooperationRecordMapper cooperationMapper; |
| | | |
| | | /** |
| | | * 供应商新增 |
| | | * @param supplierManage |
| | | * @return |
| | | */ |
| | | @Override |
| | | public void saveSupplier(SupplierManage supplierManage) { |
| | | Long tenantId = SecurityUtils.getLoginUser().getTenantId(); |
| | | LambdaQueryWrapper<SupplierManage> supplierManageLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | supplierManageLambdaQueryWrapper.eq(SupplierManage::getSupplierName,supplierManage.getSupplierName()) |
| | | .eq(SupplierManage::getTenantId, tenantId).eq(SupplierManage::getDeleteFlag, 0); |
| | | if (supplierMapper.selectCount(supplierManageLambdaQueryWrapper) > 0) { |
| | | throw new RuntimeException("供应商已存在"); |
| | | } |
| | | supplierManage.setTenantId(tenantId); |
| | | supplierManage.setDeleteFlag(0); |
| | | supplierManage.setCooperationStatus(supplierManage.getCooperationStatus() == null ? 1 : supplierManage.getCooperationStatus()); |
| | | supplierManage.setQualificationStatus(supplierManage.getQualificationStatus() == null ? 0 : supplierManage.getQualificationStatus()); |
| | | supplierManage.setMaintainUserId(SecurityUtils.getUserId().intValue()); |
| | | supplierManage.setMaintainTime(LocalDate.now()); |
| | | supplierMapper.insert(supplierManage); |
| | | if (supplierManage.getSupplierCode() == null || supplierManage.getSupplierCode().trim().isEmpty()) { |
| | | supplierManage.setSupplierCode(String.format("SUP%06d", supplierManage.getId())); |
| | | supplierMapper.updateById(supplierManage); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 供应商删除 |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @Override |
| | | public int delSupplier(List<Integer> ids) { |
| | | Long tenantId=SecurityUtils.getLoginUser().getTenantId(); |
| | | List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new QueryWrapper<PurchaseLedger>().lambda() |
| | | .in(PurchaseLedger::getSupplierId, ids).eq(PurchaseLedger::getTenantId,tenantId)); |
| | | if (purchaseLedgers.size() > 0) { |
| | | throw new RuntimeException("供应商已存在采购合同"); |
| | | } |
| | | int count=0; |
| | | for(Integer id:ids){ |
| | | SupplierManage supplier=supplierDetail(id); |
| | | if(supplier!=null){ supplier.setDeleteFlag(1); supplier.setCooperationStatus(0); count+=supplierMapper.updateById(supplier); } |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | | * 供应商详情 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public SupplierManage supplierDetail(Integer id) { |
| | | return supplierMapper.selectOne(new LambdaQueryWrapper<SupplierManage>().eq(SupplierManage::getId,id) |
| | | .eq(SupplierManage::getTenantId, SecurityUtils.getLoginUser().getTenantId()) |
| | | .eq(SupplierManage::getDeleteFlag,0)); |
| | | } |
| | | |
| | | /** |
| | | * 供应商修改 |
| | | * @param supplierManage |
| | | * @return |
| | | */ |
| | | @Override |
| | | public int supplierUpdate(SupplierManage supplierManage) { |
| | | SupplierManage current = supplierDetail(supplierManage.getId()); |
| | | if (current == null) throw new RuntimeException("供应商不存在"); |
| | | supplierManage.setTenantId(current.getTenantId()); |
| | | return supplierMapper.update(supplierManage, new LambdaQueryWrapper<SupplierManage>() |
| | | .eq(SupplierManage::getId, supplierManage.getId()).eq(SupplierManage::getTenantId,current.getTenantId())); |
| | | } |
| | | |
| | | /** |
| | | * 供应商分页查询 |
| | | * @param page |
| | | * @param supplierManageDto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<SupplierManage> supplierListPage(Page page, SupplierManageDto supplierManageDto) { |
| | | supplierManageDto.setTenantId(SecurityUtils.getLoginUser().getTenantId()); |
| | | return supplierMapper.supplierListPage(page,supplierManageDto); |
| | | } |
| | | |
| | | /** |
| | | * 供应商导出 |
| | | * @param response |
| | | * @param supplierManageDto |
| | | */ |
| | | @Override |
| | | public void supplierExport(HttpServletResponse response, SupplierManageDto supplierManageDto) { |
| | | supplierManageDto.setTenantId(SecurityUtils.getLoginUser().getTenantId()); |
| | | List<SupplierManageExcelDto> supplierManageList = supplierMapper.supplierExportList(supplierManageDto); |
| | | ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class); |
| | | util.exportExcel(response, supplierManageList, "供应商导出"); |
| | | } |
| | | |
| | | @Override |
| | | public Boolean importData(MultipartFile file) { |
| | | |
| | | try { |
| | | ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class); |
| | | List<SupplierManageExcelDto> list = util.importExcel(file.getInputStream()); |
| | | ArrayList<SupplierManage> supplierManages = new ArrayList<>(); |
| | | list.stream().forEach(dto -> { |
| | | SupplierManage supplierManage = new SupplierManage(); |
| | | BeanUtils.copyProperties(dto,supplierManage); |
| | | supplierManage.setMaintainTime(LocalDate.now()); |
| | | Long userId = SecurityUtils.getLoginUser().getUser().getUserId(); |
| | | |
| | | supplierManage.setMaintainUserId(Integer.parseInt(userId+"")); |
| | | supplierManages.add(supplierManage); |
| | | }); |
| | | |
| | | this.saveOrUpdateBatch(supplierManages); |
| | | return true; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public SupplierArchiveDetailVo supplierArchiveDetail(Integer id) { |
| | | SupplierManage supplier = supplierDetail(id); |
| | | if (supplier == null) throw new RuntimeException("供应商不存在"); |
| | | long supplierId = id.longValue(); |
| | | long tenantId = supplier.getTenantId(); |
| | | SupplierArchiveDetailVo result = new SupplierArchiveDetailVo(); |
| | | result.setSupplier(supplier); |
| | | result.setQualifications(qualificationMapper.selectList(new LambdaQueryWrapper<SupplierQualification>() |
| | | .eq(SupplierQualification::getSupplierId,supplierId).eq(SupplierQualification::getTenantId,tenantId) |
| | | .eq(SupplierQualification::getDeleteFlag,0).orderByAsc(SupplierQualification::getExpiryDate))); |
| | | result.setProducts(productMapper.selectList(new LambdaQueryWrapper<SupplierProduct>() |
| | | .eq(SupplierProduct::getSupplierId,supplierId).eq(SupplierProduct::getTenantId,tenantId) |
| | | .eq(SupplierProduct::getDeleteFlag,0))); |
| | | result.setCooperationRecords(cooperationMapper.selectList(new LambdaQueryWrapper<SupplierCooperationRecord>() |
| | | .eq(SupplierCooperationRecord::getSupplierId,supplierId).eq(SupplierCooperationRecord::getTenantId,tenantId) |
| | | .eq(SupplierCooperationRecord::getDeleteFlag,0).orderByDesc(SupplierCooperationRecord::getCooperationDate))); |
| | | List<PurchaseLedger> purchases = purchaseLedgerMapper.selectList(new LambdaQueryWrapper<PurchaseLedger>() |
| | | .eq(PurchaseLedger::getSupplierId,supplierId).eq(PurchaseLedger::getTenantId,tenantId)); |
| | | result.setPurchaseCount((long)purchases.size()); |
| | | result.setPurchaseAmount(purchases.stream().map(PurchaseLedger::getContractAmount).filter(java.util.Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | result.setInvoicedAmount(ticketRegistrationMapper.selectList(new LambdaQueryWrapper<TicketRegistration>() |
| | | .eq(TicketRegistration::getSupplierId,supplierId).eq(TicketRegistration::getTenantId,tenantId) |
| | | .eq(TicketRegistration::getDeleteFlag,0)).stream().map(TicketRegistration::getInvoiceAmount) |
| | | .filter(java.util.Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Integer saveArchive(SupplierArchiveSaveDto archive) { |
| | | if (archive == null || archive.getSupplier() == null) throw new RuntimeException("供应商信息不能为空"); |
| | | SupplierManage supplier = archive.getSupplier(); |
| | | if (supplier.getId() == null) saveSupplier(supplier); else supplierUpdate(supplier); |
| | | long tenantId = SecurityUtils.getLoginUser().getTenantId(); |
| | | long userId = SecurityUtils.getUserId(); |
| | | long supplierId = supplier.getId().longValue(); |
| | | qualificationMapper.delete(new LambdaQueryWrapper<SupplierQualification>().eq(SupplierQualification::getSupplierId,supplierId).eq(SupplierQualification::getTenantId,tenantId)); |
| | | productMapper.delete(new LambdaQueryWrapper<SupplierProduct>().eq(SupplierProduct::getSupplierId,supplierId).eq(SupplierProduct::getTenantId,tenantId)); |
| | | LocalDate nearest = null; |
| | | if (archive.getQualifications() != null) for (SupplierQualification item : archive.getQualifications()) { |
| | | item.setId(null); item.setSupplierId(supplierId); item.setTenantId(tenantId); item.setCreateUser(userId); |
| | | item.setCreateTime(LocalDateTime.now()); item.setUpdateTime(LocalDateTime.now()); item.setDeleteFlag(0); |
| | | item.setStatus(item.getStatus() == null ? 1 : item.getStatus()); qualificationMapper.insert(item); |
| | | if (item.getExpiryDate()!=null && (nearest==null || item.getExpiryDate().isBefore(nearest))) nearest=item.getExpiryDate(); |
| | | } |
| | | if (archive.getProducts() != null) for (SupplierProduct item : archive.getProducts()) { |
| | | if (item.getProductName()==null || item.getProductName().trim().isEmpty()) continue; |
| | | item.setId(null); item.setSupplierId(supplierId); item.setTenantId(tenantId); item.setCreateUser(userId); |
| | | item.setCreateTime(LocalDateTime.now()); item.setUpdateTime(LocalDateTime.now()); item.setDeleteFlag(0); |
| | | item.setStatus(item.getStatus()==null?1:item.getStatus()); item.setCurrency(item.getCurrency()==null?"CNY":item.getCurrency()); productMapper.insert(item); |
| | | } |
| | | supplier.setQualificationExpiryDate(nearest); |
| | | supplier.setQualificationStatus(nearest==null?0:(nearest.isBefore(LocalDate.now())?3:(nearest.isBefore(LocalDate.now().plusDays(31))?2:1))); |
| | | if (archive.getProducts()!=null) supplier.setSuppliedProductsSummary(archive.getProducts().stream().map(SupplierProduct::getProductName) |
| | | .filter(java.util.Objects::nonNull).limit(5).collect(Collectors.joining("、"))); |
| | | supplierUpdate(supplier); |
| | | return supplier.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public void addCooperationRecord(Integer supplierId, SupplierCooperationRecord record) { |
| | | SupplierManage supplier = supplierDetail(supplierId); |
| | | if (supplier == null) throw new RuntimeException("供应商不存在"); |
| | | record.setId(null); record.setSupplierId(supplierId.longValue()); record.setTenantId(supplier.getTenantId()); |
| | | record.setCreateUser(SecurityUtils.getUserId()); record.setUpdateUser(SecurityUtils.getUserId()); |
| | | record.setCreateTime(LocalDateTime.now()); record.setUpdateTime(LocalDateTime.now()); record.setDeleteFlag(0); |
| | | cooperationMapper.insert(record); |
| | | } |
| | | } |