| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.mapper.CustomerMapper; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.sales.dto.SalesLedgerDto; |
| | | import com.ruoyi.sales.mapper.SalesLedgerMapper; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import com.ruoyi.sales.service.ISalesLedgerService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 销售台账Service业务层处理 |
| | |
| | | * @date 2025-05-08 |
| | | */ |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class SalesLedgerServiceImpl extends ServiceImpl<SalesLedgerMapper, SalesLedger> implements ISalesLedgerService { |
| | | @Autowired |
| | | |
| | | private SalesLedgerMapper salesLedgerMapper; |
| | | |
| | | private CustomerMapper customerMapper; |
| | | |
| | | private SalesLedgerProductMapper salesLedgerProductMapper; |
| | | |
| | | @Override |
| | | public List<SalesLedger> selectSalesLedgerList(SalesLedger salesLedger) { |
| | | return salesLedgerMapper.selectList(new LambdaQueryWrapper<>()); |
| | | } |
| | | |
| | | public List<SalesLedgerDto> getSalesLedgerWithProducts() { |
| | | List<SalesLedger> ledgers = salesLedgerMapper.selectList(new LambdaQueryWrapper<>()); |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<>()); |
| | | |
| | | Map<Long, List<SalesLedgerProduct>> productMap = products.stream() |
| | | .collect(Collectors.groupingBy(SalesLedgerProduct::getSalesLedgerId)); |
| | | |
| | | return ledgers.stream().map(ledger -> { |
| | | SalesLedgerDto dto = new SalesLedgerDto(); |
| | | org.springframework.beans.BeanUtils.copyProperties(ledger, dto); |
| | | |
| | | List<SalesLedgerProduct> ledgerProducts = productMap.getOrDefault(ledger.getId(), Collections.emptyList()); |
| | | if (!ledgerProducts.isEmpty()) { |
| | | dto.setHasChildren(true); |
| | | dto.setChildren(ledgerProducts); |
| | | } |
| | | return dto; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return salesLedgerMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | | |
| | | @Override |
| | | public int insertSalesLedger(SalesLedger salesLedger) { |
| | | return salesLedgerMapper.insert(salesLedger); |
| | | public int addOrUpdateSalesLedger(SalesLedger salesLedger) { |
| | | LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(Customer::getId, salesLedger.getCustomerId()); |
| | | Customer customer = customerMapper.selectOne(queryWrapper); |
| | | if (customer == null) { |
| | | throw new BaseException("未查询到对应的 Customer 信息"); |
| | | } |
| | | salesLedger.setCustomerName(customer.getCustomerName()); |
| | | salesLedger.setTenantId(customer.getTenantId()); |
| | | return saveOrUpdates(salesLedger); |
| | | } |
| | | |
| | | @Override |
| | | public int updateSalesLedger(SalesLedger salesLedger) { |
| | | return salesLedgerMapper.updateById(salesLedger); |
| | | private int saveOrUpdates(SalesLedger salesLedger) { |
| | | if (salesLedger.getId() == null) { |
| | | return salesLedgerMapper.insert(salesLedger); |
| | | } else { |
| | | return salesLedgerMapper.updateById(salesLedger); |
| | | } |
| | | } |
| | | } |