- 后端:重构 oauth2 模块,方便后续 User 接入。

- 后端:重写 Admin 安全拦截器,实现类似 Shiro 的效果。
This commit is contained in:
YunaiV 2019-05-16 20:24:31 +08:00
parent b14169a747
commit aa8917f69a
18 changed files with 100 additions and 261 deletions

View File

@ -2,7 +2,6 @@ package cn.iocoder.mall.admin.application;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication(scanBasePackages = {"cn.iocoder.mall.admin"}) @SpringBootApplication(scanBasePackages = {"cn.iocoder.mall.admin"})
@ -10,14 +9,7 @@ import org.springframework.scheduling.annotation.EnableAsync;
public class SystemApplication { public class SystemApplication {
public static void main(String[] args) { public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(SystemApplication.class, args); SpringApplication.run(SystemApplication.class, args);
// Object bean = ctx.getBean("test");
// System.out.println(AopUtils.getTargetClass(bean));
// System.out.println(bean);
// ConfigurableApplicationContext ctx =
// System.out.println(); // TODO 后面去掉这里是临时的
} }
} }

View File

@ -7,14 +7,12 @@ import cn.iocoder.mall.admin.api.dto.datadict.DataDictAddDTO;
import cn.iocoder.mall.admin.api.dto.datadict.DataDictUpdateDTO; import cn.iocoder.mall.admin.api.dto.datadict.DataDictUpdateDTO;
import cn.iocoder.mall.admin.application.convert.DataDictConvert; import cn.iocoder.mall.admin.application.convert.DataDictConvert;
import cn.iocoder.mall.admin.application.vo.datadict.DataDictEnumVO; import cn.iocoder.mall.admin.application.vo.datadict.DataDictEnumVO;
import cn.iocoder.mall.admin.application.vo.datadict.DataDictVO;
import cn.iocoder.mall.admin.sdk.annotation.RequiresPermissions; import cn.iocoder.mall.admin.sdk.annotation.RequiresPermissions;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -22,6 +20,8 @@ import org.springframework.web.bind.annotation.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@RestController @RestController
@RequestMapping("admins/data_dict") @RequestMapping("admins/data_dict")
@Api("数据字典模块") @Api("数据字典模块")
@ -33,9 +33,8 @@ public class DataDictController {
@GetMapping("/list") @GetMapping("/list")
@ApiOperation(value = "数据字典全列表") @ApiOperation(value = "数据字典全列表")
@RequiresPermissions("system.dataDict.list") @RequiresPermissions("system.dataDict.list")
public CommonResult<List<DataDictVO>> list() { public CommonResult<List<DataDictBO>> list() {
CommonResult<List<DataDictBO>> result = dataDictService.selectDataDictList(); return success( dataDictService.selectDataDictList());
return DataDictConvert.INSTANCE.convert(result);
} }
@GetMapping("/tree") @GetMapping("/tree")
@ -43,12 +42,9 @@ public class DataDictController {
@ApiOperation(value = "数据字典树结构", notes = "该接口返回的信息更为精简。一般用于前端缓存数据字典到本地。") @ApiOperation(value = "数据字典树结构", notes = "该接口返回的信息更为精简。一般用于前端缓存数据字典到本地。")
public CommonResult<List<DataDictEnumVO>> tree() { public CommonResult<List<DataDictEnumVO>> tree() {
// 查询数据字典全列表 // 查询数据字典全列表
CommonResult<List<DataDictBO>> result = dataDictService.selectDataDictList(); List<DataDictBO> dataDicts = dataDictService.selectDataDictList();
if (result.isError()) {
return CommonResult.error(result);
}
// 构建基于 enumValue 聚合的 Multimap // 构建基于 enumValue 聚合的 Multimap
ImmutableListMultimap<String, DataDictBO> dataDictMap = Multimaps.index(result.getData(), DataDictBO::getEnumValue); // KEY enumValue VALUE DataDictBO 数组 ImmutableListMultimap<String, DataDictBO> dataDictMap = Multimaps.index(dataDicts, DataDictBO::getEnumValue); // KEY enumValue VALUE DataDictBO 数组
// 构建返回结果 // 构建返回结果
List<DataDictEnumVO> dataDictEnumVOs = new ArrayList<>(dataDictMap.size()); List<DataDictEnumVO> dataDictEnumVOs = new ArrayList<>(dataDictMap.size());
dataDictMap.keys().forEach(enumValue -> { dataDictMap.keys().forEach(enumValue -> {
@ -56,53 +52,21 @@ public class DataDictController {
.setValues(DataDictConvert.INSTANCE.convert2(dataDictMap.get(enumValue))); .setValues(DataDictConvert.INSTANCE.convert2(dataDictMap.get(enumValue)));
dataDictEnumVOs.add(dataDictEnumVO); dataDictEnumVOs.add(dataDictEnumVO);
}); });
return CommonResult.success(dataDictEnumVOs); return success(dataDictEnumVOs);
} }
@PostMapping("/add") @PostMapping("/add")
@RequiresPermissions("system.dataDict.add") @RequiresPermissions("system.dataDict.add")
@ApiOperation(value = "创建数据字典") @ApiOperation(value = "创建数据字典")
@ApiImplicitParams({ public CommonResult<DataDictBO> add(DataDictAddDTO dataDictAddDTO) {
@ApiImplicitParam(name = "enumValue", value = "大类枚举值", required = true, example = "gender"), return success(dataDictService.addDataDict(AdminSecurityContextHolder.getContext().getAdminId(), dataDictAddDTO));
@ApiImplicitParam(name = "value", value = "小类数值", required = true, example = "1"),
@ApiImplicitParam(name = "displayName", value = "展示名", required = true, example = ""),
@ApiImplicitParam(name = "sort", required = true, value = "排序值", defaultValue = "10"),
@ApiImplicitParam(name = "memo", value = "备注", example = "你猜我猜不猜"),
})
public CommonResult<DataDictVO> add(@RequestParam("enumValue") String enumValue,
@RequestParam("value") String value,
@RequestParam("displayName") String displayName,
@RequestParam("sort") Integer sort,
@RequestParam(value = "memo", required = false) String memo) {
// 创建 DataDictAddDTO 对象
DataDictAddDTO dataDictAddDTO = new DataDictAddDTO().setEnumValue(enumValue).setValue(value).setDisplayName(displayName)
.setSort(sort).setMemo(memo);
// 保存数据字典
CommonResult<DataDictBO> result = dataDictService.addDataDict(AdminSecurityContextHolder.getContext().getAdminId(), dataDictAddDTO);
// 返回结果
return DataDictConvert.INSTANCE.convert2(result);
} }
@PostMapping("/update") @PostMapping("/update")
@RequiresPermissions("system.dataDict.update") @RequiresPermissions("system.dataDict.update")
@ApiOperation(value = "更新数据字典") @ApiOperation(value = "更新数据字典")
@ApiImplicitParams({ public CommonResult<Boolean> update(DataDictUpdateDTO dataDictUpdateDTO) {
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "100"), return success(dataDictService.updateDataDict(AdminSecurityContextHolder.getContext().getAdminId(), dataDictUpdateDTO));
@ApiImplicitParam(name = "value", value = "小类数值", required = true, example = "1"),
@ApiImplicitParam(name = "displayName", value = "展示名", required = true, example = ""),
@ApiImplicitParam(name = "sort", required = true, value = "排序值", defaultValue = "10"),
@ApiImplicitParam(name = "memo", value = "备注", example = "你猜我猜不猜"),
})
public CommonResult<Boolean> update(@RequestParam("id") Integer id,
@RequestParam("value") String value,
@RequestParam("displayName") String displayName,
@RequestParam("sort") Integer sort,
@RequestParam(value = "memo", required = false) String memo) {
// 创建 DataDictAddDTO 对象
DataDictUpdateDTO dataDictUpdateDTO = new DataDictUpdateDTO().setId(id).setValue(value).setDisplayName(displayName)
.setSort(sort).setMemo(memo);
// 更新数据字典
return dataDictService.updateDataDict(AdminSecurityContextHolder.getContext().getAdminId(), dataDictUpdateDTO);
} }
@PostMapping("/delete") @PostMapping("/delete")
@ -110,7 +74,7 @@ public class DataDictController {
@ApiOperation(value = "删除数据字典") @ApiOperation(value = "删除数据字典")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "100") @ApiImplicitParam(name = "id", value = "编号", required = true, example = "100")
public CommonResult<Boolean> delete(@RequestParam("id") Integer id) { public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
return dataDictService.deleteDataDict(AdminSecurityContextHolder.getContext().getAdminId(), id); return success(dataDictService.deleteDataDict(AdminSecurityContextHolder.getContext().getAdminId(), id));
} }
} }

View File

@ -1,9 +1,7 @@
package cn.iocoder.mall.admin.application.convert; package cn.iocoder.mall.admin.application.convert;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.bo.datadict.DataDictBO; import cn.iocoder.mall.admin.api.bo.datadict.DataDictBO;
import cn.iocoder.mall.admin.application.vo.datadict.DataDictVO; import cn.iocoder.mall.admin.application.vo.datadict.DataDictEnumVO;
import cn.iocoder.mall.admin.application.vo.datadict.DataDictValueVO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -16,18 +14,6 @@ public interface DataDictConvert {
DataDictConvert INSTANCE = Mappers.getMapper(DataDictConvert.class); DataDictConvert INSTANCE = Mappers.getMapper(DataDictConvert.class);
@Mappings({}) @Mappings({})
DataDictVO convert(DataDictBO dataDictBO); List<DataDictEnumVO.Value> convert2(List<DataDictBO> dataDictBOs);
@Mappings({})
List<DataDictVO> convert(List<DataDictBO> dataDictBOs);
@Mappings({})
CommonResult<List<DataDictVO>> convert(CommonResult<List<DataDictBO>> result);
@Mappings({})
CommonResult<DataDictVO> convert2(CommonResult<DataDictBO> result);
@Mappings({})
List<DataDictValueVO> convert2(List<DataDictBO> dataDictBOs);
} }

View File

@ -14,7 +14,21 @@ public class DataDictEnumVO {
@ApiModelProperty(value = "大类枚举值", required = true, example = "gender") @ApiModelProperty(value = "大类枚举值", required = true, example = "gender")
private String enumValue; private String enumValue;
@ApiModelProperty(value = "小类数值数组", required = true) @ApiModelProperty(value = "小类数值数组", required = true)
private List<DataDictValueVO> values; private List<Value> values;
@ApiModel("数据字典枚举值 VO")
@Data
@Accessors(chain = true)
public static class Value {
@ApiModelProperty(value = "小类数值", required = true, example = "1")
private String value;
@ApiModelProperty(value = "展示名", required = true, example = "")
private String displayName;
}
} }

View File

@ -1,26 +0,0 @@
package cn.iocoder.mall.admin.application.vo.datadict;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
@ApiModel("数据字典 VO")
@Data
@Accessors(chain = true)
public class DataDictVO {
@ApiModelProperty(value = "编号", required = true, example = "1")
private Integer id;
@ApiModelProperty(value = "大类枚举值", required = true, example = "gender")
private String enumValue;
@ApiModelProperty(value = "小类数值", required = true, example = "1")
private String value;
@ApiModelProperty(value = "展示名", required = true, example = "")
private String displayName;
@ApiModelProperty(value = "排序值", required = true, example = "10")
private Integer sort;
@ApiModelProperty(value = "备注", example = "你猜")
private String memo;
}

View File

@ -1,18 +0,0 @@
package cn.iocoder.mall.admin.application.vo.datadict;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
@ApiModel("数据字典枚举值 VO")
@Data
@Accessors(chain = true)
public class DataDictValueVO {
@ApiModelProperty(value = "小类数值", required = true, example = "1")
private String value;
@ApiModelProperty(value = "展示名", required = true, example = "")
private String displayName;
}

View File

@ -10,13 +10,13 @@ import java.util.List;
public interface DataDictService { public interface DataDictService {
CommonResult<List<DataDictBO>> selectDataDictList(); List<DataDictBO> selectDataDictList();
CommonResult<DataDictBO> addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO); DataDictBO addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO);
CommonResult<Boolean> updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO); Boolean updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO);
CommonResult<Boolean> deleteDataDict(Integer adminId, Integer dataDictId); Boolean deleteDataDict(Integer adminId, Integer dataDictId);
/** /**
* 获取字典值 - 单个 * 获取字典值 - 单个
@ -28,6 +28,7 @@ public interface DataDictService {
* @return * @return
*/ */
CommonResult<DataDictBO> getDataDict(String dictKey, Object dictValue); CommonResult<DataDictBO> getDataDict(String dictKey, Object dictValue);
CommonResult<List<DataDictBO>> getDataDict(String dictKey); CommonResult<List<DataDictBO>> getDataDict(String dictKey);
/** /**

View File

@ -1,5 +1,7 @@
package cn.iocoder.mall.admin.api.dto.datadict; package cn.iocoder.mall.admin.api.dto.datadict;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@ -7,36 +9,28 @@ import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.io.Serializable; import java.io.Serializable;
/** @ApiModel("数据字典添加 DTO")
* 数据字典添加 DTO
*/
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class DataDictAddDTO implements Serializable { public class DataDictAddDTO implements Serializable {
/** @ApiModelProperty(value = "大类枚举值", required = true, example = "gender")
* 大类枚举值
*/
@NotEmpty(message = "大类枚举值不能为空") @NotEmpty(message = "大类枚举值不能为空")
private String enumValue; private String enumValue;
/**
* 小类数值 @ApiModelProperty(value = "小类数值", required = true, example = "1")
*/
@NotEmpty(message = "小类数值不能为空") @NotEmpty(message = "小类数值不能为空")
private String value; private String value;
/**
* 展示名 @ApiModelProperty(value = "展示名", required = true, example = "")
*/
@NotEmpty(message = "展示名不能为空") @NotEmpty(message = "展示名不能为空")
private String displayName; private String displayName;
/**
* 排序值 @ApiModelProperty(required = true, value = "排序值", example = "123")
*/
@NotNull(message = "排序值不能为空") @NotNull(message = "排序值不能为空")
private Integer sort; private Integer sort;
/**
* 备注 @ApiModelProperty(value = "备注", example = "你猜我猜不猜")
*/
private String memo; private String memo;
} }

View File

@ -1,5 +1,6 @@
package cn.iocoder.mall.admin.api.dto.datadict; package cn.iocoder.mall.admin.api.dto.datadict;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@ -14,29 +15,27 @@ import java.io.Serializable;
@Accessors(chain = true) @Accessors(chain = true)
public class DataDictUpdateDTO implements Serializable { public class DataDictUpdateDTO implements Serializable {
/** @ApiModelProperty(value = "数据字典编号", required = true, example = "1")
* 编号 @NotNull(message = "数据字典编号不能为空")
*/
@NotNull(message = "编号不能为空")
private Integer id; private Integer id;
/**
* 小类数值 @ApiModelProperty(value = "大类枚举值", required = true, example = "gender")
*/ @NotEmpty(message = "大类枚举值不能为空")
private String enumValue;
@ApiModelProperty(value = "小类数值", required = true, example = "1")
@NotEmpty(message = "小类数值不能为空") @NotEmpty(message = "小类数值不能为空")
private String value; private String value;
/**
* 展示名 @ApiModelProperty(value = "展示名", required = true, example = "")
*/
@NotEmpty(message = "展示名不能为空") @NotEmpty(message = "展示名不能为空")
private String displayName; private String displayName;
/**
* 排序值 @ApiModelProperty(required = true, value = "排序值", example = "123")
*/
@NotNull(message = "排序值不能为空") @NotNull(message = "排序值不能为空")
private Integer sort; private Integer sort;
/**
* 备注 @ApiModelProperty(value = "备注", example = "你猜我猜不猜")
*/
private String memo; private String memo;
} }

View File

@ -1,6 +1,8 @@
package cn.iocoder.mall.admin.dao; package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.DataDictDO; import cn.iocoder.mall.admin.dataobject.DataDictDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -8,9 +10,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
@Repository @Repository
public interface DataDictMapper { public interface DataDictMapper extends BaseMapper<DataDictDO> {
DataDictDO selectById(@Param("id") Integer id);
DataDictDO selectByEnumValueAndValue( DataDictDO selectByEnumValueAndValue(
@Param("enumValue") String enumValue, @Param("enumValue") String enumValue,
@ -26,10 +26,9 @@ public interface DataDictMapper {
@Param("enumValue") String enumValue @Param("enumValue") String enumValue
); );
List<DataDictDO> selectList(); default List<DataDictDO> selectList() {
return selectList(new QueryWrapper<>());
}
void insert(DataDictDO dataDict);
int update(DataDictDO dataDict);
} }

View File

@ -1,13 +1,17 @@
package cn.iocoder.mall.admin.dao; package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.OAuth2AccessTokenDO; import cn.iocoder.mall.admin.dataobject.OAuth2AccessTokenDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface OAuth2AccessTokenMapper extends BaseMapper<OAuth2AccessTokenDO> { public interface OAuth2AccessTokenMapper extends BaseMapper<OAuth2AccessTokenDO> {
int updateToInvalidByAdminId(@Param("adminId") Integer adminId); default int updateToInvalidByAdminId(Integer adminId) {
QueryWrapper<OAuth2AccessTokenDO> query = new QueryWrapper<OAuth2AccessTokenDO>()
.eq("admin_id", adminId).eq("valid", true);
return update(new OAuth2AccessTokenDO().setValid(false), query);
}
} }

View File

@ -1,13 +1,17 @@
package cn.iocoder.mall.admin.dao; package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.OAuth2RefreshTokenDO; import cn.iocoder.mall.admin.dataobject.OAuth2RefreshTokenDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface OAuth2RefreshTokenMapper extends BaseMapper<OAuth2RefreshTokenDO> { public interface OAuth2RefreshTokenMapper extends BaseMapper<OAuth2RefreshTokenDO> {
int updateToInvalidByAdminId(@Param("adminId") Integer adminId); default int updateToInvalidByAdminId(Integer adminId) {
QueryWrapper<OAuth2RefreshTokenDO> query = new QueryWrapper<OAuth2RefreshTokenDO>()
.eq("admin_id", adminId).eq("valid", true);
return update(new OAuth2RefreshTokenDO().setValid(false), query);
}
} }

View File

@ -13,7 +13,7 @@ import java.util.Set;
@Repository @Repository
public interface ResourceMapper extends BaseMapper<ResourceDO> { public interface ResourceMapper extends BaseMapper<ResourceDO> {
@Deprecated // TODO 芋艿后续改造
List<ResourceDO> selectListByTypeAndRoleIds(@Param("type") Integer type, List<ResourceDO> selectListByTypeAndRoleIds(@Param("type") Integer type,
@Param("roleIds") Set<Integer> roleIds); @Param("roleIds") Set<Integer> roleIds);

View File

@ -1,6 +1,7 @@
package cn.iocoder.mall.admin.dataobject; package cn.iocoder.mall.admin.dataobject;
import cn.iocoder.common.framework.dataobject.DeletableDO; import cn.iocoder.common.framework.dataobject.DeletableDO;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@ -13,6 +14,7 @@ import lombok.experimental.Accessors;
* value1 * value1
* value2 * value2
*/ */
@TableName("data_dict")
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class DataDictDO extends DeletableDO { public class DataDictDO extends DeletableDO {

View File

@ -31,16 +31,16 @@ public class DataDictServiceImpl implements DataDictService {
private DataDictMapper dataDictMapper; private DataDictMapper dataDictMapper;
@Override @Override
public CommonResult<List<DataDictBO>> selectDataDictList() { public List<DataDictBO> selectDataDictList() {
List<DataDictDO> dataDicts = dataDictMapper.selectList(); List<DataDictDO> dataDicts = dataDictMapper.selectList();
return CommonResult.success(DataDictConvert.INSTANCE.convert(dataDicts)); return DataDictConvert.INSTANCE.convert(dataDicts);
} }
@Override @Override
public CommonResult<DataDictBO> addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO) { public DataDictBO addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO) {
// 校验数据字典重复 // 校验数据字典重复
if (dataDictMapper.selectByEnumValueAndValue(dataDictAddDTO.getEnumValue(), dataDictAddDTO.getValue()) != null) { if (dataDictMapper.selectByEnumValueAndValue(dataDictAddDTO.getEnumValue(), dataDictAddDTO.getValue()) != null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode()); throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
} }
// 保存到数据库 // 保存到数据库
DataDictDO dataDict = DataDictConvert.INSTANCE.convert(dataDictAddDTO); DataDictDO dataDict = DataDictConvert.INSTANCE.convert(dataDictAddDTO);
@ -49,45 +49,43 @@ public class DataDictServiceImpl implements DataDictService {
dataDictMapper.insert(dataDict); dataDictMapper.insert(dataDict);
// TODO 插入操作日志 // TODO 插入操作日志
// 返回成功 // 返回成功
return CommonResult.success(DataDictConvert.INSTANCE.convert(dataDict)); return DataDictConvert.INSTANCE.convert(dataDict);
} }
@Override @Override
public CommonResult<Boolean> updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO) { public Boolean updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO) {
// 校验数据字典不存在 // 校验数据字典不存在
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictUpdateDTO.getId()); DataDictDO existsDataDict = dataDictMapper.selectById(dataDictUpdateDTO.getId());
if (existsDataDict == null) { if (existsDataDict == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode()); throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
} }
// 校验数据字典重复 // 校验数据字典重复
DataDictDO duplicateDataDict = dataDictMapper.selectByEnumValueAndValue(existsDataDict.getEnumValue(), dataDictUpdateDTO.getValue()); DataDictDO duplicateDataDict = dataDictMapper.selectByEnumValueAndValue(existsDataDict.getEnumValue(), dataDictUpdateDTO.getValue());
if (duplicateDataDict != null && !duplicateDataDict.getId().equals(dataDictUpdateDTO.getId())) { if (duplicateDataDict != null && !duplicateDataDict.getId().equals(dataDictUpdateDTO.getId())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode()); throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
} }
// 更新到数据库 // 更新到数据库
DataDictDO updateDataDict = DataDictConvert.INSTANCE.convert(dataDictUpdateDTO); DataDictDO updateDataDict = DataDictConvert.INSTANCE.convert(dataDictUpdateDTO);
dataDictMapper.update(updateDataDict); dataDictMapper.updateById(updateDataDict);
// TODO 插入操作日志 // TODO 插入操作日志
// 返回成功 // 返回成功
return CommonResult.success(true); return true;
} }
// 一般情况下不要删除数据字典 // 一般情况下不要删除数据字典
// 因为业务数据正在使用该数据字典删除后可能有不可预知的问题 // 因为业务数据正在使用该数据字典删除后可能有不可预知的问题
@Override @Override
public CommonResult<Boolean> deleteDataDict(Integer adminId, Integer dataDictId) { public Boolean deleteDataDict(Integer adminId, Integer dataDictId) {
// 校验数据字典不存在 // 校验数据字典不存在
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictId); DataDictDO existsDataDict = dataDictMapper.selectById(dataDictId);
if (existsDataDict == null) { if (existsDataDict == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode()); throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
} }
// 更新到数据库 // 标记删除
DataDictDO updateDataDict = new DataDictDO().setId(dataDictId); dataDictMapper.deleteById(dataDictId);
updateDataDict.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
dataDictMapper.update(updateDataDict);
// TODO 插入操作日志 // TODO 插入操作日志
// 返回成功 // 返回成功
return CommonResult.success(true); return true;
} }
@Override @Override
@ -106,7 +104,7 @@ public class DataDictServiceImpl implements DataDictService {
@Override @Override
public CommonResult<List<DataDictBO>> getDataDictList(String dictKey, Collection<?> dictValueList) { public CommonResult<List<DataDictBO>> getDataDictList(String dictKey, Collection<?> dictValueList) {
Set<String> convertDictValueList = dictValueList.stream().map(o -> String.valueOf(o)).collect(Collectors.toSet()); Set<String> convertDictValueList = dictValueList.stream().map(String::valueOf).collect(Collectors.toSet());
List<DataDictDO> dataDictDOList = dataDictMapper.selectByEnumValueAndValues(dictKey, convertDictValueList); List<DataDictDO> dataDictDOList = dataDictMapper.selectByEnumValueAndValues(dictKey, convertDictValueList);
List<DataDictBO> dataDictBOList = DataDictConvert.INSTANCE.convert(dataDictDOList); List<DataDictBO> dataDictBOList = DataDictConvert.INSTANCE.convert(dataDictDOList);
return CommonResult.success(dataDictBOList); return CommonResult.success(dataDictBOList);

View File

@ -29,21 +29,6 @@
</foreach> </foreach>
</select> </select>
<select id="selectById" resultType="DataDictDO">
SELECT
<include refid="FIELDS"/>
FROM data_dict
WHERE id = #{id}
AND deleted = 0
</select>
<select id="selectList" resultType="DataDictDO">
SELECT
<include refid="FIELDS"/>
FROM data_dict
WHERE deleted = 0
</select>
<select id="selectByEnumValue" resultType="cn.iocoder.mall.admin.dataobject.DataDictDO"> <select id="selectByEnumValue" resultType="cn.iocoder.mall.admin.dataobject.DataDictDO">
SELECT SELECT
<include refid="FIELDS"/> <include refid="FIELDS"/>
@ -52,39 +37,4 @@
AND enum_value = #{enumValue} AND enum_value = #{enumValue}
</select> </select>
<insert id="insert" parameterType="DataDictDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO data_dict (
id, enum_value, value, display_name, sort,
memo, create_time, deleted
) VALUES (
#{id}, #{enumValue}, #{value}, #{displayName}, #{sort},
#{memo}, #{createTime}, #{deleted}
)
</insert>
<update id="update" parameterType="DataDictDO">
UPDATE data_dict
<set>
<if test="enumValue != null">
enum_value = #{enumValue},
</if>
<if test="value != null">
value = #{value},
</if>
<if test="displayName != null">
display_name = #{displayName},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="memo != null">
memo = #{memo},
</if>
<if test="deleted != null">
deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
</mapper> </mapper>

View File

@ -1,12 +0,0 @@
<?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="cn.iocoder.mall.admin.dao.OAuth2AccessTokenMapper">
<update id="updateToInvalidByAdminId" parameterType="Integer">
UPDATE oauth2_access_token
SET valid = 0
WHERE admin_id = #{adminId}
AND valid = 1
</update>
</mapper>

View File

@ -1,12 +0,0 @@
<?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="cn.iocoder.mall.admin.dao.OAuth2RefreshTokenMapper">
<update id="updateToInvalidByAdminId" parameterType="Integer">
UPDATE oauth2_refresh_token
SET valid = 0
WHERE admin_id = #{adminId}
AND valid = 1
</update>
</mapper>