<?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.campaigndetail.CampaignDetailMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.yixiang.co/MyBatis/x-plugins/
     -->

    <select id="getCampaignDetails"
            resultType="co.yixiang.yshop.module.product.controller.admin.campaigndetail.vo.CampaignSkuDetailRespVO">
        SELECT
        yspav.product_id,ysp.store_name as productName,yspav.id as skuId,yspav.sku,ysp.spec_type,
        yspav.image as image,
        if(ycd.stock is null, yspav.stock, ycd.stock + yspav.stock) as originalStock,
        ysp.image as productImage,yspav.price as originalPrice,ysp.price as productPrice,
        ycd.price, ycd.discount, ycd.stock, ycd.total
        FROM
        yshop_store_product_attr_value AS yspav
        LEFT JOIN (select * from yshop_campaign_detail where campaign_id = #{campaignId} and deleted = 0) AS ycd ON
        ycd.sku_id = yspav.id
        inner join yshop_store_product as ysp on yspav.product_id = ysp.id
        WHERE
        yspav.product_id in (select DISTINCT product_id from yshop_campaign_detail where campaign_id = #{campaignId} and
        deleted = 0)
    </select>

    <update id="deleteCampaignDetails" parameterType="java.lang.Long">
        update yshop_campaign_detail set deleted = 1
        where campaign_id = #{campaignId}
    </update>

    <select id="getCampaignDataDetail"
            resultType="co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.CampaignDataDetailRespVO">
        select ysp.store_name as productName,ycd.sku_id,yspav.sku,yspav.`unique`,
        if(yspav.image is null or yspav.image ='',ysp.image,yspav.image) as image
        from yshop_campaign_detail as ycd
        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 ycd.campaign_id = #{pageReqVO.id}
    </select>

    <select id="getCampaignSkus"
            resultType="co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.AppCampaignSkuRespVO">
        select ycd.product_id,ycd.sku_id,yci.type,ycd.price,ycd.discount,ycd.stock,ycd.total, yci.person,
        yspav.price as campaignOriginalPrice,yspav.sku,yci.start_time,yci.end_time,
        if(yspav.image is null or yspav.image ='',ysp.image,yspav.image) as image, yci.state,yci.id, ycd.id as detailId,
        `unique` as productAttrUnique
        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 ycd.deleted = 0 and yci.deleted = 0
        and ycd.product_id in (
        <foreach collection="productIds" item="productId" separator=",">
            #{productId}
        </foreach>
        )
        order by yspav.id
    </select>

</mapper>
