<?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.bonus.material.push.mapper.IwsCostPushMapper">

    <select id="findAgreement" resultType="com.bonus.material.push.domain.IwsCostPushBean">
        SELECT
            bp.pro_id AS projectId, bp.pro_name AS projectName, bp.pro_code AS projectCode,
            bu.unit_name AS unitName,
            bma.agreement_id AS agreementId,bma.`agreement_code` AS agreementCode, bma.sign_time AS signTime, bma.is_push AS isPush,
            IF(saa.audit_time IS NULL,0,1) AS isSettlement, saa.audit_time AS settlementTime,
            IF(saas.audit_time IS NULL,0,1) AS safetyIsSettlement, saas.audit_time AS safetySettlementTime
        FROM
            bm_agreement_info bma
        LEFT JOIN bm_project bp ON bp.pro_id = bma.project_id
        LEFT JOIN bm_unit bu ON bu.unit_id = bma.unit_id
        LEFT JOIN slt_agreement_apply saa ON saa.agreement_id = bma.agreement_id AND saa.`status` = '2' AND saa.settlement_type = 1
        LEFT JOIN slt_agreement_apply saas ON saas.agreement_id = bma.agreement_id AND saas.`status` = '2' AND saas.settlement_type = 2
        <where>
            <if test="agreementCode != null and agreementCode != ''">
                AND bma.agreement_code LIKE CONCAT('%',#{agreementCode},'%')
            </if>
            <if test="projectId != null and projectId != ''">
                AND bma.project_id = #{projectId}
            </if>
            <if test="unitId != null and unitId != ''">
                AND bma.unit_id = #{unitId}
            </if>
            <if test="isPush != null">
                AND bma.is_push = #{isPush}
            </if>
            <if test="isSettlement != null and isSettlement != ''">
                AND bma.is_slt = #{isSettlement}
            </if>
        </where>
        GROUP BY bma.agreement_id
    </select>

    <update id="updateAgreementIsPush">
        UPDATE bm_agreement_info SET is_push = #{isPush} WHERE agreement_id = #{agreementId}
    </update>

    <select id="getCostPushCheckList" resultType="com.bonus.material.push.domain.IwsCostPushBean">
        SELECT
            pmc.id AS id, pmc.AGREEMENT_ID AS agreementId, IF(ISNULL(pmc.check_status), 0, pmc.check_status) AS checkStatus,
            pmc.LEASE_MONEY AS leaseMoney, pmc.LOST_MONEY AS lostMoney, pmc.REPAIR_MONEY AS repairMoney, pmc.SCRAP_MONEY AS scrapMoney,
            bma.agreement_code AS agreementCode, bma.is_slt AS isSettlement, pmc.TYPE AS settlementType,
            bp.pro_name AS projectName, bu.unit_name AS unitName,
            ROUND(
                SUM(ifnull( pmc.LEASE_MONEY, 0 )+ ifnull( pmc.LOST_MONEY, 0 )+ ifnull( pmc.REPAIR_MONEY, 0 )+ ifnull( pmc.SCRAP_MONEY, 0 )), 2
            ) AS money
        FROM
            project_month_costs pmc
        LEFT JOIN calc_project_month cpm on pmc.task_id = cpm.id
        LEFT JOIN bm_agreement_info bma ON pmc.AGREEMENT_ID = bma.agreement_id
        LEFT JOIN bm_project bp ON bp.pro_id = bma.project_id
        LEFT JOIN bm_unit bu ON bu.unit_id = bma.unit_id
        WHERE
            ( pmc.LEASE_MONEY > 0 OR pmc.LOST_MONEY > 0 OR pmc.REPAIR_MONEY > 0 OR pmc.SCRAP_MONEY > 0 )
            <if test="checkStatus != null">
                AND pmc.check_status = #{checkStatus}
            </if>
            <if test="agreementCode != null and agreementCode != ''">
                AND bma.agreement_code LIKE CONCAT('%',#{agreementCode},'%')
            </if>
            <if test="projectId != null and projectId != ''">
                AND bma.project_id = #{projectId}
            </if>
            <if test="unitId != null and unitId != ''">
                AND bma.unit_id = #{unitId}
            </if>
            <if test="isSettlement != null and isSettlement != ''">
                AND bma.is_slt = #{isSettlement}
            </if>
            <if test="month != null">
                AND cpm.month = #{month}
            </if>
            <if test="settlementType != null">
                AND pmc.TYPE = #{settlementType}
            </if>

        GROUP BY
            pmc.AGREEMENT_ID, pmc.TYPE
    </select>

    <insert id="insertCalcMonthRecord" parameterType="com.bonus.material.push.domain.IwsCostPushBean" useGeneratedKeys="true" keyProperty="taskId">
        INSERT INTO calc_project_month (MONTH)
        VALUES (#{month})
    </insert>

    <select id="getPaidSltBaseInfo" resultType="com.bonus.material.push.domain.IwsCostPushBean">
        SELECT
            mt1.type_name as typeName,
            mt.type_name as modelName,
            mt.unit_name AS unitName,
            sai.lease_price as leasePrice,
            bma.agreement_id AS agreementId,sai.type_id as typeId,
            IFNULL(mt.lease_price ,0) as nowLeasePrice,
            sai.buy_price as buyPrice,sai.num AS leaseNum,sai.start_time AS leaseDate,sai.end_time AS backDate,
            IF(sai.`status` = 0,sai.num,0) as returnNum,
            (datediff(sai.end_time,sai.start_time)+1) * (sai.num) * (sai.lease_price) AS leaseMoney,
            bma.`agreement_code` AS agreementCode,bma.project_id as projectId,
            bma.is_slt AS isSettlement,
            bma.sign_time AS settlementTime,
            bma.is_push AS isPush
        FROM bm_agreement_info bma
        LEFT JOIN slt_agreement_info sai ON bma.agreement_id = sai.agreement_id
        LEFT JOIN ma_type mt ON mt.type_id = sai.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        <where>
            bma.agreement_id = #{agreementId}
        </where>
    </select>

    <select id="getLeaseCostsByAgreementIdAndMonth" resultType="com.bonus.material.push.domain.IwsCostPushBean">
        SELECT
            pmi.typeId as typeId, pmi.leaseNum as leaseNum, pmi.leaseDate, pmi.returnDate as backDate,
            pmi.leasePrice as leasePrice, pmi.leaseMoney as leaseMoney, pmi.jiju_type as settlementType,
            mt1.type_name as typeName,
            mt.type_name as modelName,
            mt.unit_name AS unitName
        FROM
            project_month_info pmi
                INNER JOIN calc_project_month cpm ON pmi.taskId = cpm.ID AND cpm.`MONTH` = #{month}
                LEFT JOIN ma_type mt ON mt.type_id = pmi.typeId
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        WHERE
            pmi.agreementId = #{agreementId}
            AND pmi.jiju_type = #{settlementType,jdbcType=TINYINT}
    </select>

    <insert id="insertProjectMonthCosts">
        INSERT INTO project_month_costs(
            AGREEMENT_ID,
            LEASE_MONEY,
            CONSUME_MONEY,
            LOST_MONEY,
            REPAIR_MONEY,
            SCRAP_MONEY,
            TASK_ID,
            TYPE
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
                #{item.agreementId},
                #{item.leaseMoney},
                #{item.consumeMoney},
                #{item.lostMoney},
                #{item.repairMoney},
                #{item.scrapMoney},
                #{item.taskId},
                #{item.settlementType}
            )
        </foreach>
    </insert>

    <select id="getProLeaseInfo" resultType="com.bonus.material.push.domain.IwsCostPushBean">
        SELECT
        mt1.type_name as typeName,
        mt.type_name as modelName,
        mt.unit_name AS unitName,
        sai.lease_price as leasePrice,
        bma.agreement_id AS agreementId,sai.type_id as typeId,
        IFNULL(mt.lease_price ,0) as nowLeasePrice,
        sai.buy_price as buyPrice,sai.num AS leaseNum,sai.start_time AS leaseDate,sai.end_time AS backDate,
        IF(sai.`status` = 0,sai.num,0) as returnNum,
        (datediff(sai.end_time,sai.start_time)+1) * (sai.num) * (sai.lease_price) AS leaseMoney,
        bma.`agreement_code` AS agreementCode,bma.project_id as projectId,
        bma.is_slt AS isSettlement,
        bma.sign_time AS settlementTime,
        bma.is_push AS isPush
        FROM bm_agreement_info bma
        LEFT JOIN slt_agreement_info sai ON bma.agreement_id = sai.agreement_id
        LEFT JOIN ma_type mt ON mt.type_id = sai.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        where 1=1
        <if test="projectId != null and projectId != ''">
            and bma.project_id in (${projectId})
        </if>
    </select>

    <select id="getTotalInfo" resultType="com.bonus.material.push.domain.IwsCostPushBean">
        SELECT
            parent_id as parentId,
            pre_num AS preNum
        FROM
            clz_lease_apply_details
        WHERE
            type_id = #{typeId}
          AND parent_id = #{parentId}
    </select>

    <select id="getEquipmentListByType" resultType="com.bonus.material.clz.domain.vo.MaterialRetainedEquipmentInfo">
        SELECT
        mt.type_id                          AS typeId,
        mt4.type_name                       AS constructionType,
        mt4.type_id                         AS firstTypeId,
        mt3.type_name                       AS materialType,
        mt3.type_id                         AS secondTypeId,
        mt2.type_name                       AS typeName,
        mt2.type_id                         AS thirdTypeId,
        mt.type_name                        AS typeModelName,
        mt.unit_name                        AS unit,
        IFNULL(mt.lease_price, 0)             AS buyPrice,
        IFNULL(subquery1.usNum, 0)          AS storeNum,
        IFNULL(subquery3.usNum, 0)          AS usNum,
        IFNULL(subquery1.usNum, 0) + IFNULL(subquery3.usNum, 0)
        AS allNum,
        CASE mt.manage_type
        WHEN 0 THEN
        '编码'
        ELSE
        '数量'
        END                                manageType
        FROM ma_type mt
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt4.type_name AS constructionType,
        mt4.type_id AS firstTypeId,
        mt3.type_name AS materialType,
        mt3.type_id AS secondTypeId,
        mt2.type_name AS typeName,
        mt2.type_id AS thirdTypeId,
        mt.type_name AS typeModelName,
        SUM(IFNULL( sai.num, 0 )) AS usNum
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_type mt ON mt.type_id = sai.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        left join bm_agreement_info bai on sai.agreement_id=bai.agreement_id
        WHERE
        sai.`status` = '0'
        <if test="agreementCode != null and agreementCode != ''">
            and bai.agreement_code =#{agreementCode}
        </if>
        <if test="projectId != null and projectId != ''">
            and bai.project_id in (${projectId})
        </if>
        AND sai.end_time IS NULL
        AND sai.back_id IS NULL
        GROUP BY mt.type_id
        ) AS subquery1
        ON mt.type_id = subquery1.type_id
        LEFT JOIN (
        SELECT
        subquery3.type_id,
        subquery3.typeName,
        subquery3.typeModelName,
        IFNULL(subquery3.outNum, 0)  AS outNum,
        IFNULL(subquery4.backNum, 0) AS backNum,
        CASE
        WHEN IFNULL(subquery3.outNum, 0) - IFNULL(subquery4.backNum, 0) > 0 THEN
        IFNULL(subquery3.outNum, 0) - IFNULL(subquery4.backNum, 0)
        ELSE 0
        END                      AS usNum
        FROM (
        SELECT
        mt.type_id,
        mt4.type_name               AS constructionType,
        mt4.type_id                 AS firstTypeId,
        mt3.type_name               AS materialType,
        mt3.type_id                 AS secondTypeId,
        mt2.type_name               AS typeName,
        mt2.type_id                 AS thirdTypeId,
        mt.type_name                AS typeModelName,
        SUM(IFNULL(lod.out_num, 0)) AS outNum
        FROM clz_lease_out_details lod
        LEFT JOIN ma_type mt ON mt.type_id = lod.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE lod.is_finished = '1'
        GROUP BY mt.type_id) AS subquery3
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt4.type_name AS constructionType,
        mt4.type_id   AS firstTypeId,
        mt3.type_name AS materialType,
        mt3.type_id          AS secondTypeId,
        mt2.type_name AS             typeName,
        mt2.type_id          AS thirdTypeId,
        mt.type_name  AS             typeModelName,
        SUM(IFNULL(bcd.back_num, 0)) backNum
        FROM clz_back_check_details bcd
        LEFT JOIN ma_type mt ON mt.type_id = bcd.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        LEFT JOIN ma_machine mm ON mm.ma_id = bcd.ma_id
        WHERE bcd.is_finished = '1'
        GROUP BY mt.type_id) AS subquery4
        ON subquery3.type_id = subquery4.type_id) AS subquery3
        ON mt.type_id = subquery3.type_id
        LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE mt.`level` = 4 and mt.del_flag = '0'
        <if test="deviceName != null and deviceName != ''">
            and mt2.type_name like concat('%',#{deviceName},'%')
        </if>
        <if test="deviceModel != null and deviceModel != ''">
            and mt.type_name like concat('%',#{deviceModel},'%')
        </if>
        <if test="maType != null and maType != ''">
            and mt3.type_name like concat('%',#{maType},'%')
        </if>
    </select>

    <select id="getEquipmentListByAgreementCode" resultType="com.bonus.material.clz.domain.vo.MaterialRetainedEquipmentInfo">
        SELECT
        mt.type_id                          AS typeId,
        mt4.type_name                       AS constructionType,
        mt4.type_id                         AS firstTypeId,
        mt3.type_name                       AS materialType,
        mt3.type_id                         AS secondTypeId,
        mt2.type_name                       AS typeName,
        mt2.type_id                         AS thirdTypeId,
        mt.type_name                        AS typeModelName,
        mt.unit_name                        AS unit,
        IFNULL(mt.lease_price, 0)             AS buyPrice,
        IFNULL(subquery1.usNum, 0)          AS storeNum,
        IFNULL(subquery3.usNum, 0)          AS usNum,
        IFNULL(subquery1.usNum, 0) + IFNULL(subquery3.usNum, 0)
        AS allNum,
        CASE mt.manage_type
        WHEN 0 THEN
        '编码'
        ELSE
        '数量'
        END                                manageType
        FROM ma_type mt
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt4.type_name AS constructionType,
        mt4.type_id AS firstTypeId,
        mt3.type_name AS materialType,
        mt3.type_id AS secondTypeId,
        mt2.type_name AS typeName,
        mt2.type_id AS thirdTypeId,
        mt.type_name AS typeModelName,
        SUM(IFNULL( sai.num, 0 )) AS usNum
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_type mt ON mt.type_id = sai.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE
        sai.`status` = '0'
        AND sai.end_time IS NULL
        AND sai.back_id IS NULL
        GROUP BY mt.type_id
        ) AS subquery1
        ON mt.type_id = subquery1.type_id
        LEFT JOIN (
        SELECT
        subquery3.type_id,
        subquery3.typeName,
        subquery3.typeModelName,
        IFNULL(subquery3.outNum, 0)  AS outNum,
        IFNULL(subquery4.backNum, 0) AS backNum,
        CASE

        WHEN IFNULL(subquery3.outNum, 0) - IFNULL(subquery4.backNum, 0) > 0 THEN
        IFNULL(subquery3.outNum, 0) - IFNULL(subquery4.backNum, 0)
        ELSE 0
        END                      AS usNum
        FROM (
        SELECT
        mt.type_id,
        mt4.type_name               AS constructionType,
        mt4.type_id                 AS firstTypeId,
        mt3.type_name               AS materialType,
        mt3.type_id                 AS secondTypeId,
        mt2.type_name               AS typeName,
        mt2.type_id                 AS thirdTypeId,
        mt.type_name                AS typeModelName,
        SUM(IFNULL(lod.out_num, 0)) AS outNum
        FROM clz_lease_out_details lod
        LEFT JOIN ma_type mt ON mt.type_id = lod.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE lod.is_finished = '1'
        GROUP BY mt.type_id) AS subquery3
        LEFT JOIN (
        SELECT
        mt.type_id,
        mt4.type_name AS constructionType,
        mt4.type_id   AS firstTypeId,
        mt3.type_name AS materialType,
        mt3.type_id          AS secondTypeId,
        mt2.type_name AS             typeName,
        mt2.type_id          AS thirdTypeId,
        mt.type_name  AS             typeModelName,
        SUM(IFNULL(bcd.back_num, 0)) backNum
        FROM clz_back_check_details bcd
        LEFT JOIN ma_type mt ON mt.type_id = bcd.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        LEFT JOIN ma_machine mm ON mm.ma_id = bcd.ma_id
        WHERE bcd.is_finished = '1'
        GROUP BY mt.type_id) AS subquery4
        ON subquery3.type_id = subquery4.type_id) AS subquery3
        ON mt.type_id = subquery3.type_id
        LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        LEFT JOIN ma_type mt3 ON mt3.type_id = mt2.parent_id
        LEFT JOIN ma_type mt4 ON mt4.type_id = mt3.parent_id
        WHERE mt.`level` = 4 and mt.del_flag = '0'
        <if test="maModelName != null and maModelName != ''">
            and mt.type_name like concat('%',#{maModelName},'%')
        </if>
        <if test="maTypeName != null and maTypeName != ''">
            and mt2.type_name like concat('%',#{maTypeName},'%')
        </if>
    </select>

    <insert id="insertProjectMonthCostsDetails" parameterType="com.bonus.material.settlement.domain.vo.PeriodCostResultVo">
        insert into project_month_info(agreementId, typeId, leaseNum, leaseDate, returnDate,
        leasePrice, leaseMoney, taskId, buy_price, money, jiju_type)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.agreementId},
            #{item.typeId},
            #{item.num},
            #{item.calcStartTime},
            #{item.calcEndTime},
            #{item.leasePrice},
            #{item.leaseCost},
            #{item.taskId},
            #{item.buyPrice},
            #{item.consumeCost},
            #{item.equipmentType}
            )
        </foreach>
    </insert>


</mapper>
