| | |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.approve.pojo.ApproveProcess; |
| | | import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl; |
| | | import com.ruoyi.approve.bean.dto.ApprovalInstanceDto; |
| | | import com.ruoyi.approve.bean.vo.ApproveGetAndUpdateVo; |
| | | import com.ruoyi.approve.bean.vo.ApproveProcessVO; |
| | | import com.ruoyi.basic.dto.CustomerPrivatePoolDto; |
| | | import com.ruoyi.basic.mapper.CustomerPrivatePoolMapper; |
| | | import com.ruoyi.approve.mapper.ApprovalTemplateMapper; |
| | | import com.ruoyi.approve.pojo.ApprovalInstance; |
| | | import com.ruoyi.approve.pojo.ApprovalTemplate; |
| | | import com.ruoyi.approve.pojo.ApproveProcess; |
| | | import com.ruoyi.approve.service.ApprovalInstanceService; |
| | | import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl; |
| | | import com.ruoyi.basic.mapper.CustomerMapper; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | | import com.ruoyi.common.enums.IsDeleteEnum; |
| | | import com.ruoyi.common.utils.OrderUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | private final SalesQuotationProductService salesQuotationProductService; |
| | | |
| | | private final ApproveProcessServiceImpl approveProcessService; |
| | | private final CustomerPrivatePoolMapper customerPrivatePoolMapper; |
| | | private final CustomerMapper customerMapper; |
| | | private final ApprovalTemplateMapper approvalTemplateMapper; |
| | | private final ApprovalInstanceService approvalInstanceService; |
| | | |
| | | @Override |
| | | public IPage<SalesQuotationDto> listPage(Page page, SalesQuotationDto salesQuotationDto) { |
| | |
| | | if(CollectionUtils.isEmpty(salesQuotationDtoIPage.getRecords())){ |
| | | return salesQuotationDtoIPage; |
| | | } |
| | | salesQuotationDtoIPage.getRecords().forEach(record -> { |
| | | List<SalesQuotationProduct> products = salesQuotationProductMapper.selectBySalesQuotationId(record.getId()); |
| | | record.setProducts(products); |
| | | }); |
| | | |
| | | // 批量查询产品,避免 N+1 问题 |
| | | List<Long> quotationIds = salesQuotationDtoIPage.getRecords().stream() |
| | | .map(SalesQuotationDto::getId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (!quotationIds.isEmpty()) { |
| | | List<SalesQuotationProduct> allProducts = salesQuotationProductMapper.selectList( |
| | | new LambdaQueryWrapper<SalesQuotationProduct>() |
| | | .in(SalesQuotationProduct::getSalesQuotationId, quotationIds) |
| | | ); |
| | | |
| | | Map<Long, List<SalesQuotationProduct>> productMap = allProducts.stream() |
| | | .collect(Collectors.groupingBy(SalesQuotationProduct::getSalesQuotationId)); |
| | | |
| | | salesQuotationDtoIPage.getRecords().forEach(record -> |
| | | record.setProducts(productMap.getOrDefault(record.getId(), new ArrayList<>())) |
| | | ); |
| | | } |
| | | return salesQuotationDtoIPage; |
| | | } |
| | | |
| | |
| | | public boolean add(SalesQuotationDto salesQuotationDto) { |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | SalesQuotation salesQuotation = new SalesQuotation(); |
| | | CustomerPrivatePoolDto customerPrivatePoolDto = customerPrivatePoolMapper.selectInfo(Long.valueOf(salesQuotationDto.getCustomer())); |
| | | if (ObjectUtils.isNotEmpty(customerPrivatePoolDto)) { |
| | | BeanUtils.copyProperties(salesQuotationDto, salesQuotation); |
| | | salesQuotation.setCustomer(customerPrivatePoolDto.getCustomerName()); |
| | | BeanUtils.copyProperties(salesQuotationDto, salesQuotation); |
| | | salesQuotation.setId(null); |
| | | Customer customer = customerMapper.selectById(Long.valueOf(salesQuotationDto.getCustomerId())); |
| | | if (ObjectUtils.isNotEmpty(customer)) { |
| | | salesQuotation.setCustomer(customer.getCustomerName()); |
| | | } |
| | | String quotationNo = OrderUtils.countTodayByCreateTime(salesQuotationMapper, "QT","quotation_no"); |
| | | String quotationNo = OrderUtils.countTodayByCreateTime(salesQuotationMapper, "QT","quotation_no", salesQuotationDto.getCreateTime() != null ? salesQuotationDto.getCreateTime() : LocalDateTime.now()); |
| | | salesQuotation.setQuotationNo(quotationNo); |
| | | salesQuotation.setStatus("待审批"); |
| | | salesQuotationMapper.insert(salesQuotation); |
| | |
| | | }).collect(Collectors.toList()); |
| | | salesQuotationProductService.saveBatch(products); |
| | | // 报价审批 |
| | | ApproveProcessVO approveProcessVO = new ApproveProcessVO(); |
| | | approveProcessVO.setApproveType(6); |
| | | approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId()); |
| | | approveProcessVO.setApproveReason(quotationNo); |
| | | approveProcessVO.setApproveUserIds(salesQuotationDto.getApproveUserIds()); |
| | | approveProcessVO.setApproveUser(loginUser.getUserId()); |
| | | approveProcessVO.setApproveTime(LocalDate.now().toString()); |
| | | approveProcessVO.setPrice(salesQuotationDto.getTotalAmount()); |
| | | ApprovalInstanceDto approvalInstance = new ApprovalInstanceDto(); |
| | | approvalInstance.setTemplateId(approvalTemplateMapper.selectOne(new LambdaQueryWrapper<ApprovalTemplate>().eq(ApprovalTemplate::getBusinessType,6L).orderByDesc(ApprovalTemplate::getId).last("LIMIT 1")).getId()); |
| | | approvalInstance.setTemplateName(approvalTemplateMapper.selectOne(new LambdaQueryWrapper<ApprovalTemplate>().eq(ApprovalTemplate::getBusinessType,6L).orderByDesc(ApprovalTemplate::getId).last("LIMIT 1")).getTemplateName()); |
| | | approvalInstance.setBusinessId(salesQuotation.getId()); |
| | | approvalInstance.setBusinessType(6L); |
| | | approvalInstance.setCurrentLevel(1); |
| | | approvalInstance.setTitle(quotationNo+"审批"); |
| | | approvalInstance.setApplicantId(loginUser.getUserId()); |
| | | approvalInstance.setApplicantName(loginUser.getNickName()); |
| | | approvalInstance.setApplyTime(LocalDateTime.now()); |
| | | try { |
| | | approveProcessService.addApprove(approveProcessVO); |
| | | }catch (Exception e){ |
| | | log.error("SalesQuotationServiceImpl error:{}", e); |
| | | throw new RuntimeException("审批失败"); |
| | | approvalInstanceService.add(approvalInstance); |
| | | } catch (Exception e) { |
| | | log.error("SalesQuotationServiceImpl approve error for quotationNo: {}", e); |
| | | throw new RuntimeException("审批失败: " + e.getMessage(), e); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | |
| | | salesQuotationProductService.saveBatch(products); |
| | | // 修改报价审批 |
| | | vo.setApproveUserIds(salesQuotationDto.getApproveUserIds()); |
| | | vo.setApproveType(6); |
| | | vo.setApproveReason(salesQuotationDto.getQuotationNo()); |
| | | approveProcessService.updateApproveUser(vo); |
| | | // 先结束之前未结束的报价审批 |
| | | approvalInstanceService.lambdaUpdate().set(ApprovalInstance::getStatus,"REJECTED").eq(ApprovalInstance::getBusinessId,salesQuotation.getId()).eq(ApprovalInstance::getBusinessType,6L).update(); |
| | | |
| | | ApprovalInstanceDto approvalInstance = new ApprovalInstanceDto(); |
| | | approvalInstance.setTemplateId(approvalTemplateMapper.selectOne(new LambdaQueryWrapper<ApprovalTemplate>().eq(ApprovalTemplate::getBusinessType,6L).orderByDesc(ApprovalTemplate::getId).last("LIMIT 1")).getId()); |
| | | approvalInstance.setTemplateName(approvalTemplateMapper.selectOne(new LambdaQueryWrapper<ApprovalTemplate>().eq(ApprovalTemplate::getBusinessType,6L).orderByDesc(ApprovalTemplate::getId).last("LIMIT 1")).getTemplateName()); |
| | | approvalInstance.setBusinessId(salesQuotation.getId()); |
| | | approvalInstance.setBusinessType(6L); |
| | | approvalInstance.setCurrentLevel(1); |
| | | approvalInstance.setTitle(salesQuotation.getQuotationNo()+"审批"); |
| | | approvalInstance.setApplicantId(SecurityUtils.getUserId()); |
| | | approvalInstance.setApplicantName(SecurityUtils.getLoginUser().getNickName()); |
| | | approvalInstance.setApplyTime(LocalDateTime.now()); |
| | | try { |
| | | approvalInstanceService.add(approvalInstance); |
| | | } catch (Exception e) { |
| | | log.error("SalesQuotationServiceImpl approve error for quotationNo: {}", e); |
| | | throw new RuntimeException("审批失败: " + e.getMessage(), e); |
| | | } |
| | | return true; |
| | | } |
| | | @Override |
| | |
| | | // 删除报价审批 |
| | | ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>() |
| | | .eq(ApproveProcess::getApproveType, 6) |
| | | .eq(ApproveProcess::getApproveDelete, IsDeleteEnum.NOT_DELETED) |
| | | .eq(ApproveProcess::getApproveReason, salesQuotation.getQuotationNo())); |
| | | if(one != null){ |
| | | approveProcessService.delByIds(Collections.singletonList(one.getId())); |