package com.ruoyi.business.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
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.entity.Customer;
|
import com.ruoyi.basic.mapper.CustomerMapper;
|
import com.ruoyi.business.dto.SalesRecordDto;
|
import com.ruoyi.business.entity.OfficialInventory;
|
import com.ruoyi.business.entity.SalesRecord;
|
import com.ruoyi.business.mapper.OfficialInventoryMapper;
|
import com.ruoyi.business.mapper.SalesRecordMapper;
|
import com.ruoyi.business.service.SalesRecordService;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.exception.base.BaseException;
|
import com.ruoyi.common.utils.bean.BeanUtils;
|
import com.ruoyi.system.mapper.SysUserMapper;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.time.LocalDate;
|
|
/**
|
* <p>
|
* 销售记录表 服务实现类
|
* </p>
|
*
|
* @author ruoyi
|
* @since 2025-06-11
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class SalesRecordServiceImpl extends ServiceImpl<SalesRecordMapper, SalesRecord> implements SalesRecordService {
|
|
private final SalesRecordMapper salesRecordMapper;
|
|
private final SysUserMapper userMapper;
|
|
private final CustomerMapper customerMapper;
|
|
private final OfficialInventoryMapper officialInventoryMapper;
|
|
@Override
|
public IPage<SalesRecord> selectSalesRecordList(Page page, SalesRecordDto salesRecordDto) {
|
LambdaQueryWrapper<SalesRecord> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.orderByDesc(SalesRecord::getCreateTime);
|
return salesRecordMapper.selectPage(page, queryWrapper);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int addOrEditSalesRecord(SalesRecordDto salesRecordDto) {
|
// 参数校验
|
validateSalesRecordDto(salesRecordDto);
|
|
// 更新正式库待补库数量
|
OfficialInventory officialInventory = officialInventoryMapper.selectById(salesRecordDto.getCoalId());
|
if (officialInventory == null) {
|
throw new BaseException("正式库煤种信息不存在");
|
}
|
if (salesRecordDto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0){
|
throw new BaseException("销售数量不能大于库存数量");
|
}
|
officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(salesRecordDto.getSaleQuantity()));
|
officialInventory.setPendingReplenishment(salesRecordDto.getSaleQuantity());
|
officialInventoryMapper.updateById(officialInventory);
|
|
// 构建销售记录实体
|
SalesRecord salesRecord = buildSalesRecord(salesRecordDto,officialInventory.getCoal());
|
|
// 处理新增/更新逻辑
|
if (salesRecordDto.getId() == null) {
|
return insertSalesRecord(salesRecord);
|
} else {
|
return updateSalesRecord(salesRecord);
|
}
|
}
|
|
private void validateSalesRecordDto(SalesRecordDto dto) {
|
if (dto == null) {
|
throw new BaseException("销售记录数据不能为空");
|
}
|
if (dto.getRegistrantId() == null) {
|
throw new BaseException("登记人ID不能为空");
|
}
|
if (dto.getCustomerId() == null) {
|
throw new BaseException("客户ID不能为空");
|
}
|
if (dto.getCoalId() == null) {
|
throw new BaseException("请选择一条煤种信息");
|
}
|
}
|
|
private SalesRecord buildSalesRecord(SalesRecordDto dto,String coal) {
|
SalesRecord record = new SalesRecord();
|
BeanUtils.copyProperties(dto, record);
|
|
// 设置登记人信息
|
SysUser registrant = userMapper.selectUserById(dto.getRegistrantId());
|
if (registrant == null) {
|
throw new BaseException("登记人信息不存在");
|
}
|
record.setRegistrant(registrant.getUserName());
|
|
// 设置客户信息
|
Customer customer = customerMapper.selectById(dto.getCustomerId());
|
if (customer == null) {
|
throw new BaseException("客户信息不存在");
|
}
|
record.setCustomer(customer.getCustomerName());
|
|
// 设置日期信息
|
LocalDate now = LocalDate.now();
|
if (record.getId() == null) {
|
// 新增时设置日期
|
record.setSaleDate(now);
|
record.setRegistrationDate(now);
|
} else {
|
// 更新时不覆盖原有日期
|
SalesRecord existing = salesRecordMapper.selectById(record.getId());
|
if (existing == null) {
|
throw new BaseException("销售记录不存在");
|
}
|
record.setSaleDate(existing.getSaleDate());
|
record.setRegistrationDate(existing.getRegistrationDate());
|
}
|
|
// 煤种
|
record.setCoal(coal);
|
|
return record;
|
}
|
|
private int insertSalesRecord(SalesRecord record) {
|
int result = salesRecordMapper.insert(record);
|
if (result <= 0) {
|
throw new BaseException("销售记录创建失败");
|
}
|
return result;
|
}
|
|
private int updateSalesRecord(SalesRecord record) {
|
int result = salesRecordMapper.updateById(record);
|
if (result <= 0) {
|
throw new BaseException("销售记录更新失败");
|
}
|
return result;
|
}
|
|
@Override
|
public int delByIds(Long[] ids) {
|
// 检查参数
|
if (ids == null || ids.length == 0) {
|
return 0;
|
}
|
// 构造更新条件
|
UpdateWrapper<SalesRecord> updateWrapper = new UpdateWrapper<>();
|
updateWrapper.in("id", ids)
|
.set("deleted", 1); // 设置 deleted 为 1 表示已删除
|
// 执行批量逻辑删除
|
return salesRecordMapper.update(null, updateWrapper);
|
}
|
}
|