<?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">

    <sql id="page">
        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.bar_code,
               pi.length,
               pi.width,
               pi.height,
               pi.net_weight,
               pi.technology_id,
               pi.name_english,
               pi.technology_id,
               pi.hs_code,
               pi.material,
               pi.frontal_texture,
               pi.reverse_texture,
               pi.color_layer,
               pi.color,
               pi.stock_threshold,
               pi.price,
               pi.currency,
               pi.cost_price,
               pi.cost_currency,
               pi.raw_material_id,
               pi.prod_img_path,
               pi.prod_file_path,
               pi.company_id
        FROM product_info pi
    </sql>

    <select id="getPage" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        <include refid="page"/>
        ${ew.customSqlSegment}
    </select>

    <select id="getList" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        <include refid="page"/>
        ${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 contract_product pc ON pi.id = pc.product_id
                 left join contract bc on pc.contract_id = bc.id
                 left join 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 contract_product pc ON pi.id = pc.product_id
                 left join contract bc on pc.contract_id = bc.id
                 left join purchase_detail pd on pi.id = pd.bussiness_id
            ${ew.customSqlSegment}
    </select>

    <select id="getCustomerProductList" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        <include refid="page"/>
        LEFT JOIN product_classify pc ON pi.product_classify_id = pc.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>
    <select id="getProductAnalysisPage" resultType="com.fjhx.item.entity.product.bo.ProductAnalysisBo">
        select pi.id   AS productId,
               pi.name AS productName
        from product_info pi
            ${ew.customSqlSegment}
    </select>

    <select id="getProductByMaterialId" resultType="com.fjhx.item.entity.product.vo.ProductInfoVo">
        SELECT DISTINCT pi.*
        FROM product_info pi
                 JOIN product_bom_detail pbd ON pbd.product_id = pi.id
            ${ew.customSqlSegment}
    </select>
    <select id="getProcessesByTechnologyId" resultType="com.fjhx.item.entity.product.ProcessesBo">
        SELECT pp.*
        FROM production_processes pp
                 LEFT JOIN technology_process_line tpl ON tpl.target_processes_id = pp.id
        WHERE tpl.technology_id = #{technologyId}
        GROUP BY pp.id
    </select>
</mapper>
