<?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.supplier.mapper.SupplierMapper">

    <select id="getSumList" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT

        t1.*,
        t3.TechnologyType AS TechnologyType,
        (
        <include refid="warningCount_filed"/>
        ) AS warningCount,
        (
        <include refid="warningTypeCount_filed"/>
        ) AS warningTypeCount,
        (select `Name` from material_category where `Code` = t1.CategoryCode) AS categoryName,
        FORMAT(IFNULL((<include refid="sumMoney_filed"/>),0),2)AS sumMoney,
        FORMAT(IFNULL((<include refid="lastYearMoney_filed"/>),0),2)AS lastYearMoney,
        FORMAT(IFNULL((<include refid="yearMoney_filed"/>),0),2)AS yearMoney,
        FORMAT(IFNULL((<include refid="lastMonthMoney_filed"/>),0),2)AS lastMonthMoney,
        FORMAT(IFNULL((<include refid="monthMoney_filed"/>),0),2)AS monthMoney

        FROM
        supplier t1
        LEFT JOIN supplier_price sp ON t1.ID = sp.FactoryId
        LEFT JOIN material t3 ON t3.`Code` = sp.MaterialCode
        <include refid="list_condition"/>
        GROUP BY t1.ID
        ORDER BY t1.CreatedTime DESC
        <include refid="sql_limit"/>
    </select>

    <select id="getSumListCount" resultType="java.lang.Integer">
        SELECT
        Count( 1 )
        FROM
        (
        SELECT
        count(1)
        FROM
        supplier t1
        LEFT JOIN supplier_price sp ON t1.ID = sp.FactoryId
        LEFT JOIN material t3 ON t3.`Code` = sp.MaterialCode
        <include refid="list_condition"/>
        GROUP BY t1.ID
        )AS a
    </select>
    <select id="getList" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        t1.*,
        t3.TechnologyType AS TechnologyType,
        (
        <include refid="warningCount_filed"/>
        ) AS warningCount,
        (
        <include refid="warningTypeCount_filed"/>
        ) AS warningTypeCount,
        (select `Name` from material_category where `Code` = t1.CategoryCode) AS categoryName
        FROM
        material t3
        LEFT JOIN supplier_price sp ON t3.`Code` = sp.MaterialCode
        LEFT JOIN supplier t1 ON sp.FactoryId = t1.ID
        <include refid="list_condition"/>
        AND t3.IsDelete = 0
        GROUP BY t1.ID,
        t3.TechnologyType
        ORDER BY t3.TechnologyType
        <include refid="sql_limit"/>
    </select>
    <select id="getListCount" resultType="java.lang.Integer">
        SELECT
        Count( 1 )
        FROM
        (
        SELECT
        count(1)
        FROM
        material t3
        LEFT JOIN supplier_price sp ON t3.`Code` = sp.MaterialCode
        LEFT JOIN supplier t1 ON sp.FactoryId = t1.ID
        <include refid="list_condition"/>
        AND t3.IsDelete = 0
        GROUP BY t1.ID,
        t3.TechnologyType
        )AS a
    </select>
    <select id="getPurList" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        t1.*,
        t3.CategoryCode,
        t3.TechnologyType AS TechnologyType,
        t3.Purpose AS purpose,
        (
        <include refid="warningCount_filed"/>
        ) AS warningCount,
        (
        <include refid="warningTypeCount_filed"/>
        ) AS warningTypeCount,
        FORMAT(IFNULL((<include refid="sumMoney_filed"/>),0),2)AS sumMoney,
        FORMAT(IFNULL((<include refid="lastYearMoney_filed"/>),0),2)AS lastYearMoney,
        FORMAT(IFNULL((<include refid="yearMoney_filed"/>),0),2)AS yearMoney,
        FORMAT(IFNULL((<include refid="lastMonthMoney_filed"/>),0),2)AS lastMonthMoney,
        FORMAT(IFNULL((<include refid="monthMoney_filed"/>),0),2)AS monthMoney
        FROM
        material t3
        LEFT JOIN supplier_price sp ON t3.`Code` = sp.MaterialCode
        LEFT JOIN supplier t1 ON sp.FactoryId = t1.ID
        <include refid="list_condition"/>
        AND t3.IsDelete = 0
        GROUP BY t1.ID,
        t3.TechnologyType
        ORDER BY t3.TechnologyType
        <include refid="sql_limit"/>
    </select>
    <select id="getPurListCount" resultType="java.lang.Integer">
        SELECT
        Count( 1 )
        FROM
        (
        SELECT
        count(1)
        FROM
        material t3
        LEFT JOIN supplier_price sp ON t3.`Code` = sp.MaterialCode
        LEFT JOIN supplier t1 ON sp.FactoryId = t1.ID
        <include refid="list_condition"/>
        AND t3.IsDelete = 0
        GROUP BY t1.ID,
        t3.TechnologyType
        ) AS a
    </select>
    <select id="getGroupByCategory" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        TechnologyType AS TechnologyType,
        Count( 1 ) AS CategoryCount
        FROM
        (
        SELECT
        t3.TechnologyType AS TechnologyType
        FROM
        material t3
        INNER JOIN supplier_price sp ON t3.`Code` = sp.MaterialCode
        INNER JOIN supplier t1 ON sp.FactoryId = t1.ID
        <where>
            t1.IsDelete = 0
            AND t3.IsDelete = 0
            <if test="search neq null and search neq '' ">
                AND ((INSTR(t1.`Name`, #{search}) > 0) OR (INSTR(t1.`Code`, #{search}) > 0) )
            </if>
            <if test="Purpose neq null and Purpose neq '' ">
                AND INSTR( t3.Purpose, #{Purpose} ) > 0
            </if>
            <if test="accountDate neq null">
                AND t1.AccountDate = #{accountDate}
            </if>
            <if test="type eq 1 ">
                and t3.SafetyWarnDay between 0 and 5
            </if>
            <if test="type eq 2 ">
                and t3.SafetyWarnDay between 6 and 15
            </if>
            <if test="type eq 3 ">
                and t3.SafetyWarnDay between 16 and 30
            </if>
            <if test="type eq 4 ">
                and t3.SafetyWarnDay between 31 and 45
            </if>
            <if test="type eq 5 ">
                and t3.SafetyWarnDay &gt; 45
            </if>
        </where>
        GROUP BY t1.ID,t3.TechnologyType
        ) detail
        GROUP BY
        TechnologyType
        ORDER BY
        TechnologyType
    </select>
    <select id="getSuById" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        t1.*,
        (
        <include refid="warningCount_filed"/>
        ) AS warningCount,
        (
        <include refid="warningTypeCount_filed"/>
        ) AS warningTypeCount
        FROM
        supplier t1
        WHERE IsDelete = 0
        AND id = #{id}
    </select>
    <select id="getMaterialTypeCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM (SELECT count(1)
              FROM supplier t1
                       LEFT JOIN purchase_contract t2 ON t1.ID = t2.SupplierId
              WHERE t1.`id` = #{id}
              GROUP BY t2.MaterialCode
             ) AS t1
    </select>
    <select id="getBackCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM supplier t1
                 LEFT JOIN purchase_contract t2 ON t1.ID = t2.SupplierId
                 LEFT JOIN apply_scenebackdetail t3 ON t2.PurchaseBillNo = t3.PurchaseBillNo
        WHERE t1.`id` = #{id}
          AND t3.PurchaseBillNo IS NOT NULL
          AND t3.SceneBackType = 1
    </select>
    <select id="getSuPurMaterialAnalyse" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        t3.`Name` AS materialName,
        t4.`Name` AS categoryName,
        t5.totalMoney AS totalMoney,
        FORMAT( IFNULL( SUM( t2.TotalAmount ), 0 ), 2 ) AS sumMoney,
        FORMAT(IFNULL((
        <include refid="lastMonthMoney_filed"/>
        AND MaterialCode = t2.MaterialCode),0),2)AS lastMonthMoney,
        FORMAT(IFNULL((
        <include refid="monthMoney_filed"/>
        AND MaterialCode = t2.MaterialCode),0),2)AS monthMoney,
        count( 1 ) AS count
        FROM
        supplier t1
        LEFT JOIN purchase_contract t2 ON t1.ID = t2.SupplierId
        LEFT JOIN material t3 ON t2.MaterialCode = t3.`Code`
        LEFT JOIN material_category t4 ON t1.CategoryCode = t4.`Code`
        LEFT JOIN ( SELECT IFNULL(SUM( TotalAmount ),0) AS totalMoney, MaterialCode FROM purchase_contract WHERE
        IsDelete = 0 GROUP BY MaterialCode ) t5 ON t5.MaterialCode = t2.MaterialCode
        WHERE
        t1.`id` = #{id}
        AND t2.IsDelete = 0
        GROUP BY
        t2.MaterialCode
        ORDER BY SUM( t2.TotalAmount ) DESC
        <include refid="sql_limit"/>
    </select>
    <select id="getSuPurMaterialAnalyseCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM (SELECT count(1) AS count
              FROM supplier t1
                       LEFT JOIN purchase_contract t2 ON t1.ID = t2.SupplierId
                       LEFT JOIN material t3 ON t2.MaterialCode = t3.`Code`
                       LEFT JOIN material_category t4 ON t1.CategoryCode = t4.`Code`
              WHERE t1.`id` = #{id}
              GROUP BY t2.MaterialCode) AS a
    </select>

    <select id="getBackSumMoneyAndCount" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        a.CreatedTime,
        b.count,
        b.sumMoney
        FROM
        (<include refid="getMonth"/>)AS a
        LEFT JOIN (
        SELECT
        count(1)AS count,
        SUM(t3.Amount)AS sumMoney,
        t3.CreatedTime AS CreatedTime
        FROM
        supplier t1
        LEFT JOIN purchase_contract t2 ON t1.ID = t2.SupplierId
        LEFT JOIN apply_scenebackdetail t3 ON t2.PurchaseBillNo = t3.PurchaseBillNo
        WHERE
        t1.`id` = #{id}
        AND t3.PurchaseBillNo IS NOT NULL
        AND t3.SceneBackType = 1
        AND DATE_FORMAT(t2.CreatedTime,'%Y') = #{date}
        GROUP BY
        DATE_FORMAT(t3.CreatedTime,"%Y-%m"))AS b
        ON DATE_FORMAT(a.`CreatedTime`,'%c') = DATE_FORMAT(b.CreatedTime,"%c")
        ORDER BY a.CreatedTime ASC
    </select>

    <select id="getAllSumMoneyAndCount" resultType="java.util.Map">
        SELECT Count(1)                  AS count,
               IFNULL(SUM(t3.Amount), 0) AS sumMoney
        FROM supplier t1
                 LEFT JOIN purchase_contract t2 ON t1.ID = t2.SupplierId
                 LEFT JOIN apply_scenebackdetail t3 ON t2.PurchaseBillNo = t3.PurchaseBillNo
        WHERE t1.`id` = #{id}
          AND t3.PurchaseBillNo IS NOT NULL
          AND t3.SceneBackType = 1
          AND DATE_FORMAT(t2.CreatedTime, '%Y') = #{date}
    </select>

    <select id="getMonthSumPurMoney" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        a.CreatedTime,
        IFNULL(b.money,0) AS money
        FROM
        (<include refid="getMonth"/>)AS a
        LEFT JOIN (
        SELECT
        SUM( TotalAmount ) AS money,
        t2.CreatedTime AS CreatedTime
        FROM
        supplier t1
        LEFT JOIN purchase_contract t2 ON t1.ID = t2.SupplierId
        WHERE
        t1.ID = #{id}
        AND t2.isDelete = 0
        AND DATE_FORMAT( t2.CreatedTime, '%Y' ) = #{date}
        GROUP BY
        DATE_FORMAT( t2.CreatedTime, '%Y-%m' ))AS b
        ON DATE_FORMAT(a.`CreatedTime`,'%c') = DATE_FORMAT(b.CreatedTime,"%c")
        ORDER BY a.CreatedTime ASC
    </select>

    <select id="selectLists" resultType="com.fjhx.entity.supplier.Supplier">
        SELECT
        *
        FROM
        supplier t1
        <include refid="list_condition"/>
        ORDER BY CreatedTime DESC
        <include refid="sql_limit"/>
    </select>

    <select id="getSafetyWarnDayList" resultType="java.util.Map">
        select m.SafetyWarnDay safetyWarnDay,
               sp.FactoryId    factoryId
        from material m
                 inner join supplier_price sp on sp.MaterialCode = m.Code and sp.IsDelete = 0
                 inner join supplier s on s.ID = sp.FactoryId and s.IsDelete = 0
        where m.IsDelete = 0
    </select>

    <select id="getSafetyWarnDayBySupplier" resultType="java.util.Map">
        select m.Name          materialName,
               m.Code          materialCode,
               m.SafetyWarnDay safetyWarnDay
        from supplier_price sp
                 inner join material m on sp.MaterialCode = m.code
        where sp.FactoryId = #{supplierId}
    </select>

    <select id="paymentGetBackAmount" resultType="java.math.BigDecimal">
        SELECT sum(asod.Amount) - sum(ifnull(app.BackAmount, 0)) AS BackAmount
        FROM apply_stockoutdetail asod
                 LEFT JOIN apply_purchase_pay app ON asod.BalancePurBillNo = app.PurchaseBillNo
            AND app.ApplyPayState = 2
        WHERE BalancePurBillNo = #{purchaseBillNo}
        GROUP BY BalancePurBillNo
    </select>


    <sql id="warningCount_filed">
        SELECT count(1)
        FROM supplier_problem
        WHERE SupplierId = t1.id
    </sql>
    <sql id="warningTypeCount_filed">
        SELECT SUM(ProblemTypeCount)
        FROM supplier_problem
        WHERE SupplierId = t1.id
    </sql>
    <sql id="sumMoney_filed">
        SELECT SUM(TotalAmount)
        FROM purchase_contract
        WHERE IsDelete = 0
          AND SupplierId = t1.ID
    </sql>
    <sql id="yearMoney_filed">
        select SUM(TotalAmount)
        from purchase_contract
        where IsDelete = 0
          AND SupplierId = t1.ID
          AND YEAR(CreatedTime) = YEAR(NOW())
    </sql>
    <sql id="lastYearMoney_filed">
        select SUM(TotalAmount)
        from purchase_contract
        where IsDelete = 0
          AND SupplierId = t1.ID
          AND YEAR(CreatedTime) = YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR))
    </sql>
    <sql id="monthMoney_filed">
        select SUM(TotalAmount)
        from purchase_contract
        where IsDelete = 0
          AND SupplierId = t1.ID
          AND DATE_FORMAT(CreatedTime, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')
    </sql>
    <sql id="lastMonthMoney_filed">
        select SUM(TotalAmount)
        from purchase_contract
        where IsDelete = 0
          AND SupplierId = t1.ID
          AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'), DATE_FORMAT(CreatedTime, '%Y%m')) = 1
    </sql>
    <sql id="list_condition">
        <where>
            t1.IsDelete = 0
            <if test="search neq null and search neq '' ">
                AND ((INSTR(t1.`Name`, #{search}) > 0) OR (INSTR(t1.`Code`, #{search}) > 0) )
            </if>
            <if test="TechnologyType neq null">
                AND t3.TechnologyType = #{TechnologyType}
            </if>
            <if test="Purpose neq null and Purpose neq '' ">
                AND INSTR( t3.Purpose, #{Purpose} ) > 0
            </if>

            <if test="materialCode neq null and materialCode neq '' ">
                AND t1.ID in ( select sp.FactoryId from supplier_price sp where sp.MaterialCode = #{materialCode} )
            </if>

            <if test="accountDate neq null">
                AND t1.AccountDate = #{accountDate}
            </if>
            <if test="type eq 1 ">
                and t3.SafetyWarnDay between 0 and 5
            </if>
            <if test="type eq 2 ">
                and t3.SafetyWarnDay between 6 and 15
            </if>
            <if test="type eq 3 ">
                and t3.SafetyWarnDay between 16 and 30
            </if>
            <if test="type eq 4 ">
                and t3.SafetyWarnDay between 31 and 45
            </if>
            <if test="type eq 5 ">
                and t3.SafetyWarnDay &gt; 45
            </if>

        </where>
    </sql>
    <sql id="sql_limit">
        <if test="start != null and end != null">
            LIMIT #{start},#{end}
        </if>
    </sql>
    <sql id="getMonth">
        SELECT CONCAT(#{date}, "-01-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-02-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-03-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-04-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-05-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-06-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-07-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-08-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-09-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-10-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-11-01 00:00:00") AS CreatedTime
        UNION
        SELECT CONCAT(#{date}, "-12-01 00:00:00") AS CreatedTime
    </sql>
</mapper>
