<?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.yinet.yyqh.mapper.YyqhMapper">

    <resultMap type="Yyqh" id="YyqhResult">
        <result property="yyqhId" column="yyqh_id"/>
        <result property="applyForName" column="apply_for_name"/>
        <result property="guidanceId" column="guidance_id"/>
        <result property="idNum" column="id_num"/>
        <result property="phongNum" column="phong_num"/>
        <result property="bookedDay" column="booked_day"/>
        <result property="status" column="status"/>
        <result property="bookedTime" column="booked_time"/>
        <result property="submitTime" column="submit_time"/>
        <result property="guidanceName" column="guidance_name"/>
        <result property="tickNumDate" column="tick_num_date"/>
    </resultMap>

    <sql id="selectYyqhVo">
        select y.yyqh_id,y.guidance_id,y.apply_for_name,y.id_num,y.phong_num,y.booked_day,y.booked_time,y.status,y.submit_time,yg.guidance_name from yyqh y LEFT JOIN yyqh_guidance yg on y.guidance_id=yg.yyqh_guidance_id
    </sql>

    <select id="selectYyqhList" parameterType="RegisterUser" resultMap="YyqhResult">
        <include refid="selectYyqhVo"/>
        <where>
            <if test="idNum != null  and idNum != ''">and id_num = #{idNum}</if>
            <if test="phongNum != null  and phongNum != ''">and phong_num = #{phongNum}</if>
            <if test="bookedDay != null">and booked_day = #{bookedDay}</if>
            <if test="bookedTime != null  and bookedTime != ''">and booked_time = #{bookedTime}</if>
            <if test="applyForName != null  and applyForName != ''">and apply_for_name like concat('%',
                #{applyForName},'%')
            </if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="submitTime != null ">and submit_time = #{submitTime}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and date_format(booked_day,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and date_format(booked_day,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
    </select>
    <select id="selectYyqhListByCard" parameterType="RegisterUser" resultMap="YyqhResult">
        <include refid="selectYyqhVo"/>
        where status=1
        and id_num = #{idNum}
    </select>
    <select id="selectYyqhListZh" parameterType="RegisterUser" resultMap="YyqhResult">
        select
        y.yyqh_id,y.guidance_id,y.apply_for_name,y.id_num,y.phong_num,y.booked_day,(select time_quantum from
        wsyy_quantum where wsyy_quantum_id = y.booked_time) as booked_time,y.status,y.submit_time,yg.guidance_name
        from yyqh y LEFT JOIN yyqh_guidance yg on y.guidance_id=yg.yyqh_guidance_id
        <where>
            <if test="idNum != null  and idNum != ''">and id_num = #{idNum}</if>
            <if test="phongNum != null  and phongNum != ''">and phong_num = #{phongNum}</if>
            <if test="bookedDay != null">and booked_day = #{bookedDay}</if>
            <if test="bookedTime != null  and bookedTime != ''">and booked_time = #{bookedTime}</if>
            <if test="applyForName != null  and applyForName != ''">and apply_for_name like concat('%',
                #{applyForName},'%')
            </if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="submitTime != null ">and submit_time = #{submitTime}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and date_format(booked_day,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and date_format(booked_day,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
    </select>
    <insert id="insertYyqh" parameterType="RegisterUser">
        INSERT INTO yyqh (
        yyqh_id,
        guidance_id,
        id_num,
        phong_num,
        booked_day,
        booked_time,
        apply_for_name,
        status,
        submit_time
        )VALUES(
        #{yyqh.yyqhId},
        #{yyqh.guidanceId},
        #{yyqh.idNum},
        #{yyqh.phongNum},
        #{yyqh.bookedDay},
        #{yyqh.bookedTime},
        #{yyqh.applyForName},
        #{yyqh.status},
        #{yyqh.submitTime}
        );
    </insert>

    <delete id="deleteYyqhByIds" parameterType="String">
        delete from yyqh where yyqh_id in
        <foreach item="yyqhId" collection="array" open="(" separator="," close=")">
            #{yyqhId}
        </foreach>
    </delete>
    <select id="selectCountForTime" resultType="Integer">
        select count(*) from yyqh where booked_time=#{time} and booked_day=#{bookedDay} and (status=1 or status=2)
        <if test="guidanceParent != null">
            and guidance_id=#{guidanceParent}
        </if>
    </select>
    <select id="selectYyqhListByIdNum" resultMap="YyqhResult">
        <include refid="selectYyqhVo"/>
        <where>
            id_num = #{idNum} and status = 1
        </where>
        order by submit_time desc
        LIMIT 1
    </select>
    <update id="changeStatus">
        update yyqh
        <set>
            status=2,
            tick_num=#{tickNum},
            tick_num_date=sysdate()
        </set>
        where yyqh_id=#{id}
    </update>
    <update id="cancelYy">
        update yyqh
        <set>
            status=3
        </set>
        where yyqh_id=#{id}
    </update>
</mapper>