<?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.NascManualVerifyMapper">

    <resultMap id="manualVerifyEntity" type="java.util.HashMap">
        <result property="id" column="id"/>
        <result property="dealUser" column="deal_user"/>
        <result property="dealStatus" column="deal_status"/>
        <result property="matterId" column="matter_id"/>
        <result property="materialId" column="material_id"/>
        <result property="materialName" column="material_name"/>
        <result property="dealId" column="deal_id"/>
        <result property="reason" column="reason"/>
        <result property="createTime" column="create_time"/>
        <result property="verifyTime" column="verify_time"/>
    </resultMap>

    <insert id="insertManualVerify" parameterType="NascManualVerify" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO nasc_manual_verify (deal_user, deal_status, matter_id, material_id, material_name, deal_id, create_time)
        VALUES (#{dealUser}, #{dealStatus}, #{matterId}, #{materialId}, #{materialName}, #{dealId}, sysdate());
    </insert>

    <update id="updateManualVerifyDealId">
        UPDATE nasc_manual_verify SET deal_id = #{dealId}
        WHERE id IN
        <foreach collection="manualVerifyList" item="item" index="index"
                 separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <resultMap id="manualVerifyResult" type="java.util.HashMap">
        <result property="id" column="id"/>
        <result property="dealStatus" column="deal_status"/>
        <result property="createTime" column="create_time"/>
        <result property="verifyTime" column="verify_time"/>
        <result property="itemName" column="item_name"/>
        <result property="materialName" column="material_name"/>
        <result property="serialNumber" column="serial_number"/>
        <result property="withType" column="with_type"/>
        <result property="contacts" column="contacts"/>
        <result property="enterpriseName" column="enterprise_name"/>
        <result property="idCard" column="id_card"/>
        <result property="phone" column="phone"/>
        <result property="userName" column="user_name"/>
        <result property="deptName" column="dept_name"/>
        <result property="reason" column="reason"/>
    </resultMap>

    <select id="selectManualVerifyList" resultMap="manualVerifyResult">
        SELECT mv.id, mv.deal_status, mv.reason,
               DATE_FORMAT(mv.create_time, '%Y-%m-%d %H:%i:%s') AS create_time,
               DATE_FORMAT(mv.verify_time, '%Y-%m-%d %H:%i:%s') AS verify_time,
               mv.material_name, m.item_name,
               dr.serial_number, dr.with_type, dr.contacts, dr.enterprise_name, dr.id_card, dr.phone,
               u.user_name, d.dept_name
        FROM nasc_manual_verify mv
        LEFT JOIN nasc_matter m ON m.id = mv.matter_id
        LEFT JOIN nasc_deal_record dr ON dr.id = mv.deal_id
        LEFT JOIN sys_user u ON u.user_id = mv.deal_user
        LEFT JOIN sys_dept d ON d.dept_id = u.dept_id
        WHERE mv.deal_id IS NOT NULL
        <if test="serialNumber != null and serialNumber != ''">
            AND dr.serial_number = #{serialNumber}
        </if>
        <if test="dealStatus != null">
            AND mv.deal_status = #{dealStatus}
        </if>
        ORDER BY mv.deal_status ASC, mv.create_time DESC
    </select>

    <update id="updateManualVerify">
        UPDATE nasc_manual_verify
        SET deal_user = #{dealUser}, deal_status = #{dealStatus}, reason = #{reason}, verify_time = sysdate()
        WHERE id = #{id}
    </update>

    <select id="getManualVerifyByIds" resultMap="manualVerifyEntity">
        SELECT mv.id, mv.deal_user, mv.deal_status, mv.matter_id, mv.material_id, mv.material_name, mv.deal_id, mv.reason,
               DATE_FORMAT(mv.create_time, '%Y-%m-%d %H:%i:%s') AS create_time,
               DATE_FORMAT(mv.verify_time, '%Y-%m-%d %H:%i:%s') AS verify_time
        FROM nasc_manual_verify mv
        WHERE id IN
        <foreach collection="ids" item="item" index="index"
                 separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

</mapper> 