如何添加主表子表规范


/**
 * @Copyright(c) 2020-2021 Dalian JunFunSoft Co., Ltd
 * @Project sumec-cloud
 * @Date 2023年5月24日 下午2:06:11
 * @Author
 * @Description:
 */

@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(description = "安全环保证书")
@TableName("t_zb_env_safe_cert")
@MPJTableAlias("t_zb_env_safe_cert")
@KeySequence(value = "snowKeyGenerator")
@LoggerModel(value = "安全环保证书")
public class EnvSafeCert extends PmsEntity implements Serializable {


    /***
     * 主键
     *
     */
    @TableId(value = "_AutoID", type = IdType.ASSIGN_UUID)
    @NotBlank(groups = {UpdateValidation.class}, message = "主键不能为空")
    @Size(max = 32, message = "主键不能超过32长度")
    @ApiModelProperty(value = "主键", required = true)
    @LoggerField(value = "主键")
    @TableField(value = "_AutoID")
    private String autoId;

    /***
     * 船舶ID
     *
     */
    @NotBlank(message = "船舶ID不能为空")
    @Size(max = 32, message = "船舶ID不能超过32长度")
    @ApiModelProperty(value = "船舶ID", required = true)
    @LoggerField(value = "船舶ID")
    @TableField(value = "vShipId")
    private String vShipId;

    /***
     * 船舶名称
     *
     */
    @NotBlank(message = "船舶名称不能为空")
    @Size(max = 100, message = "船舶名称不能超过100长度")
    @ApiModelProperty(value = "船舶名称", required = true)
    @LoggerField(value = "船舶名称")
    @TableField(value = "vShipName")
    private String vShipName;

    /***
     * 证书编号
     *
     */
    @NotBlank(message = "证书编号不能为空")
    @Size(max = 32, message = "证书编号不能超过32长度")
    @ApiModelProperty(value = "证书编号", required = true)
    @LoggerField(value = "证书编号")
    @TableField(value = "vCertCode")
    private String vCertCode;

    /***
     * 证书名称
     *
     */
    @NotBlank(message = "证书名称不能为空")
    @Size(max = 32, message = "证书名称不能超过32长度")
    @ApiModelProperty(value = "证书名称", required = true)
    @LoggerField(value = "证书名称")
    @TableField(value = "vCertName")
    private String vCertName;

    /***
     * 发证单位
     *
     */
    @NotBlank(message = "发证单位不能为空")
    @Size(max = 32, message = "发证单位不能超过32长度")
    @ApiModelProperty(value = "发证单位", required = true)
    @LoggerField(value = "发证单位")
    @TableField(value = "vCertCompany")
    private String vCertCompany;

    /***
     * 设备信息
     *
     */
    @NotBlank(message = "设备信息不能为空")
    @Size(max = 32, message = "设备信息不能超过32长度")
    @ApiModelProperty(value = "设备信息", required = true)
    @LoggerField(value = "设备信息")
    @TableField(value = "vDeviceInfo")
    private String vDeviceInfo;

    /***
     * 设备编码
     *
     */
    @NotBlank(message = "设备编码不能为空")
    @Size(max = 32, message = "设备编码不能超过32长度")
    @ApiModelProperty(value = "设备编码", required = true)
    @LoggerField(value = "设备编码")
    @TableField(value = "vDeviceCode")
    private String vDeviceCode;

    /***
     * 设备型号
     *
     */
    @NotBlank(message = "设备型号不能为空")
    @Size(max = 32, message = "设备型号不能超过32长度")
    @ApiModelProperty(value = "设备型号", required = true)
    @LoggerField(value = "设备型号")
    @TableField(value = "vDeviceModel")
    private String vDeviceModel;

    /***
     * 发证日期
     *
     */
    @ApiModelProperty(value = "发证日期", required = true)
    @JSONField(format = "yyyy-MM-dd")
    @LoggerField(value = "发证日期")
    @TableField(value = "dtCertStartDate")
    private Date dtCertStartDate;

    /***
     * 是否长期有效Y/N
     *
     */
    @Size(max = 1, message = "是否长期有效Y/N不能超过1长度")
    @ApiModelProperty(value = "是否长期有效Y/N")
    @LoggerField(value = "是否长期有效Y/N")
    @TableField(value = "vIsLongTerm")
    private String vIsLongTerm;

    /***
     * 保存位置
     *
     */
    @Size(max = 100, message = "保存位置不能超过100长度")
    @ApiModelProperty(value = "保存位置")
    @LoggerField(value = "保存位置")
    @TableField(value = "vSavePlace")
    private String vSavePlace;

    /***
     * 换证日期
     *
     */
    @ApiModelProperty(value = "换证日期", required = true)
    @JSONField(format = "yyyy-MM-dd")
    @LoggerField(value = "换证日期")
    @TableField(value = "dtCertReplaceDate")
    private Date dtCertReplaceDate;

    /***
     * 下次检验日期
     *
     */
    @ApiModelProperty(value = "下次检验日期", required = true)
    @JSONField(format = "yyyy-MM-dd")
    @LoggerField(value = "下次检验日期")
    @TableField(value = "dtnextCheckDate")
    private Date dtnextCheckDate;

    /***
     * 操作日期
     *
     */
    @ApiModelProperty(value = "操作日期")
    @JSONField(format = "yyyy-MM-dd")
    @LoggerField(value = "操作日期")
    @TableField(value = "dtReportDate")
    private Date dtReportDate;

    /***
     * 操作人
     *
     */
    @Size(max = 32, message = "操作人不能超过32长度")
    @ApiModelProperty(value = "操作人")
    @LoggerField(value = "操作人")
    @TableField(value = "vReporter")
    private String vReporter;

    /***
     * 检验日期
     *
     */
    @ApiModelProperty(value = "检验日期", required = true)
    @JSONField(format = "yyyy-MM-dd")
    @LoggerField(value = "检验日期")
    @TableField(value = "dtCheckDate")
    private Date dtCheckDate;

    /***
     * 检验周期(月)
     *
     */
    @NotNull(message = "检验周期(月)不能为空")
    @Max(value = 9999999999L, message = "检验周期(月)不能大于9999999999")
    @ApiModelProperty(value = "检验周期(月)", required = true)
    @LoggerField(value = "检验周期(月)")
    @TableField(value = "iCheckCyle")
    private Integer iCheckCyle;

    /***
     * 检验提醒(月)
     *
     */
    @NotNull(message = "检验提醒(月)不能为空")
    @Max(value = 9999999999L, message = "检验提醒(月)不能大于9999999999")
    @ApiModelProperty(value = "检验提醒(月)", required = true)
    @LoggerField(value = "检验提醒(月)")
    @TableField(value = "iCheckRemind")
    private Integer iCheckRemind;

    /***
     * 换证周期(月)
     *
     */
    @NotNull(message = "换证周期(月)不能为空")
    @Max(value = 9999999999L, message = "换证周期(月)不能大于9999999999")
    @ApiModelProperty(value = "换证周期(月)", required = true)
    @LoggerField(value = "换证周期(月)")
    @TableField(value = "iReplaceCyle")
    private Integer iReplaceCyle;

    /***
     * 换证提醒(月)
     *
     */
    @NotNull(message = "换证提醒(月)不能为空")
    @Max(value = 9999999999L, message = "换证提醒(月)不能大于9999999999")
    @ApiModelProperty(value = "换证提醒(月)", required = true)
    @LoggerField(value = "换证提醒(月)")
    @TableField(value = "iReplaceRemind")
    private Integer iReplaceRemind;

    /***
     * 状态(正常状态、检验预警、换证预警)
     *
     */
    @Size(max = 32, message = "状态(正常状态、检验预警、换证预警)不能超过32长度")
    @ApiModelProperty(value = "状态(正常状态、检验预警、换证预警)")
    @LoggerField(value = "状态(正常状态、检验预警、换证预警)")
    @TableField(value = "vState")
    private String vState;

    /***
     * 备注
     *
     */
    @Size(max = 65535, message = "备注不能超过65535长度")
    @ApiModelProperty(value = "备注")
    @LoggerField(value = "备注")
    @TableField(value = "tMemo")
    private String tMemo;

    @ApiModelProperty(value = "attach数据")
    @JSONField
    @TableField(exist = false)
    @Transient
    private List attachList = new ArrayList<>();
}
  @Override
    public ResultObject save(EnvSafeCert data) {
        data.setDtnextCheckDate(DateUtils.addMonths(data.getDtCertStartDate(), data.getICheckCyle()));
        mapper.insert(data);
        Optional> dataOption = Optional.ofNullable(data.getAttachList());
        if (dataOption.isPresent()) {
            List list = dataOption.get();
            list.forEach(x -> x.setVBusinessId(data.getAutoId()));
            if (list.size() > 0) {
                attachMapper.batchInsert(list);
            }
        }
        ResultObject rs = new ResultObject<>();
        rs.setResult(data);
        return rs;
    }

   @Override
public ResultObject update(EnvSafeCert data) {
    // 查询指定业务ID的旧附件列表
    LambdaQueryWrapper attachWrapper = new LambdaQueryWrapper<>();
    attachWrapper.eq(EnvSafeCertAttach::getVBusinessId, data.getAutoId());
    List dataOld = Optional.ofNullable(attachMapper.selectList(attachWrapper)).orElseGet(ArrayList::new);
    
    // 获取当前附件列表
    List dataCurrent = Optional.ofNullable(data.getAttachList()).orElseGet(ArrayList::new);
    
    // 比较旧附件列表和当前附件列表差异
    List[] compareList = CompareUtils.allSet(dataOld, dataCurrent, EnvSafeCertAttach::getAutoId);
    List addList = compareList[0];  // 添加的附件列表
    List updList = compareList[1];  // 修改的附件列表
    List delList = compareList[2];  // 删除的附件列表
    
    // 更新主表
    mapper.updateById(data);
    
    // 批量插入新增的附件
    if (!addList.isEmpty()) {
        addList.forEach(add -> add.setVBusinessId(data.getAutoId()));
        attachMapper.batchInsert(addList);
    }
    
    // 修改附件
    if (!updList.isEmpty()) {
        updList.forEach(up -> attachMapper.updateById(up));
    }
    
    // 删除附件
    if (!delList.isEmpty()) {
        // 获取将要被删除的附件ID列表
        List delIdList = delList.stream().map(EnvSafeCertAttach::getAutoId).collect(Collectors.toList());
        attachMapper.delete(new LambdaQueryWrapper().in(EnvSafeCertAttach::getAutoId, delIdList));
    }
    
    ResultObject rs = new ResultObject<>();
    rs.setResult(data);  // 设置返回结果
    rs.setSucess(SundryNormalCode.UPD.getName());  // 设置返回信息
    return rs;  // 返回结果
}
package com.junfun.pms.sundry.service;

import com.junfun.pms.sundry.entity.EnvSafeCert;
import com.junfun.seafar.common.entity.ResultObject;
import com.junfun.seafar.dao.mybatis.plus.service.BaseService;

import java.util.List;
import java.util.Set;


public interface EnvSafeCertService  extends BaseService{

    ResultObject save(EnvSafeCert data);

    ResultObject update(EnvSafeCert data);

    ResultObject delete(String id);

    boolean checkCertState(List list);
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class EnvSafeCertServiceImpl extends AbstractBaseService implements EnvSafeCertService {

    @Autowired
    private EnvSafeCertMapper mapper;
    @Autowired
    private EnvSafeCertAttachMapper attachMapper;

    @Override
    public BaseMapper mapper() {
        return mapper;
    }

    @Override
    public ResultObject save(EnvSafeCert data) {
        data.setDtnextCheckDate(DateUtils.addMonths(data.getDtCertStartDate(), data.getICheckCyle()));
        mapper.insert(data);
        Optional> dataOption = Optional.ofNullable(data.getAttachList());
        if (dataOption.isPresent()) {
            List list = dataOption.get();
            list.forEach(x -> x.setVBusinessId(data.getAutoId()));
            if (list.size() > 0) {
                attachMapper.batchInsert(list);
            }
        }
        ResultObject rs = new ResultObject<>();
        rs.setResult(data);
        return rs;
    }

    @Override
    public ResultObject update(EnvSafeCert data) {
        LambdaQueryWrapper attachWrapper = new LambdaQueryWrapper<>();
        attachWrapper.eq(EnvSafeCertAttach::getVBusinessId, data.getAutoId());
        List dataOld =
                Optional.ofNullable(attachMapper.selectList(attachWrapper)).orElseGet(ArrayList::new);
        List dataCurrent =
                Optional.ofNullable(data.getAttachList()).orElseGet(ArrayList::new);
        List[] compareList = CompareUtils.allSet(dataOld, dataCurrent, EnvSafeCertAttach::getAutoId);
        List addList = compareList[0];
        List updList = compareList[1];
        List delList = compareList[2];
        // 添加
        mapper.updateById(data);
        if (!addList.isEmpty()) {
            addList.forEach(add -> add.setVBusinessId(data.getAutoId()));
            attachMapper.batchInsert(addList);
        }
        // 修改明细
        if (!updList.isEmpty()) {
            updList.forEach(up -> attachMapper.updateById(up));
        }
        // 删除明细
        if (!delList.isEmpty()) {
            attachMapper.delete(new LambdaQueryWrapper().in(EnvSafeCertAttach::getAutoId,
                    delList.stream().map(EnvSafeCertAttach::getAutoId).collect(Collectors.toList())));
        }
        ResultObject rs = new ResultObject<>();
        rs.setResult(data);
        rs.setSucess(SundryNormalCode.UPD.getName());
        return rs;

    }

    @Override
    public ResultObject delete(String id) {
        LambdaQueryWrapper wp = new LambdaQueryWrapper<>();
        wp.eq(EnvSafeCertAttach::getVBusinessId, id);
        mapper.deleteById(id);
        attachMapper.delete(wp);
        ResultObject rs = new ResultObject<>();
        rs.setSucess(SundryNormalCode.DEL.getName());
        return rs;
    }

    @Override
    public boolean checkCertState(List list) {
        if (list.size() > 0) {
            Boolean checkState = false;
            for (EnvSafeCert envSafeCert : list) {
                if (!DateUtils.isSameDay(new Date(), envSafeCert.getDtCheckDate())) {
                    checkState = true;
                }
            }
            if (checkState) {
                //检查证书状态
                Set set = new HashSet<>();
                list.forEach(l -> set.add(l.getVShipId()));
                changeData(set);
                return true;
            }
        }
        return false;
    }

    /**
     * 根据shipId修改数据状态
     * @param shipIds shipid
     */
    private void changeData(Set shipIds) {
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
        wrapper.in(EnvSafeCert::getVShipId, shipIds);
        wrapper.eq(EnvSafeCert::getIsDel, 0);
        List list = mapper.selectList(wrapper);
        for (EnvSafeCert cert : list) {
            Date start = cert.getDtCertStartDate();
            int check = cert.getICheckCyle() - cert.getICheckRemind();
            int replace = cert.getIReplaceCyle() - cert.getIReplaceRemind();
            Date current = new Date();
            //N无需检验 C待检验 R待换证 O已超期
            if ("Y".equals(cert.getVIsLongTerm())) {
                cert.setVState("N");
            } else {
                if (current.after(DateUtils.addMonths(start, check)) &&
                        current.before(DateUtils.addMonths(start, replace))) {
                    cert.setVState("C");
                } else if (current.after(DateUtils.addMonths(start, replace)) &&
                        current.before(DateUtils.addMonths(start, cert.getIReplaceCyle()))) {
                    cert.setVState("R");
                } else if (current.after(DateUtils.addMonths(start, cert.getIReplaceCyle()))) {
                    cert.setVState("O");
                } else {
                    cert.setVState("N");
                }
            }
            cert.setDtCheckDate(new Date());
        }
        if (list.size() > 0) {
            list.forEach(x -> mapper.updateById(x));
        }
    }

}

全部service:不带注释

package com.junfun.pms.sundry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.junfun.pms.base.utils.CompareUtils;
import com.junfun.pms.sundry.entity.EnvSafeCert;
import com.junfun.pms.sundry.entity.EnvSafeCertAttach;
import com.junfun.pms.sundry.exception.SundryNormalCode;
import com.junfun.pms.sundry.mapper.EnvSafeCertAttachMapper;
import com.junfun.pms.sundry.mapper.EnvSafeCertMapper;
import com.junfun.pms.sundry.service.EnvSafeCertService;
import com.junfun.seafar.common.entity.ResultObject;
import com.junfun.seafar.dao.mybatis.plus.mapper.BaseMapper;
import com.junfun.seafar.dao.mybatis.plus.service.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class EnvSafeCertServiceImpl extends AbstractBaseService implements EnvSafeCertService {

    @Autowired
    private EnvSafeCertMapper mapper;
    @Autowired
    private EnvSafeCertAttachMapper attachMapper;

    @Override
    public BaseMapper mapper() {
        return mapper;
    }

    @Override
    public ResultObject save(EnvSafeCert data) {
        data.setDtnextCheckDate(DateUtils.addMonths(data.getDtCertStartDate(), data.getICheckCyle()));
        mapper.insert(data);
        Optional> dataOption = Optional.ofNullable(data.getAttachList());
        if (dataOption.isPresent()) {
            List list = dataOption.get();
            list.forEach(x -> x.setVBusinessId(data.getAutoId()));
            if (list.size() > 0) {
                attachMapper.batchInsert(list);
            }
        }
        ResultObject rs = new ResultObject<>();
        rs.setResult(data);
        return rs;
    }

    @Override
    public ResultObject update(EnvSafeCert data) {
        LambdaQueryWrapper attachWrapper = new LambdaQueryWrapper<>();
        attachWrapper.eq(EnvSafeCertAttach::getVBusinessId, data.getAutoId());
        List dataOld =
                Optional.ofNullable(attachMapper.selectList(attachWrapper)).orElseGet(ArrayList::new);
        List dataCurrent =
                Optional.ofNullable(data.getAttachList()).orElseGet(ArrayList::new);
        List[] compareList = CompareUtils.allSet(dataOld, dataCurrent, EnvSafeCertAttach::getAutoId);
        List addList = compareList[0];
        List updList = compareList[1];
        List delList = compareList[2];
        // 添加
        mapper.updateById(data);
        if (!addList.isEmpty()) {
            addList.forEach(add -> add.setVBusinessId(data.getAutoId()));
            attachMapper.batchInsert(addList);
        }
        // 修改明细
        if (!updList.isEmpty()) {
            updList.forEach(up -> attachMapper.updateById(up));
        }
        // 删除明细
        if (!delList.isEmpty()) {
            attachMapper.delete(new LambdaQueryWrapper().in(EnvSafeCertAttach::getAutoId,
                    delList.stream().map(EnvSafeCertAttach::getAutoId).collect(Collectors.toList())));
        }
        ResultObject rs = new ResultObject<>();
        rs.setResult(data);
        rs.setSucess(SundryNormalCode.UPD.getName());
        return rs;

    }

    @Override
    public ResultObject delete(String id) {
        LambdaQueryWrapper wp = new LambdaQueryWrapper<>();
        wp.eq(EnvSafeCertAttach::getVBusinessId, id);
        mapper.deleteById(id);
        attachMapper.delete(wp);
        ResultObject rs = new ResultObject<>();
        rs.setSucess(SundryNormalCode.DEL.getName());
        return rs;
    }

    @Override
    public boolean checkCertState(List list) {
        if (list.size() > 0) {
            Boolean checkState = false;
            for (EnvSafeCert envSafeCert : list) {
                if (!DateUtils.isSameDay(new Date(), envSafeCert.getDtCheckDate())) {
                    checkState = true;
                }
            }
            if (checkState) {
                //检查证书状态
                Set set = new HashSet<>();
                list.forEach(l -> set.add(l.getVShipId()));
                changeData(set);
                return true;
            }
        }
        return false;
    }

    /**
     * 根据shipId修改数据状态
     * @param shipIds shipid
     */
    private void changeData(Set shipIds) {
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
        wrapper.in(EnvSafeCert::getVShipId, shipIds);
        wrapper.eq(EnvSafeCert::getIsDel, 0);
        List list = mapper.selectList(wrapper);
        for (EnvSafeCert cert : list) {
            Date start = cert.getDtCertStartDate();
            int check = cert.getICheckCyle() - cert.getICheckRemind();
            int replace = cert.getIReplaceCyle() - cert.getIReplaceRemind();
            Date current = new Date();
            //N无需检验 C待检验 R待换证 O已超期
            if ("Y".equals(cert.getVIsLongTerm())) {
                cert.setVState("N");
            } else {
                if (current.after(DateUtils.addMonths(start, check)) &&
                        current.before(DateUtils.addMonths(start, replace))) {
                    cert.setVState("C");
                } else if (current.after(DateUtils.addMonths(start, replace)) &&
                        current.before(DateUtils.addMonths(start, cert.getIReplaceCyle()))) {
                    cert.setVState("R");
                } else if (current.after(DateUtils.addMonths(start, cert.getIReplaceCyle()))) {
                    cert.setVState("O");
                } else {
                    cert.setVState("N");
                }
            }
            cert.setDtCheckDate(new Date());
        }
        if (list.size() > 0) {
            list.forEach(x -> mapper.updateById(x));
        }
    }

}

你可能感兴趣的:(java,servlet,开发语言)