<?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.yc.ship.module.ota.dal.mysql.distributorproduct.DistributorProductMapper">

    <select id="getPage"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.DistributorProductRespVO">
        select t1.id,t4.id distributorId,t4.name distributorName,t2.id productId,t2.product_name productName,t2.category_id categoryId,t3.spec_type
        specType,t1.product_sku_id,t1.sale_price salePrice,t1.sale_price_raw salePriceRaw,ifnull(t1.is_use,0) isUse,t1.start_date startDate,t1.end_date endDate,
        t1.route_plan_id routePlanId,t5.name planName
        from ota_distributor_product t1
        INNER JOIN ota_distributor t4 ON t4.id = t1.distributor_id
        INNER JOIN product_spu t2 on t1.product_id = t2.id
        LEFT JOIN product_sku t3 on t1.product_sku_id = t3.id
        LEFT JOIN product_route_plan t5 ON t5.id = t1.route_plan_id AND t5.deleted = 0
        where t2.deleted=0 and t1.deleted =0
        <if test="params.distributorId != null and params.distributorId !=''">
            and t1.distributor_id = #{params.distributorId}
        </if>
        <if test="params.distributorId == null || params.distributorId ==''">
            <if test="params.productSkuId != null and params.productSkuId !=''">
                and t1.product_sku_id = #{params.productSkuId}
            </if>
            <if test="params.productSkuId == null || params.productSkuId ==''">
                and t1.product_sku_id is null
            </if>
        </if>
        <if test="params.productId != null and params.productId !=''">
            and t1.product_id = #{params.productId}
        </if>
        <if test="params.productName != null and params.productName !=''">
            and t2.product_name like concat('%',#{params.productName},'%')
        </if>
        <if test="params.distributorName != null and params.distributorName !=''">
            and t4.name like concat('%',#{params.distributorName},'%')
        </if>
        <if test="params.otaCategoryId != null and params.otaCategoryId !=''">
            and t4.ota_category_id  = #{params.otaCategoryId}
        </if>
        <if test="params.isUse != null">
            and t1.is_use  = #{params.isUse}
        </if>
    </select>

    <select id="getDistributorProductList"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.DistributorProductRespVO">
        select t1.* from ota_distributor_product t1
        where 1=1
        <if test="distributorId != null and distributorId !=''">
            and t1.distributor_id = #{distributorId}
        </if>
        <if test="productId != null and productId !=''">
            and t1.product_id = #{productId}
        </if>
        <if test="skuId != null and skuId !=''">
            and t1.product_sku_id = #{skuId}
        </if>
    </select>

    <select id="getProductPage"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.product.ProductRespVO">
        <choose>
            <when test="params.channelType == 1">
                <include refid="otaProductPage"/>
            </when>
            <otherwise>
                <include refid="otcProductPage"/>
            </otherwise>
        </choose>
    </select>

    <sql id="otaProductPage">
        select t3.id              productSkuId,
               t1.id              productId,
               t1.category_id     categoryId,
               t1.product_name    productName,
               t3.spec_type       specType,
               t3.sale_price      salePrice,
               t1.is_use          isUse,
               t2.sale_start_date startDate,
               t2.sale_end_date   endDate
        from product_spu t1
                 INNER JOIN product_basic_rule t2 on t1.id = t2.id
                 INNER JOIN product_sku t3 on t1.id = t3.product_id
        where EXISTS (select * from product_sale_channel sc where sc.product_id = t1.id and sc.channel_id = 0)
          and t1.deleted = 0
          and t3.deleted = 0
        <if test="params.productName != null and params.productName !=''">
            and t1.product_name like concat('%',#{params.productName},'%')
        </if>
        <if test="params.categoryId != null and params.categoryId !=''">
            and t1.category_id = #{params.categoryId}
        </if>
        order by t1.create_time desc
    </sql>

    <sql id="otcProductPage">
        select t1.id              productId,
                t1.category_id     categoryId,
               t1.product_name    productName,
                max(t3.sale_price)      salePrice,
               t1.is_use          isUse,
               t2.sale_start_date startDate,
               t2.sale_end_date   endDate
        from product_spu t1
                 INNER JOIN product_basic_rule t2 on t1.id = t2.id
        inner join product_sku t3 on t1.id = t3.product_id
        where EXISTS (select * from product_sale_channel sc where sc.product_id = t1.id and sc.channel_id = 1)
          and t1.deleted = 0 and t3.deleted = 0
        <if test="params.productName != null and params.productName !=''">
            and t1.product_name like concat('%',#{params.productName},'%')
        </if>
        <if test="params.categoryId != null and params.categoryId !=''">
            and t1.category_id = #{params.categoryId}
        </if>
        <if test="params.isUse != null">
            and t1.is_use = #{params.isUse}
        </if>
        GROUP BY t1.id
        order by t1.create_time desc
    </sql>

    <select id="getProductSku"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.DistributorProductRespVO">
        select t1.id         productSkuId,
               t1.product_id productId,
               t1.cost_price costPrice,
               t1.sale_price salePrice,
               t1.spec_type  specType,
               t1.stock
        from product_sku t1
        where t1.deleted = 0
          and t1.id = #{skuId}
    </select>

    <select id="getProductSkuList"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.DistributorProductRespVO">
        select t1.id         productSkuId,
               t1.product_id productId,
               t1.cost_price costPrice,
               t1.sale_price salePrice,
               t1.spec_type  specType,
               t1.stock
        from product_sku t1
        where t1.deleted = 0
          and t1.product_id = #{productId}
    </select>

    <select id="getProductSpu"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.DistributorProductRespVO">
        select t1.id product_id, t1.sale_price salePrice, t1.cost_price costPrice
        from product_spu t1
        where t1.deleted = 0
          and t1.id = #{params}
    </select>

    <select id="deleteDistributor">
        delete from ota_distributor_product where distributor_id not in
        <foreach collection="distributorIds" item="distributorId" open="(" separator="," close=")">
            #{distributorId}
        </foreach>
        and product_id=#{productId}
        <if test="productSkuId!=null">
            and product_sku_id = #{productSkuId}
        </if>
        <if test="productSkuId==null">
            and product_sku_id is null
        </if>
    </select>

    <select id="selectProjectType"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.product.ProductGroupRespVO">
        SELECT
            pg.product_id,pg.product_base_id,pb.`name` productBaseName,pg.specification_id,pp.`name` specificationName,
            pg.cost_price,pg.sale_price,pg.spec_type,pg.need_choose
        FROM
            product_group pg
            INNER JOIN product_base pb on pg.product_base_id = pb.id
            INNER JOIN product_project pp on pp.id = pg.specification_id
        WHERE
            pg.product_id = #{productId}
            AND pg.deleted = 0
            and (pg.spec_type = #{specType} or pg.spec_type = "common")

        GROUP BY
            pg.product_base_id
        <if test="type == 1">
            ,pg.specification_id
        </if>
    </select>

    <select id="selectBaseNum"
            resultType="java.util.Map">
        SELECT
            T.product_base_id baseId,
            COUNT(T.product_base_id) num
        FROM
            (
                SELECT
                    pg.product_base_id
                FROM
                    product_group pg
                        INNER JOIN product_base pb ON pg.product_base_id = pb.id
                        INNER JOIN product_project pp ON pp.id = pg.specification_id
                WHERE
                    pg.product_id = #{productId}
                  AND pg.deleted = 0
                  and pg.spec_type = #{specType}
                GROUP BY
                    pg.product_base_id,
                    pg.specification_id
            ) T
        GROUP BY
            product_base_id
    </select>

    <update id="updateListStatus">
        <foreach collection="idList" item="item" separator=";"  close=";">
            UPDATE ota_distributor_product SET is_use = #{isUse}
            WHERE id = #{item}
        </foreach>
    </update>

    <update id="update2ListStatus">
        <foreach collection="productIds" item="productId" separator=";"  close=";">
            UPDATE ota_distributor_product SET is_use = #{isUse}
            WHERE product_id = #{productId}
            <if test="distributorIds != null">
                and distributor_id in
                <foreach collection="distributorIds" item="distributorId" open="(" separator="," close=")">
                    #{distributorId}
                </foreach>
            </if>
        </foreach>
    </update>

    <select id="selectCategoryList" resultType="java.util.Map">
        SELECT id,parent_id parentId,cate_name cateName FROM product_category
        WHERE deleted = 0 AND id IN (
            WITH recursive temp AS (
                SELECT c.* FROM product_category c WHERE c.id = #{categoryId}
                UNION ALL
                SELECT c.* FROM product_category c INNER JOIN temp t2 ON t2.parent_id = c.id
                WHERE c.deleted = 0 AND c.use_status = 1
            ) SELECT pc.id
            FROM temp pc )
    </select>

    <select id="selectDistributorProductList"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.DistributorProductRespVO">
        SELECT dp.id, spu.id productId, spu.category_id categoryId, spu.product_name productName,
        if(dp.id is null, spu.is_use, dp.is_use) isUse,
        sku.id productSkuId, sku.spec_type specType,
        dp.distributor_id distributorId,
        if(dp.id is null, sku.sale_price, dp.sale_price) salePrice,
        if(dp.id is null, br.sale_start_date, dp.start_date) startDate,
        if(dp.id is null, br.sale_end_date, dp.end_date) endDate
        FROM
        product_spu spu
        INNER JOIN product_basic_rule br ON spu.id = br.id
        INNER JOIN product_sku sku ON spu.id = sku.product_id
        LEFT JOIN product_sale_channel sc ON sc.product_id = spu.id AND sc.deleted = 0
        LEFT JOIN ota_distributor_product dp on dp.product_id = spu.id and dp.deleted = 0
        <if test="params.distributorId != null">
            AND dp.distributor_id = #{params.distributorId}
        </if>
        <if test="params.channelType == 1">
            AND sc.channel_id = 0
            AND dp.product_sku_id = sku.id
        </if>
        <if test="params.channelType == 2">
            AND sc.channel_id = 1
        </if>
        WHERE spu.deleted = 0 AND sku.deleted = 0
        <if test="params.productName != null and params.productName !=''">
            and spu.product_name like concat('%',#{params.productName},'%')
        </if>
        <if test="params.categoryId != null and params.categoryId !=''">
            and spu.category_id = #{params.categoryId}
        </if>
        <if test="params.channelType == 2">
            GROUP BY spu.id
        </if>
        ORDER BY spu.create_time DESC
    </select>

    <select id="selectProductList"
            resultType="com.yc.ship.module.ota.controller.admin.distributorproduct.vo.product.ProductRespVO">
        SELECT spu.id productId, spu.category_id categoryId, spu.product_name productName, spu.is_use isUse,
               spu.sort_num sort, sku.id productSkuId, sku.spec_type specType, sku.sale_price salePrice,
               br.sale_start_date startDate, br.sale_end_date endDate
        FROM
        product_spu spu
        INNER JOIN product_basic_rule br ON spu.id = br.id
        INNER JOIN product_sku sku ON spu.id = sku.product_id
        LEFT JOIN product_sale_channel sc ON sc.product_id = spu.id AND sc.deleted = 0
        <if test="params.channelType == 1">
            AND sc.channel_id = 0
        </if>
        <if test="params.channelType == 2">
            AND sc.channel_id = 1
        </if>
        WHERE
        sc.id IS NOT NULL AND spu.deleted = 0 AND sku.deleted = 0
        <if test="params.productName != null and params.productName !=''">
            and spu.product_name like concat('%',#{params.productName},'%')
        </if>
        <if test="params.categoryId != null and params.categoryId !=''">
            and spu.category_id = #{params.categoryId}
        </if>
        <if test="params.channelType == 2">
            GROUP BY spu.id
        </if>
        ORDER BY spu.create_time DESC
    </select>
</mapper>