<?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.fjhx.item.mapper.product.ProductInfoMapper">

    <select id="getPage" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        SELECT
            pi.id,
            pi.definition,
            pi.product_classify_id,
            pi.CODE,
            pi.type,
            pi.NAME,
            pi.spec,
            pi.unit,
            pi.remark,
            pi.custom_code,
            pi.create_user,
            pi.create_time,
            pi.update_user,
            pi.update_time,
            pi.victoriatourist_json,
            pi.bar_code,
            IF(DATEDIFF(now(),pi.create_time)> json_unquote( victoriatourist_json -> '$.growUpDay' ),3,IF( DATEDIFF(now(),pi.create_time)> json_unquote( victoriatourist_json -> '$.newProductsDay' ), 2, 1 )) AS lifeCycle,
            pi.unit_price,
            pi.standard_json
        FROM
            product_info pi
            ${ew.customSqlSegment}
    </select>

    <select id="getListByProductIds" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        SELECT
            t1.*,
            t2.`name` AS classifyName
        FROM
            product_info t1
        LEFT JOIN product_classify t2 ON t1.product_classify_id = t2.id
        <where>
            <if test="productIds neq null and productIds.size() > 0">
                <foreach collection="productIds" item="productId" open="t1.id IN (" separator="," close=")">
                    #{productId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getListByProductType" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        SELECT
        t1.*,
        t2.`name` AS classifyName
        FROM
        product_info t1
        LEFT JOIN product_classify t2 ON t1.product_classify_id = t2.id
        <where>
            <if test="productType neq null ">
                t1.type = #{productType}
            </if>
            <if test="definition neq null">
                and t1.definition = #{definition}
            </if>

            <if test="productName neq null and productName neq '' ">
                and t1.name = #{productName}
            </if>
            <if test="productCode neq null and productName neq ''">
                and t1.code = #{productCode}
            </if>
        </where>
    </select>
    <select id="productTypeRanking" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        SELECT
            pi.type type,
            ifnull(SUM(pc.amount),0) contractAmount,
            ifnull(SUM(pc.quantity),0) contractQuantity,
            ifnull(sum(pd.amount),0) purchaseAmount,
            IFNULL(SUM(pd.count),0) purchaseQuantity
        FROM
            product_info pi
                LEFT JOIN bytesailing_sale.contract_product pc ON pi.id = pc.product_id
                left join bytesailing_sale.contract bc on pc.contract_id = bc.id
                left join bytesailing_purchase.purchase_detail pd on pi.id = pd.bussiness_id
            ${ew.customSqlSegment}
    </select>
    <select id="productRanking" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        SELECT
            pi.name name,
            ifnull(SUM(pc.amount),0) contractAmount,
            ifnull(SUM(pc.quantity),0) contractQuantity,
            ifnull(sum(pd.amount),0) purchaseAmount,
            IFNULL(SUM(pd.count),0) purchaseQuantity
        FROM
            product_info pi
                LEFT JOIN bytesailing_sale.contract_product pc ON pi.id = pc.product_id
                left join bytesailing_sale.contract bc on pc.contract_id = bc.id
                left join bytesailing_purchase.purchase_detail pd on pi.id = pd.bussiness_id
            ${ew.customSqlSegment}
    </select>

    <select id="getCustomerProductList" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        SELECT pi.id,
               pi.definition,
               pi.product_classify_id,
               pi.CODE,
               pi.type,
               pi.NAME,
               pi.spec,
               pi.unit,
               pi.remark,
               pi.custom_code,
               pi.create_user,
               pi.create_time,
               pi.update_user,
               pi.update_time,
               pi.ehsd_json,
               pi.bar_code,
               c.`name` customerName
        FROM product_info pi
                 LEFT JOIN product_classify pc ON pi.product_classify_id = pc.id
                 LEFT JOIN bytesailing_customer.customer c ON json_unquote(pi.ehsd_json -> '$.customerId') = c.id
            ${ew.customSqlSegment}
    </select>

    <select id="statisticsProduct" resultType="java.util.Map">
        SELECT
            `type` AS `type`,
            count( 1 ) AS count
        FROM
            product_info
        WHERE `type` IS NOT NULL
          AND `type` != "null"
          AND `type` != ""
          AND definition = 1
        GROUP BY
            `type`
    </select>
</mapper>
