| New file |
| | |
| | | package com.ruoyi.web.controller.product; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.ruoyi.common.core.controller.BaseController; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.im.util.ConverterUtil; |
| | | import com.ruoyi.system.domain.GroupWelcomeConfig; |
| | | import com.ruoyi.system.domain.InsuranceFeature; |
| | | import com.ruoyi.system.domain.InsuranceProduct; |
| | | import com.ruoyi.system.domain.IpBlacklist; |
| | | import com.ruoyi.system.domain.dto.InsuranceProductUpdateDTO; |
| | | import com.ruoyi.system.service.InsuranceFeatureService; |
| | | import com.ruoyi.system.service.InsuranceProductService; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.validation.Valid; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.ruoyi.common.utils.PageUtils.startPage; |
| | | |
| | | /** |
| | | * @program: ruoyiim |
| | | * @description:保险产品 |
| | | * @create: 2025-09-17 17:17 |
| | | **/ |
| | | @RestController |
| | | @RequestMapping("/product") |
| | | public class InsuranceProductController extends BaseController { |
| | | |
| | | @Autowired |
| | | InsuranceProductService insuranceProductService; |
| | | |
| | | @Autowired |
| | | InsuranceFeatureService insuranceFeatureService; |
| | | |
| | | /** |
| | | * 修改产品信息(包含产品特色) |
| | | */ |
| | | @PostMapping("/update") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult update(@Valid @RequestBody InsuranceProductUpdateDTO dto) { |
| | | // 1. 查询产品是否存在 |
| | | InsuranceProduct insuranceProduct = insuranceProductService.getById(dto.getId()); |
| | | if (insuranceProduct == null) { |
| | | return AjaxResult.error("产品不存在"); |
| | | } |
| | | |
| | | // 2. 更新产品基本信息 |
| | | insuranceProduct.setProductName(dto.getProductName()); |
| | | insuranceProduct.setProductCode(dto.getProductCode()); |
| | | insuranceProduct.setDescription(dto.getDescription()); |
| | | insuranceProduct.setCoverageAmount(dto.getCoverageAmount()); |
| | | insuranceProduct.setPremium(dto.getPremium()); |
| | | insuranceProduct.setTerm(dto.getTerm()); |
| | | |
| | | // 处理枚举状态 |
| | | if (dto.getStatus() != null) { |
| | | try { |
| | | InsuranceProduct.ProductStatus status = |
| | | InsuranceProduct.ProductStatus.valueOf(dto.getStatus().toUpperCase()); |
| | | insuranceProduct.setStatus(status); |
| | | } catch (IllegalArgumentException e) { |
| | | return AjaxResult.error("产品状态参数错误"); |
| | | } |
| | | } |
| | | |
| | | insuranceProduct.setHospitalNumber(dto.getHospitalNumber()); |
| | | insuranceProduct.setGuaranteeExplanation(dto.getGuaranteeExplanation()); |
| | | insuranceProduct.setInsureAge(dto.getInsureAge()); |
| | | insuranceProduct.setObligationDisclose(dto.getObligationDisclose()); |
| | | insuranceProduct.setProductFeature(dto.getProductFeature()); |
| | | insuranceProduct.setLiabilityExemption(dto.getLiabilityExemption()); |
| | | insuranceProduct.setImg1(dto.getImg1()); |
| | | insuranceProduct.setImg2(dto.getImg2()); |
| | | |
| | | // 3. 更新产品信息 |
| | | boolean productSuccess = insuranceProductService.updateById(insuranceProduct); |
| | | |
| | | if (!productSuccess) { |
| | | return AjaxResult.error("产品信息更新失败"); |
| | | } |
| | | |
| | | // 4. 处理产品特色 |
| | | if (dto.getFeatureDtoList() != null && !dto.getFeatureDtoList().isEmpty()) { |
| | | try { |
| | | // 先删除该产品原有的所有特色 |
| | | deleteByProductId(insuranceProduct.getId()); |
| | | |
| | | // 转换DTO为实体并设置产品ID |
| | | List<InsuranceFeature> features = dto.getFeatureDtoList().stream() |
| | | .map(f -> { |
| | | InsuranceFeature insuranceFeature = new InsuranceFeature(); |
| | | insuranceFeature.setProductId(insuranceProduct.getId()); |
| | | insuranceFeature.setTitle(f.getTitle()); |
| | | insuranceFeature.setDetail(f.getDetail()); |
| | | insuranceFeature.setCreatedAt(LocalDateTime.now()); |
| | | insuranceFeature.setUpdatedAt(LocalDateTime.now()); |
| | | return insuranceFeature; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量保存新的产品特色 |
| | | boolean featureSuccess = insuranceFeatureService.saveBatch(features); |
| | | |
| | | if (!featureSuccess) { |
| | | throw new RuntimeException("产品特色保存失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | // 事务会回滚 |
| | | return AjaxResult.error("产品特色处理失败: " + e.getMessage()); |
| | | } |
| | | } else { |
| | | // 如果传入的特色列表为空,则删除所有特色 |
| | | deleteByProductId(insuranceProduct.getId()); |
| | | } |
| | | |
| | | return AjaxResult.success("保存成功!"); |
| | | } |
| | | |
| | | public void deleteByProductId(Integer productId) { |
| | | LambdaQueryWrapper<InsuranceFeature> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(InsuranceFeature::getProductId, productId); |
| | | insuranceFeatureService.remove(wrapper); |
| | | } |
| | | |
| | | /** |
| | | * 查询产品信息 |
| | | */ |
| | | @GetMapping("/get") |
| | | public AjaxResult get(@RequestParam(value = "id") Integer id) { |
| | | InsuranceProduct insuranceProduct = insuranceProductService.getById(id); |
| | | return AjaxResult.success(insuranceProduct); |
| | | } |
| | | |
| | | /** |
| | | * 产品信息列表 |
| | | */ |
| | | @GetMapping("/list") |
| | | public TableDataInfo list(InsuranceProductUpdateDTO dto) { |
| | | startPage(); |
| | | |
| | | LambdaQueryWrapper<InsuranceProduct> wrapper = new LambdaQueryWrapper<>(); |
| | | |
| | | // 产品名称模糊查询 |
| | | if (StringUtils.isNotBlank(dto.getProductName())) { |
| | | wrapper.like(InsuranceProduct::getProductName, dto.getProductName()); |
| | | } |
| | | |
| | | // 产品代码模糊查询 |
| | | if (StringUtils.isNotBlank(dto.getProductCode())) { |
| | | wrapper.like(InsuranceProduct::getProductCode, dto.getProductCode()); |
| | | } |
| | | |
| | | // 产品状态精确查询 |
| | | if (dto.getStatus() != null) { |
| | | wrapper.eq(InsuranceProduct::getStatus, dto.getStatus()); |
| | | } |
| | | |
| | | // 按创建时间倒序排列 |
| | | wrapper.orderByDesc(InsuranceProduct::getCreatedAt); |
| | | |
| | | List<InsuranceProduct> list = insuranceProductService.list(wrapper); |
| | | return getDataTable(list); |
| | | } |
| | | |
| | | /** |
| | | * 新增产品(包含产品特色) |
| | | */ |
| | | @PostMapping("/create") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult create(@Valid @RequestBody InsuranceProductUpdateDTO dto) { |
| | | // 1. 检查产品代码是否已存在 |
| | | long count = insuranceProductService.count(new LambdaQueryWrapper<InsuranceProduct>().eq(InsuranceProduct::getProductCode, dto.getProductCode())); |
| | | if (count > 0) { |
| | | return AjaxResult.error("产品代码已存在"); |
| | | } |
| | | |
| | | // 2. 创建产品基本信息 |
| | | InsuranceProduct product = new InsuranceProduct(); |
| | | product.setProductName(dto.getProductName()); |
| | | product.setProductCode(dto.getProductCode()); |
| | | product.setDescription(dto.getDescription()); |
| | | product.setCoverageAmount(dto.getCoverageAmount()); |
| | | product.setPremium(dto.getPremium()); |
| | | product.setTerm(dto.getTerm()); |
| | | |
| | | // 处理状态 |
| | | if (dto.getStatus() != null) { |
| | | try { |
| | | InsuranceProduct.ProductStatus status = |
| | | InsuranceProduct.ProductStatus.valueOf(dto.getStatus().toUpperCase()); |
| | | product.setStatus(status); |
| | | } catch (IllegalArgumentException e) { |
| | | return AjaxResult.error("产品状态参数错误"); |
| | | } |
| | | } else { |
| | | product.setStatus(InsuranceProduct.ProductStatus.INACTIVE); // 默认下架 |
| | | } |
| | | |
| | | product.setHospitalNumber(dto.getHospitalNumber()); |
| | | product.setGuaranteeExplanation(dto.getGuaranteeExplanation()); |
| | | product.setInsureAge(dto.getInsureAge()); |
| | | product.setObligationDisclose(dto.getObligationDisclose()); |
| | | product.setProductFeature(dto.getProductFeature()); |
| | | product.setLiabilityExemption(dto.getLiabilityExemption()); |
| | | product.setImg1(dto.getImg1()); |
| | | product.setImg2(dto.getImg2()); |
| | | |
| | | // 设置创建时间和更新时间 |
| | | product.setCreatedAt(LocalDateTime.now()); |
| | | product.setUpdatedAt(LocalDateTime.now()); |
| | | |
| | | // 3. 保存产品信息 |
| | | boolean productSuccess = insuranceProductService.save(product); |
| | | |
| | | if (!productSuccess) { |
| | | return AjaxResult.error("产品创建失败"); |
| | | } |
| | | |
| | | // 4. 处理产品特色 |
| | | if (dto.getFeatureDtoList() != null && !dto.getFeatureDtoList().isEmpty()) { |
| | | try { |
| | | // 转换DTO为实体并设置产品ID |
| | | List<InsuranceFeature> features = dto.getFeatureDtoList().stream() |
| | | .map(f -> { |
| | | InsuranceFeature insuranceFeature = new InsuranceFeature(); |
| | | insuranceFeature.setProductId(product.getId()); |
| | | insuranceFeature.setTitle(f.getTitle()); |
| | | insuranceFeature.setDetail(f.getDetail()); |
| | | insuranceFeature.setCreatedAt(LocalDateTime.now()); |
| | | insuranceFeature.setUpdatedAt(LocalDateTime.now()); |
| | | return insuranceFeature; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量保存产品特色 |
| | | boolean featureSuccess = insuranceFeatureService.saveBatch(features); |
| | | |
| | | if (!featureSuccess) { |
| | | throw new RuntimeException("产品特色保存失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | // 事务会回滚 |
| | | return AjaxResult.error("产品特色处理失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | return AjaxResult.success("创建成功"); |
| | | } |
| | | |
| | | /** |
| | | * 删除产品 |
| | | */ |
| | | @DeleteMapping("/{id}") |
| | | public AjaxResult delete(@PathVariable Integer id) { |
| | | InsuranceProduct product = insuranceProductService.getById(id); |
| | | if (product == null) { |
| | | return AjaxResult.error("产品不存在"); |
| | | } |
| | | |
| | | boolean success = insuranceProductService.removeById(id); |
| | | return success ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败"); |
| | | } |
| | | } |