<?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.purchase.mapper.PurchaseContractMapper">

    <select id="getNewDataByMaterialCode" resultType="com.fjhx.entity.purchase.PurchaseContract">
        SELECT
            t1.*,
            t2.`Name` AS materialName,
            t3.`Name` AS supplierName
        FROM
            purchase_contract t1
        LEFT JOIN material t2 ON t1.MaterialCode = t2.`Code`
        LEFT JOIN supplier t3 ON t1.SupplierId = t3.ID
        WHERE
          t1.ApproveBillState = 2
          AND MaterialCode = #{materialCode}
        ORDER BY
            t2.CreatedTime
            LIMIT 1
    </select>

    <select id="historyPayList" resultType="java.util.Map">
        SELECT
            fac.LinkId AS payBillId,
            IFNULL(ur.`Name`,'') AS roleName,
            IF
                ( fac.CheckState = 0, '已驳回', '' ) AS state
        FROM
            (
                SELECT
                    LinkId,
                    NodeId,
                    CheckState,
                    ROW_NUMBER() over ( PARTITION BY LinkId ORDER BY CreatedTime DESC ) AS seq
                FROM
                    flow_applycheck
                WHERE
                    1 = 1
                  AND FlowId = '18349246d2a940bbaf5011508e3b0b70'
            ) fac
                LEFT JOIN flow_node fn ON fn.id = fac.NodeId
                LEFT JOIN u_role ur ON fn.RoleKey = ur.`Key`
        WHERE
            fac.seq = 1
    </select>

    <select id="getPayList" resultType="com.fjhx.entity.purchase.PurchaseContract">
        SELECT
           <include refid="sql_filed"/>
        FROM
            apply_purchase_pay t5
        LEFT JOIN purchase_contract t1 ON t5.PurchaseBillNo = t1.PurchaseBillNo
        LEFT JOIN supplier t2 ON t1.SupplierId = t2.ID
        LEFT JOIN material t3 ON t1.MaterialCode = t3.`Code`
        LEFT JOIN stock_inoutbill t4 ON t1.PurchaseBillNo = t4.FromBillNo
        <include refid="pay_condition"/>
        ORDER BY t5.ApprovalTime DESC
        <include refid="com.fjhx.supplier.mapper.SupplierMapper.sql_limit"/>
    </select>
    <select id="getPayListCount" resultType="java.lang.Integer">
        SELECT
            count (1)
        FROM
        apply_purchase_pay t5
        LEFT JOIN purchase_contract t1 ON t5.PurchaseBillNo = t1.PurchaseBillNo
        LEFT JOIN supplier t2 ON t1.SupplierId = t2.ID
        LEFT JOIN material t3 ON t1.MaterialCode = t3.`Code`
        LEFT JOIN stock_inoutbill t4 ON t1.PurchaseBillNo = t4.FromBillNo
        <include refid="pay_condition"/>
    </select>
    <select id="getPayGroupBySu" resultType="java.util.Map">
        SELECT
            IFNULL( SUM( t1.HadPayAmount ), 0 ) AS sumMoney,
            IFNULL( t2.`Name`, '' ) AS supplierName,
            IFNULL(t2.`id`,'') AS supplierId
        FROM
            apply_purchase_pay t5
        LEFT JOIN purchase_contract t1 ON t5.PurchaseBillNo = t1.PurchaseBillNo
        LEFT JOIN supplier t2 ON t1.SupplierId = t2.ID
        LEFT JOIN material t3 ON t1.MaterialCode = t3.`Code`
        <include refid="pay_condition"/>
        GROUP BY t2.ID
    </select>
    <select id="getSumMoney" resultType="java.math.BigDecimal">
        SELECT
            IFNULL(SUM( TotalAmount ),0) AS money
        FROM
            purchase_contract
        WHERE
            IsDelete = 0
          AND DATE_FORMAT( CreatedTime, '%Y' ) = #{date}
    </select>
    <sql id="pay_condition">
        <where>
            t5.ApplyPayState = 2
            <if test="supplierName neq null and supplierName neq '' ">
                AND (INSTR(t2.`Name`, #{supplierName}) > 0)
            </if>
            <if test="supplierId neq null and supplierId neq '' ">
                AND t2.id = #{supplierId}
            </if>
            <if test="materialName neq null and materialName neq '' ">
                AND (INSTR(t3.`Name`, #{materialName}) > 0)
            </if>
            <if test="beginTime neq null and beginTime neq '' ">
                and t5.ApprovalTime &gt;= #{beginTime,jdbcType=VARCHAR}
            </if>
            <if test="endTime neq null and endTime neq '' ">
                and t5.ApprovalTime &lt;= #{endTime,jdbcType=VARCHAR}
            </if>
        </where>
    </sql>
    <sql id="sql_filed">
        t1.*,
        t2.`Name` AS supplierName,
        t3.`Name` AS materialName,
        t3.Spec AS materialSpec,
        t4.inOutStorageNo AS inOutStorageNo,
        DATE_FORMAT(t4.CompleteStockTime,'%Y-%m-%d') AS completeStockTime,
        IFNULL(t2.AccountDate,0) AS accountDate,
        ( SELECT GROUP_CONCAT( CheckFinish ) FROM stock_quantitycheck WHERE PurchaseBillNo = t1.PurchaseBillNo ) AS quantityStatus,
        ( SELECT GROUP_CONCAT( ContractDocType ) FROM purchase_contractdetail WHERE PurchaseBillNo = t1.PurchaseBillNo ) AS contractStatus,
        ( SELECT SUM( Amount ) FROM purchase_contractdetail WHERE PurchaseBillNo = t1.PurchaseBillNo AND ContractDocType = 1 ) AS contractAmount
    </sql>
</mapper>
