liding
14 小时以前 4fedbed9949c6160dcfa216d6660bd3c625f7bce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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.CoalInfo;
import com.ruoyi.basic.entity.Customer;
import com.ruoyi.basic.mapper.CoalInfoMapper;
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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <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;
 
    private final CoalInfoMapper coalInfoMapper;
 
    @Override
    public IPage<SalesRecordDto> selectSalesRecordList(Page<SalesRecord> page, SalesRecordDto salesRecordDto) {
        // 1. 创建查询条件,按创建时间倒序排序
        LambdaQueryWrapper<SalesRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SalesRecord::getCreateTime);
 
        // 2. 获取分页的销售记录
        IPage<SalesRecord> salesRecordPage = salesRecordMapper.selectPage(page, queryWrapper);
 
        // 3. 批量查询所有CoalInfo
        List<Long> coalIds = salesRecordPage.getRecords().stream()
                .map(SalesRecord::getCoalId)  // 获取所有SalesRecord的coalId
                .collect(Collectors.toList());
        Map<Long, CoalInfo> coalInfoMap;
 
        // 如果有煤炭ID,执行批量查询
        if (!coalIds.isEmpty()) {
            // 使用selectList进行批量查询
            LambdaQueryWrapper<CoalInfo> coalInfoQueryWrapper = new LambdaQueryWrapper<>();
            coalInfoQueryWrapper.in(CoalInfo::getId, coalIds);
            List<CoalInfo> coalInfos = coalInfoMapper.selectList(coalInfoQueryWrapper);
 
            // 将查询结果放入Map中,煤炭ID为键,CoalInfo为值
            coalInfoMap = coalInfos.stream()
                    .collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
        } else {
            coalInfoMap = new HashMap<>();
        }
 
        // 4. 创建返回结果页,使用BeanUtils进行属性复制
        Page<SalesRecordDto> resultPage = new Page<>();
        BeanUtils.copyProperties(salesRecordPage, resultPage);  // 复制分页信息
 
        // 5. 转换SalesRecord为SalesRecordDto,并设置每条销售记录的煤炭信息
        List<SalesRecordDto> dtoList = salesRecordPage.getRecords().stream().map(salesRecord -> {
            SalesRecordDto dto = new SalesRecordDto();
            BeanUtils.copyProperties(salesRecord, dto);  // 将SalesRecord的属性复制到SalesRecordDto中
 
            // 设置Coal信息
            CoalInfo coalInfo = coalInfoMap.get(salesRecord.getCoalId());
            if (coalInfo != null) {
                dto.setCoal(coalInfo.getCoal());
            }
 
            return dto;
        }).collect(Collectors.toList());
 
        resultPage.setRecords(dtoList);  // 设置转换后的DTO列表
 
        return resultPage;
    }
 
 
    @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.getCoalId());
 
        // 处理新增/更新逻辑
        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, Long coalId) {
        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.setCoalId(coalId);
 
        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);
    }
}