<?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.xinelu.manage.mapper.patientcouponreceive.PatientCouponReceiveMapper">

    <resultMap type="PatientCouponReceive" id="PatientCouponReceiveResult">
        <result property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="couponId" column="coupon_id"/>
        <result property="receiveSource" column="receive_source"/>
        <result property="expirationStartTime" column="expiration_start_time"/>
        <result property="expirationEndTime" column="expiration_end_time"/>
        <result property="receiveTime" column="receive_time"/>
        <result property="useStatus" column="use_status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="couponTitle" column="coupon_title"/>
        <result property="couponPrice" column="coupon_price"/>
        <result property="couponConsumePrice" column="coupon_consume_price"/>
        <result property="couponReductionDays" column="coupon_reduction_days"/>
        <result property="couponDescription" column="coupon_description"/>
    </resultMap>

    <sql id="selectPatientCouponReceiveVo">
        select id,
               patient_id,
               coupon_id,
               receive_source,
               expiration_start_time,
               expiration_end_time,
               receive_time,
               use_status,
               create_by,
               create_time,
               update_by,
               update_time,
               coupon_title,
               coupon_price,
               coupon_consume_price,
               coupon_description,
               coupon_reduction_days
        from patient_coupon_receive
    </sql>

    <select id="selectPatientCouponReceiveList" parameterType="PatientCouponReceive"
            resultMap="PatientCouponReceiveResult">
        <include refid="selectPatientCouponReceiveVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="couponId != null ">
                and coupon_id = #{couponId}
            </if>
            <if test="receiveSource != null  and receiveSource != ''">
                and receive_source = #{receiveSource}
            </if>
            <if test="expirationStartTime != null ">
                and expiration_start_time = #{expirationStartTime}
            </if>
            <if test="expirationEndTime != null ">
                and expiration_end_time = #{expirationEndTime}
            </if>
            <if test="receiveTime != null ">
                and receive_time = #{receiveTime}
            </if>
            <if test="useStatus != null  and useStatus != ''">
                and use_status = #{useStatus}
            </if>
            <if test="couponTitle != null  and couponTitle != ''">
                and coupon_title = #{couponTitle}
            </if>
            <if test="couponPrice != null ">
                and coupon_price = #{couponPrice}
            </if>
            <if test="couponConsumePrice != null ">
                and coupon_consume_price = #{couponConsumePrice}
            </if>
            <if test="couponDescription != null  and couponDescription != ''">
                and coupon_description = #{couponDescription}
            </if>
        </where>
    </select>

    <select id="selectPatientCouponReceiveById" parameterType="Long"
            resultMap="PatientCouponReceiveResult">
        <include refid="selectPatientCouponReceiveVo"/>
        where id = #{id}
    </select>

    <insert id="insertPatientCouponReceive" parameterType="PatientCouponReceive" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_coupon_receive
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientId != null">patient_id,
            </if>
            <if test="couponId != null">coupon_id,
            </if>
            <if test="receiveSource != null">receive_source,
            </if>
            <if test="expirationStartTime != null">expiration_start_time,
            </if>
            <if test="expirationEndTime != null">expiration_end_time,
            </if>
            <if test="receiveTime != null">receive_time,
            </if>
            <if test="useStatus != null">use_status,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="couponTitle != null">coupon_title,
            </if>
            <if test="couponPrice != null">coupon_price,
            </if>
            <if test="couponConsumePrice != null">coupon_consume_price,
            </if>
            <if test="couponDescription != null">coupon_description,
            </if>
            <if test="couponReductionDays != null">coupon_reduction_days,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patientId != null">#{patientId},
            </if>
            <if test="couponId != null">#{couponId},
            </if>
            <if test="receiveSource != null">#{receiveSource},
            </if>
            <if test="expirationStartTime != null">#{expirationStartTime},
            </if>
            <if test="expirationEndTime != null">#{expirationEndTime},
            </if>
            <if test="receiveTime != null">#{receiveTime},
            </if>
            <if test="useStatus != null">#{useStatus},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="couponTitle != null">#{couponTitle},
            </if>
            <if test="couponPrice != null">#{couponPrice},
            </if>
            <if test="couponConsumePrice != null">#{couponConsumePrice},
            </if>
            <if test="couponDescription != null">#{couponDescription},
            </if>
            <if test="couponReductionDays != null">#{couponReductionDays},
            </if>
        </trim>
    </insert>

    <update id="updatePatientCouponReceive" parameterType="PatientCouponReceive">
        update patient_coupon_receive
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="couponId != null">coupon_id =
                #{couponId},
            </if>
            <if test="receiveSource != null">receive_source =
                #{receiveSource},
            </if>
            <if test="expirationStartTime != null">expiration_start_time =
                #{expirationStartTime},
            </if>
            <if test="expirationEndTime != null">expiration_end_time =
                #{expirationEndTime},
            </if>
            <if test="receiveTime != null">receive_time =
                #{receiveTime},
            </if>
            <if test="useStatus != null">use_status =
                #{useStatus},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="couponTitle != null">coupon_title =
                #{couponTitle},
            </if>
            <if test="couponPrice != null">coupon_price =
                #{couponPrice},
            </if>
            <if test="couponConsumePrice != null">coupon_consume_price =
                #{couponConsumePrice},
            </if>
            <if test="couponDescription != null">coupon_description =
                #{couponDescription},
            </if>
            <if test="couponReductionDays != null">coupon_reduction_days =
                #{couponReductionDays},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePatientCouponReceiveById" parameterType="Long">
        delete
        from patient_coupon_receive
        where id = #{id}
    </delete>

    <delete id="deletePatientCouponReceiveByIds" parameterType="String">
        delete from patient_coupon_receive where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertBatchPatientReceive" parameterType="java.util.List">
        insert into patient_coupon_receive(
        patient_id,
        coupon_id,
        receive_source,
        expiration_start_time,
        expiration_end_time,
        receive_time,
        use_status,
        create_time,
        coupon_title,
        coupon_price,
        coupon_consume_price,
        coupon_description,
        coupon_reduction_days
        ) values
        <foreach item="couponReceive" index="index" collection="couponReceiveList" separator=",">
            (
            #{couponReceive.patientId},
            #{couponReceive.couponId},
            #{couponReceive.receiveSource},
            #{couponReceive.expirationStartTime},
            #{couponReceive.expirationEndTime},
            #{couponReceive.receiveTime},
            #{couponReceive.useStatus},
            #{couponReceive.createTime},
            #{couponReceive.couponTitle},
            #{couponReceive.couponPrice},
            #{couponReceive.couponConsumePrice},
            #{couponReceive.couponDescription},
            #{couponReceive.couponReductionDays}
            )
        </foreach>
    </insert>

    <select id="getPatientCouponExpirationEndTime"
            resultMap="PatientCouponReceiveResult">
        select id,
        patient_id,
        coupon_id,
        receive_source,
        expiration_start_time,
        expiration_end_time,
        receive_time,
        use_status,
        create_by,
        create_time,
        update_by,
        update_time,
        coupon_title,
        coupon_price,
        coupon_consume_price,
        coupon_description,
        coupon_reduction_days
        from patient_coupon_receive
        <where>
            <if test="expirationEndTime != null ">
                and expiration_end_time &lt;= #{expirationEndTime} and use_status &lt;&gt; #{couponUseStatus}
            </if>
        </where>
    </select>

    <update id="updateCouponReceiveStatus">
        update patient_coupon_receive set use_status = #{couponUseStatus}, update_time = now()
        where id in
        <foreach item="id" collection="idList" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateCouponReceiveStatusList">
        update patient_coupon_receive set use_status = #{couponUseStatus}, update_time = now()
        <where>
            id in
            <foreach item="id" collection="idList" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
    </update>

    <select id="getPatientCouponInfo" resultType="com.xinelu.manage.vo.patientcouponreceive.PatientCouponReceiveVO">
        SELECT pcr.id,
               pcr.patient_id,
               pcr.coupon_id,
               pcr.receive_source,
               pcr.expiration_start_time,
               pcr.expiration_end_time,
               pcr.receive_time,
               pcr.use_status,
               cp.coupon_price,
               IFNULL(cp.coupon_title, '') coupon_title,
               cp.coupon_consume_price
        FROM patient_coupon_receive pcr
                 INNER JOIN coupon cp ON pcr.coupon_id = cp.id
        WHERE patient_id = #{patientId}
          and coupon_id = #{couponId}
    </select>

    <update id="updatePatientCouponUseStatus">
        update patient_coupon_receive
        set use_status  = #{useStatus},
            update_time = now()
        where patient_id = #{patientId}
          and coupon_id = #{couponId}
          and use_status &lt;&gt; #{useStatus}
    </update>

    <select id="selectPatientCouponReceive"
            resultType="com.xinelu.manage.vo.patientcouponreceive.PatientCouponReceiveInfoVO">
        select pcr.id,
               pcr.patient_id,
               pcr.coupon_id,
               pcr.receive_source,
               pcr.expiration_start_time,
               pcr.expiration_end_time,
               pcr.receive_time,
               pcr.use_status,
               pcr.create_by,
               pcr.create_time,
               pcr.update_by,
               pcr.update_time,
               pcr.coupon_title,
               pcr.coupon_price,
               pcr.coupon_consume_price,
               pcr.coupon_description,
               pcr.coupon_reduction_days,
               (SELECT coupon_type FROM coupon where id = #{couponId}) coupon_type
        from patient_coupon_receive pcr
        where pcr.patient_id = #{patientId}
          and pcr.coupon_id = #{couponId}
    </select>

    <select id="getCouponReceiveByReceiveSource" resultType="java.lang.Integer">
        select
            count(1)
        from patient_coupon_receive
        where
            patient_id = #{patientId}
        and receive_source = #{receiveSource}
    </select>
</mapper>
