<?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.qs.mp.admin.mapper.TicketAwardsPrizeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qs.mp.admin.domain.TicketAwardsPrize">
        <id column="prize_id" property="prizeId" />
        <result column="box_id" property="boxId" />
        <result column="awards_id" property="awardsId" />
        <result column="ref_id" property="refId" />
        <result column="title" property="title" />
        <result column="pic_url" property="picUrl" />
        <result column="prize_type" property="prizeType" />
        <result column="quantity" property="quantity" />
        <result column="cashed_qty" property="cashedQty" />
        <result column="remain_qty" property="remainQty" />
        <result column="value" property="value" />
        <result column="sort_weight" property="sortWeight" />
        <result column="is_deleted" property="isDeleted" />
        <result column="created_time" property="createdTime" />
        <result column="updated_time" property="updatedTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        prize_id, box_id, awards_id, ref_id, title, pic_url, prize_type, quantity, cashed_qty, remain_qty, value, sort_weight, is_deleted, created_time, updated_time
    </sql>

    <select id="listPrizeVO" resultType="com.qs.mp.admin.domain.vo.TicketAwardsPrizeVO">
        select
            case t2.prize_type
                when 'goods' then if(t3.title is not null, t3.title, t2.title)
                when 'coupon' then if(t4.title is not null, t4.title, t2.title)
                else t2.title
                end as title,
            if(t2.prize_type = 'goods' AND t3.title is not null,t3.pic_url,t2.pic_url) as pic_url,
            if(t2.prize_type = 'goods' AND t3.value is not null,t3.value,t2.value) as value,
            if(t4.coupon_detail is not null, t4.coupon_detail, '') as coupon_detail,
            t1.*,
            t2.*
        from mp_ticket_awards t1
        left join mp_ticket_awards_prize t2 on t1.awards_id = t2.awards_id
        left join mp_goods t3 on t2.ref_id = t3.goods_id
        left join mp_coupon t4 on t2.ref_id = t4.coupon_id
            ${ew.customSqlSegment}
    </select>

    <select id="listByQueryWrapper" resultType="com.qs.mp.admin.domain.TicketAwardsPrize">
        select
            case t1.prize_type
                when 'goods' then if(t2.title is not null, t2.title, t1.title)
                when 'coupon' then if(t3.title is not null, t3.title, t1.title)
                when 'coupon_pkg' then if(t4.title is not null, t4.title, t1.title)
                else t1.title
            end as title,

            case t1.prize_type
                when 'goods' then if(t2.pic_url is not null, t2.pic_url, t1.pic_url)
                when 'coupon' then if(t3.pic_url is not null, t3.pic_url, t1.pic_url)
                when 'coupon_pkg' then if(t4.pic_url is not null, t4.pic_url, t1.pic_url)
                else t1.pic_url
            end as pic_url,

            case t1.prize_type
                when 'goods' then if(t2.value is not null, t2.value, t1.value)
                when 'coupon' then if(t3.discount is not null, t3.discount, t1.value)
                when 'coupon_pkg' then if(t4.face_price is not null, t4.face_price, t1.value)
                else t1.value
            end as value,

            case t1.prize_type
                when 'goods' then if(t2.cost is not null, t2.cost, 0)
                else 0
            end as cost,

            if(t3.coupon_detail is not null, t3.coupon_detail, '') as coupon_detail,
            t1.*
        from mp_ticket_awards_prize t1
            left join mp_goods t2 on t1.ref_id = t2.goods_id
            left join mp_coupon t3 on t1.ref_id = t3.coupon_id
            left join mp_coupon_pkg t4 on t1.ref_id = t4.id
        ${ew.customSqlSegment}
    </select>

    <select id="listTicketDetailRelGoodsId" resultType="com.qs.mp.admin.domain.TicketBox">
        select
        DISTINCT t1.box_id,t1.title,t1.status
        from mp_ticket_box t1 left join mp_ticket_awards_prize t2 on t1.box_id = t2.box_id
        ${ew.customSqlSegment}
    </select>
</mapper>
