<?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="co.yixiang.yshop.module.product.dal.mysql.campaigninfo.CampaignInfoMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.yixiang.co/MyBatis/x-plugins/
     -->
    <select id="checkRepeat"
            parameterType="co.yixiang.yshop.module.product.controller.admin.campaigninfo.param.CampaignJudgmentParam"
            resultType="java.lang.Long">
        SELECT ycd.product_id
        FROM yshop_campaign_info as yci
        inner join yshop_campaign_detail as ycd on yci.id = ycd.campaign_id
        where state != 2 and yci.deleted = 0 and ycd.deleted = 0
        and ((if(yci.if_enable = 2, date_sub(yci.start_time, interval yci.enable_time minute), yci.start_time) &lt;=
        #{startTime} and yci.end_time &gt;= #{startTime})
        OR (if(yci.if_enable = 2, date_sub(yci.start_time, interval yci.enable_time minute), yci.start_time) &lt;=
        #{endTime} and yci.end_time &gt;= #{endTime}))
        and ycd.product_id in (
        <foreach collection="productIds" item="productId" separator=",">
            #{productId}
        </foreach>
        )
        <if test="campaignId != null">
            and yci.id <![CDATA[ != ]]> #{campaignId}
        </if>

    </select>

    <select id="getCampaignInfoList"
            resultType="co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.CampaignOrderVo">
        select ycd.campaign_id,yso.id as oid,yso.pay_price as price,yso.uid,ysod.product_attr_unique as `unique`
        from yshop_store_order as yso
        inner join yshop_campaign_detail as ycd on yso.campaign_detail_id = ycd.id
        inner join yshop_store_order_detail as ysod on ysod.oid = yso.id
        where
        yso.paid = 1 and yso.status not in (-1,-2) and yso.refund_status in (0,1)
        and ysod.state in (1,2)
        and ycd.campaign_id in (
        <foreach collection="campaignIds" item="campaignId" separator=",">
            #{campaignId}
        </foreach>
        )
    </select>


    <select id="getCampaignProductPage"
            resultType="co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductRespVO">
        select ycd.sku_id, ycd.product_id as id, yspav.price as originalPrice, ycd.price,ycd.stock,ycd.total,yci.person,
        if(yspav.image is null or yspav.image ='',ysp.image,yspav.image) as image, ysp.store_name, yspav.sku
        from yshop_campaign_detail as ycd
        inner join yshop_campaign_info as yci on yci.id = ycd.campaign_id
        inner join yshop_store_product_attr_value as yspav on ycd.sku_id = yspav.id
        inner join yshop_store_product as ysp on ycd.product_id = ysp.id
        where
        yci.state in (1,3) and yci.deleted = 0 and ycd.deleted = 0
        <if test="reqVO.type != null">
            and yci.type = #{reqVO.type}
        </if>
        <if test="reqVO.productName != null and reqVO.productName != ''">
            and ysp.store_name like CONCAT('%',#{reqVO.productName},'%')
        </if>
        <if test="reqVO.startTime != null and reqVO.endTime != null">
            and yci.start_time &lt; #{reqVO.startTime} and yci.end_time &gt; #{reqVO.endTime}
        </if>
        <if test="reqVO.ids != null and reqVO.ids.size > 0">
            and ycd.campaign_id in (
            <foreach collection="reqVO.ids" item="campaignId" separator=",">
                #{campaignId}
            </foreach>
            )
        </if>
        group by ycd.product_id
        order by ycd.id
    </select>

    <select id="getCampaignsByProductAttrUniques"
            resultType="co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignSkuVO">
        select ycd.price, yspav.`unique` as productAttrUnique, ycd.stock
        from yshop_campaign_detail as ycd
        inner join yshop_campaign_info as yci on yci.id = ycd.campaign_id
        inner join yshop_store_product_attr_value as yspav on ycd.sku_id = yspav.id
        where
        yci.state = 1 and yci.deleted = 0 and ycd.deleted = 0
        and yspav.`unique` in (
        <foreach collection="productAttrUniques" item="productAttrUnique" separator=",">
            #{productAttrUnique}
        </foreach>
        )
        order by ycd.id
    </select>

    <select id="numberJudge" resultType="java.lang.Integer">
        select if(yci.if_limit = 1, null, yci.limit_number -count(yso.id))
        from yshop_campaign_detail as ycd
        left join
        (select * from yshop_store_order where uid = #{vo.uid} and `status` not in (-1, -2, 4) and refund_status in
        (0,1))
        as yso on yso.campaign_detail_id = ycd.id
        inner join yshop_campaign_info as yci on yci.id = ycd.campaign_id
        inner join yshop_store_product_attr_value as yspav on ycd.sku_id = yspav.id
        where yci.state = 1
        and yci.deleted = 0
        and ycd.deleted = 0
        and yspav.`unique` = #{vo.productAttrUnique}
    </select>

    <select id="stockJudge" resultType="java.lang.Integer">
        select ycd.stock - #{vo.number}
        from yshop_campaign_detail as ycd
        inner join yshop_campaign_info as yci on yci.id = ycd.campaign_id
        inner join yshop_store_product_attr_value as yspav on ycd.sku_id = yspav.id
        where yci.state = 1
        and yci.deleted = 0
        and ycd.deleted = 0
        and yspav.`unique` = #{vo.productAttrUnique}
    </select>

    <update id="deStock">
        update yshop_campaign_detail as ycd
        join yshop_store_product_attr_value as yspav
        on ycd.sku_id = yspav.id
        join yshop_campaign_info as yci on yci.id = ycd.campaign_id
        set ycd.stock = ycd.stock - #{number}
        where yspav.`unique` = #{productAttrUnique} and yci.state = 1
    </update>
</mapper>
