<?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.product.dal.mysql.productSpu.ProductSpuMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <resultMap id="ProductSpuRespMap" type="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuRespVO">
        <result property="id" column="id"/>
        <result property="productName" column="product_name"/>
        <result property="productCode" column="product_code"/>
        <result property="productTag" column="product_tag"/>
        <result property="productTag2" column="product_tag2"/>
        <result property="productTag3" column="product_tag3"/>
        <result property="productTag4" column="product_tag4"/>
        <result property="shortName" column="short_name"/>
        <result property="productType" column="product_type"/>
        <result property="categoryId" column="category_id"/>
        <result property="sortNum" column="sort_num"/>
        <result property="salePrice" column="sale_price"/>
        <result property="costPrice" column="cost_price"/>
        <result property="isUse" column="is_use"/>
        <result property="dayNum" column="day_num"/>
        <result property="isValidated" column="is_validated"/>
        <result property="bookingInstructions" column="booking_instructions"/>
        <result property="productIntroduction" column="product_introduction"/>
        <result property="coverImgHorizontal" column="cover_img_horizontal"/>
        <result property="coverImgVertical" column="cover_img_vertical"/>
        <result property="createTime" column="create_time"/>
        <result property="categoryName" column="cate_name"/>
        <association property="productBasicRule" javaType="com.yc.ship.module.product.controller.admin.productSpu.baseRuleVO.ProductBasicRuleRespVO">
            <id property="id" column="basicId"/>
            <result property="createTime" column="create_time"/>
            <result property="needPlayDate" column="need_play_date"/>
            <result property="validityType" column="validity_type"/>
            <result property="validityNum" column="validity_num"/>
            <result property="validityDate" column="validity_date"/>
            <result property="saleStartDate" column="sale_start_date"/>
            <result property="saleEndDate" column="sale_end_date"/>
            <result property="checkStartDate" column="check_start_date"/>
            <result property="checkEndDate" column="check_end_date"/>
            <result property="saleStartTime" column="sale_start_time"/>
            <result property="saleEndTime" column="sale_end_time"/>
            <result property="checkStartTime" column="check_start_time"/>
            <result property="checkEndTime" column="check_end_time"/>
            <result property="stock" column="stock"/>
        </association>
        <collection property="productGroupList" column="id" select="selectSpuGroupList" javaType="ArrayList"
                    ofType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupRespVO"/>
    </resultMap>

    <select id="selectPage2" resultMap="ProductSpuRespMap">
        SELECT
            p.id,
            p.product_name,
            p.product_code,
            p.product_type,
            p.category_id,
            p.sort_num,
            p.sale_price,
            p.cost_price,
            p.is_use,
            p.product_tag,
            p.product_tag2,
            p.product_tag3,
            p.product_tag4,
            p.day_num,
            p.is_validated,
            p.booking_instructions,
            p.product_introduction,
            p.cover_img_horizontal,
            p.cover_img_vertical,
            p.create_time,
            c.cate_name,
            br.id basicId,
            br.need_play_date,
            br.validity_type,
            br.validity_num,
            br.validity_date,
            br.sale_start_date,
            br.sale_end_date,
            br.check_start_date,
            br.check_end_date,
            br.sale_start_time,
            br.sale_end_time,
            br.check_start_time,
            br.check_end_time,
            br.stock
        FROM
            product_spu p
                INNER JOIN product_basic_rule br ON br.id = p.id
                INNER JOIN product_category c ON c.id = p.category_id
                <if test="param.channelId != null">
                    INNER JOIN product_sale_channel sc ON sc.product_id = p.id
                </if>
        where p.deleted = 0
        <if test="param.productId != null">
            AND p.id = #{param.productId}
        </if>
        <if test="param.productName != null and param.productName != ''">
            AND p.product_name like CONCAT('%',#{param.productName},'%')
        </if>
        <if test="param.productCode != null and param.productCode != ''">
            AND p.product_code = #{param.productCode}
        </if>
        <if test="param.productType != null">
            AND p.product_type = #{param.productType}
        </if>
        <if test="param.categoryId != null">
            AND p.category_id = #{param.categoryId}
        </if>
        <if test="param.isUse != null">
            AND p.is_use = #{param.isUse}
        </if>
        <if test="param.isValidated != null">
            AND p.is_validated = #{param.isValidated}
        </if>
        <if test="param.channelId != null">
            AND sc.channel_id = #{param.channelId}
        </if>
        GROUP BY p.id
        order by if (p.sort_num is null, 9999, p.sort_num), p.create_time desc
    </select>

    <select id="selectPlatSpuList" resultType="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuRespVO">
        SELECT
            p.id,
            p.product_name productName,
            p.product_code productCode,
            p.product_type productType,
            p.category_id categoryId,
            p.sort_num sortNum,
            p.day_num dayNum,
            p.sale_price salePrice,
            p.cost_price costPrice,
            p.is_validated isValidated,
            odp.is_use isUse,
            odp.is_use_otc isUseOtc,
            pc.cate_name categoryName
        FROM
            product_spu p
                INNER JOIN ota_distributor_product odp ON odp.product_id = p.id
                INNER JOIN ota_distributor od ON od.id = odp.distributor_id
                INNER JOIN product_category pc ON pc.id = p.category_id
        WHERE
            od.deleted = 0
            AND odp.is_use = 1
            AND od.is_use = 1
            <if test="param.isAdmin == null">
                AND odp.is_use_otc = 1
            </if>
            <if test="param.categoryId != null and param.categoryId != ''">
                AND pc.id = #{param.categoryId}
            </if>
            <if test="param.productName != null and param.productName != ''">
                AND p.product_name like  CONCAT('%',#{param.productName},'%')
            </if>
            AND odp.deleted = 0
            AND p.deleted = 0
            AND od.account_tenant_id = #{param.tenantId}
        ORDER BY p.id,p.update_time
    </select>

    <select id="selectAppletPage" resultType="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuRespVO">
        SELECT p.*,odp.is_use isUse, odp.is_use_otc isUseOtc, pc.cate_name categoryName
        FROM product_spu p
        INNER JOIN product_sale_channel sc ON sc.product_id = p.id
        INNER JOIN ota_distributor_product odp ON odp.product_id = p.id
        INNER JOIN ota_distributor od ON od.id = odp.distributor_id
        INNER JOIN product_category pc ON pc.id = p.category_id
        WHERE
        p.deleted = 0 AND sc.channel_id = 3
        AND od.deleted = 0
        AND od.is_use = 1
        AND odp.deleted = 0
        AND p.deleted = 0
        <if test="param.productName != null and param.productName != ''">
            AND p.product_name like  CONCAT('%',#{param.productName},'%')
        </if>
        <if test="param.productCode != null and param.productCode != ''">
            AND p.product_code = #{param.productCode}
        </if>
        <if test="param.productType != null and param.productType != ''">
            AND p.product_type = #{param.productType}
        </if>
        <if test="param.categoryId != null and param.categoryId != ''">
            AND p.category_id = #{param.categoryId}
        </if>
        <if test="param.isUse != null and param.isUse != ''">
            AND p.is_use = #{param.isUse}
        </if>
        <if test="param.isValidated != null and param.isValidated != ''">
            AND p.is_validated = #{param.isValidated}
        </if>
        GROUP BY p.id
        ORDER BY p.create_time desc
    </select>

    <select id="selectSpuGroupList" resultType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupRespVO">
        SELECT
        pg.id,
        pg.product_id productId,
        pg.product_base_id productBaseId,
        pg.product_base_type productBaseType,
        pg.sku_id skuId,
        pg.project_id projectId,
        pg.specification_id specificationId,
        pg.first_resource_id firstResourceId,
        pg.cost_price costPrice,
        pg.sale_price salePrice,
        pg.spec_type specType,
        pg.need_choose needChoose,
        p.product_name productName,
        pb.`name` productBaseName,
        pb.can_repeat_buy canRepeatBuy,
        pp.`name` projectName,
        pp.`name` specificationName
        FROM
        product_group pg
        INNER JOIN product_spu p ON p.id = pg.product_id
        INNER JOIN product_base pb ON pb.id = pg.product_base_id
        INNER JOIN product_project pp ON pp.id = pg.project_id
        WHERE
        pg.deleted = 0
        and pg.product_base_type = 0
        AND pg.product_id = #{id}
        ORDER BY pg.id,pg.update_time
    </select>

    <select id="getProductSpuListByStatus" resultType="com.yc.ship.module.product.dal.dataobject.productSpu.ProductSpuDO">
        SELECT
            t1.*
        FROM
            product_spu t1
            INNER JOIN product_sale_channel t2 ON t1.id = t2.product_id
        WHERE
            t1.deleted = 0
        AND t2.deleted = 0
        AND t1.is_use = #{status}
        <if test="channelId!=null">
          AND t2.channel_id = #{channelId}
        </if>
        <if test="categoryId!=null">
            AND t1.category_id = #{categoryId}
        </if>
        ORDER BY t1.create_time DESC
    </select>

    <select id="getProductSpuList" resultType="java.util.Map">
        SELECT
            t1.id,t1.product_name productName,t1.product_code productCode,t1.category_id categoryId,t1.sale_price salePrice,t1.cover_img_horizontal coverImgHorizontal
        FROM
            product_spu t1
            WHERE t1.is_use = 1
            AND t1.deleted = 0
        <if test="categoryIds != null and categoryIds.size() > 0">
            AND t1.category_id IN
            <foreach collection="categoryIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY t1.sort_num ASC
    </select>
    <select id="selectSimpleList" resultType="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuSimpleVO">
        SELECT
            ps.id,ps.product_name productName,
            ps.product_code productCode,
            ps.product_type productType,
            ps.category_id categoryId,
            pc.cate_name categoryName,
            pc.sort_num cateSort,
            ps.sale_price salePrice,
            ps.cost_price costPrice,
            ps.is_use isUse,
            ps.day_num dayNum,
            ps.sort_num sortNum
        FROM
            product_spu ps
        inner join product_category pc on ps.category_id = pc.id
        <if test="param.productBaseId != null and param.productBaseId != ''">
            inner join product_group g on g.product_id = ps.id
        </if>
        WHERE
            ps.deleted = 0
        <if test="param.productName != null and param.productName != ''">
            AND ps.product_name like  CONCAT('%',#{param.productName},'%')
        </if>
        <if test="param.productCode != null and param.productCode != ''">
            AND ps.product_code = #{param.productCode}
        </if>
        <if test="param.productType != null and param.productType != ''">
            AND ps.product_type = #{param.productType}
        </if>
        <if test="param.categoryId != null and param.categoryId != ''">
            AND ps.category_id = #{param.categoryId}
        </if>
        <if test="param.isUse != null and param.isUse != ''">
            AND ps.is_use = #{param.isUse}
        </if>
        <if test="param.productBaseId != null and param.productBaseId != ''">
            AND g.product_base_id = #{param.productBaseId} and g.deleted = 0 group by  ps.id
        </if>
    </select>

    <select id="listAllSpu" resultType="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuSimpleVO">
        SELECT
        distinct
        ps.id,ps.product_name productName,
        ps.product_code productCode,
        ps.product_type productType,
        ps.category_id categoryId,
        pc.cate_name categoryName,
        pc.sort_num cateSort,
        ps.sale_price salePrice,
        ps.cost_price costPrice,
        ps.is_use isUse,
        ps.day_num dayNum,
        ps.sort_num sortNum
        FROM
        product_spu ps
        inner join product_category pc on ps.category_id = pc.id
        inner join product_basic_rule pbr on pbr.id = ps.id and pbr.deleted = 0
        inner join product_spu_ship t3 on ps.id = t3.product_id
        WHERE
        ps.deleted = 0 AND ps.is_use = 1
    </select>

    <select id="listByShipId" resultType="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuSimpleVO">
        SELECT
            distinct
            ps.id,ps.product_name productName,
            ps.product_code productCode,
            ps.product_type productType,
            ps.category_id categoryId,
            pc.cate_name categoryName,
            pc.sort_num cateSort,
            ps.sale_price salePrice,
            ps.cost_price costPrice,
            ps.is_use isUse,
            ps.day_num dayNum,
            ps.sort_num sortNum
        FROM
            product_spu ps
        inner join product_category pc on ps.category_id = pc.id
        inner join product_basic_rule pbr on pbr.id = ps.id and pbr.deleted = 0
        inner join product_spu_ship t3 on ps.id = t3.product_id
        WHERE
            ps.deleted = 0 AND ps.is_use = 1
        <![CDATA[
                AND ((DATE_FORMAT(pbr.sale_start_date,"%Y-%m-%d") <= DATE_FORMAT(now(),"%Y-%m-%d") AND DATE_FORMAT(pbr.sale_end_date,"%Y-%m-%d") >= DATE_FORMAT(now(),"%Y-%m-%d")) OR ( pbr.sale_start_date is null AND pbr.sale_end_date is null))
                ]]>
        <![CDATA[
                AND (
                (pbr.sale_start_time <= DATE_FORMAT(now(),"%H:%i") AND pbr.sale_end_time >= DATE_FORMAT(now(),"%H:%i") )
                OR
                ( (pbr.sale_start_time is null or pbr.sale_start_time = '') AND (pbr.sale_end_time is null or pbr.sale_end_time = '') )
                )
                ]]>
        <if test="shipId != null and shipId != ''">
            and t3.ship_id = #{shipId}
        </if>
    </select>
    <select id="selectPlatSimpleList" resultType="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuSimpleVO">
        SELECT
        ps.id,ps.product_name productName,
        ps.product_code productCode,
        ps.product_type productType,
        ps.product_tag productTag,
        ps.product_tag2 productTag2,
        ps.product_tag3 productTag3,
        ps.product_tag4 productTag4,
        ps.category_id categoryId,
        pc.cate_name categoryName,
        pc.sort_num cateSort,
        ps.sale_price salePrice,
        ps.cost_price costPrice,
        ps.is_use isUse,
        ps.day_num dayNum,
        ps.sort_num sortNum
        FROM
        product_spu ps
        inner join product_category pc on ps.category_id = pc.id
        <if test="param.productBaseId != null and param.productBaseId != ''">
            inner join product_group g on g.product_id = ps.id
        </if>
        <if test="param.distributorId != null and param.distributorId != ''">
            INNER JOIN ota_distributor_product odp ON odp.product_id = ps.id AND odp.is_use = 1
        </if>
        WHERE
        ps.deleted = 0 and ps.tenant_id= #{param.tenantId}
        <if test="param.productName != null and param.productName != ''">
            AND ps.product_name like  CONCAT('%',#{param.productName},'%')
        </if>
        <if test="param.productCode != null and param.productCode != ''">
            AND ps.product_code = #{param.productCode}
        </if>
        <if test="param.productType != null and param.productType != ''">
            AND ps.product_type = #{param.productType}
        </if>
        <if test="param.categoryId != null and param.categoryId != ''">
            AND ps.category_id = #{param.categoryId}
        </if>
        <if test="param.isUse != null and param.isUse != ''">
            AND ps.is_use = #{param.isUse}
        </if>
        <if test="param.distributorId != null and param.distributorId != ''">
            AND odp.distributor_id = #{param.distributorId}
        </if>
        <if test="param.productBaseId != null and param.productBaseId != ''">
            AND g.product_base_id = #{param.productBaseId} and g.deleted = 0 group by  ps.id
        </if>
    </select>



    <select id="getPlatProductlist" resultType="com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuSimpleVO">
        SELECT
            distinct t1.id,t1.product_name productName,
            t1.product_code productCode,
            t1.product_type productType,
            t1.category_id categoryId,
            t1.sort_num cateSort,
            t1.sale_price salePrice,
            t1.cost_price costPrice,
            t1.is_use isUse,
            t1.day_num dayNum,
            t1.sort_num sortNum
       from product_spu t1 inner join ota_distributor_product t2 on t1.id= t2.product_id
       inner join product_group t3 on t2.id = t3.product_base_id
       inner join product_spu t4 on t3.product_id =t4.id
       where t4.id = #{productId}
    </select>

    <select id="getPlatProductIdsById" resultType="java.util.Map">
        SELECT DISTINCT
            t1.id platProductId
        FROM product_spu t1
                 INNER JOIN ota_distributor_product t2 ON t1.id = t2.product_id and t2.deleted =0
                 INNER JOIN product_group t3 ON t2.id = t3.product_base_id and t3.deleted =0
                 INNER JOIN product_spu t4 ON t3.product_id = t4.id
        WHERE t4.id = #{productId} and t1.deleted = 0
    </select>
</mapper>
