seatonwan9
2025-08-19 84c6e1df4b6bd48ee0517a33778b514008022875
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
package com.webmanage.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.webmanage.common.BusinessException;
import com.webmanage.emun.PriceTypeEnum;
import com.webmanage.entity.ProductPricing;
import com.webmanage.mapper.ProductPricingMapper;
import com.webmanage.service.ProductPricingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * 产品定价服务实现类
 * 
 * @author webmanage
 * @date 2024-08-07
 */
@Slf4j
@Service
public class ProductPricingServiceImpl extends ServiceImpl<ProductPricingMapper, ProductPricing> implements ProductPricingService {
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addProductPricing(ProductPricing productPricing) {
        try {
            // 设置创建时间
            productPricing.setCreatedAt(LocalDateTime.now());
            productPricing.setUpdatedAt(LocalDateTime.now());
            
            // 验证必填字段
            if (!StringUtils.hasText(productPricing.getSuiteName())) {
                throw new BusinessException("产品套件名称不能为空");
            }
            if (!StringUtils.hasText(productPricing.getSalesForm())) {
                throw new BusinessException("销售形式不能为空");
            }
            if (!StringUtils.hasText(productPricing.getCustomerType())) {
                throw new BusinessException("客户对象不能为空");
            }
            if (!StringUtils.hasText(productPricing.getPriceType())) {
                throw new BusinessException("价格设置不能为空");
            }
            if (productPricing.getPriceType().indexOf(PriceTypeEnum.POINTS.getName()) > -1
                    && productPricing.getPointsPrice() == null ||
                    productPricing.getPriceType().indexOf(PriceTypeEnum.POINTS.getName()) > -1
                            &&productPricing.getPointsPrice().doubleValue() < 0) {
                throw new BusinessException("积分价格值不能为空且不能为负数");
            }
            if (productPricing.getPriceType().indexOf(PriceTypeEnum.CURRENCY.getName()) > -1
                    && productPricing.getCurrencyPrice()== null ||
                    productPricing.getPriceType().indexOf(PriceTypeEnum.CURRENCY.getName()) > -1
                            && productPricing.getCurrencyPrice().doubleValue() < 0){
                throw new BusinessException("货币价格值不能为空且不能为负数");
            }
            if (productPricing.getProductId() == null) {
                throw new BusinessException("产品ID不能为空");
            }
            
            // 设置默认值
            if (productPricing.getIsActive() == null) {
                productPricing.setIsActive(true);
            }
            
            boolean result = save(productPricing);
            if (result) {
                log.info("新增产品定价成功: {}", productPricing.getSuiteName());
            }
            return result;
        } catch (Exception e) {
            log.error("新增产品定价失败: ", e);
            throw new BusinessException("新增产品定价失败: " + e.getMessage());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProductPricing(ProductPricing productPricing) {
        try {
            // 检查定价是否存在
            ProductPricing existingPricing = getById(productPricing.getId());
            if (existingPricing == null) {
                throw new BusinessException("产品定价不存在");
            }
            
            // 设置更新时间
            productPricing.setUpdatedAt(LocalDateTime.now());
            
            // 验证必填字段
            if (!StringUtils.hasText(productPricing.getSuiteName())) {
                throw new BusinessException("产品套件名称不能为空");
            }
            if (!StringUtils.hasText(productPricing.getSalesForm())) {
                throw new BusinessException("销售形式不能为空");
            }
            if (!StringUtils.hasText(productPricing.getCustomerType())) {
                throw new BusinessException("客户对象不能为空");
            }
            if (!StringUtils.hasText(productPricing.getPriceType())) {
                throw new BusinessException("价格设置不能为空");
            }
            if (productPricing.getPriceType().indexOf(PriceTypeEnum.POINTS.getName()) > -1
                    && productPricing.getPointsPrice() == null ||
                    productPricing.getPriceType().indexOf(PriceTypeEnum.POINTS.getName()) > -1
                            &&productPricing.getPointsPrice().doubleValue() < 0) {
                throw new BusinessException("积分价格值不能为空且不能为负数");
            }
            if (productPricing.getPriceType().indexOf(PriceTypeEnum.CURRENCY.getName()) > -1
                    && productPricing.getCurrencyPrice()== null ||
                    productPricing.getPriceType().indexOf(PriceTypeEnum.CURRENCY.getName()) > -1
                            && productPricing.getCurrencyPrice().doubleValue() < 0) {
                throw new BusinessException("货币价格值不能为空且不能为负数");
            }
            
            boolean result = updateById(productPricing);
            if (result) {
                log.info("更新产品定价成功: {}", productPricing.getSuiteName());
            }
            return result;
        } catch (Exception e) {
            log.error("更新产品定价失败: ", e);
            throw new BusinessException("更新产品定价失败: " + e.getMessage());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteProductPricing(Long id) {
        try {
            // 检查定价是否存在
            ProductPricing existingPricing = getById(id);
            if (existingPricing == null) {
                throw new BusinessException("产品定价不存在");
            }
            
            boolean result = removeById(id);
            if (result) {
                log.info("删除产品定价成功: {}", existingPricing.getSuiteName());
            }
            return result;
        } catch (Exception e) {
            log.error("删除产品定价失败: ", e);
            throw new BusinessException("删除产品定价失败: " + e.getMessage());
        }
    }
 
    @Override
    public IPage<ProductPricing> getProductPricingPage(Page<ProductPricing> page, Long productId) {
        try {
            return baseMapper.selectProductPricingPage(page, productId);
        } catch (Exception e) {
            log.error("分页查询产品定价失败: ", e);
            throw new BusinessException("分页查询产品定价失败: " + e.getMessage());
        }
    }
 
    @Override
    public List<ProductPricing> getPricingByProductId(Long productId) {
        try {
            if (productId == null) {
                throw new BusinessException("产品ID不能为空");
            }
            return baseMapper.selectByProductId(productId);
        } catch (Exception e) {
            log.error("根据产品ID查询定价失败: ", e);
            throw new BusinessException("根据产品ID查询定价失败: " + e.getMessage());
        }
    }
 
    @Override
    public List<ProductPricing> getPricingByCondition(String suiteName, String salesForm, 
                                                    String customerType, String priceType, Boolean isActive) {
        try {
            QueryWrapper<ProductPricing> queryWrapper = new QueryWrapper<>();
            
            if (StringUtils.hasText(suiteName)) {
                queryWrapper.like("suite_name", suiteName);
            }
            if (StringUtils.hasText(salesForm)) {
                queryWrapper.eq("sales_form", salesForm);
            }
            if (StringUtils.hasText(customerType)) {
                queryWrapper.eq("customer_type", customerType);
            }
            if (StringUtils.hasText(priceType)) {
                queryWrapper.eq("price_type", priceType);
            }
            if (isActive != null) {
                queryWrapper.eq("is_active", isActive);
            }
            
            queryWrapper.orderByDesc("created_at");
            
            return list(queryWrapper);
        } catch (Exception e) {
            log.error("根据条件查询产品定价失败: ", e);
            throw new BusinessException("根据条件查询产品定价失败: " + e.getMessage());
        }
    }
}