Merge remote-tracking branch 'origin/main'

This commit is contained in:
liuzhaotian
2024-08-01 14:58:57 +08:00
48 changed files with 821 additions and 201 deletions
@@ -14,6 +14,7 @@ import com.cf.imes.framework.common.util.json.JsonUtils;
import com.cf.imes.framework.common.util.servlet.ServletUtils; import com.cf.imes.framework.common.util.servlet.ServletUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
@@ -212,6 +213,18 @@ public class GlobalExceptionHandler {
return CommonResult.error(ex.getCode(), ex.getMessage()); return CommonResult.error(ex.getCode(), ex.getMessage());
} }
/**
* 处理数据库DuplicateKeyException,违反唯一约束异常
*
* @param ex
* @return
*/
@ExceptionHandler(value = DuplicateKeyException.class)
public CommonResult<?> duplicateKeyExceptionHandler(DuplicateKeyException ex) {
log.info("[duplicateKeyExceptionHandler]", ex);
return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), "请求数据已存在,请检查");
}
/** /**
* 处理系统异常,兜底处理所有的一切 * 处理系统异常,兜底处理所有的一切
*/ */
@@ -0,0 +1,33 @@
package com.cf.imes.module.executor.api.datasource;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.module.executor.api.datasource.dto.PartGoodsRespDTO;
import com.cf.imes.module.executor.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* @author Gqr
* @since 2024/7/30 9:22
*/
@FeignClient(name = ApiConstants.NAME)
@Tag(name = "RPC 服务 - 配件报表数据源")
public interface OrderPartDatasourceApi {
String PREFIX = ApiConstants.PREFIX + "/order-part";
@GetMapping(PREFIX + "/detail")
@Operation(summary = "配件明细")
@Parameter(name = "orderId", description = "生产单id", required = true, example = "1")
CommonResult<List<PartGoodsRespDTO>> selectPartDetail(@RequestParam("orderId") Long orderId);
@GetMapping(PREFIX + "/summary")
@Operation(summary = "配件汇总")
@Parameter(name = "orderId", description = "生产单id", required = true, example = "1")
CommonResult<List<PartGoodsRespDTO>> selectPartAll(@RequestParam("orderId") Long orderId);
}
@@ -21,8 +21,13 @@ import java.util.List;
public interface OrderPlateDatasourceApi { public interface OrderPlateDatasourceApi {
String PREFIX = ApiConstants.PREFIX + "/order-plate"; String PREFIX = ApiConstants.PREFIX + "/order-plate";
@GetMapping(PREFIX + "/summary/byPlate") @GetMapping(PREFIX + "/summary")
@Operation(summary = "板材汇总板材数据") @Operation(summary = "板材汇总")
@Parameter(name = "orderId", description = "生产单id", required = true, example = "1") @Parameter(name = "orderId", description = "生产单id", required = true, example = "1")
CommonResult<List<PlateGoodsRespDTO>> selectPlateGoodsListSummary(@RequestParam("orderId") Long orderId); CommonResult<List<PlateGoodsRespDTO>> selectPlateGoodsListSummary(@RequestParam("orderId") Long orderId);
@GetMapping(PREFIX + "/detail")
@Operation(summary = "板材明细")
@Parameter(name = "orderId", description = "生产单id", required = true, example = "1")
CommonResult<List<PlateGoodsRespDTO>> selectPlateGoodsList(@RequestParam("orderId") Long orderId);
} }
@@ -0,0 +1,80 @@
package com.cf.imes.module.executor.api.datasource.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "管理后台 - 配件对应板材信息 Resp DTO")
public class PartGoodsRespDTO {
@Schema(description = "柜体号", example = "11087")
private Long bodyId;
@Schema(description = "柜体名称", example = "11087")
private String bodyName;
@Schema(description = "房间号", example = "11087")
private Long roomId;
@Schema(description = "房间名称", example = "11087")
private String roomName;
@Schema(description = "配件 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "29347")
private Long id;
@Schema(description = "生产单号", requiredMode = Schema.RequiredMode.REQUIRED)
private Long orderId;
@Schema(description = "商品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "1195")
private String goodsId;
@Schema(description = "配件名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "晨丰")
private String name;
@Schema(description = "材质", requiredMode = Schema.RequiredMode.REQUIRED)
private String material;
@Schema(description = "配件类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
private String type;
@Schema(description = "型号", requiredMode = Schema.RequiredMode.REQUIRED)
private String model;
@Schema(description = "规格", requiredMode = Schema.RequiredMode.REQUIRED)
private String spec;
@Schema(description = "品牌", requiredMode = Schema.RequiredMode.REQUIRED)
private String brand;
@Schema(description = "厂家", requiredMode = Schema.RequiredMode.REQUIRED)
private String factory;
@Schema(description = "单位", requiredMode = Schema.RequiredMode.REQUIRED)
private String unit;
@Schema(description = "价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "29507")
private Double price;
@Schema(description = "是否复合部件:0 否 1 是", requiredMode = Schema.RequiredMode.REQUIRED)
private Boolean isComposite;
@Schema(description = "备注", requiredMode = Schema.RequiredMode.REQUIRED, example = "你猜")
private String remark;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
@Schema(description = "数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "29507")
private Double num;
@Schema(description = "金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "29507")
private Double totalPrice;
}
@@ -154,7 +154,7 @@ public class PlateGoodsRespDTO {
@Schema(description = "是否孤形对角") @Schema(description = "是否孤形对角")
private Boolean isArcAcross; private Boolean isArcAcross;
@Schema(description = "是否异") @Schema(description = "是否异")
private Boolean isSpecialShaped; private Boolean isSpecialShaped;
@Schema(description = "是否造型") @Schema(description = "是否造型")
@@ -0,0 +1,40 @@
package com.cf.imes.module.executor.enums;
/**
* 板材开门类型枚举
*
* @author Gqr
* @since 2024/7/29 9:58
*/
public enum OrderPlateOpenDoorTypeEnum {
NO(0, ""),
LEFT(1, ""),
RIGHT(2, ""),
UP(3, ""),
DOWN(4, "");
private final int code;
private final String desc;
OrderPlateOpenDoorTypeEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
public static String getDescFromCode(int code) {
for (OrderPlateOpenDoorTypeEnum orderPlateOpenDoorTypeEnum : OrderPlateOpenDoorTypeEnum.values()) {
if (orderPlateOpenDoorTypeEnum.getCode() == code) {
return orderPlateOpenDoorTypeEnum.getDesc();
}
}
return null;
}
}
@@ -0,0 +1,42 @@
package com.cf.imes.module.executor.api.datasource;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.executor.api.datasource.dto.PartGoodsRespDTO;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.PartGoodsRespVO;
import com.cf.imes.module.executor.dal.mysql.orderParts.OrderPartsMapper;
import com.cf.imes.module.executor.enums.OrderDeletedEnum;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @author Gqr
* @since 2024/7/30 9:21
*/
@RestController // 提供 RESTful API 接口,给 Feign 调用
@Validated
public class OrderPartDatasourceApiImpl implements OrderPartDatasourceApi {
@Resource
private OrderPartsMapper orderPartsMapper;
@Override
public CommonResult<List<PartGoodsRespDTO>> selectPartDetail(Long orderId) {
Integer notDeletedStatus = OrderDeletedEnum.NOT_DELETED.getStatus();
// 查询明细列表
List<PartGoodsRespVO> partGoodsRespVOS = orderPartsMapper.selectPartDetail(orderId, SecurityFrameworkUtils.getLoginUser().getOrganId(), notDeletedStatus);
return CommonResult.success(BeanUtils.toBean(partGoodsRespVOS, PartGoodsRespDTO.class));
}
@Override
public CommonResult<List<PartGoodsRespDTO>> selectPartAll(Long orderId) {
Integer notDeletedStatus = OrderDeletedEnum.NOT_DELETED.getStatus();
// 查询明细列表
List<PartGoodsRespVO> partGoodsRespVOS = orderPartsMapper.selectPartAll(orderId, SecurityFrameworkUtils.getLoginUser().getOrganId(), notDeletedStatus);
return CommonResult.success(BeanUtils.toBean(partGoodsRespVOS, PartGoodsRespDTO.class));
}
}
@@ -31,4 +31,12 @@ public class OrderPlateDatasourceApiImpl implements OrderPlateDatasourceApi {
List<PlateGoodsRespVO> plateGoodsRespVOS = plateMapper.selectPlateGoodsListSummary(orderId, SecurityFrameworkUtils.getLoginUser().getOrganId(), notDeletedStatus); List<PlateGoodsRespVO> plateGoodsRespVOS = plateMapper.selectPlateGoodsListSummary(orderId, SecurityFrameworkUtils.getLoginUser().getOrganId(), notDeletedStatus);
return CommonResult.success(BeanUtils.toBean(plateGoodsRespVOS, PlateGoodsRespDTO.class)); return CommonResult.success(BeanUtils.toBean(plateGoodsRespVOS, PlateGoodsRespDTO.class));
} }
@Override
public CommonResult<List<PlateGoodsRespDTO>> selectPlateGoodsList(Long orderId) {
// 查询分组列表
Integer notDeletedStatus = OrderDeletedEnum.NOT_DELETED.getStatus();
List<PlateGoodsRespVO> plateGoodsRespVOS = plateMapper.selectPlateGoodsList(orderId, SecurityFrameworkUtils.getLoginUser().getOrganId(), notDeletedStatus);
return CommonResult.success(BeanUtils.toBean(plateGoodsRespVOS, PlateGoodsRespDTO.class));
}
} }
@@ -357,7 +357,6 @@
left join order_goods ogs on op.goods_id = ogs.id left join order_goods ogs on op.goods_id = ogs.id
left join order_item its on op.id = its.plate_id left join order_item its on op.id = its.plate_id
left join order_body bdy on bdy.id = its.body_id left join order_body bdy on bdy.id = its.body_id
left join order_goods gods on gods.id = op.goods_id
where op.order_id = #{orderId} where op.order_id = #{orderId}
and op.organ_id = #{organId} and op.organ_id = #{organId}
and op.deleted = #{deleted} and op.deleted = #{deleted}
@@ -385,7 +384,6 @@
left join order_goods ogs on op.goods_id = ogs.id left join order_goods ogs on op.goods_id = ogs.id
left join order_item its on op.id = its.plate_id left join order_item its on op.id = its.plate_id
left join order_body bdy on bdy.id = its.body_id left join order_body bdy on bdy.id = its.body_id
left join order_goods gods on gods.id = op.goods_id
where op.order_id = #{orderId} where op.order_id = #{orderId}
and op.organ_id = #{organId} and op.organ_id = #{organId}
and op.deleted = #{deleted} and op.deleted = #{deleted}
@@ -62,8 +62,8 @@ public class PlateController {
@DeleteMapping("/delete") @DeleteMapping("/delete")
@Operation(summary = "删除板材信息") @Operation(summary = "删除板材信息")
@Parameters({ @Parameters({
@Parameter(name = "id", description = "编号", required = true), @Parameter(name = "id", description = "编号", required = true, example = "1024"),
@Parameter(name = "organId", description = "板材所属组织ID") @Parameter(name = "organId", description = "板材所属组织ID", example = "1024")
}) })
// @PreAuthorize("@ss.hasPermission('manage:plate:delete')") // @PreAuthorize("@ss.hasPermission('manage:plate:delete')")
@PreAuthorize("@ss.hasAnyPermissions('manage:plate:delete','base:plate:delete')") @PreAuthorize("@ss.hasAnyPermissions('manage:plate:delete','base:plate:delete')")
@@ -1,12 +1,13 @@
package com.cf.imes.module.manage.framework.rpc.config; package com.cf.imes.module.manage.framework.rpc.config;
import com.cf.imes.module.system.api.organ.OrganApi; import com.cf.imes.module.system.api.organ.OrganApi;
import com.cf.imes.module.system.api.permission.PermissionApi;
import com.cf.imes.module.system.api.process.ProcessApi; import com.cf.imes.module.system.api.process.ProcessApi;
import com.cf.imes.module.system.api.user.AdminUserApi; import com.cf.imes.module.system.api.user.AdminUserApi;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = {AdminUserApi.class, OrganApi.class, ProcessApi.class}) @EnableFeignClients(clients = {AdminUserApi.class, OrganApi.class, ProcessApi.class, PermissionApi.class})
public class RpcConfiguration { public class RpcConfiguration {
} }
@@ -10,6 +10,7 @@ import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportResp
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlatePageReqVO; import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlatePageReqVO;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateSaveReqVO; import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateSaveReqVO;
import com.cf.imes.module.system.api.organ.OrganApi; import com.cf.imes.module.system.api.organ.OrganApi;
import com.cf.imes.module.system.api.permission.PermissionApi;
import com.cf.imes.module.system.enums.ErrorCodeConstants; import com.cf.imes.module.system.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@@ -26,6 +27,7 @@ import com.cf.imes.module.manage.dal.mysql.plate.PlateMapper;
import javax.annotation.Resource; import javax.annotation.Resource;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.PLATE_NOT_EXISTS; import static com.cf.imes.module.system.enums.ErrorCodeConstants.PLATE_NOT_EXISTS;
/** /**
@@ -43,22 +45,29 @@ public class PlateServiceImpl implements PlateService {
@Resource @Resource
private OrganApi organApi; private OrganApi organApi;
@Resource
private PermissionApi permissionApi;
private final static String BASE_PLATE_IMPORT_PERMISSION = "base:plate:import"; // 导入板材
private final static String BASE_PLATE_DELETE_PERMISSION = "base:plate:delete"; // 删除板材信息
private final static String BASE_PLATE_CREATE_PERMISSION = "base:plate:create"; // 创建板材信息
private final static String BASE_PLATE_UPDATE_PERMISSION = "base:plate:update"; // 更新板材信息
private final static String BASE_PLATE_QUERY_PERMISSION = "base:plate:query"; // 获得板材信息
@Override @Override
@OrganIgnore @OrganIgnore
public Long createPlate(PlateSaveReqVO createReqVO) { public Long createPlate(PlateSaveReqVO createReqVO) {
PlateDO plate = BeanUtils.toBean(createReqVO, PlateDO.class); PlateDO plate = BeanUtils.toBean(createReqVO, PlateDO.class);
if (createReqVO.getOrganId() != null && createReqVO.getOrganId() != 0 ){ if (permissionApi.hasRoles(getLoginUser().getId(), BASE_PLATE_CREATE_PERMISSION).getData()){
validateOrganExists(createReqVO.getOrganId()); if (plate.getOrganId() != null && plate.getOrganId() != 0 ){
validateGoodExists(createReqVO.getGoodsId(), createReqVO.getOrganId()); validateOrganExists(plate.getOrganId());
}else { }
} else {
plate.setOrganId(OrganContextHolder.getOrganId()); plate.setOrganId(OrganContextHolder.getOrganId());
} }
// 判断板材是否存在 // 判断板材是否存在
PlateDO existPlate = plateMapper.selectByGoodID(createReqVO.getGoodsId(), plate.getOrganId()); validateGoodExists(plate.getGoodsId(),plate.getOrganId());
if (existPlate != null){
throw exception(ErrorCodeConstants.PLATE_EXISTS);
}
plateMapper.insert(plate);// 组织id未传输 plateMapper.insert(plate);// 组织id未传输
// 返回 // 返回
return plate.getId(); return plate.getId();
@@ -69,10 +78,13 @@ public class PlateServiceImpl implements PlateService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updatePlate(PlateSaveReqVO updateReqVO) { public void updatePlate(PlateSaveReqVO updateReqVO) {
// 更新 判断是否有组织id // 更新 判断是否有组织id
if (updateReqVO.getOrganId() == null || updateReqVO.getOrganId() == 0){ if (permissionApi.hasRoles(getLoginUser().getId(), BASE_PLATE_UPDATE_PERMISSION).getData()){
if (updateReqVO.getOrganId() != null && updateReqVO.getOrganId() != 0 ){
validateOrganExists(updateReqVO.getOrganId());
}
} else {
updateReqVO.setOrganId(OrganContextHolder.getOrganId()); updateReqVO.setOrganId(OrganContextHolder.getOrganId());
} }
validateOrganExists(updateReqVO.getOrganId());
// 校验存在 // 校验存在
validatePlateExists(updateReqVO.getId(), updateReqVO.getOrganId()); validatePlateExists(updateReqVO.getId(), updateReqVO.getOrganId());
@@ -84,10 +96,13 @@ public class PlateServiceImpl implements PlateService {
@OrganIgnore @OrganIgnore
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void deletePlate(Long id,Long organId) { public void deletePlate(Long id,Long organId) {
if (organId == null || organId == 0){ if (permissionApi.hasRoles(getLoginUser().getId(), BASE_PLATE_DELETE_PERMISSION).getData()){
if (organId != null && organId != 0 ){
validateOrganExists(organId);
}
} else {
organId = OrganContextHolder.getOrganId(); organId = OrganContextHolder.getOrganId();
} }
validateOrganExists(organId);
// 校验存在 // 校验存在
validatePlateExists(id,organId); validatePlateExists(id,organId);
@@ -115,6 +130,13 @@ public class PlateServiceImpl implements PlateService {
@Override @Override
@OrganIgnore @OrganIgnore
public PageResult<PlateDO> getPlatePage(PlatePageReqVO pageReqVO) { public PageResult<PlateDO> getPlatePage(PlatePageReqVO pageReqVO) {
if (permissionApi.hasRoles(getLoginUser().getId(), BASE_PLATE_QUERY_PERMISSION).getData()){
if (pageReqVO.getOrganId() != null && pageReqVO.getOrganId() != 0 ){
validateOrganExists(pageReqVO.getOrganId());
}
} else {
pageReqVO.setOrganId(OrganContextHolder.getOrganId());
}
if (pageReqVO.getOrganId() == null || pageReqVO.getOrganId() == 0){ if (pageReqVO.getOrganId() == null || pageReqVO.getOrganId() == 0){
pageReqVO.setOrganId(OrganContextHolder.getOrganId()); pageReqVO.setOrganId(OrganContextHolder.getOrganId());
} }
@@ -130,25 +152,32 @@ public class PlateServiceImpl implements PlateService {
// if (CollUtil.isEmpty(importPlates)) { // if (CollUtil.isEmpty(importPlates)) {
// throw ServiceExceptionUtil.exception(ErrorCodeConstants.PLATE_IMPORT_LIST_IS_EMPTY); // throw ServiceExceptionUtil.exception(ErrorCodeConstants.PLATE_IMPORT_LIST_IS_EMPTY);
// } // }
if (permissionApi.hasRoles(getLoginUser().getId(), BASE_PLATE_IMPORT_PERMISSION).getData()){
if (organId != null && organId != 0 ){
validateOrganExists(organId); validateOrganExists(organId);
}
} else {
organId = OrganContextHolder.getOrganId();
}
PlateImportRespVO respVO = PlateImportRespVO.builder().createPlateNames(new ArrayList<>()) PlateImportRespVO respVO = PlateImportRespVO.builder().createPlateNames(new ArrayList<>())
.updatePlateNames(new ArrayList<>()).failurePlateNames(new LinkedHashMap<>()).build(); .updatePlateNames(new ArrayList<>()).failurePlateNames(new LinkedHashMap<>()).build();
// 批量插入集合 // 批量插入集合
List<PlateDO> insertList = new ArrayList<>(); List<PlateDO> insertList = new ArrayList<>();
// 批量更新集合 // 批量更新集合
List<PlateDO> updateList = new ArrayList<>(); List<PlateDO> updateList = new ArrayList<>();
Long finalOrganId = organId;
importPlates.forEach(importPlate -> { importPlates.forEach(importPlate -> {
// 判断如果不存在,在进行插入 // 判断如果不存在,在进行插入
PlateDO existPlate = plateMapper.selectByGoodID(importPlate.getGoodsId(), organId); PlateDO existPlate = plateMapper.selectByGoodID(importPlate.getGoodsId(), finalOrganId);
if (existPlate == null) { if (existPlate == null) {
respVO.getCreatePlateNames().add(importPlate.getGoodsName()); respVO.getCreatePlateNames().add(importPlate.getGoodsName());
insertList.add(BeanUtils.toBean(importPlate, PlateDO.class).setOrganId(organId).setTexture(Boolean.valueOf(importPlate.getTexture()))); insertList.add(BeanUtils.toBean(importPlate, PlateDO.class).setOrganId(finalOrganId).setTexture(Boolean.valueOf(importPlate.getTexture())));
return; return;
} }
// 判断是否允许更新 // 判断是否允许更新
if (!isUpdateSupport) { if (!isUpdateSupport) {
respVO.getFailurePlateNames().put(importPlate.getGoodsName(), ErrorCodeConstants.PLATE_EXISTS.getMsg()); respVO.getFailurePlateNames().put(importPlate.getGoodsName(), ErrorCodeConstants.PLATE_EXISTS.getMsg());
PlateDO plateDO = BeanUtils.toBean(importPlate, PlateDO.class).setOrganId(organId).setId(existPlate.getId()).setTexture(Boolean.valueOf(importPlate.getTexture())); PlateDO plateDO = BeanUtils.toBean(importPlate, PlateDO.class).setOrganId(finalOrganId).setId(existPlate.getId()).setTexture(Boolean.valueOf(importPlate.getTexture()));
updateList.add(plateDO); updateList.add(plateDO);
} }
}); });
@@ -11,16 +11,18 @@ import com.cf.imes.framework.common.exception.ErrorCode;
public final class ErrorCodeConstants { public final class ErrorCodeConstants {
// ========== UREPORT template模块 1-003-001-000 ========== // ========== UREPORT template模块 1-003-001-000 ==========
public static ErrorCode TEMPLATE_NOT_EXISTS = new ErrorCode(1_003_001_001, "报表模板信息不存在"); public static final ErrorCode TEMPLATE_NOT_EXISTS = new ErrorCode(1_003_001_001, "报表模板信息不存在");
// ========== UREPORT datasource模块 1-003-002-000 ========== // ========== UREPORT datasource模块 1-003-002-000 ==========
public static ErrorCode DATASOURCE_NOT_EXISTS = new ErrorCode(1_003_002_001, "报表数据源不存在"); public static final ErrorCode DATASOURCE_NOT_EXISTS = new ErrorCode(1_003_002_001, "报表数据源不存在");
public static ErrorCode DATASOURCE_CONNECT_FAIL = new ErrorCode(1_003_002_001, "报表数据源连接失败"); public static final ErrorCode DATASOURCE_CONNECT_FAIL = new ErrorCode(1_003_002_002, "报表数据源连接失败");
public static final ErrorCode DATASOURCE_BUILDIN_OPERATION_PERMISSION_ERROR = new ErrorCode(1_003_002_003, "内置报表数据源只能由超级管理员操作");
// ========== UREPORT dataset模块 1-003-003-000 ========== // ========== UREPORT dataset模块 1-003-003-000 ==========
public static ErrorCode DATASET_NOT_EXISTS = new ErrorCode(1_003_003_001, "报表数据集不存在"); public static final ErrorCode DATASET_NOT_EXISTS = new ErrorCode(1_003_003_001, "报表数据集不存在");
public static ErrorCode DATASET_SQL_INJECTION_RISK = new ErrorCode(1_003_003_002, "存在SQL注入风险"); public static final ErrorCode DATASET_SQL_INJECTION_RISK = new ErrorCode(1_003_003_002, "存在SQL注入风险");
public static ErrorCode DATASET_SQL_REQUIRED = new ErrorCode(1_003_003_003, "SQL语句不能为空"); public static final ErrorCode DATASET_SQL_REQUIRED = new ErrorCode(1_003_003_003, "SQL语句不能为空");
public static ErrorCode DATASET_SQL_ILLEGAL = new ErrorCode(1_003_003_004, "SQL语句非法"); public static final ErrorCode DATASET_SQL_ILLEGAL = new ErrorCode(1_003_003_004, "SQL语句非法");
public static ErrorCode DATASET_GET_FIELDS_ERROR = new ErrorCode(1_003_003_005, "获取表字段"); public static final ErrorCode DATASET_GET_FIELDS_ERROR = new ErrorCode(1_003_003_005, "获取表字段");
} }
@@ -1,26 +0,0 @@
package com.cf.imes.module.report.enums.template;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 报表模板类型
*
* @author Gqr
* @since 2024/7/5 9:25
*/
@Getter
@AllArgsConstructor
public enum ReportTemplateTypeEnum {
/**
* 内置
*/
SYSTEM(1),
/**
* 自定义
*/
CUSTOM(2);
private final Integer type;
}
@@ -30,7 +30,7 @@ public class ReportDatasetSaveReqVO implements Serializable {
@Schema(description = "数据集名称", example = "测试数据集") @Schema(description = "数据集名称", example = "测试数据集")
private String name; private String name;
@Schema(description = "数据源id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "数据源id", example = "1")
private Long datasourceId; private Long datasourceId;
@Schema(description = "动态查询SQL") @Schema(description = "动态查询SQL")
@@ -95,6 +95,13 @@ public class ReportDatasourceController {
return success(datasourceService.loadBeanMethods(beanId)); return success(datasourceService.loadBeanMethods(beanId));
} }
@GetMapping("/buildin/datasources")
@Operation(summary = "获取内置数据源")
@PreAuthorize("@ss.hasPermission('report:datasource:query')")
public CommonResult<List<ReportDatasourceRespVO>> getBuildinDatasources() {
return success(BeanUtils.toBean(datasourceService.getBuildinDatasources(), ReportDatasourceRespVO.class));
}
@PostMapping("/datasource/connect") @PostMapping("/datasource/connect")
@Operation(summary = "测试数据源连接") @Operation(summary = "测试数据源连接")
@PreAuthorize("@ss.hasPermission('report:datasource:query')") @PreAuthorize("@ss.hasPermission('report:datasource:query')")
@@ -30,6 +30,6 @@ public class ReportDatasourceReqVO implements Serializable {
@Schema(description = "数据源名称", example = "测试库") @Schema(description = "数据源名称", example = "测试库")
private String name; private String name;
@Schema(description = "数据源类型,0 jdbc、1 buildin、2 spring", example = "jdbc") @Schema(description = "类型,0 内置、1 自定义", example = "1")
private Integer type; private Integer type;
} }
@@ -28,8 +28,11 @@ public class ReportDatasourceRespVO {
@Schema(description = "数据源名称", example = "测试库") @Schema(description = "数据源名称", example = "测试库")
private String name; private String name;
@Schema(description = "数据源类型,jdbc、buildin、spring", example = "jdbc") @Schema(description = "数据源类型,jdbc、spring", example = "jdbc")
private ReportDatasourceTypeEnum type; private ReportDatasourceTypeEnum dsType;
@Schema(description = "模板类型,0内置、1自定义", example = "0")
private Integer type;
@Schema(description = "数据源驱动类",example = "com.mysql.cj.jdbc.Driver") @Schema(description = "数据源驱动类",example = "com.mysql.cj.jdbc.Driver")
private String driver; private String driver;
@@ -2,13 +2,13 @@ package com.cf.imes.module.report.controller.admin.datasource.vo;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetSaveReqVO; import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetSaveReqVO;
import com.cf.imes.module.report.validation.datasource.ReportDatasourceTypeInEnum; import com.cf.imes.module.report.validation.datasource.ReportDatasourceTypeInEnum;
import com.cf.imes.module.report.validation.template.ReportTemplateTypeInEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
@@ -30,15 +30,19 @@ public class ReportDatasourceSaveReqVO implements Serializable {
@Schema(description = "数据源id", example = "1") @Schema(description = "数据源id", example = "1")
private Long id; private Long id;
@Schema(description = "模板id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "模板id", example = "1")
private Long templateId; private Long templateId;
@Schema(description = "数据源名称", example = "测试库") @Schema(description = "数据源名称", example = "测试库")
private String name; private String name;
@Schema(description = "数据源类型,jdbc、buildin、spring", example = "jdbc") @Schema(description = "模板类型,0内置、1自定义", example = "1")
@ReportTemplateTypeInEnum
private Integer type;
@Schema(description = "数据源类型,jdbc、spring", example = "jdbc")
@ReportDatasourceTypeInEnum @ReportDatasourceTypeInEnum
private String type; private String dsType;
@Schema(description = "spring型数据源id") @Schema(description = "spring型数据源id")
private String beanId; private String beanId;
@@ -18,11 +18,11 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
@@ -43,7 +43,7 @@ import static com.cf.imes.framework.common.pojo.CommonResult.success;
@Validated @Validated
@Slf4j @Slf4j
public class ReportTemplateController { public class ReportTemplateController {
@Autowired @Resource
private ReportTemplateService templateService; private ReportTemplateService templateService;
@PutMapping("/template") @PutMapping("/template")
@@ -28,7 +28,7 @@ public class ReportTemplateRespVO {
private String content; private String content;
@Schema(description = "模板类型,0内置、1自定义", example = "0") @Schema(description = "模板类型,0内置、1自定义", example = "0")
private Long type; private Integer type;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime; private LocalDateTime createTime;
@@ -1,6 +1,7 @@
package com.cf.imes.module.report.controller.admin.template.vo; package com.cf.imes.module.report.controller.admin.template.vo;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceSaveReqVO; import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceSaveReqVO;
import com.cf.imes.module.report.validation.template.ReportTemplateTypeInEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
@@ -34,6 +35,7 @@ public class ReportTemplateSaveReqVO {
private List<ReportDatasourceSaveReqVO> datasource = new ArrayList<>(); private List<ReportDatasourceSaveReqVO> datasource = new ArrayList<>();
@Schema(description = "模板类型,0内置、1自定义", example = "1") @Schema(description = "模板类型,0内置、1自定义", example = "1")
@ReportTemplateTypeInEnum
private Integer type; private Integer type;
@Schema(description = "备注", example = "该模板仅供生产使用") @Schema(description = "备注", example = "该模板仅供生产使用")
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import com.cf.imes.framework.mybatis.core.dataobject.BaseDO; import com.cf.imes.framework.mybatis.core.dataobject.BaseDO;
import com.cf.imes.module.report.dal.dataobject.dataset.ReportDatasetDO; import com.cf.imes.module.report.dal.dataobject.dataset.ReportDatasetDO;
import com.cf.imes.module.report.enums.datasource.ReportDatasourceTypeEnum; import com.cf.imes.module.report.enums.datasource.ReportDatasourceTypeEnum;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import lombok.*; import lombok.*;
import java.util.List; import java.util.List;
@@ -40,9 +41,15 @@ public class ReportDatasourceDO extends BaseDO {
*/ */
private String name; private String name;
/** /**
* 模板类型,jdbc、buildin、spring * 模板类型,jdbc、spring
*/ */
private ReportDatasourceTypeEnum type; private ReportDatasourceTypeEnum dsType;
/**
* 模板类型,0内置、1自定义
*/
private ReportTemplateTypeEnum type;
/** /**
* spring型数据源id * spring型数据源id
*/ */
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.*;
import com.cf.imes.framework.mybatis.core.dataobject.BaseDO; import com.cf.imes.framework.mybatis.core.dataobject.BaseDO;
import com.cf.imes.framework.mybatis.core.type.CompressStringTypeHandler; import com.cf.imes.framework.mybatis.core.type.CompressStringTypeHandler;
import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO; import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import lombok.*; import lombok.*;
import java.util.List; import java.util.List;
@@ -40,7 +41,7 @@ public class ReportTemplateDO extends BaseDO {
/** /**
* 模板类型,0内置、1自定义 * 模板类型,0内置、1自定义
*/ */
private Integer type; private ReportTemplateTypeEnum type;
/** /**
* 备注 * 备注
*/ */
@@ -14,8 +14,7 @@ import lombok.Getter;
@AllArgsConstructor @AllArgsConstructor
public enum ReportDatasourceTypeEnum { public enum ReportDatasourceTypeEnum {
JDBC(0,"jdbc"), JDBC(0,"jdbc"),
BUILDIN(1,"buildin"), SPRING(1,"spring");
SPRING(2,"spring");
@EnumValue @EnumValue
private final Integer code; private final Integer code;
@@ -0,0 +1,38 @@
package com.cf.imes.module.report.enums.template;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 报表模板类型
*
* @author Gqr
* @since 2024/7/5 9:25
*/
@Getter
@AllArgsConstructor
public enum ReportTemplateTypeEnum {
/**
* 内置
*/
SYSTEM(0),
/**
* 自定义
*/
CUSTOM(1);
@EnumValue
private final Integer type;
@JsonCreator
public static ReportTemplateTypeEnum fromType(Integer type) {
for (ReportTemplateTypeEnum value : values()) {
if (value.getType().equals(type)) {
return value;
}
}
return null;
}
}
@@ -1,11 +1,12 @@
package com.cf.imes.module.report.framework.rpc.config; package com.cf.imes.module.report.framework.rpc.config;
import com.cf.imes.module.executor.api.datasource.OrderDatasourceApi; import com.cf.imes.module.executor.api.datasource.OrderDatasourceApi;
import com.cf.imes.module.executor.api.datasource.OrderPartDatasourceApi;
import com.cf.imes.module.executor.api.datasource.OrderPlateDatasourceApi; import com.cf.imes.module.executor.api.datasource.OrderPlateDatasourceApi;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = {OrderPlateDatasourceApi.class, OrderDatasourceApi.class}) @EnableFeignClients(clients = {OrderPlateDatasourceApi.class, OrderDatasourceApi.class, OrderPartDatasourceApi.class})
public class RpcConfiguration { public class RpcConfiguration {
} }
@@ -1,29 +0,0 @@
package com.cf.imes.module.report.framework.ureport.bean;
import com.cf.imes.module.report.framework.ureport.annotation.CfReportSpringbeanDatasource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Gqr
* @since 2024/7/11 17:15
*/
@CfReportSpringbeanDatasource(value = "MockBeanDatasource", name = "模拟bean数据源")
public class MockBeanDatasource {
public List<Map<String, Object>> mockList(String dsName, String datasetName, Map<String, Object> parameters) {
List<Map<String, Object>> testList = new ArrayList<>();
testList.add(new HashMap<String, Object>() {{
put("a", "11111");
put("b", "一一一一一一");
}});
testList.add(new HashMap<String, Object>() {{
put("a", "22222");
put("b", "二二二二二二");
}});
return testList;
}
}
@@ -4,9 +4,12 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.pojo.CommonResult; import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.module.executor.api.datasource.OrderDatasourceApi; import com.cf.imes.module.executor.api.datasource.OrderDatasourceApi;
import com.cf.imes.module.executor.api.datasource.OrderPartDatasourceApi;
import com.cf.imes.module.executor.api.datasource.OrderPlateDatasourceApi; import com.cf.imes.module.executor.api.datasource.OrderPlateDatasourceApi;
import com.cf.imes.module.executor.api.datasource.dto.OrderDTO; import com.cf.imes.module.executor.api.datasource.dto.OrderDTO;
import com.cf.imes.module.executor.api.datasource.dto.PartGoodsRespDTO;
import com.cf.imes.module.executor.api.datasource.dto.PlateGoodsRespDTO; import com.cf.imes.module.executor.api.datasource.dto.PlateGoodsRespDTO;
import com.cf.imes.module.executor.enums.OrderPlateOpenDoorTypeEnum;
import com.cf.imes.module.report.framework.ureport.annotation.CfReportSpringbeanDatasource; import com.cf.imes.module.report.framework.ureport.annotation.CfReportSpringbeanDatasource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -33,7 +36,14 @@ public class OrderSummaryBeanDatasource {
@Resource @Resource
private OrderDatasourceApi orderDatasourceApi; private OrderDatasourceApi orderDatasourceApi;
@Resource
private OrderPartDatasourceApi orderPartDatasourceApi;
// 常用的字段key
private static final String ORDER_ID_FIELD_NAME = "orderId"; private static final String ORDER_ID_FIELD_NAME = "orderId";
private static final String BODY_NAME_FIELD_NAME = "bodyName";
private static final String ROOM_NAME_FIELD_NAME = "roomName";
private static final String REMARK_FIELD_NAME = "remark";
/** /**
* 生产单信息 * 生产单信息
@@ -44,6 +54,7 @@ public class OrderSummaryBeanDatasource {
* @return * @return
*/ */
public List<Map<String, Object>> orderInfo(String dsName, String datasetName, Map<String, Object> parameters) { public List<Map<String, Object>> orderInfo(String dsName, String datasetName, Map<String, Object> parameters) {
log.info("报表数据源[OrderSummaryBeanDatasource][orderInfo][{}][{}]调用开始", dsName, datasetName);
List<Map<String, Object>> resultList = new ArrayList<>(); List<Map<String, Object>> resultList = new ArrayList<>();
Object orderId = parameters.get(ORDER_ID_FIELD_NAME); Object orderId = parameters.get(ORDER_ID_FIELD_NAME);
@@ -57,32 +68,33 @@ public class OrderSummaryBeanDatasource {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
if (orderDTO != null) { if (orderDTO != null) {
// 把orderInfo转map并put到resultList中 HashMap<String, Object> resultMap = new HashMap<>();
resultList.add(new HashMap<>() {{
// 构造报表所需结构 // 构造报表所需结构
put(ORDER_ID_FIELD_NAME, orderDTO.getId()); resultMap.put(ORDER_ID_FIELD_NAME, orderDTO.getId());
put("customOrderNo", orderDTO.getCustomOrderNo()); resultMap.put("customOrderNo", orderDTO.getCustomOrderNo());
put("dealer", orderDTO.getDealer()); resultMap.put("dealer", orderDTO.getDealer());
LocalDateTime orderDate = orderDTO.getOrderDate(); LocalDateTime orderDate = orderDTO.getOrderDate();
put("orderDate", ObjectUtil.isNotNull(orderDate) ? formatter.format(orderDate) : null); resultMap.put("orderDate", ObjectUtil.isNotNull(orderDate) ? formatter.format(orderDate) : null);
LocalDateTime deliveryDate = orderDTO.getDeliveryDate(); LocalDateTime deliveryDate = orderDTO.getDeliveryDate();
put("deliveryDate", ObjectUtil.isNotNull(deliveryDate) ? formatter.format(deliveryDate) : null); resultMap.put("deliveryDate", ObjectUtil.isNotNull(deliveryDate) ? formatter.format(deliveryDate) : null);
put("customer", orderDTO.getCustomer()); resultMap.put("customer", orderDTO.getCustomer());
put("phoneNumber", orderDTO.getPhoneNumber()); resultMap.put("phoneNumber", orderDTO.getPhoneNumber());
}}); resultList.add(resultMap);
} }
log.info("报表数据源[OrderSummaryBeanDatasource][orderInfo][{}][{}]调用结束", dsName, datasetName);
return resultList; return resultList;
} }
/** /**
* 板材汇总-按板材 * 板材汇总
* *
* @param dsName * @param dsName
* @param datasetName * @param datasetName
* @param parameters * @param parameters
* @return * @return
*/ */
public List<Map<String, Object>> orderPlateSummaryByPlate(String dsName, String datasetName, Map<String, Object> parameters) { public List<Map<String, Object>> orderPlateGoodsSummary(String dsName, String datasetName, Map<String, Object> parameters) {
log.info("报表数据源[OrderSummaryBeanDatasource][orderPlateGoodsSummary][{}][{}]调用开始", dsName, datasetName);
List<Map<String, Object>> resultList = new ArrayList<>(); List<Map<String, Object>> resultList = new ArrayList<>();
Object orderId = parameters.get(ORDER_ID_FIELD_NAME); Object orderId = parameters.get(ORDER_ID_FIELD_NAME);
@@ -92,24 +104,159 @@ public class OrderSummaryBeanDatasource {
if (CollUtil.isNotEmpty(plateGoodsRespDTOS)) { if (CollUtil.isNotEmpty(plateGoodsRespDTOS)) {
plateGoodsRespDTOS.forEach(p -> { plateGoodsRespDTOS.forEach(p -> {
resultList.add(new HashMap<>() {{ Map<String, Object> resultMap = new HashMap<>();
BigDecimal area = p.getArea(); BigDecimal area = p.getArea();
Integer count = p.getCount(); Integer count = p.getCount();
// 构造报表所需结构 // 构造报表所需结构
put("roomName", p.getRoomName()); resultMap.put(ROOM_NAME_FIELD_NAME, p.getRoomName());
put("bodyName", p.getBodyName()); resultMap.put(BODY_NAME_FIELD_NAME, p.getBodyName());
put("thickness", p.getThickness()); resultMap.put("thickness", stripTrailingZeros(p.getThickness()));
put("name", p.getName()); resultMap.put("name", p.getName());
put("height", p.getHeight()); resultMap.put("height", stripTrailingZeros(p.getHeight()));
put("width", p.getWidth()); resultMap.put("width", stripTrailingZeros(p.getWidth()));
put("count", count); resultMap.put("count", count);
put("area", area); resultMap.put("area", stripTrailingZeros(area));
put("openDoorType", p.getOpenDoorType()); resultMap.put("openDoorType", OrderPlateOpenDoorTypeEnum.getDescFromCode(p.getOpenDoorType()));
put("remark", p.getRemark()); resultMap.put("material", p.getMaterial());
put("areaAll", area.multiply(BigDecimal.valueOf(count))); resultMap.put("color", p.getColor());
}}); resultMap.put(REMARK_FIELD_NAME, p.getRemark());
resultMap.put("areaAll", stripTrailingZeros(area.multiply(BigDecimal.valueOf(count))));
resultList.add(resultMap);
}); });
} }
log.info("报表数据源[OrderSummaryBeanDatasource][orderPlateGoodsSummary][{}][{}]调用结束", dsName, datasetName);
return resultList; return resultList;
} }
/**
* 板材明细
*
* @param dsName
* @param datasetName
* @param parameters
* @return
*/
public List<Map<String, Object>> orderPlateGoodsList(String dsName, String datasetName, Map<String, Object> parameters) {
log.info("报表数据源[OrderSummaryBeanDatasource][orderPlateGoodsList][{}][{}]调用开始", dsName, datasetName);
List<Map<String, Object>> resultList = new ArrayList<>();
Object orderId = parameters.get(ORDER_ID_FIELD_NAME);
// 查询统计信息
CommonResult<List<PlateGoodsRespDTO>> commonResult = orderPlateDatasourceApi.selectPlateGoodsList(Long.parseLong(orderId.toString()));
List<PlateGoodsRespDTO> plateGoodsRespDTOS = commonResult.getData();
if (CollUtil.isNotEmpty(plateGoodsRespDTOS)) {
plateGoodsRespDTOS.forEach(p -> {
Map<String, Object> resultMap = new HashMap<>();
BigDecimal area = p.getArea();
Integer count = p.getCount();
// 构造报表所需结构
resultMap.put("goodsName", p.getGoodsName());
resultMap.put("name", p.getName());
resultMap.put("material", p.getMaterial());
resultMap.put("color", p.getColor());
resultMap.put("goodsThickness", p.getGoodsThickness());
resultMap.put("count", p.getCount());
resultMap.put(ROOM_NAME_FIELD_NAME, p.getRoomName());
resultMap.put(BODY_NAME_FIELD_NAME, p.getBodyName());
resultMap.put("plateNo", p.getPlateNo());
resultMap.put("height", stripTrailingZeros(p.getHeight()));
resultMap.put("width", stripTrailingZeros(p.getWidth()));
resultMap.put("thickness", stripTrailingZeros(p.getThickness()));
resultMap.put("area", stripTrailingZeros(area));
resultMap.put(REMARK_FIELD_NAME, p.getRemark());
resultMap.put("isSpecialShaped", getBooleanString(p.getIsSpecialShaped()));
resultMap.put("isSculpt", getBooleanString(p.getIsSculpt()));
resultMap.put("openDoorType", OrderPlateOpenDoorTypeEnum.getDescFromCode(p.getOpenDoorType()));
resultMap.put("seal", String.join(",", stripTrailingZeros(p.getSealLeft()), stripTrailingZeros(p.getSealRight()), stripTrailingZeros(p.getSealUp()), stripTrailingZeros(p.getSealDown())));
resultMap.put("areaAll", stripTrailingZeros(area.multiply(BigDecimal.valueOf(count))));
resultList.add(resultMap);
});
}
log.info("报表数据源[OrderSummaryBeanDatasource][orderPlateGoodsList][{}][{}]调用结束", dsName, datasetName);
return resultList;
}
public List<Map<String, Object>> orderPartDetailList(String dsName, String datasetName, Map<String, Object> parameters) {
log.info("报表数据源[OrderSummaryBeanDatasource][orderPartDetailList][{}][{}]调用开始", dsName, datasetName);
List<Map<String, Object>> resultList = new ArrayList<>();
Object orderId = parameters.get(ORDER_ID_FIELD_NAME);
CommonResult<List<PartGoodsRespDTO>> commonResult = orderPartDatasourceApi.selectPartDetail(Long.parseLong(orderId.toString()));
List<PartGoodsRespDTO> partGoodsRespDTOS = commonResult.getData();
if (CollUtil.isNotEmpty(partGoodsRespDTOS)) {
partGoodsRespDTOS.forEach(p -> {
Map<String, Object> resultMap = new HashMap<>();
// 构造报表所需结构
resultMap.put(ROOM_NAME_FIELD_NAME, p.getRoomName());
resultMap.put(BODY_NAME_FIELD_NAME, p.getBodyName());
resultMap.put("name", p.getName());
resultMap.put("brand", p.getBrand());
resultMap.put("factory", p.getFactory());
resultMap.put("model", p.getModel());
resultMap.put("spec", p.getSpec());
resultMap.put("num", p.getNum());
resultMap.put("unit", p.getUnit());
resultMap.put("price", p.getPrice());
resultMap.put("totalPrice", p.getTotalPrice());
resultMap.put(REMARK_FIELD_NAME, p.getRemark());
resultList.add(resultMap);
});
}
log.info("报表数据源[OrderSummaryBeanDatasource][orderPartDetailList][{}][{}]调用结束", dsName, datasetName);
return resultList;
}
public List<Map<String, Object>> orderPartSummary(String dsName, String datasetName, Map<String, Object> parameters) {
log.info("报表数据源[OrderSummaryBeanDatasource][orderPartSummary][{}][{}]调用开始", dsName, datasetName);
List<Map<String, Object>> resultList = new ArrayList<>();
Object orderId = parameters.get(ORDER_ID_FIELD_NAME);
CommonResult<List<PartGoodsRespDTO>> commonResult = orderPartDatasourceApi.selectPartAll(Long.parseLong(orderId.toString()));
List<PartGoodsRespDTO> partGoodsRespDTOS = commonResult.getData();
if (CollUtil.isNotEmpty(partGoodsRespDTOS)) {
partGoodsRespDTOS.forEach(p -> {
Map<String, Object> resultMap = new HashMap<>();
// 构造报表所需结构
resultMap.put("name", p.getName());
resultMap.put("model", p.getModel());
resultMap.put("spec", p.getSpec());
resultMap.put("num", p.getNum());
resultMap.put("unit", p.getUnit());
resultMap.put("brand", p.getBrand());
resultMap.put(REMARK_FIELD_NAME, p.getRemark());
resultList.add(resultMap);
});
}
log.info("报表数据源[OrderSummaryBeanDatasource][orderPartSummary][{}][{}]调用结束", dsName, datasetName);
return resultList;
}
/**
* 移除尾数的0600.000 -> 600
*
* @param data
* @return
*/
private String stripTrailingZeros(BigDecimal data) {
if (ObjectUtil.isNotNull(data)) {
return data.stripTrailingZeros().toPlainString();
} else {
return "";
}
}
/**
* 布尔值转是否
*
* @param b
* @return
*/
private String getBooleanString(Boolean b) {
return b ? "" : "";
}
} }
@@ -31,7 +31,6 @@ public class CfReportSpringbeanRegistrar implements ImportBeanDefinitionRegistra
Set<BeanDefinition> candidates = scanner.findCandidateComponents("com.cf.imes.module.report.framework.ureport.bean"); Set<BeanDefinition> candidates = scanner.findCandidateComponents("com.cf.imes.module.report.framework.ureport.bean");
for (BeanDefinition candidate : candidates) { for (BeanDefinition candidate : candidates) {
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotatedBeanDefinition beanDef = (AnnotatedBeanDefinition) candidate; AnnotatedBeanDefinition beanDef = (AnnotatedBeanDefinition) candidate;
Map<String, Object> attributes = beanDef.getMetadata().getAnnotationAttributes(CfReportSpringbeanDatasource.class.getName()); Map<String, Object> attributes = beanDef.getMetadata().getAnnotationAttributes(CfReportSpringbeanDatasource.class.getName());
if (attributes != null) { if (attributes != null) {
@@ -45,7 +44,9 @@ public class CfReportSpringbeanRegistrar implements ImportBeanDefinitionRegistra
if (!StringUtils.hasText(name)) { if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("@CfReportSpringbean requires a non-empty 'name' attribute."); throw new IllegalArgumentException("@CfReportSpringbean requires a non-empty 'name' attribute.");
} }
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(candidate.getBeanClassName()); String beanClassName = candidate.getBeanClassName();
if (StringUtils.hasText(beanClassName)) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClassName);
registry.registerBeanDefinition(value, builder.getBeanDefinition()); registry.registerBeanDefinition(value, builder.getBeanDefinition());
} }
} }
@@ -63,6 +63,12 @@ public interface ReportDatasourceService {
*/ */
List<ReportBeanDatasourceRespVO> getBeanDatasourceList(); List<ReportBeanDatasourceRespVO> getBeanDatasourceList();
/**
* 获取内置数据源列表
* @return
*/
List<ReportDatasourceDO> getBuildinDatasources();
/** /**
* 获取bean方法 * 获取bean方法
* *
@@ -1,7 +1,9 @@
package com.cf.imes.module.report.service.datasource; package com.cf.imes.module.report.service.datasource;
import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import com.bstek.ureport.definition.dataset.Field; import com.bstek.ureport.definition.dataset.Field;
import com.bstek.ureport.definition.dataset.Parameter; import com.bstek.ureport.definition.dataset.Parameter;
import com.bstek.ureport.definition.dataset.SqlDatasetDefinition; import com.bstek.ureport.definition.dataset.SqlDatasetDefinition;
@@ -9,15 +11,23 @@ import com.bstek.ureport.utils.ProcedureUtils;
import com.cf.imes.framework.common.exception.ServiceException; import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.util.object.BeanUtils; import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX; import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.organ.core.aop.OrganIgnore;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetFieldVO; import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetFieldVO;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetParameterVO; import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetParameterVO;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetReqVO;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportBeanDatasourceRespVO; import com.cf.imes.module.report.controller.admin.datasource.vo.ReportBeanDatasourceRespVO;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceReqVO; import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceReqVO;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceSaveReqVO; import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceSaveReqVO;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceTestConnReqVO; import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceTestConnReqVO;
import com.cf.imes.module.report.dal.dataobject.dataset.ReportDatasetDO;
import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO; import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO;
import com.cf.imes.module.report.dal.mysql.dataset.ReportDatasetMapper;
import com.cf.imes.module.report.dal.mysql.datasource.ReportDatasourceMapper; import com.cf.imes.module.report.dal.mysql.datasource.ReportDatasourceMapper;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import com.cf.imes.module.report.framework.ureport.annotation.CfReportSpringbeanDatasource; import com.cf.imes.module.report.framework.ureport.annotation.CfReportSpringbeanDatasource;
import com.cf.imes.module.report.service.dataset.ReportDatasetService;
import com.cf.imes.module.report.util.SqlInjectionUtil; import com.cf.imes.module.report.util.SqlInjectionUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -48,6 +58,7 @@ import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_GET_FIE
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_ILLEGAL; import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_ILLEGAL;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_INJECTION_RISK; import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_INJECTION_RISK;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_REQUIRED; import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_REQUIRED;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_BUILDIN_OPERATION_PERMISSION_ERROR;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_NOT_EXISTS; import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_NOT_EXISTS;
/** /**
@@ -63,6 +74,12 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
@Resource @Resource
private ReportDatasourceMapper datasourceMapper; private ReportDatasourceMapper datasourceMapper;
@Resource
private ReportDatasetService datasetService;
@Resource
private ReportDatasetMapper datasetMapper;
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
public ReportDatasourceServiceImpl(ApplicationContext applicationContext) { public ReportDatasourceServiceImpl(ApplicationContext applicationContext) {
@@ -71,9 +88,13 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
@Override @Override
public Long createDatasource(ReportDatasourceSaveReqVO createReqVO) { public Long createDatasource(ReportDatasourceSaveReqVO createReqVO) {
// 插入
ReportDatasourceDO datasource = BeanUtils.toBean(createReqVO, ReportDatasourceDO.class); ReportDatasourceDO datasource = BeanUtils.toBean(createReqVO, ReportDatasourceDO.class);
// 校验内置数据源操作权限
validateBuildinsource(datasource);
// 插入
datasourceMapper.insert(datasource); datasourceMapper.insert(datasource);
// 解析数据集
analyzeDataset(datasource);
// 返回 // 返回
return datasource.getId(); return datasource.getId();
} }
@@ -82,9 +103,54 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
public void updateDatasource(ReportDatasourceSaveReqVO updateReqVO) { public void updateDatasource(ReportDatasourceSaveReqVO updateReqVO) {
// 校验存在 // 校验存在
validateDatasourceExists(updateReqVO.getId()); validateDatasourceExists(updateReqVO.getId());
// 更新
ReportDatasourceDO updateObj = BeanUtils.toBean(updateReqVO, ReportDatasourceDO.class); ReportDatasourceDO updateObj = BeanUtils.toBean(updateReqVO, ReportDatasourceDO.class);
// 校验内置数据源操作权限
validateBuildinsource(updateObj);
// 更新
datasourceMapper.updateById(updateObj); datasourceMapper.updateById(updateObj);
// 解析数据集
analyzeDataset(updateObj);
}
/**
* 校验只有超管可以操作内置数据源
*/
private void validateBuildinsource(ReportDatasourceDO datasource) {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
boolean isSuperAdmin = loginUser != null && loginUser.getIsSupAdmin();
// 非超管不能操作内置数据源
if (ReportTemplateTypeEnum.SYSTEM.equals(datasource.getType()) && !isSuperAdmin) {
throw exception(DATASOURCE_BUILDIN_OPERATION_PERMISSION_ERROR);
}
}
/**
* 解析数据源下的数据集
*
* @param datasource
*/
private void analyzeDataset(ReportDatasourceDO datasource) {
List<ReportDatasetDO> datasets = datasource.getDatasets();
List<ReportDatasetDO> batchInsertDataset = new ArrayList<>();
List<ReportDatasetDO> batchupdateDataset = new ArrayList<>();
datasets.forEach(dataset -> {
// 更新/新增数据集
if (ObjectUtil.isNotNull(dataset.getId())) {
batchupdateDataset.add(dataset);
} else {
dataset.setDatasourceId(datasource.getId());
batchInsertDataset.add(dataset);
}
});
// 批量插入数据集
if (CollUtil.isNotEmpty(batchInsertDataset)) {
datasetMapper.insertBatch(batchInsertDataset);
}
// 批量更新数据集
if (CollUtil.isNotEmpty(batchupdateDataset)) {
datasetMapper.updateBatch(batchupdateDataset);
}
} }
@Override @Override
@@ -108,7 +174,10 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
@Override @Override
public ReportDatasourceDO getDatasource(Long id) { public ReportDatasourceDO getDatasource(Long id) {
return datasourceMapper.selectById(id); ReportDatasourceDO reportDatasourceDO = datasourceMapper.selectById(id);
// 查询数据集
queryDatasetInSource(reportDatasourceDO);
return reportDatasourceDO;
} }
@Override @Override
@@ -116,8 +185,14 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
List<ReportDatasourceDO> reportDatasourceDOS = datasourceMapper.selectList(new LambdaQueryWrapperX<ReportDatasourceDO>() List<ReportDatasourceDO> reportDatasourceDOS = datasourceMapper.selectList(new LambdaQueryWrapperX<ReportDatasourceDO>()
.eqIfPresent(ReportDatasourceDO::getTemplateId, reqVO.getTemplateId()) .eqIfPresent(ReportDatasourceDO::getTemplateId, reqVO.getTemplateId())
.likeIfPresent(ReportDatasourceDO::getName, reqVO.getName()) .likeIfPresent(ReportDatasourceDO::getName, reqVO.getName())
.eqIfPresent(ReportDatasourceDO::getType, reqVO.getType()) .eqIfPresent(ReportDatasourceDO::getDsType, reqVO.getType())
.orderByDesc(ReportDatasourceDO::getCreateTime)); .orderByDesc(ReportDatasourceDO::getCreateTime));
// 查询数据集
if (CollUtil.isNotEmpty(reportDatasourceDOS)) {
reportDatasourceDOS.forEach(reportDatasourceDO -> {
queryDatasetInSource(reportDatasourceDO);
});
}
return reportDatasourceDOS; return reportDatasourceDOS;
} }
@@ -135,6 +210,21 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
return beanDatasourceRespVOList; return beanDatasourceRespVOList;
} }
@Override
@OrganIgnore
public List<ReportDatasourceDO> getBuildinDatasources() {
List<ReportDatasourceDO> reportDatasourceDOS = datasourceMapper.selectList(new LambdaQueryWrapperX<ReportDatasourceDO>()
.eqIfPresent(ReportDatasourceDO::getType, ReportTemplateTypeEnum.SYSTEM)
.orderByDesc(ReportDatasourceDO::getCreateTime));
// 查询数据集
if (CollUtil.isNotEmpty(reportDatasourceDOS)) {
reportDatasourceDOS.forEach(reportDatasourceDO -> {
queryDatasetInSource(reportDatasourceDO);
});
}
return reportDatasourceDOS;
}
@Override @Override
public List<String> loadBeanMethods(String beanId) { public List<String> loadBeanMethods(String beanId) {
Object obj = applicationContext.getBean(beanId); Object obj = applicationContext.getBean(beanId);
@@ -222,13 +312,14 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
} }
// 获取数据库连接 // 获取数据库连接
conn = buildConn(reqVO); conn = buildConn(reqVO);
// 校验sql // 自定义工具校验sql
if (SqlInjectionUtil.checkEditSql(sql)) { if (SqlInjectionUtil.checkEditSql(sql)) {
throw exception(DATASET_SQL_ILLEGAL); throw exception(DATASET_SQL_ILLEGAL);
} }
// 检查参数sql注入 // 检查参数sql注入
for (ReportDatasetParameterVO parameterVO : parameters) { for (ReportDatasetParameterVO parameterVO : parameters) {
if (SqlInjectionUtil.checkParam(parameterVO.getDefaultValue())) { // mybatis-plus util检查参数
if (SqlInjectionUtils.check(parameterVO.getDefaultValue())) {
throw exception(DATASET_SQL_INJECTION_RISK); throw exception(DATASET_SQL_INJECTION_RISK);
} }
} }
@@ -292,4 +383,17 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService {
} }
return conn; return conn;
} }
/**
* 查询数据源下的数据集
*
* @param reportDatasourceDO
*/
private void queryDatasetInSource(ReportDatasourceDO reportDatasourceDO) {
if (ObjectUtil.isNotNull(reportDatasourceDO)) {
// 查询数据集
List<ReportDatasetDO> datasetList = datasetService.getDatasetList(ReportDatasetReqVO.builder().datasourceId(reportDatasourceDO.getId()).build());
reportDatasourceDO.setDatasets(datasetList);
}
}
} }
@@ -217,8 +217,8 @@ public class ReportTemplateServiceImpl implements ReportTemplateService {
// 查询数据集 // 查询数据集
List<ReportDatasetDO> datasetList = datasetService.getDatasetList(ReportDatasetReqVO.builder().datasourceId(ds.getId()).build()); List<ReportDatasetDO> datasetList = datasetService.getDatasetList(ReportDatasetReqVO.builder().datasourceId(ds.getId()).build());
List<DatasetDefinition> datasetDefinitions = new ArrayList<>(); List<DatasetDefinition> datasetDefinitions = new ArrayList<>();
switch (ds.getType()) { switch (ds.getDsType()) {
case JDBC, BUILDIN -> { case JDBC -> {
// 转换对应的ureport对象 // 转换对应的ureport对象
JdbcDatasourceDefinition jdbcDatasourceDefinition = BeanUtils.toBean(ds, JdbcDatasourceDefinition.class); JdbcDatasourceDefinition jdbcDatasourceDefinition = BeanUtils.toBean(ds, JdbcDatasourceDefinition.class);
datasetDefinitions.addAll(BeanUtils.toBean(datasetList, SqlDatasetDefinition.class)); datasetDefinitions.addAll(BeanUtils.toBean(datasetList, SqlDatasetDefinition.class));
@@ -16,12 +16,6 @@ public class SqlInjectionUtil {
private static final Pattern SQL_EDIT_PATTERN = Pattern.compile("(insert|delete|update|create|drop|truncate|grant|alter|deny|revoke|call|execute|exec|declare|show|rename|set)" + private static final Pattern SQL_EDIT_PATTERN = Pattern.compile("(insert|delete|update|create|drop|truncate|grant|alter|deny|revoke|call|execute|exec|declare|show|rename|set)" +
"\\s+.*(into|from|set|where|table|database|view|index|on|cursor|procedure|trigger|for|password|union|and|or)", Pattern.CASE_INSENSITIVE); "\\s+.*(into|from|set|where|table|database|view|index|on|cursor|procedure|trigger|for|password|union|and|or)", Pattern.CASE_INSENSITIVE);
/**
* SQL语法检查正则:符合两个关键字(有先后顺序)才算匹配
*/
private static final Pattern SQL_SYNTAX_PATTERN = Pattern.compile("(insert|delete|update|select|create|drop|truncate|grant|alter|deny|revoke|call|execute|exec|declare|show|rename|set)" +
"\\s+.*(into|from|set|where|table|database|view|index|on|cursor|procedure|trigger|for|password|union|and|or)|(select\\s*\\*\\s*from\\s+)|(and|or)\\s+.*(like|=|>|<|in|between|is|not|exists)", Pattern.CASE_INSENSITIVE);
/** /**
* 使用'、;或注释截断SQL检查正则 * 使用'、;或注释截断SQL检查正则
*/ */
@@ -38,26 +32,6 @@ public class SqlInjectionUtil {
return SQL_COMMENT_PATTERN.matcher(sql).find() || SQL_EDIT_PATTERN.matcher(sql).find(); return SQL_COMMENT_PATTERN.matcher(sql).find() || SQL_EDIT_PATTERN.matcher(sql).find();
} }
/** private SqlInjectionUtil() {
* 检查参数是否存在 SQL 注入
*
* @param value 检查参数
* @return true 非法 false 合法
*/
public static boolean checkParam(String value) {
Objects.requireNonNull(value);
// 处理是否包含SQL注释字符 || 检查是否包含SQL注入敏感字符
return SQL_COMMENT_PATTERN.matcher(value).find() || SQL_SYNTAX_PATTERN.matcher(value).find();
}
/**
* 刪除字段转义符单引号双引号
*
* @param text 待处理字段
* @return
*/
public static String removeEscapeCharacter(String text) {
Objects.nonNull(text);
return text.replace("\"", "").replace("'", "");
} }
} }
@@ -28,10 +28,9 @@ import java.lang.annotation.Target;
validatedBy = {ReportDatasourceTypeInEnumValidator.class} validatedBy = {ReportDatasourceTypeInEnumValidator.class}
) )
public @interface ReportDatasourceTypeInEnum { public @interface ReportDatasourceTypeInEnum {
String message() default "数据源类型[type]错误,请检查是否jdbc/buildin/spring"; String message() default "数据源类型[dsType]错误,请检查是否jdbc/spring";
Class<?>[] groups() default {}; Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {}; Class<? extends Payload>[] payload() default {};
} }
@@ -2,7 +2,6 @@ package com.cf.imes.module.report.validation.datasource;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.module.report.enums.datasource.ReportDatasourceTypeEnum; import com.cf.imes.module.report.enums.datasource.ReportDatasourceTypeEnum;
import org.apache.commons.lang3.StringUtils;
import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext; import javax.validation.ConstraintValidatorContext;
@@ -22,9 +21,6 @@ public class ReportDatasourceTypeInEnumValidator implements ConstraintValidator<
@Override @Override
public boolean isValid(String value, ConstraintValidatorContext context) { public boolean isValid(String value, ConstraintValidatorContext context) {
if (StringUtils.isEmpty(value)) {
return true;
} else {
ReportDatasourceTypeEnum byType = ReportDatasourceTypeEnum.fromDesc(value); ReportDatasourceTypeEnum byType = ReportDatasourceTypeEnum.fromDesc(value);
if (ObjectUtil.isNotNull(byType)) { if (ObjectUtil.isNotNull(byType)) {
return true; return true;
@@ -32,7 +28,6 @@ public class ReportDatasourceTypeInEnumValidator implements ConstraintValidator<
return false; return false;
} }
} }
}
} }
@@ -0,0 +1,36 @@
package com.cf.imes.module.report.validation.template;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义报表模板类型入参校验注解
*
* @author Gqr
* @since 2024/7/17 9:33
*/
@Target({
ElementType.METHOD,
ElementType.FIELD,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.PARAMETER,
ElementType.TYPE_USE
})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(
validatedBy = {ReportTemplateTypeInEnumValidator.class}
)
public @interface ReportTemplateTypeInEnum {
String message() default "类型[type]错误,请检查是否0(内置)/1(自定义)";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@@ -0,0 +1,33 @@
package com.cf.imes.module.report.validation.template;
import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* 自定义报表模板类型入参校验器
*
* @author Gqr
* @since 2024/7/17 9:33
*/
public class ReportTemplateTypeInEnumValidator implements ConstraintValidator<ReportTemplateTypeInEnum, Integer> {
@Override
public void initialize(ReportTemplateTypeInEnum constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
ReportTemplateTypeEnum byType = ReportTemplateTypeEnum.fromType(value);
if (ObjectUtil.isNotNull(byType)) {
return true;
} else {
return false;
}
}
}
@@ -84,6 +84,8 @@ jeecg:
--- #################### 晨丰相关配置 #################### --- #################### 晨丰相关配置 ####################
chenfeng: chenfeng:
dynamicDataSource:
enabled: true
info: info:
version: 1.0.0 version: 1.0.0
base-package: com.cf.imes.module.report base-package: com.cf.imes.module.report
@@ -42,7 +42,8 @@ public abstract class ReportCommonServiceImplTest {
.id(id) .id(id)
.name("单元测试数据源") .name("单元测试数据源")
.templateId(reportId) .templateId(reportId)
.type(ReportDatasourceTypeEnum.SPRING.getDesc()) .dsType(ReportDatasourceTypeEnum.SPRING.getDesc())
.type(ReportTemplateTypeEnum.SYSTEM.getType())
.driver("com.mysql.cj.jdbc.Driver") .driver("com.mysql.cj.jdbc.Driver")
.url("jdbc:mysql://192.168.1.205:3307/imes_base?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true") .url("jdbc:mysql://192.168.1.205:3307/imes_base?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true")
.username("root") .username("root")
@@ -88,7 +88,7 @@ public class ReportDatasetServiceImplTest extends ReportCommonServiceImplTest{
List<ReportDatasetDO> datasetList = reportDatasetService.getDatasetList(reqVO); List<ReportDatasetDO> datasetList = reportDatasetService.getDatasetList(reqVO);
assertNotNull(datasetList); assertNotNull(datasetList);
assertNotEquals(datasetList.size(), 0); assertNotEquals(0, datasetList.size());
// 删除 // 删除
reportTemplateService.deleteReportTemplate(templateId); reportTemplateService.deleteReportTemplate(templateId);
@@ -73,7 +73,7 @@ public class ReportDatasourceServiceImplTest extends ReportCommonServiceImplTest
List<ReportDatasourceDO> templateDatasourceList = reportDatasourceService.getTemplateDatasourceList(reqVO); List<ReportDatasourceDO> templateDatasourceList = reportDatasourceService.getTemplateDatasourceList(reqVO);
assertNotNull(templateDatasourceList); assertNotNull(templateDatasourceList);
assertNotEquals(templateDatasourceList.size(),0); assertNotEquals(0, templateDatasourceList.size());
// 删除 // 删除
reportTemplateService.deleteReportTemplate(templateId); reportTemplateService.deleteReportTemplate(templateId);
@@ -44,7 +44,7 @@ public class ReportTemplateServiceImplTest extends ReportCommonServiceImplTest {
String templateUnZip = template.getContent(); String templateUnZip = template.getContent();
System.out.println("从库中读取template大小:" + templateUnZip.length()); System.out.println("从库中读取template大小:" + templateUnZip.length());
// 校验解压内容是否正确 // 校验解压内容是否正确
assertEquals(JsonUtil.unzipString(templateUnZip),TEMPLATE); assertEquals(TEMPLATE, JsonUtil.unzipString(templateUnZip));
// 删除 // 删除
reportTemplateService.deleteReportTemplate(templateId); reportTemplateService.deleteReportTemplate(templateId);
} }
@@ -120,8 +120,8 @@ public class ReportTemplateServiceImplTest extends ReportCommonServiceImplTest {
List<ReportTemplateDO> reportTemplateList = reportTemplateService.getReportTemplateList(reqVO); List<ReportTemplateDO> reportTemplateList = reportTemplateService.getReportTemplateList(reqVO);
assertNotNull(reportTemplateList); assertNotNull(reportTemplateList);
assertNotEquals(reportTemplateList.size(),0); assertNotEquals(0, reportTemplateList.size());
assertEquals(reportTemplateList.get(0).getOrganId(),2L); assertEquals(2L, reportTemplateList.get(0).getOrganId());
// 删除 // 删除
reportTemplateService.deleteReportTemplate(templateId); reportTemplateService.deleteReportTemplate(templateId);
@@ -48,4 +48,17 @@ public interface PermissionApi {
@Parameter(name = "userId", description = "用户编号", example = "2", required = true) @Parameter(name = "userId", description = "用户编号", example = "2", required = true)
CommonResult<DeptDataPermissionRespDTO> getDeptDataPermission(@RequestParam("userId") Long userId); CommonResult<DeptDataPermissionRespDTO> getDeptDataPermission(@RequestParam("userId") Long userId);
@GetMapping(PREFIX + "/user-role-id-list-by-user-id")
@Operation(summary = "获得拥有多个角色的用户编号集合")
@Parameter(name = "roleIds", description = "角色编号集合", example = "1,2", required = true)
CommonResult<Set<String>> getUserRoleIdListByUserIds(@RequestParam("userId") Long userId);
@GetMapping(PREFIX + "/has-roles")
@Operation(summary = "判断用户是否拥有权限标识")
@Parameters({
@Parameter(name = "userId", description = "用户编号", example = "1", required = true),
@Parameter(name = "permission", description = "权限标识", example = "2", required = true)
})
CommonResult<Boolean> hasRoles(@RequestParam("userId") Long userId,
@RequestParam("permission") String permission);
} }
@@ -40,4 +40,15 @@ public class PermissionApiImpl implements PermissionApi {
return success(deptDataPermission); return success(deptDataPermission);
} }
@Override
public CommonResult<Set<String>> getUserRoleIdListByUserIds(Long userId) {
return success(permissionService.getUserPermissions(userId));
}
@Override
public CommonResult<Boolean> hasRoles(Long userId, String permission) {
Set<String> permissions = permissionService.getUserPermissions(userId);
return success(permissions.contains(permission));
}
} }
@@ -171,4 +171,14 @@ public interface PermissionService {
* @return * @return
*/ */
Set<Long> getListRoleUsers(Long roleId, Long organId); Set<Long> getListRoleUsers(Long roleId, Long organId);
/**
* 获取用户权限标识
*/
Set<String> getUserPermissions(Long userId);
/**
* 判断用户是否拥有相对应的权限
*/
Boolean hasPermission(Long userId, String permission);
} }
@@ -18,6 +18,7 @@ import com.cf.imes.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import com.cf.imes.module.system.constants.permission.InternalRoleConstants; import com.cf.imes.module.system.constants.permission.InternalRoleConstants;
import com.cf.imes.module.system.controller.admin.permission.vo.permission.PermissionAssignRoleUserReqVO; import com.cf.imes.module.system.controller.admin.permission.vo.permission.PermissionAssignRoleUserReqVO;
import com.cf.imes.module.system.controller.admin.permission.vo.permission.PermissionAssignUserRoleReqVO; import com.cf.imes.module.system.controller.admin.permission.vo.permission.PermissionAssignUserRoleReqVO;
import com.cf.imes.module.system.convert.auth.AuthConvert;
import com.cf.imes.module.system.dal.dataobject.permission.MenuDO; import com.cf.imes.module.system.dal.dataobject.permission.MenuDO;
import com.cf.imes.module.system.dal.dataobject.permission.RoleDO; import com.cf.imes.module.system.dal.dataobject.permission.RoleDO;
import com.cf.imes.module.system.dal.dataobject.permission.RoleMenuDO; import com.cf.imes.module.system.dal.dataobject.permission.RoleMenuDO;
@@ -53,8 +54,10 @@ import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.common.util.collection.CollectionUtils.convertSet; import static com.cf.imes.framework.common.util.collection.CollectionUtils.convertSet;
import static com.cf.imes.framework.common.util.json.JsonUtils.toJsonString; import static com.cf.imes.framework.common.util.json.JsonUtils.toJsonString;
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.ROLE_ME_ERROR; import static com.cf.imes.module.system.enums.ErrorCodeConstants.ROLE_ME_ERROR;
/** /**
@@ -596,4 +599,31 @@ public class PermissionServiceImpl implements PermissionService {
return SpringUtil.getBean(getClass()); return SpringUtil.getBean(getClass());
} }
/**
* 获取用户权限标识
*/
@Override
public Set<String> getUserPermissions(Long userId) {
// 1.2 获得角色列表
Set<Long> roleIds = getUserRoleIdListByUserId(getLoginUserId());
if (CollUtil.isEmpty(roleIds)) {
return Collections.emptySet();
}
List<RoleDO> roles = roleService.getRoleList1(roleIds);
roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色
// 1.3 获得菜单列表
Set<Long> menuIds = getRoleMenuListByRoleId2(convertSet(roles, RoleDO::getId));
List<MenuDO> menuList = menuService.getMenuList1(menuIds);
menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单
return convertSet(menuList, MenuDO::getPermission);
}
@Override
public Boolean hasPermission(Long userId, String permission) {
Set<String> permissions = getUserPermissions(userId);
return permissions.contains(permission);
}
} }