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 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 getProductPricingPage(Page page, Long productId) { try { return baseMapper.selectProductPricingPage(page, productId); } catch (Exception e) { log.error("分页查询产品定价失败: ", e); throw new BusinessException("分页查询产品定价失败: " + e.getMessage()); } } @Override public List 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 getPricingByCondition(String suiteName, String salesForm, String customerType, String priceType, Boolean isActive) { try { QueryWrapper 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()); } } }