package com.njcn.gather.aireport.testreport.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.gather.aireport.testreport.pojo.param.TestReportParam;
import com.njcn.gather.aireport.testreport.pojo.po.TestReportPO;
import com.njcn.gather.aireport.testreport.pojo.vo.TestReportVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TestReportMapper extends BaseMapper<TestReportPO> {

    @Select({
            "<script>",
            "select r.id, r.multi_id as multiId, r.no, r.client_unit_id as clientUnitId, cu.name as clientUnitName,",
            "r.report_model_id as reportModelId, rm.name as reportModelName, r.template_dir_name as templateDirName,",
            "r.template_dir_path as templateDirPath, r.sort_no as sortNo, r.create_unit as createUnit,",
            "r.contract_number as contractNumber, r.standard, r.data, r.phonenumber, r.check_id as checkId,",
            "checker.name as checkerName, r.check_time as checkTime, r.check_result as checkResult, r.state,",
            "r.report_generate_state as reportGenerateState,",
            "(select count(1) from ai_testreport_point p where p.report_id = r.id and p.deleted = 0) as pointCount,",
            "(select count(1) from ai_testreport_group_report gr where gr.report_id = r.id and gr.deleted = 0) as groupCount,",
            "r.status, r.create_by as createBy, creator.name as createByName, r.create_time as createTime,",
            "r.update_by as updateBy, updater.name as updateByName, r.update_time as updateTime",
            "from ai_testreport r",
            "left join ai_clientunit cu on r.client_unit_id = cu.id",
            "left join ai_reportmodel rm on r.report_model_id = rm.id",
            "left join sys_user creator on r.create_by = creator.id",
            "left join sys_user updater on r.update_by = updater.id",
            "left join sys_user checker on r.check_id = checker.id",
            "where r.status = 1",
            "<if test='param.keyword != null and param.keyword != \"\"'>",
            "and (r.no like concat('%', #{param.keyword}, '%')",
            "or r.contract_number like concat('%', #{param.keyword}, '%')",
            "or cu.name like concat('%', #{param.keyword}, '%')",
            "or rm.name like concat('%', #{param.keyword}, '%'))",
            "</if>",
            "<if test='param.searchBeginTime != null and param.searchBeginTime != \"\"'>",
            "and r.create_time &gt;= #{param.searchBeginTime}",
            "</if>",
            "<if test='param.searchEndTime != null and param.searchEndTime != \"\"'>",
            "and r.create_time &lt;= #{param.searchEndTime}",
            "</if>",
            "<if test='param.state != null and param.state != \"\"'>",
            "and r.state = #{param.state}",
            "</if>",
            "order by r.create_time desc",
            "</script>"
    })
    Page<TestReportVO> selectReportPage(Page<TestReportVO> page, @Param("param") TestReportParam.QueryParam param);

    @Select({
            "select r.id, r.multi_id as multiId, r.no, r.client_unit_id as clientUnitId, cu.name as clientUnitName,",
            "r.report_model_id as reportModelId, rm.name as reportModelName, r.template_dir_name as templateDirName,",
            "r.template_dir_path as templateDirPath, r.sort_no as sortNo, r.create_unit as createUnit,",
            "r.contract_number as contractNumber, r.standard, r.data, r.phonenumber, r.check_id as checkId,",
            "checker.name as checkerName, r.check_time as checkTime, r.check_result as checkResult, r.state,",
            "r.report_generate_state as reportGenerateState,",
            "(select count(1) from ai_testreport_point p where p.report_id = r.id and p.deleted = 0) as pointCount,",
            "(select count(1) from ai_testreport_group_report gr where gr.report_id = r.id and gr.deleted = 0) as groupCount,",
            "r.status, r.create_by as createBy, creator.name as createByName, r.create_time as createTime,",
            "r.update_by as updateBy, updater.name as updateByName, r.update_time as updateTime",
            "from ai_testreport r",
            "left join ai_clientunit cu on r.client_unit_id = cu.id",
            "left join ai_reportmodel rm on r.report_model_id = rm.id",
            "left join sys_user creator on r.create_by = creator.id",
            "left join sys_user updater on r.update_by = updater.id",
            "left join sys_user checker on r.check_id = checker.id",
            "where r.id = #{id} and r.status = 1"
    })
    TestReportVO selectReportDetail(@Param("id") String id);

    @Select("select count(1) from ai_clientunit where id = #{id} and status = 1")
    int countNormalClientUnit(@Param("id") String id);

    @Select("select count(1) from ai_reportmodel where id = #{id} and status = 1")
    int countNormalReportModel(@Param("id") String id);

    @Select("select multi_id from ai_testreport where id = #{id} limit 1")
    String selectReportMultiId(@Param("id") String id);

    @Select({
            "select r.id, r.multi_id as multiId, r.no, r.client_unit_id as clientUnitId, cu.name as clientUnitName,",
            "r.report_model_id as reportModelId, rm.name as reportModelName, r.template_dir_name as templateDirName,",
            "r.template_dir_path as templateDirPath, r.sort_no as sortNo, r.create_unit as createUnit,",
            "r.contract_number as contractNumber, r.standard, r.data, r.phonenumber, r.check_id as checkId,",
            "checker.name as checkerName, r.check_time as checkTime, r.check_result as checkResult, r.state,",
            "r.report_generate_state as reportGenerateState,",
            "(select count(1) from ai_testreport_point p where p.report_id = r.id and p.deleted = 0) as pointCount,",
            "(select count(1) from ai_testreport_group_report gr where gr.report_id = r.id and gr.deleted = 0) as groupCount,",
            "r.status, r.create_by as createBy, creator.name as createByName, r.create_time as createTime,",
            "r.update_by as updateBy, updater.name as updateByName, r.update_time as updateTime",
            "from ai_testreport r",
            "left join ai_clientunit cu on r.client_unit_id = cu.id",
            "left join ai_reportmodel rm on r.report_model_id = rm.id",
            "left join sys_user creator on r.create_by = creator.id",
            "left join sys_user updater on r.update_by = updater.id",
            "left join sys_user checker on r.check_id = checker.id",
            "where r.multi_id = #{multiId} and r.status = 1",
            "order by r.sort_no asc, r.create_time asc"
    })
    java.util.List<TestReportVO> selectByMultiId(@Param("multiId") String multiId);
}
