|
|
@@ -0,0 +1,155 @@
|
|
|
+package cn.iocoder.yudao.module.pms.service.qhse.soc;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.exception.ErrorCode;
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.qhse.soc.vo.IotSocSourcePageReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.qhse.soc.vo.IotSocSourceSaveReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.soc.IotSocSourceDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.mysql.qhse.soc.IotSocSourceMapper;
|
|
|
+import cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant;
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * SOC卡分析数据源 Service 实现类
|
|
|
+ *
|
|
|
+ * @author 超级管理员
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+public class IotSocSourceServiceImpl implements IotSocSourceService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IotSocSourceMapper iotSocSourceMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createIotSocSource(IotSocSourceSaveReqVO createReqVO) {
|
|
|
+ if (createReqVO.getParentId() == null) {
|
|
|
+ createReqVO.setParentId(IotProductClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentSoc(null, createReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateSocNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
|
|
+ // 插入
|
|
|
+ IotSocSourceDO iotSocSource = BeanUtils.toBean(createReqVO, IotSocSourceDO.class);
|
|
|
+ iotSocSource.setDeleted(false);
|
|
|
+ iotSocSourceMapper.insert(iotSocSource);
|
|
|
+ // 返回
|
|
|
+ return iotSocSource.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateIotSocSource(IotSocSourceSaveReqVO updateReqVO) {
|
|
|
+ if (updateReqVO.getParentId() == null) {
|
|
|
+ updateReqVO.setParentId(IotProductClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验存在
|
|
|
+ validateIotSocSourceExists(updateReqVO.getId());
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentSoc(updateReqVO.getId(), updateReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateSocNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
|
|
|
+ // 更新
|
|
|
+ IotSocSourceDO updateObj = BeanUtils.toBean(updateReqVO, IotSocSourceDO.class);
|
|
|
+ iotSocSourceMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteIotSocSource(Long id) {
|
|
|
+ // 校验存在
|
|
|
+ validateIotSocSourceExists(id);
|
|
|
+ // 校验是否有子产品分类
|
|
|
+ if (iotSocSourceMapper.selectCountByParentId(id) > 0) {
|
|
|
+ throw exception(ErrorCodeConstant.PRODUCT_EXITS_CHILDREN);
|
|
|
+ }
|
|
|
+ // 删除
|
|
|
+ iotSocSourceMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateIotSocSourceExists(Long id) {
|
|
|
+ if (iotSocSourceMapper.selectById(id) == null) {
|
|
|
+ throw exception(new ErrorCode(1,"不存在"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IotSocSourceDO getIotSocSource(Long id) {
|
|
|
+ return iotSocSourceMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<IotSocSourceDO> getIotSocSourcePage(IotSocSourcePageReqVO pageReqVO) {
|
|
|
+ return iotSocSourceMapper.selectPage(pageReqVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotSocSourceDO> getSocList(IotSocSourcePageReqVO reqVO) {
|
|
|
+ List<IotSocSourceDO> list = iotSocSourceMapper.selectList(reqVO);
|
|
|
+ list.sort(Comparator.comparing(IotSocSourceDO::getSort));
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateParentSoc(Long id, Long parentId) {
|
|
|
+ if (parentId == null || IotProductClassifyDO.PARENT_ID_ROOT.equals(parentId)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1. 不能设置自己为父产品分类
|
|
|
+ if (Objects.equals(id, parentId)) {
|
|
|
+ throw exception(DEPT_PARENT_ERROR);
|
|
|
+ }
|
|
|
+ // 2. 父产品分类不存在
|
|
|
+ IotSocSourceDO parentDept = iotSocSourceMapper.selectById(parentId);
|
|
|
+ if (parentDept == null) {
|
|
|
+ throw exception(DEPT_PARENT_NOT_EXITS);
|
|
|
+ }
|
|
|
+ // 3. 递归校验父产品分类,如果父产品分类是自己的子产品分类,则报错,避免形成环路
|
|
|
+ if (id == null) { // id 为空,说明新增,不需要考虑环路
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < Short.MAX_VALUE; i++) {
|
|
|
+ // 3.1 校验环路
|
|
|
+ parentId = parentDept.getParentId();
|
|
|
+ if (Objects.equals(id, parentId)) {
|
|
|
+ throw exception(DEPT_PARENT_IS_CHILD);
|
|
|
+ }
|
|
|
+ // 3.2 继续递归下一级父产品分类
|
|
|
+ if (parentId == null || IotProductClassifyDO.PARENT_ID_ROOT.equals(parentId)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ parentDept = iotSocSourceMapper.selectById(parentId);
|
|
|
+ if (parentDept == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateSocNameUnique(Long id, Long parentId, String name) {
|
|
|
+ IotSocSourceDO dept = iotSocSourceMapper.selectByParentIdAndName(parentId, name);
|
|
|
+ if (dept == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 如果 id 为空,说明不用比较是否为相同 id 的产品分类
|
|
|
+ if (id == null) {
|
|
|
+ throw exception(DEPT_NAME_DUPLICATE);
|
|
|
+ }
|
|
|
+ if (ObjectUtil.notEqual(dept.getId(), id)) {
|
|
|
+ throw exception(DEPT_NAME_DUPLICATE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|