<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.business.mapper.InformationQueryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="informationQueryResultMap" type="org.springblade.business.entity.InformationQuery">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="create_dept" property="createDept"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="name" property="name"/>
        <result column="number" property="number"/>
        <result column="category" property="category"/>
        <result column="parent_id" property="parentId"/>
        <result column="project_id" property="projectId"/>
        <result column="contract_id" property="contractId"/>
        <result column="classify" property="classify"/>
        <result column="wbs_id" property="wbsId"/>
        <result column="is_experiment" property="isExperiment"/>
        <result column="data_id" property="dataId"/>
        <result column="task_id" property="taskId"/>
        <result column="task_status" property="taskStatus"/>
        <result column="file_user_id_and_name" property="fileUserIdAndName"/>
        <result column="audit_user_id_and_name" property="auditUserIdAndName"/>
        <result column="report_number" property="reportNumber"/>
        <result column="completion_status" property="completionStatus"/>
        <result column="source_type" property="sourceType"/>
    </resultMap>

    <resultMap id="intResultMap" type="java.lang.Integer"/>

    <select id="getInformationQueryByWbsId" resultMap="informationQueryResultMap">
        select * from u_information_query where is_deleted = 0 and wbs_id = #{wbsId} and classify = #{classify}
    </select>

    <select id="getReportNumberByContractId" resultMap="intResultMap">
        select report_number from u_information_query where is_deleted = 0 and contract_id = #{contractId} and classify = #{classify} group by report_number order by report_number ASC
    </select>

    <select id="queryFileUserByContractId" resultMap="informationQueryResultMap">
        select file_user_id_and_name from u_information_query where is_deleted = 0 and contract_id = #{contractId} and classify = #{classify}
    </select>

    <select id="countInformationQuery" resultType="java.lang.Integer">
        select count(id) from u_information_query
        where
            is_deleted = 0
        and classify = #{query.classify}
        and contract_id = #{query.contractId}
        <if test="query.taskStatus != null and query.taskStatus != ''"> and task_status = #{query.taskStatus} </if>
        <if test="query.sourceType != null and query.sourceType != ''"> and source_type = #{query.sourceType} </if>
        <if test="query.reportNumber != null and query.reportNumber != ''"> and report_number = #{query.reportNumber} </if>
        <if test="query.fileUserIdAndName != null and query.fileUserIdAndName != ''"> and file_user_id_and_name like concat('%',#{query.fileUserIdAndName},'%') </if>
        <if test="query.queryValue != null and query.queryValue != ''"> and (name like concat('%',#{query.queryValue},'%') OR number like concat('%',#{query.queryValue},'%')) </if>
        <if test="query.wbsIds != null">
            and wbs_id in
            <foreach collection="query.wbsIds" item="wbsId" open="(" separator="," close=")">
                #{wbsId}
            </foreach>
        </if>
    </select>

    <select id="selectInformationQueryPage" resultMap="informationQueryResultMap">
        select
            query.id,
            query.name,
            query.number,
            query.create_time,
            query.task_status,
            query.report_number,
            query.file_user_id_and_name
        from
        (
            select
                id,
                name,
                number,
                create_time,
                task_status,
                report_number,
                file_user_id_and_name,
                date_format(create_time,'%Y-%m-%d') as createTimes
            from u_information_query
            where
                is_deleted = 0
                and classify = #{query.classify}
                and contract_id = #{query.contractId}
            <if test="query.taskStatus != null and query.taskStatus != ''"> and task_status = #{query.taskStatus} </if>
            <if test="query.sourceType != null and query.sourceType != ''"> and source_type = #{query.sourceType} </if>
            <if test="query.reportNumber != null and query.reportNumber != ''"> and report_number = #{query.reportNumber} </if>
            <if test="query.fileUserIdAndName != null and query.fileUserIdAndName != ''"> and file_user_id_and_name like concat('%',#{query.fileUserIdAndName},'%') </if>
            <if test="query.queryValue != null and query.queryValue != ''"> and (name like concat('%',#{query.queryValue},'%') OR number like concat('%',#{query.queryValue},'%')) </if>
            <if test="query.wbsIds != null">
                and wbs_id in
                <foreach collection="query.wbsIds" item="wbsId" open="(" separator="," close=")">
                    #{wbsId}
                </foreach>
            </if>
        ) AS query
        where
          1 = 1
        <if test="query.startTime != null and query.startTime != '' and query.endTime != null and query.endTime != ''">
            and query.createTimes between #{query.startTime} and #{query.endTime}
        </if>
        limit #{current}, #{size}
    </select>

</mapper>
