Jelajahi Sumber

Merge remote-tracking branch 'origin/main'

classic_blue 2 minggu lalu
induk
melakukan
9097c198bf

+ 39 - 1
fuintBackend/fuint-application/src/main/java/com/fuint/common/config/MybatisPlusConfig.java

@@ -1,14 +1,21 @@
 package com.fuint.common.config;
 
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
 import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
 import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
+import com.fuint.common.dto.AccountInfo;
+import com.fuint.common.util.AuthUserUtil;
+import org.apache.ibatis.reflection.MetaObject;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
+import java.util.Date;
+import java.util.Objects;
+
 /**
  * MybatisPlus配置
  *
@@ -17,7 +24,7 @@ import org.springframework.context.annotation.Configuration;
  */
 @Configuration
 @MapperScan({"com.fuint.repository.**.mapper"})
-public class MybatisPlusConfig {
+public class MybatisPlusConfig implements MetaObjectHandler {
 
     /**
      * 分页插件
@@ -48,4 +55,35 @@ public class MybatisPlusConfig {
     public OptimisticLockerInterceptor optimisticLockerInterceptor() {
         return new OptimisticLockerInterceptor();
     }
+
+    /**
+     * 插入拦截
+     * @param metaObject
+     */
+    @Override
+    public void insertFill(MetaObject metaObject) {
+        this.setFieldValByName("createTime",new Date(),metaObject);
+        this.setFieldValByName("updateTime",new Date(),metaObject);
+        AccountInfo accountInfo = AuthUserUtil.get();
+        if (Objects.nonNull(accountInfo)){
+            this.setFieldValByName("createUserId",new Date(),metaObject);
+            this.setFieldValByName("updateUserId",new Date(),metaObject);
+        }
+
+    }
+
+    /**
+     * 更新数据时拦截
+     * @param metaObject
+     */
+    @Override
+    public void updateFill(MetaObject metaObject) {
+        this.setFieldValByName("updateTime",new Date(),metaObject);
+        AccountInfo accountInfo = AuthUserUtil.get();
+        if (Objects.nonNull(accountInfo)){
+            this.setFieldValByName("updateUserId",new Date(),metaObject);
+        }
+    }
+
+
 }

+ 48 - 0
fuintBackend/fuint-application/src/main/java/com/fuint/common/dto/ext/CountryDto.java

@@ -0,0 +1,48 @@
+package com.fuint.common.dto.ext;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+@Data
+public class CountryDto {
+
+	@ApiModelProperty("主键")
+	@TableId(value = "ID", type = IdType.AUTO)
+	private Long id;
+
+	@ApiModelProperty("区号")
+	private String areaCode;
+
+	@ApiModelProperty("国家代码")
+	private String countryCode;
+
+	@ApiModelProperty("国家/洲名称")
+	private String countryName;
+
+	@ApiModelProperty("英文名称")
+	private String englishName;
+
+	@ApiModelProperty("文件/图片id")
+	private String fileId;
+
+	@ApiModelProperty("拼音首字母")
+	private String firstCode;
+
+	@ApiModelProperty("所属洲")
+	private Long parentId;
+
+	@ApiModelProperty("拼音")
+	private String pinYin;
+
+	@ApiModelProperty("服务状态,默认0,-0未开通,-1开通")
+	private Integer serveState;
+
+	@ApiModelProperty("服务费比率")
+	private BigDecimal serviceChargeValue;
+
+}

+ 11 - 2
fuintBackend/fuint-application/src/main/java/com/fuint/common/service/CountryService.java

@@ -1,6 +1,7 @@
 package com.fuint.common.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.fuint.common.dto.ext.CountryDto;
 import com.fuint.framework.pagination.PaginationRequest;
 import com.fuint.framework.pagination.PaginationResponse;
 import com.fuint.repository.model.MtCountry;
@@ -54,11 +55,11 @@ public interface CountryService extends IService<MtCountry> {
 
     /**
      * 更新国家表
-     * @param  mtCountry
+     * @param  countryDto
      * @throws BusinessCheckException
      * @return
      * */
-    MtCountry updateCountry(MtCountry mtCountry) throws BusinessCheckException;
+    boolean updateCountry(CountryDto countryDto,Long accountId) throws BusinessCheckException;
 
     /**
      * 根据条件搜索国家表
@@ -68,4 +69,12 @@ public interface CountryService extends IService<MtCountry> {
      * @return
      * */
     List<MtCountry> queryCountryListByParams(Map<String, Object> params) throws BusinessCheckException;
+
+    /**
+     * 新增国家
+     * @param countryDto
+     * @param accountInfoId
+     * @return
+     */
+	boolean saveCountry(CountryDto countryDto, Long accountInfoId) throws BusinessCheckException;
 }

+ 63 - 9
fuintBackend/fuint-application/src/main/java/com/fuint/common/service/impl/CountryServiceImpl.java

@@ -3,8 +3,10 @@ package com.fuint.common.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fuint.common.dto.ext.CountryDto;
 import com.fuint.framework.annoation.OperationServiceLog;
 import com.fuint.framework.exception.BusinessCheckException;
+import com.fuint.framework.exception.BusinessRuntimeException;
 import com.fuint.framework.pagination.PaginationRequest;
 import com.fuint.framework.pagination.PaginationResponse;
 import com.fuint.repository.model.MtCountry;
@@ -17,6 +19,7 @@ import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import com.github.pagehelper.Page;
+import org.springframework.beans.BeanUtils;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.stereotype.Service;
@@ -114,24 +117,57 @@ public class CountryServiceImpl extends ServiceImpl<MtCountryMapper, MtCountry>
     /**
      * 修改国家表数据
      *
-     * @param mtCountry
+     * @param countryDto
      * @throws BusinessCheckException
      * @return
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
     @OperationServiceLog(description = "更新国家表")
-    public MtCountry updateCountry(MtCountry mtCountry) throws BusinessCheckException {
-        mtCountry = queryCountryById(mtCountry.getId());
-        if (mtCountry == null) {
-            throw new BusinessCheckException("该国家表状态异常");
+    public boolean updateCountry(CountryDto countryDto, Long accountId) throws BusinessCheckException {
+        // 1. 获取原数据
+        MtCountry mtCountry = Optional.ofNullable(this.getById(countryDto.getId())).orElseThrow(() ->
+                new BusinessRuntimeException("目标记录不存在"));
+        // 2. 校验父子关系变更
+        validateParentChange(countryDto.getId(), countryDto.getParentId());
+
+        // 3. 构建更新对象
+        MtCountry country = new MtCountry();
+        BeanUtils.copyProperties(countryDto, country);
+        return this.updateById(country);
+    }
+
+    private void validateParentChange(Long currentId, Long newParentId) throws BusinessCheckException {
+        // 禁止设置自己为父节点
+        if (currentId.equals(newParentId)) {
+            throw new BusinessCheckException("禁止设置自己为父节点");
         }
-        mtCountry.setUpdateTime(new Date());
-        mtCountryMapper.updateById(mtCountry);
-        return mtCountry;
+
+        // 检查新父节点是否存在
+        if (newParentId != 0L) { // 0表示根节点
+            int parentCount = this.baseMapper.checkParentExist(newParentId);
+            if (parentCount == 0) {
+                throw new BusinessCheckException("新父节点不存在或已被删除");
+            }
+        }
+        // 检查是否形成循环依赖(递归校验)
+        checkCircularDependency(currentId, newParentId);
     }
 
-   /**
+    private void checkCircularDependency(Long currentId, Long checkParentId) throws BusinessCheckException {
+        if (checkParentId == 0L) return; // 根节点无需检查
+
+        MtCountry parent = this.getById(checkParentId);
+        if (parent == null) return;
+
+        if (currentId.equals(parent.getParentId())) {
+            throw new BusinessCheckException("检测到循环依赖");
+        }
+
+        // 递归检查父节点的父节点
+        checkCircularDependency(currentId, parent.getParentId());
+    }
+    /**
     * 根据条件搜索国家表
     *
     * @param  params 查询参数
@@ -150,4 +186,22 @@ public class CountryServiceImpl extends ServiceImpl<MtCountryMapper, MtCountry>
         List<MtCountry> dataList = mtCountryMapper.selectList(lambdaQueryWrapper);
         return dataList;
     }
+
+    @Override
+    public boolean saveCountry(CountryDto countryDto, Long accountInfoId) throws BusinessCheckException {
+        // 1. 校验父节点
+        if (countryDto.getParentId() != null && countryDto.getParentId() != 0L) {
+            int parentCount = this.baseMapper.checkParentExist(countryDto.getParentId());
+            if (parentCount == 0) {
+                throw new BusinessCheckException("父节点不存在或已被删除");
+            }
+        }
+        // 2. 构建实体
+        MtCountry country = new MtCountry();
+        BeanUtils.copyProperties(countryDto, country);
+        country.setServeState(countryDto.getServeState() != null ? countryDto.getServeState() : 0);
+        country.setDeleteFlag(0);
+        //保存数据
+        return this.save(country);
+    }
 }

+ 20 - 9
fuintBackend/fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendCountryController.java

@@ -1,6 +1,9 @@
 package com.fuint.module.backendApi.controller;
 
+import com.baomidou.mybatisplus.extension.api.R;
 import com.fuint.common.dto.AccountInfo;
+import com.fuint.common.dto.ext.CountryDto;
+import com.fuint.common.util.AuthUserUtil;
 import com.fuint.common.util.TokenUtil;
 import com.fuint.framework.web.BaseController;
 import com.fuint.framework.web.ResponseObject;
@@ -58,28 +61,36 @@ public class BackendCountryController extends BaseController {
      * @return
      */
     @ApiOperation(value = "更新国家表状态")
-    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
+    @RequestMapping(value = "/updateCountry", method = RequestMethod.POST)
     @CrossOrigin
     @PreAuthorize("@pms.hasPermission('country:edit')")
-    public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
-
-
-        return getSuccessResult(true);
+    public ResponseObject updateCountry(CountryDto countryDto) throws BusinessCheckException {
+        AccountInfo accountInfo = AuthUserUtil.get();
+        if (accountInfo == null) {
+            return getFailureResult(1001, "请先登录");
+        }
+        Long accountInfoId = accountInfo.getId();
+        boolean result = countryService.updateCountry(countryDto,accountInfoId);
+        return result ?getSuccessResult(true):getFailureResult(1002,"更新失败");
     }
 
     /**
      * 保存国家表
      *
-     * @param request HttpServletRequest对象
      * @return
      */
     @ApiOperation(value = "保存国家表")
     @RequestMapping(value = "/save", method = RequestMethod.POST)
     @CrossOrigin
     @PreAuthorize("@pms.hasPermission('country:add')")
-    public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
-
-        return getSuccessResult(true);
+    public ResponseObject saveHandler(@RequestBody CountryDto countryDto) throws BusinessCheckException {
+        AccountInfo accountInfo = AuthUserUtil.get();
+        if (accountInfo == null) {
+            return getFailureResult(1001, "请先登录");
+        }
+        Long accountInfoId = accountInfo.getId();
+        boolean result = countryService.saveCountry(countryDto,accountInfoId);
+        return result?getSuccessResult(true):getFailureResult(1002,"保持失败");
     }
 
     /**

+ 3 - 1
fuintBackend/fuint-repository/src/main/java/com/fuint/repository/mapper/MtCountryMapper.java

@@ -2,6 +2,7 @@ package com.fuint.repository.mapper;
 
 import com.fuint.repository.model.MtCountry;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * 国家表 Mapper 接口
@@ -11,4 +12,5 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  */
 public interface MtCountryMapper extends BaseMapper<MtCountry> {
 
-}
+	int checkParentExist(@Param("parentId") Long parentId);
+}

+ 7 - 3
fuintBackend/fuint-repository/src/main/java/com/fuint/repository/model/MtCountry.java

@@ -1,8 +1,7 @@
 package com.fuint.repository.model;
 
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.*;
+
 import java.io.Serializable;
 import java.math.BigDecimal;
 import java.util.Date;
@@ -34,13 +33,16 @@ public class MtCountry implements Serializable {
     @ApiModelProperty("国家/洲名称")
     private String countryName;
 
+    @TableField(fill = FieldFill.INSERT)
     @ApiModelProperty("创建时间")
     private Date createTime;
 
+    @TableField(fill = FieldFill.INSERT)
     @ApiModelProperty("创建用户id")
     private String createUserId;
 
     @ApiModelProperty("是否删除 0否 1是")
+    @TableLogic
     private Integer deleteFlag;
 
     @ApiModelProperty("英文名称")
@@ -71,9 +73,11 @@ public class MtCountry implements Serializable {
     @ApiModelProperty("服务费比率")
     private BigDecimal serviceChargeValue;
 
+    @TableField(fill = FieldFill.INSERT_UPDATE)
     @ApiModelProperty("更新时间")
     private Date updateTime;
 
+    @TableField(fill = FieldFill.INSERT_UPDATE)
     @ApiModelProperty("修改用户id")
     private String updateUserId;
 

+ 6 - 4
fuintBackend/fuint-repository/src/main/resources/mapper/MtCountryMapper.xml

@@ -1,5 +1,7 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="com.fuint.repository.mapper.MtCountryMapper}">
-
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.fuint.repository.mapper.MtCountryMapper">
+    <select id="checkParentExist" resultType="java.lang.Integer">
+        SELECT COUNT(1) FROM mt_country WHERE id = #{parentId} AND delete_flag = 0
+    </select>
 </mapper>