¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.procurementrecord.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | 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.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.security.LoginUser; |
| | | import com.ruoyi.procurementrecord.dto.*; |
| | | import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper; |
| | | import com.ruoyi.procurementrecord.mapper.ProcurementRecordOutMapper; |
| | | import com.ruoyi.procurementrecord.pojo.ProcurementRecord; |
| | | import com.ruoyi.procurementrecord.pojo.ProcurementRecordOut; |
| | | import com.ruoyi.procurementrecord.service.ProcurementRecordService; |
| | | import com.ruoyi.quality.pojo.QualityInspect; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author :yys |
| | | * @date : 2025/7/7 14:38 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | @Slf4j |
| | | public class ProcurementRecordServiceImpl extends ServiceImpl<ProcurementRecordMapper, ProcurementRecord> implements ProcurementRecordService { |
| | | |
| | | private final ProcurementRecordMapper procurementRecordMapper; |
| | | |
| | | private final ProcurementRecordOutMapper procurementRecordOutMapper; |
| | | |
| | | private final SalesLedgerProductMapper salesLedgerProductMapper; |
| | | |
| | | @Override |
| | | public List<ProcurementDto> listProcurementBySalesLedgerId(ProcurementDto procurementDto) { |
| | | List<ProcurementDto> procurementDtos = procurementRecordMapper.listProcurementBySalesLedgerId(procurementDto); |
| | | // 计ç®å¾
å
¥åºæ°é |
| | | // æ¥è¯¢éè´è®°å½å·²å
¥åºæ°é |
| | | List<Integer> collect = procurementDtos.stream().map(ProcurementDto::getId).collect(Collectors.toList()); |
| | | if(CollectionUtils.isEmpty( collect)){ |
| | | return procurementDtos; |
| | | } |
| | | LambdaQueryWrapper<ProcurementRecord> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | procurementRecordLambdaQueryWrapper.in(ProcurementRecord::getSalesLedgerProductId, collect); |
| | | List<ProcurementRecord> procurementRecords = procurementRecordMapper.selectList(procurementRecordLambdaQueryWrapper); |
| | | if(CollectionUtils.isEmpty( procurementRecords)){ |
| | | return procurementDtos; |
| | | } |
| | | for (ProcurementDto dto : procurementDtos) { |
| | | // æ ¹æ®éè´å°è´¦IDçé对åºçå
¥åºè®°å½ |
| | | List<ProcurementRecord> collect1 = procurementRecords.stream() |
| | | .filter(procurementRecord -> procurementRecord.getSalesLedgerProductId().equals(dto.getId())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // å¦ææ²¡æç¸å
³çå
¥åºè®°å½ï¼è·³è¿è¯¥æ¡æ°æ® |
| | | if(CollectionUtils.isEmpty(collect1)){ |
| | | dto.setQuantity0(dto.getQuantity()); |
| | | continue; |
| | | } |
| | | |
| | | // 计ç®å·²å
¥åºæ°éæ»åï¼å¹¶è®¾ç½®å¾
å
¥åºæ°é |
| | | BigDecimal totalInboundNum = collect1.stream() |
| | | .map(ProcurementRecord::getInboundNum) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | // å¾
å
¥åºæ°é = æ»æ°é - å·²å
¥åºæ°é |
| | | dto.setQuantity0(dto.getQuantity().subtract(totalInboundNum)); |
| | | } |
| | | return procurementDtos; |
| | | } |
| | | |
| | | public ProcurementRecord getProcurementRecordById(Integer id){ |
| | | ProcurementRecord procurementRecord = procurementRecordMapper.selectById(id); |
| | | if(procurementRecord == null) { |
| | | throw new RuntimeException("æªæ¾å°è¯¥éè´å
¥åºè®°å½"); |
| | | } |
| | | return procurementRecord; |
| | | } |
| | | |
| | | public List<ProcurementRecord> getProcurementRecordByIds(List<Integer> id){ |
| | | List<ProcurementRecord> procurementRecord = procurementRecordMapper.selectBatchIds(id); |
| | | if(procurementRecord == null) { |
| | | throw new RuntimeException("æªæ¾å°è¯¥éè´å
¥åºè®°å½"); |
| | | } |
| | | return procurementRecord; |
| | | } |
| | | |
| | | @Override |
| | | public int updatePro(ProcurementUpdateDto procurementDto) { |
| | | ProcurementRecord procurementRecordById = getProcurementRecordById(procurementDto.getId()); |
| | | procurementRecordById.setInboundNum(procurementDto.getQuantityStock()); |
| | | return procurementRecordMapper.updateById(procurementRecordById); |
| | | } |
| | | |
| | | @Override |
| | | public int deletePro(ProcurementUpdateDto procurementDto) { |
| | | List<ProcurementRecord> procurementRecordById = getProcurementRecordByIds(procurementDto.getIds()); |
| | | procurementRecordMapper.deleteBatchIds(procurementRecordById.stream().map(ProcurementRecord::getId).collect(Collectors.toList())); |
| | | // å 餿æå¯¹åºçåºåºè®°å½ |
| | | LambdaQueryWrapper<ProcurementRecordOut> procurementRecordOutLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | procurementRecordOutLambdaQueryWrapper.in(ProcurementRecordOut::getProcurementRecordStorageId, procurementDto.getIds()); |
| | | List<ProcurementRecordOut> procurementRecordOuts = procurementRecordOutMapper.selectList(procurementRecordOutLambdaQueryWrapper); |
| | | if(!CollectionUtils.isEmpty(procurementRecordOuts)){ |
| | | procurementRecordOutMapper.deleteBatchIds(procurementRecordOuts.stream().map(ProcurementRecordOut::getId).collect(Collectors.toList())); |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | @Override |
| | | public void export(HttpServletResponse response) { |
| | | List<ProcurementPageDto> list =procurementRecordMapper.list(); |
| | | ExcelUtil<ProcurementPageDto> util = new ExcelUtil<ProcurementPageDto>(ProcurementPageDto.class); |
| | | util.exportExcel(response, list, "å
¥åºå°è´¦"); |
| | | } |
| | | |
| | | @Override |
| | | public int add(ProcurementAddDto procurementDto) { |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | // æ¹éæ°å¢ |
| | | for (Details detail : procurementDto.getDetails()) { |
| | | // æ¥è¯¢éè´å
¥åºæ°é |
| | | LambdaQueryWrapper<ProcurementRecord> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | procurementRecordLambdaQueryWrapper.eq(ProcurementRecord::getSalesLedgerProductId, detail.getId()); |
| | | Long aLong = procurementRecordMapper.selectCount(procurementRecordLambdaQueryWrapper); |
| | | |
| | | ProcurementRecord.ProcurementRecordBuilder procurementRecordBuilder = ProcurementRecord.builder() |
| | | .salesLedgerProductId(detail.getId()) |
| | | .inboundBatches(aLong.equals(0L) ? "第1æ¹æ¬¡" : "第"+ (aLong + 1) + "æ¹æ¬¡") |
| | | .inboundNum(detail.getInboundQuantity()) |
| | | .createDate(LocalDateTime.now()) |
| | | .userId(loginUser.getUserId()) |
| | | .tenantId(loginUser.getTenantId()) |
| | | .createBy(procurementDto.getNickName()); |
| | | this.save(procurementRecordBuilder.build()); |
| | | // å
¥åºæååæéè´æ°é |
| | | // LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | // salesLedgerProductLambdaQueryWrapper.eq(SalesLedgerProduct::getId, detail.getId()); |
| | | // SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectOne(salesLedgerProductLambdaQueryWrapper); |
| | | // if(salesLedgerProduct == null){ |
| | | // throw new RuntimeException("æªæ¾å°è¯¥åå"); |
| | | // } |
| | | // salesLedgerProduct.setQuantity(salesLedgerProduct.getQuantity().subtract(detail.getInboundQuantity())); |
| | | // salesLedgerProductMapper.updateById(salesLedgerProduct); |
| | | } |
| | | return 1; |
| | | } |
| | | |
| | | @Override |
| | | public IPage<ProcurementPageDto> listPage(Page page, ProcurementPageDto procurementDto) { |
| | | IPage<ProcurementPageDto> procurementPageDtoIPage = procurementRecordMapper.listPage(page, procurementDto); |
| | | List<ProcurementPageDto> procurementPageDtos = procurementPageDtoIPage.getRecords(); |
| | | // 计ç®å¾
å
¥åºæ°é |
| | | // æ¥è¯¢éè´è®°å½å·²å
¥åºæ°é |
| | | List<Integer> collect = procurementPageDtos.stream().map(ProcurementPageDto::getId).collect(Collectors.toList()); |
| | | if(CollectionUtils.isEmpty( collect)){ |
| | | return procurementPageDtoIPage; |
| | | } |
| | | LambdaQueryWrapper<ProcurementRecordOut> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | procurementRecordLambdaQueryWrapper.in(ProcurementRecordOut::getProcurementRecordStorageId, collect); |
| | | List<ProcurementRecordOut> procurementRecords = procurementRecordOutMapper.selectList(procurementRecordLambdaQueryWrapper); |
| | | if(CollectionUtils.isEmpty( procurementRecords)){ |
| | | return procurementPageDtoIPage; |
| | | } |
| | | for (ProcurementPageDto dto : procurementPageDtos) { |
| | | // æ ¹æ®éè´å°è´¦IDçé对åºçåºåºè®°å½ |
| | | List<ProcurementRecordOut> collect1 = procurementRecords.stream() |
| | | .filter(ProcurementRecordOut -> ProcurementRecordOut.getProcurementRecordStorageId().equals(dto.getId())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // å¦ææ²¡æç¸å
³çåºåºè®°å½ï¼è·³è¿è¯¥æ¡æ°æ® |
| | | if(CollectionUtils.isEmpty(collect1)){ |
| | | dto.setInboundNum0(dto.getInboundNum()); |
| | | continue; |
| | | } |
| | | |
| | | // 计ç®å·²åºåºæ°éæ»åï¼å¹¶è®¾ç½®å¾
åºåºæ°é |
| | | BigDecimal totalInboundNum = collect1.stream() |
| | | .map(ProcurementRecordOut::getInboundNum) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | // å¾
åºåºæ°é = æ»æ°é - å·²åºåºæ°é |
| | | dto.setInboundNum0(dto.getInboundNum().subtract(totalInboundNum)); |
| | | } |
| | | return procurementPageDtoIPage; |
| | | } |
| | | |
| | | } |