| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | 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.bean.dto.ApprovalInstanceDto; |
| | | import com.ruoyi.approve.bean.vo.ApproveGetAndUpdateVo; |
| | | import com.ruoyi.approve.bean.vo.ApproveProcessVO; |
| | | import com.ruoyi.approve.mapper.ApprovalTemplateMapper; |
| | | import com.ruoyi.approve.pojo.ApproveProcess; |
| | | import com.ruoyi.approve.service.IApproveProcessService; |
| | | import com.ruoyi.approve.service.ApprovalInstanceService; |
| | | import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl; |
| | | import com.ruoyi.approve.vo.ApproveGetAndUpdateVo; |
| | | import com.ruoyi.approve.vo.ApproveProcessVO; |
| | | 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 com.ruoyi.common.utils.uuid.UUID; |
| | | import com.ruoyi.framework.security.LoginUser; |
| | | import com.ruoyi.sales.dto.SalesQuotationDto; |
| | | import com.ruoyi.sales.mapper.SalesQuotationMapper; |
| | |
| | | import com.ruoyi.sales.pojo.SalesQuotationProduct; |
| | | import com.ruoyi.sales.service.SalesQuotationProductService; |
| | | import com.ruoyi.sales.service.SalesQuotationService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import lombok.RequiredArgsConstructor; |
| | | 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 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @RequiredArgsConstructor |
| | | public class SalesQuotationServiceImpl extends ServiceImpl<SalesQuotationMapper, SalesQuotation> implements SalesQuotationService { |
| | | @Autowired |
| | | private SalesQuotationMapper salesQuotationMapper; |
| | | @Autowired |
| | | private SalesQuotationProductMapper salesQuotationProductMapper; |
| | | @Autowired |
| | | private SalesQuotationProductService salesQuotationProductService; |
| | | private final SalesQuotationProductMapper salesQuotationProductMapper; |
| | | private final SalesQuotationMapper salesQuotationMapper; |
| | | private final SalesQuotationProductService salesQuotationProductService; |
| | | |
| | | @Autowired |
| | | private ApproveProcessServiceImpl approveProcessService; |
| | | private final ApproveProcessServiceImpl approveProcessService; |
| | | private final CustomerMapper customerMapper; |
| | | private final ApprovalTemplateMapper approvalTemplateMapper; |
| | | private final ApprovalInstanceService approvalInstanceService; |
| | | |
| | | @Override |
| | | public IPage<SalesQuotationDto> listPage(Page page, SalesQuotationDto salesQuotationDto) { |
| | | IPage<SalesQuotationDto> salesQuotationDtoIPage = salesQuotationMapper.listPage(page, 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; |
| | | } |
| | | |
| | |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | SalesQuotation salesQuotation = new SalesQuotation(); |
| | | BeanUtils.copyProperties(salesQuotationDto, salesQuotation); |
| | | String quotationNo = OrderUtils.countTodayByCreateTime(salesQuotationMapper, "QT","quotationNo"); |
| | | 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", salesQuotationDto.getCreateTime() != null ? salesQuotationDto.getCreateTime() : LocalDateTime.now()); |
| | | salesQuotation.setQuotationNo(quotationNo); |
| | | salesQuotation.setStatus("待审批"); |
| | | salesQuotationMapper.insert(salesQuotation); |
| | |
| | | approveProcessVO.setPrice(salesQuotationDto.getTotalAmount()); |
| | | try { |
| | | approveProcessService.addApprove(approveProcessVO); |
| | | }catch (Exception e){ |
| | | log.error("SalesQuotationServiceImpl error:{}", e); |
| | | throw new RuntimeException("审批失败"); |
| | | } catch (Exception e) { |
| | | log.error("SalesQuotationServiceImpl approve error for quotationNo: {}", e); |
| | | throw new RuntimeException("审批失败: " + e.getMessage(), e); |
| | | } |
| | | // 报价审批 |
| | | ApprovalInstanceDto approvalInstanceDto = new ApprovalInstanceDto(); |
| | | approvalInstanceDto.setTemplateId(salesQuotationDto.getTemplateId()); |
| | | approvalInstanceDto.setBusinessId(salesQuotationDto.getId()); |
| | | approvalInstanceDto.setBusinessType(7L); |
| | | approvalInstanceDto.setTitle("报价编号:" + quotationNo); |
| | | approvalInstanceDto.setApplicantId(SecurityUtils.getUserId()); |
| | | approvalInstanceDto.setTemplateName(approvalTemplateMapper.selectById(salesQuotationDto.getTemplateId()).getTemplateName()); |
| | | approvalInstanceDto.setApplicantName(SecurityUtils.getLoginUser().getNickName()); |
| | | approvalInstanceDto.setApplyTime(LocalDateTime.now()); |
| | | approvalInstanceService.add(approvalInstanceDto); |
| | | 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())); |