liding
3 天以前 18d292f1091b8b86ad29ba76e3cbe8ffaf2c222e
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
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.SalesRecord;
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;
 
    @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);
 
        // 构建销售记录实体
        SalesRecord salesRecord = buildSalesRecord(salesRecordDto);
 
        // 处理新增/更新逻辑
        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不能为空");
        }
    }
 
    private SalesRecord buildSalesRecord(SalesRecordDto dto) {
        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());
        }
 
        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);
    }
}