diff --git a/src/main/java/com/xujie/modules/srm/controller/SrmGroupController.java b/src/main/java/com/xujie/modules/srm/controller/SrmGroupController.java new file mode 100644 index 0000000..eed4e32 --- /dev/null +++ b/src/main/java/com/xujie/modules/srm/controller/SrmGroupController.java @@ -0,0 +1,79 @@ +package com.xujie.modules.srm.controller; + +import com.xujie.modules.srm.entity.SrmGroup; +import com.xujie.modules.srm.service.SrmGroupService; +import com.xujie.common.utils.R; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/srmGroup") +public class SrmGroupController { + + @Autowired + private SrmGroupService srmGroupService; + + /** + * 获取供应商分组列表 + */ + @PostMapping("/getSupplierGroupList") + public R getSupplierGroupList(@RequestBody(required = false) Map params) { + if (params == null) { + params = new HashMap<>(); + } + + List groupList = srmGroupService.getSupplierGroupList(params); + return R.ok().put("data", groupList); + } + + /** + * 新增供应商分组 + */ + @PostMapping("/addSupplierGroup") + public R addSupplierGroup(@RequestBody SrmGroup group) { + srmGroupService.addSupplierGroup(group); + return R.ok(); + } + + /** + * 更新供应商分组 + */ + @PostMapping("/updateSupplierGroup") + public R updateSupplierGroup(@RequestBody SrmGroup group) { + srmGroupService.updateSupplierGroup(group); + return R.ok(); + } + + /** + * 删除供应商分组 + */ + @PostMapping("/deleteSupplierGroup") + public R deleteSupplierGroup(@RequestBody Map params) { + // 获取必要参数 + String supplierGroup = (String) params.get("supplierGroup"); + String site = (String) params.get("site"); + + // 参数校验 + if (supplierGroup == null || supplierGroup.trim().isEmpty()) { + return R.error("供应商分组代码不能为空"); + } + + if (site == null || site.trim().isEmpty()) { + return R.error("站点不能为空"); + } + + try { + // 执行删除 + srmGroupService.deleteSupplierGroupByCode(site, supplierGroup); + return R.ok("删除成功"); + } catch (RuntimeException e) { + return R.error(e.getMessage()); + } catch (Exception e) { + return R.error("删除失败:" + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/xujie/modules/srm/mapper/SrmGroupMapper.java b/src/main/java/com/xujie/modules/srm/mapper/SrmGroupMapper.java new file mode 100644 index 0000000..efb54cf --- /dev/null +++ b/src/main/java/com/xujie/modules/srm/mapper/SrmGroupMapper.java @@ -0,0 +1,23 @@ +package com.xujie.modules.srm.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.xujie.modules.srm.entity.SrmGroup; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +@Mapper +public interface SrmGroupMapper extends BaseMapper { + + //查询供应商分组列表 + List selectSupplierGroupList(@Param("params") Map params); + + // 检查供应商分组是否已存在 + int checkSupplierGroupExists(@Param("site") String site, @Param("supplierGroup") String supplierGroup); + + // 检查供应商分组名称是否已存在 + int checkSupplierGroupDescExists(@Param("site") String site, @Param("groupDesc") String groupDesc); + +} \ No newline at end of file diff --git a/src/main/java/com/xujie/modules/srm/service/SrmGroupService.java b/src/main/java/com/xujie/modules/srm/service/SrmGroupService.java new file mode 100644 index 0000000..f2bb7fb --- /dev/null +++ b/src/main/java/com/xujie/modules/srm/service/SrmGroupService.java @@ -0,0 +1,24 @@ +package com.xujie.modules.srm.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.xujie.modules.srm.entity.SrmGroup; +import com.xujie.common.utils.PageUtils; + +import java.util.List; +import java.util.Map; + +public interface SrmGroupService extends IService { + // 获取供应商分组列表 + List getSupplierGroupList(Map params); + + + // 新增供应商分组 + void addSupplierGroup(SrmGroup group); + + + // 更新供应商分组 + void updateSupplierGroup(SrmGroup group); + + // 删除供应商分组 + void deleteSupplierGroupByCode(String site, String supplierGroup); +} \ No newline at end of file diff --git a/src/main/java/com/xujie/modules/srm/service/impl/SrmGroupServiceImpl.java b/src/main/java/com/xujie/modules/srm/service/impl/SrmGroupServiceImpl.java new file mode 100644 index 0000000..cd7a96b --- /dev/null +++ b/src/main/java/com/xujie/modules/srm/service/impl/SrmGroupServiceImpl.java @@ -0,0 +1,178 @@ +package com.xujie.modules.srm.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.xujie.modules.srm.entity.SrmGroup; +import com.xujie.modules.srm.entity.SrmSupplier; +import com.xujie.modules.srm.mapper.SrmGroupMapper; +import com.xujie.modules.srm.service.SrmGroupService; +import com.xujie.modules.srm.service.SrmSupplierService; +import jakarta.transaction.Transactional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Service +public class SrmGroupServiceImpl extends ServiceImpl implements SrmGroupService { + + @Autowired + private SrmSupplierService srmSupplierService; + @Override + public List getSupplierGroupList(Map params) { + // 使用XML中的方法进行查询 + return baseMapper.selectSupplierGroupList(params); + } + + @Override + @Transactional + public void addSupplierGroup(SrmGroup group) { + // 检查必要参数是否存在 + if (group.getSupplierGroup() == null || group.getSupplierGroup().isEmpty()) { + throw new RuntimeException("供应商分组代码不能为空"); + } + + if (group.getSite() == null || group.getSite().isEmpty()) { + throw new RuntimeException("站点信息不能为空"); + } + + // 检查供应商分组代码是否已存在(基于站点和分组代码) + int count = baseMapper.checkSupplierGroupExists(group.getSite(), group.getSupplierGroup()); + if (count > 0) { + throw new RuntimeException("供应商分组代码已存在:" + group.getSupplierGroup() + ",请使用不同的分组代码"); + } + + // 如果提供了分组名称,检查分组名称是否已存在 + if (group.getGroupDesc() != null && !group.getGroupDesc().isEmpty()) { + int descCount = baseMapper.checkSupplierGroupDescExists(group.getSite(), group.getGroupDesc()); + if (descCount > 0) { + throw new RuntimeException("供应商分组名称已存在:" + group.getGroupDesc() + ",请使用不同的分组名称"); + } + } + + // 设置默认值 + if (group.getActive() == null || group.getActive().isEmpty()) { + group.setActive("Y"); // 默认激活 + } + + if (group.getGroupType() == null || group.getGroupType().isEmpty()) { + group.setGroupType("DEFAULT"); // 默认类型 + } + + // 保存供应商分组信息 + this.save(group); + } + + @Override + @Transactional + public void updateSupplierGroup(SrmGroup group) { + // 参数校验 + if (group == null) { + throw new RuntimeException("供应商分组信息不能为空"); + } + + if (group.getSite() == null || group.getSite().trim().isEmpty()) { + throw new RuntimeException("站点不能为空"); + } + + if (group.getSupplierGroup() == null || group.getSupplierGroup().trim().isEmpty()) { + throw new RuntimeException("供应商分组代码不能为空"); + } + + if (group.getGroupDesc() == null || group.getGroupDesc().trim().isEmpty()) { + throw new RuntimeException("供应商分组描述不能为空"); + } + + // 根据站点和分组代码检查分组是否存在 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("site", group.getSite()) + .eq("supplier_group", group.getSupplierGroup()); + + SrmGroup existingGroup = this.getOne(queryWrapper); + + // 如果分组不存在,无法修改 + if (existingGroup == null) { + throw new RuntimeException(String.format( + "供应商分组不存在:站点[%s],分组代码[%s]", + group.getSite(), group.getSupplierGroup() + )); + } + + // 检查新的分组描述是否与其他分组冲突(同一站点内) + if (!existingGroup.getGroupDesc().equals(group.getGroupDesc())) { + int count = baseMapper.checkSupplierGroupDescExists(group.getSite(), group.getGroupDesc()); + if (count > 0) { + throw new RuntimeException("供应商分组名称已存在:" + group.getGroupDesc() + ",请使用不同的分组名称"); + } + } + + // 分组存在,进行修改 + existingGroup.setGroupDesc(group.getGroupDesc()); + + if (group.getActive() != null) { + existingGroup.setActive(group.getActive()); + } + + if (group.getGroupType() != null) { + existingGroup.setGroupType(group.getGroupType()); + } + + // 执行更新 + this.updateById(existingGroup); + } + + @Override + @Transactional + public void deleteSupplierGroupByCode(String site, String supplierGroup) { + // 参数校验 + if (site == null || site.trim().isEmpty()) { + throw new RuntimeException("站点不能为空"); + } + + if (supplierGroup == null || supplierGroup.trim().isEmpty()) { + throw new RuntimeException("供应商分组代码不能为空"); + } + + // 1. 检查分组是否存在 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("site", site) + .eq("supplier_group", supplierGroup); + + SrmGroup group = this.getOne(queryWrapper); + if (group == null) { + throw new RuntimeException(String.format( + "供应商分组不存在:站点[%s],分组代码[%s]", site, supplierGroup + )); + } + + // 2. 检查是否有关联数据 + checkIfHasRelatedData(site, supplierGroup); + + // 3. 执行删除操作 + boolean result = this.remove(queryWrapper); + if (!result) { + throw new RuntimeException(String.format( + "删除供应商分组失败:站点[%s],分组代码[%s]", site, supplierGroup + )); + } + } + + // 检查是否有关联数据 + private void checkIfHasRelatedData(String site, String supplierGroup) { + // 检查供应商表是否有使用此分组的记录 + QueryWrapper supplierQuery = new QueryWrapper<>(); + supplierQuery.eq("site", site) + .eq("supplier_group", supplierGroup); + + // 获取关联供应商数量 + Long supplierCount = srmSupplierService.count(supplierQuery); + + if (supplierCount != null && supplierCount > 0) { + throw new RuntimeException(String.format( + "该分组已被 %d 个供应商使用,无法删除", supplierCount + )); + } + } + +} \ No newline at end of file