package com.zt.plat.module.qms.business.reportdoc.dal.mapper;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.*;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentDataDO;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentMainDO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 检测报告业务 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface ReportDocumentMainMapper extends BaseMapperX<ReportDocumentMainDO> {


    default PageResult<ReportDocumentMainRespVO> selectPage(ReportDocumentMainPageReqVO reqVO) {
        String flowStatus = reqVO.getFlowStatus();

        MPJLambdaWrapperX<ReportDocumentMainDO> wrapper = new MPJLambdaWrapperX<>();
        wrapper.selectSub(ReportDocumentDataDO.class, s->
            s.selectCount(ReportDocumentDataDO::getMainId).eq(ReportDocumentDataDO::getMainId, ReportDocumentMainDO::getId), ReportDocumentMainRespVO::getDetailDataCount);
        wrapper.selectAll(ReportDocumentMainDO.class);
        wrapper.ne(ReportDocumentMainDO::getCancelFlag, -1);    //-1为临时数据
        wrapper.eqIfPresent(ReportDocumentMainDO::getFlowInstanceId, reqVO.getFlowInstanceId());
        wrapper.eqIfPresent(ReportDocumentMainDO::getReportDocumentTypeId, reqVO.getReportDocumentTypeId());
        wrapper.eqIfPresent(ReportDocumentMainDO::getFlowKey, reqVO.getFlowKey());
        wrapper.eqIfPresent(ReportDocumentMainDO::getFlowSerialNumber, reqVO.getFlowSerialNumber());
        wrapper.eqIfPresent(ReportDocumentMainDO::getCurrentNode, reqVO.getCurrentNode());
        wrapper.likeIfPresent(ReportDocumentMainDO::getDocumentCode, reqVO.getDocumentCode());
        wrapper.eqIfPresent(ReportDocumentMainDO::getDocumentType, reqVO.getDocumentType());
        wrapper.likeIfPresent(ReportDocumentMainDO::getDocumentTitle, reqVO.getDocumentTitle());
        wrapper.eqIfPresent(ReportDocumentMainDO::getDocumentConfig, reqVO.getDocumentConfig());
        wrapper.eqIfPresent(ReportDocumentMainDO::getDocumentSignature, reqVO.getDocumentSignature());
        wrapper.betweenIfPresent(ReportDocumentMainDO::getDocumentStartTime, reqVO.getDocumentStartTime());
        wrapper.betweenIfPresent(ReportDocumentMainDO::getDocumentEndTime, reqVO.getDocumentEndTime());
        wrapper.eqIfPresent(ReportDocumentMainDO::getFormData, reqVO.getFormData());
//        wrapper.eqIfPresent(ReportDocumentMainDO::getFlowStatus, reqVO.getFlowStatus());
        if(!ObjectUtils.isEmpty(flowStatus)){
            List<String> statusList = Arrays.asList(flowStatus.split(","));
            wrapper.in(ReportDocumentMainDO::getFlowStatus, statusList);
        }
        wrapper.eqIfPresent(ReportDocumentMainDO::getCancelFlag, reqVO.getCancelFlag());
        wrapper.eqIfPresent(ReportDocumentMainDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode());
        wrapper.betweenIfPresent(ReportDocumentMainDO::getCreateTime, reqVO.getCreateTime());
        wrapper.eqIfPresent(ReportDocumentMainDO::getRemark, reqVO.getRemark());
        wrapper.orderByDesc(ReportDocumentMainDO::getId);
        return selectJoinPage(reqVO, ReportDocumentMainRespVO.class, wrapper);
    }

}