<?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.willalp.canteen.mapper.HsGiftMapper">

    <resultMap type="com.willalp.canteen.domain.HsGift" id="HsGiftResult">
        <result property="giftId" column="gift_id"/>
        <result property="giftNumber" column="gift_number"/>
        <result property="giftContent" column="gift_content"/>
        <result property="giftName" column="gift_name"/>
        <result property="giftPictrue" column="gift_pictrue"/>
        <result property="giftPrice" column="gift_price"/>
        <result property="amount" column="amount"/>
        <result property="qybz" column="qybz"/>
        <result property="jgid" column="jgid"/>
        <result property="isDelete" column="is_delete"/>
        <result property="giftType" column="gift_type"/>
        <result property="giftEventType" column="gift_event_type"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectHsGiftVo">
        select gift_id,
               gift_number,
               gift_content,
               gift_name,
               gift_pictrue,
               gift_price,
               amount,
               qybz,
               jgid,
               gift_type,
               gift_event_type,
               is_delete,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from hs_gift hg
    </sql>
    <update id="checkAndReduceGiftInventory">
        update hs_gift
        set amount = amount - #{num}
        WHERE gift_id = #{giftId}
          AND amount &gt; 0
          AND amount &gt;= #{num}
          AND qybz = 0
          AND gift_event_type = '2'
    </update>

    <select id="selectHsGiftList" parameterType="HsGift" resultMap="HsGiftResult">
        <include refid="selectHsGiftVo"/>
        <where>
            is_delete = 0
            <if test="giftName != null  and giftName != ''">
                and gift_name like concat('%', #{giftName}, '%')
            </if>
            <if test="qybz != null  and qybz != ''">
                and qybz = #{qybz}
            </if>
            <if test="giftEventType != null  and giftEventType != ''">
                and gift_event_type = #{giftEventType}
            </if>
            <if test="jgid != null and jgid !=''">
                and jgid = #{jgid}
            </if>
            <if test="giftNumber != null and giftNumber != ''">
                and gift_number = #{giftNumber}
            </if>
        </where>
    </select>
    <select id="appSelectedHsGiftListByType" resultType="com.willalp.canteen.domain.HsGift">
        <include refid="selectHsGiftVo"/>
        <where>
            is_delete = 0 AND
            gift_type in (select id from hs_gift_type where parent_id = #{code})
        </where>
    </select>
    <select id="appSelectedHsGiftByEvent" resultType="com.willalp.canteen.domain.HsGift">
        SELECT
        gift_id,
        gift_number,
        gift_content,
        gift_name,
        gift_pictrue,
        gift_price,
        amount,
        qybz,
        jgid,
        gift_type,
        he.id eventId,
        he.event_name,
        he.event_time
        FROM
        hs_gift hg
        LEFT JOIN hs_gift_type hgt ON hgt.id = hg.gift_type
        LEFT JOIN hs_event_business_relation hebr ON hebr.business_id = hg.gift_id
        LEFT JOIN hs_event he ON he.id = hebr.event_id
        <where>
            hg.is_delete = 0 AND hg.qybz = '0'
            <if test="code1 != null and code1 != ''">
                AND hebr.event_id = #{code1}
                AND he.event_status = 3
                AND hg.gift_event_type = '1'
            </if>
            <if test="code2 != null and code2 != ''">
                AND hg.gift_type = #{code2}
                AND hg.gift_event_type = '2'
            </if>
        </where>
    </select>
    <select id="bindingList" resultType="com.willalp.canteen.domain.HsGift">
        <include refid="selectHsGiftVo"/>
        <where>
            hg.is_delete = 0
            AND hg.gift_event_type = '1'
            AND hg.gift_id not in( Select business_id from hs_event_business_relation)
        </where>
    </select>
</mapper>
