<?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="com.jeeplus.test.klgBase.question.mapper.KlgBaseQuestionsMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.test.klgBase.question.service.dto.KlgBaseQuestionsDTO">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
            <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
            <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
            <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
            <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
            <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
            <result property="title" column="title" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR"/>
            <result property="programId" column="program_id" jdbcType="VARCHAR"/>
            <result property="programName" column="program_name" jdbcType="VARCHAR"/>
            <result property="disposeBy" column="dispose_by" jdbcType="VARCHAR"/>
            <result property="expectFinishDate" column="expect_finish_date" jdbcType="TIMESTAMP"/>
            <result property="content" column="content" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="procInsId" column="proc_ins_id" jdbcType="VARCHAR"/>
            <result property="processDefinitionId" column="process_definition_id" jdbcType="VARCHAR"/>
            <result property="createBy.name" column="create_name" jdbcType="VARCHAR"/>
            <result property="finishDate" column="finish_date" jdbcType="TIMESTAMP"/>
            <result property="projectType" column="project_type" jdbcType="VARCHAR"/>
            <collection property="klgBaseDisposeDTOList" ofType="com.jeeplus.test.klgBase.question.service.dto.KlgBaseDisposeDTO" column="id" select="getDisposeList"></collection>
            <collection property="klgBaseReplyDTOList" ofType="com.jeeplus.test.klgBase.question.service.dto.KlgBaseReplyDTO" column="id" select="getReplyList"></collection>
            <collection property="workAttachmentDtoList" ofType="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto" column="id" select="getFileList"></collection>
    </resultMap>

    <resultMap id="AttachmentMap" type="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="create_name" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="VARCHAR"/>
        <result property="url" column="url" jdbcType="VARCHAR"/>
<!--        <result property="type" column="type" jdbcType="VARCHAR"/>-->
<!--        <result property="attachmentId" column="attachment_id" jdbcType="VARCHAR"/>-->
        <result property="name" column="attachment_name" jdbcType="VARCHAR"/>
<!--        <result property="attachmentFlag" column="attachment_flag" jdbcType="VARCHAR"/>-->
<!--        <result property="moduleType" column="module_type" jdbcType="VARCHAR"/>-->
<!--        <result property="attachmentType" column="attachment_type" jdbcType="VARCHAR"/>-->
        <result property="size" column="file_size" jdbcType="VARCHAR"/>
<!--        <result property="sort" column="sort" jdbcType="SMALLINT"/>-->
<!--        <result property="description" column="description" jdbcType="VARCHAR"/>-->
    </resultMap>

    <sql id="Base_Column_List">
        kbq.id,
        kbq.create_by,
        kbq.create_date,
        kbq.update_by,
        kbq.update_date,
        kbq.del_flag,
        kbq.remarks,
        kbq.title,
        kbq.type,
        kbq.program_id,
        kbq.dispose_by,
        kbq.expect_finish_date,
        kbq.content,
        kbq.status,
        kbq.proc_ins_id,
        kbq.process_definition_id,
        kbq.finish_date,
        kbq.project_type
    </sql>
    <sql id="Kbr_Column_List">
        kbr.id,
        kbr.create_by,
        kbr.create_date,
        kbr.update_by,
        kbr.update_date,
        kbr.del_flag,
        kbr.remarks,
        kbr.question_id,
        kbr.content,
        kbr.reply_by,
        kbr.reply_date,
        kbr.sort
    </sql>
    <sql id="Kbd_Column_List">
        kbd.id,
        kbd.create_by,
        kbd.create_date,
        kbd.update_by,
        kbd.update_date,
        kbd.del_flag,
        kbd.remarks,
        kbd.question_id,
        kbd.dispose_by
    </sql>
    <sql id="File_Column_List">
        wa.id,
        wa.create_by,
        wa.create_date,
        wa.update_by,
        wa.update_date,
        wa.remarks,
        wa.del_flag,
        wa.url,
        wa.type,
        wa.attachment_id,
        wa.attachment_name,
        wa.attachment_flag,
        wa.module_type,
        wa.attachment_type,
        wa.file_size,
        wa.sort,
        wa.description
    </sql>

    <select id="getFileList" resultMap="AttachmentMap">
        select
        <include refid="File_Column_List"></include>,
        su.name as create_name
        from work_attachment wa
        left join sys_user su on su.id = wa.create_by and su.del_flag = '0'
        where wa.del_flag = '0' and wa.attachment_id = #{id}
    </select>
    <select id="getDisposeList" resultType="com.jeeplus.test.klgBase.question.service.dto.KlgBaseDisposeDTO">
        select
        <include refid="Kbd_Column_List"></include>
        from klg_base_dispose kbd
        where kbd.del_flag = '0' and kbd.question_id = #{id}
    </select>

    <select id="getReplyList" resultType="com.jeeplus.test.klgBase.question.service.dto.KlgBaseReplyDTO">
        select
        <include refid="Kbr_Column_List"></include>,
        su.name as replyName
        from klg_base_reply kbr
        left join sys_user su on su.id = kbr.reply_by and su.del_flag = '0'
        where kbr.del_flag = '0' and kbr.question_id = #{id}
    </select>

    <select id="findList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        (CASE
        WHEN kbq.project_type = '1' THEN ppli.name
        WHEN kbq.project_type = '2' THEN cw_pr.project_name
        ELSE '' END) AS program_name,
        su.name as create_name
        from klg_base_questions kbq
        left join program_project_list_info ppli on ppli.id = kbq.program_id and ppli.del_flag = '0'
        left join cw_project_records cw_pr on cw_pr.id = kbq.program_id and cw_pr.del_flag = '0'
        left join sys_user su on su.id = kbq.create_by and su.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="findListFocus" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        ppli.name as program_name,
        su.name as create_name
        from klg_base_questions kbq
        left join program_project_list_info ppli on ppli.id = kbq.program_id and ppli.del_flag = '0'
        left join sys_user su on su.id = kbq.create_by and su.del_flag = '0'
        inner join klg_base_questions_focus kbqf on kbqf.question_id = kbq.id and kbqf.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        (CASE
        WHEN kbq.project_type = '1' THEN ppli.name
        WHEN kbq.project_type = '2' THEN cw_pr.project_name
        ELSE '' END) AS program_name,
        su.name as create_name
        from klg_base_questions kbq
        left join program_project_list_info ppli on ppli.id = kbq.program_id and ppli.del_flag = '0'
        left join cw_project_records cw_pr on cw_pr.id = kbq.program_id and cw_pr.del_flag = '0'
        left join sys_user su on su.id = kbq.create_by and su.del_flag = '0'
        where kbq.del_flag = '0' and kbq.id = ${id}
    </select>

    <select id="getMaxSortByQues" resultType="string">
        select
        kbr.sort
        from klg_base_reply kbr
        where kbr.question_id = #{id}
--         order by CONVERT(sort,SIGNED) desc
        order by sort desc
        limit 1
    </select>
</mapper>
