<?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.nasc.custom.mapper.NascMatterMapper">

    <resultMap id="matterResult" type="NascMatter">
        <id          property="id"              column="id"                      />
        <result      property="itemName"        column="item_name"               />
        <result      property="deptId"          column="dept_id"                 />
        <result      property="prefixQuestion"  column="prefix_question"         />
        <result      property="sortNum"         column="sort_num"                />
        <result      property="contacts"        column="contacts"                />
        <result      property="contactsPhone"   column="contacts_phone"          />
        <result      property="commonProblem"   column="common_problem"          />
        <result      property="delFlag"         column="del_flag"                />
        <result      property="createBy"        column="create_by"               />
        <result      property="createTime"      column="create_time"             />
        <result      property="updateBy"        column="update_by"               />
        <result      property="updateTime"      column="update_time"             />
        <association property="dept"   javaType="SysDept"    resultMap="deptResult"  />
        <association property="print"  javaType="NascPrint"  resultMap="printResult" />
    </resultMap>

    <resultMap id="printResult" type="NascPrint">
        <id     property="id"         column="printId"        />
        <result property="matterId"   column="matter_id" />
        <result property="content"    column="content"   />
        <result property="missContent"    column="miss_content"   />
        <result property="denyContent"    column="deny_content"   />
        <result property="disposableContent"    column="disposable_content"   />
        <result property="printBy"    column="print_by"  />
        <result property="printTime"  column="print_ime" />
    </resultMap>

    <resultMap id="deptResult" type="SysDept">
        <id     property="deptId"    column="dept_id"     />
        <result property="parentId"  column="parent_id"   />
        <result property="deptName"  column="dept_name"   />
        <result property="ancestors" column="ancestors"   />
        <result property="orderNum"  column="order_num"   />
        <result property="leader"    column="leader"      />
        <result property="status"    column="dept_status" />
    </resultMap>
    
    <select id="selectMatterList" parameterType="NascMatter" resultMap="matterResult">
		SELECT nm.id, nm.item_name, nm.dept_id, nm.prefix_question, nm.sort_num, nm.del_flag, nm.create_by, nm.create_time,
		       nm.update_by, nm.update_time, nm.contacts, nm.contacts_phone, nm.common_problem, d.dept_name
		FROM nasc_matter nm
        LEFT JOIN sys_dept d ON nm.dept_id = d.dept_id
        WHERE nm.del_flag != -1
        <if test="deptId != null and deptId != 0">
            AND nm.dept_id = #{deptId}
        </if>
        <if test="itemName != null and itemName != ''">
            AND nm.item_name like concat('%', #{itemName}, '%')
        </if>
        <if test="prefixQuestion != null and prefixQuestion != ''">
            AND nm.prefix_question = #{prefixQuestion}
        </if>
        ORDER BY nm.sort_num DESC
	</select>

    <select id="checkMatterDuplicates" parameterType="NascMatter" resultType="Long">
        SELECT COUNT(1)
        FROM nasc_matter nm
        WHERE nm.del_flag != -1 AND nm.dept_id = #{deptId} AND nm.item_name = #{itemName}
        <if test="id != null and id != 0">
            AND nm.id != #{id}
        </if>
    </select>

    <insert id="insertMatter" parameterType="NascMatter" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO nasc_matter (item_name, dept_id, prefix_question, contacts, contacts_phone, common_problem, sort_num, create_by, create_time)
        VALUES (#{itemName}, #{deptId}, #{prefixQuestion}, #{contacts}, #{contactsPhone}, #{commonProblem}, #{sortNum}, #{createBy}, sysdate())
    </insert>

    <update id="updateMatter" parameterType="NascMatter">
        UPDATE nasc_matter
        SET item_name = #{itemName}, dept_id = #{deptId}, prefix_question = #{prefixQuestion}, sort_num = #{sortNum},
            contacts = #{contacts}, contacts_phone = #{contactsPhone}, common_problem = #{commonProblem}, update_by = #{updateBy}, update_time = sysdate()
        WHERE id = #{id}
    </update>

    <update id="deleteMatterByIds" parameterType="NascMatter">
        UPDATE nasc_matter
        SET del_flag = -1
        WHERE id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getMatterInfo" parameterType="NascMatter" resultMap="matterResult">
        SELECT nm.id, nm.item_name, nm.dept_id, nm.prefix_question, nm.contacts, nm.contacts_phone, nm.common_problem, nm.sort_num, nm.del_flag, nm.create_by, nm.create_time, nm.update_by, nm.update_time,
               d.dept_name, d.dept_id, d.parent_id,
               np.id AS printId, np.matter_id, np.content, np.miss_content, np.deny_content, np.disposable_content, np.print_by, np.print_time
        FROM nasc_matter nm
        LEFT JOIN sys_dept d ON nm.dept_id = d.dept_id
        LEFT JOIN nasc_print np ON nm.id = np.matter_id
        WHERE nm.del_flag != -1 AND nm.id = #{id}
    </select>

    <select id="checkDeptMatter" parameterType="Long" resultType="Long">
        SELECT COUNT(1)
        FROM nasc_matter
        WHERE dept_id = #{deptId} AND del_flag != -1
    </select>

</mapper> 