附件小组件

附件小组件

处理主要逻辑的接口

public interface AttachmentFacadeService {
    <T> Boolean addAttachments(Collection<AddAttachmentParam> attachments,String identification);

    <T> KlPage<AttachmentDto> pageAttachments(PageAttachmentParam param);

    <T> List<AttachmentDto> listAttachments(ListAttachmentParam param);

    <T> Boolean removeAttachment(DeleteAttachmentParam param);

    default void updateAttachments(String identification,Integer associatedId,Collection<AddAttachmentParam> attachments){
        DeleteAttachmentParam deleteAttachmentParam = new DeleteAttachmentParam();
        deleteAttachmentParam.setId(associatedId);
        deleteAttachmentParam.setIdentification(identification);

        removeAttachment(deleteAttachmentParam);
        if(CollUtil.isNotEmpty(attachments)){
            attachments.forEach(attachment -> attachment.setAssociatedId(associatedId));
            addAttachments(attachments,identification);
        }
    }
}

当做标记的接口

 public interface AttachmentService {
	// 为了做支持实现这个接口的自定义身份标识(也就是后面存在iServiceMap中的key)
    default String getIdentification(){
        return null;
    }
}

实现类

@Component
public class AttachmentFacadeServiceImpl implements AttachmentFacadeService {
    private final Map<String,IService<?>> iServiceMap = new HashMap();

	// 通过构造函数和构造器注入方式初始化iServiceMap
    public AttachmentFacadeServiceImpl(List<AttachmentService> attachmentServices){
		// 将所有实现了AttachmentService的ServiceImpl存到iServiceMap中
        for (AttachmentService attachmentService : attachmentServices) {
            Object o = AopProxyUtils.getSingletonTarget(attachmentService);
            if(o instanceof IService){
                String identification = attachmentService.getIdentification();
                IService<?> iService = (IService<?>) o;
                iServiceMap.put(identification == null ? iService.getEntityClass().getSimpleName() : identification,iService);
            }
        }
    }

    @Override
    public <T> Boolean addAttachments(Collection<AddAttachmentParam> attachments, String identification) {
        if(CollUtil.isEmpty(attachments)){
            return true;
        }
        IService<T> iService = getIService(identification);
        return iService.saveBatch(attachments.stream()
                .map(param -> {
                    T entity = BeanUtils.instantiateClass(iService.getEntityClass());
                    BeanUtils.copyProperties(param,entity);
                    return entity;
                })
                .collect(Collectors.toList()));
    }

    @Override
    public <T> KlPage<AttachmentDto> pageAttachments(PageAttachmentParam param) {
        IService<T> iService = this.<T>getIService(param.getIdentification());
        return KlPage.<T,AttachmentDto>pageConvertKlPage(iService.page(param.<T>buildPage(), Wrappers.<T>query().eq("associated_id", param.getAssociatedId())), entity -> {
            AttachmentDto attachment = new AttachmentDto();
            BeanUtils.copyProperties(entity,attachment);
            return attachment;
        });
    }

    @Override
    public <T> List<AttachmentDto> listAttachments(ListAttachmentParam param) {
        IService<T> iService = getIService(param.getIdentification());
        return iService.list(Wrappers.<T>query().eq("associated_id", param.getId()))
                .stream()
                .map(entity -> {
                    AttachmentDto attachment = new AttachmentDto();
                    BeanUtils.copyProperties(entity,attachment);
                    return attachment;})
                .collect(Collectors.toList());
    }

    @Override
    public <T> Boolean removeAttachment(DeleteAttachmentParam param) {
        IService<T> iService = getIService(param.getIdentification());
        return iService.remove(Wrappers.<T>query().eq("associated_id",param.getId()));
    }

    private <T> IService<T> getIService(String identification){
        IService<T> iService = (IService<T>) iServiceMap.get(identification);
        if(Objects.isNull(iService)){
            throw new CustomException(ResultCode.FAIL);
        }
        return iService;
    }
}

附件DTO

@Data
@ApiModel("附件信息")
public class AttachmentDto extends BaseDto {
    @ApiModelProperty("id")
    private Integer id;

    @JsonIgnore
    @ApiModelProperty(hidden = true)
    private Integer fileFormat;

    @ApiModelProperty("业务类型")
    private Integer businessType;

    @ApiModelProperty("文件url")
    private String fileUrl;

    @ApiModelProperty("文件名称")
    private String fileName;

    @ApiModelProperty("文件格式(1电子文档,2照片)")
    public Integer getFileType(){
        return fileFormat;
    }
}

前置准备

1.数据库表

CREATE TABLE `internal_contract_attachment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `associated_id` int(11) DEFAULT NULL COMMENT '关联id',
  `file_name` varchar(500) DEFAULT NULL COMMENT '文件名称',
  `file_url` varchar(500) DEFAULT NULL COMMENT '附件url',
  `file_format` tinyint(2) DEFAULT NULL COMMENT '文件格式(1电子文档,2照片)',
  `create_user` int(11) DEFAULT NULL COMMENT '创建者',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_user` int(11) DEFAULT NULL COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `is_delete` tinyint(1) DEFAULT '0' COMMENT '是否删除',
  PRIMARY KEY (`id`),
  KEY `idx_associated_id` (`associated_id`) USING BTREE COMMENT '内包合同id索引'
) DEFAULT CHARSET=utf8mb4 COMMENT='内包合同附件';

2.创建满足mybatisPlus的IService接口

 public interface InternalContractAttachmentService extends IService<InternalContractAttachment>,AttachmentService {
}

3.创建满足mybatisPlus的ServiceImpl

@Service
public class InternalContractAttachmentServiceImpl extends ServiceImpl<InternalContractAttachmentMapper, InternalContractAttachment> implements InternalContractAttachmentService {
}

entity实体类不作展示

调用方法

//在代码上用法
	List<AddAttachmentParam> attachments = param.getAttachments();
	if(CollUtil.isNotEmpty(attachments)){
		attachments.forEach(attachment -> attachment.setAssociatedId(contractId));
		attachmentFacadeService.addAttachments(attachments, InternalContractAttachment.class.getSimpleName());
	}
		
	// 初始化查询合同附件的参数
	ListAttachmentParam listAttachmentParam = new ListAttachmentParam();
	listAttachmentParam.setIdentification(InternalContractAttachment.class.getSimpleName());
	listAttachmentParam.setId(param.getId());
	// 查询并设置合同附件列表
	internalContractDto.setAttachments(attachmentFacadeService.listAttachments(listAttachmentParam));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值