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

    <!-- 获取费用推送审核数据 -->
    <select id="getCostPushCheckList" parameterType="com.bonus.material.push.domain.CostPushBean" resultType="com.bonus.material.push.domain.CostPushBean">
        select
            pmc.id ,
            pmc.task_id as taskId,
            bu.unit_name as unitName,
            bai.unit_id as unitId,
            bp.pro_name as projectName,
            bp.pro_id as projectId,
            bp.pro_code as proCode,
            cpm.`MONTH`,
            pmc.LEASE_MONEY as leaseMoney,
            pmc.LOST_MONEY as lostMoney,
            pmc.SCRAP_MONEY as scrapMoney,
            pmc.REPAIR_MONEY as repairMoney,
            pmia.push_status as pushStatus,
            pmia.push_remark as pushRemark,
            ROUND(ifnull(pmc.LEASE_MONEY,0)+ifnull(pmc.LOST_MONEY,0)+ifnull(pmc.SCRAP_MONEY,0),2) as money,
            pmc.CHECK_STATUS as checkStatus
        from
            project_month_costs pmc
                LEFT JOIN bm_agreement_info bai ON bai.agreement_id = pmc.AGREEMENT_ID
                LEFT JOIN calc_project_month cpm on pmc.task_id=cpm.id
                LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id
                LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
                LEFT JOIN (
                SELECT
                    pmi.agreementId,
                    pmi.taskId,
                    pmi.push_status,
                    pmi.push_remark,
                    bai.unit_id,
                    bai.project_id
                FROM
                    project_month_info pmi
                    LEFT JOIN bm_agreement_info bai ON pmi.agreementId = bai.agreement_id
                    LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
                    LEFT JOIN calc_project_month cpm ON pmi.taskId = cpm.ID
                GROUP BY
                    pmi.agreementId
            ) pmia on bai.project_id = pmia.project_id and pmc.task_id=pmia.taskId and pmia.agreementId=pmc.AGREEMENT_ID
        where
            bp.pro_id is not null
          and
            (pmc.LEASE_MONEY > 0 or pmc.LOST_MONEY > 0 or pmc.REPAIR_MONEY > 0 or pmc.SCRAP_MONEY > 0)
            <if test="status != null">
                AND pmia.push_status = #{status}
            </if>
            <if test="month != null">
                AND cpm.month = #{month}
            </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>
        GROUP BY bai.project_id,bai.unit_id
        order by pmia.push_status
    </select>

    <!-- 获取消耗费用推送审核数据 -->
    <select id="getConsumePushCheckList" parameterType="com.bonus.material.push.domain.CostPushBean" resultType="com.bonus.material.push.domain.CostPushBean">
        SELECT
            pmc.id,
            pmc.task_id AS taskId,
            bu.unit_name AS unitName,
            bai.unit_id AS unitId,
            bp.pro_name AS projectName,
            bp.pro_id AS projectId,
            bp.pro_code AS proCode,
            cpm.`MONTH`,
            pmc.LEASE_MONEY AS leaseMoney,
            pmc.LOST_MONEY AS lostMoney,
            pmc.SCRAP_MONEY AS scrapMoney,
            pmc.REPAIR_MONEY AS repairMoney,
            pmia.push_status AS pushStatus,
            pmia.push_remark AS pushRemark,
            ROUND( ifnull( pmc.LEASE_MONEY, 0 )+ ifnull( pmc.LOST_MONEY, 0 )+ ifnull( pmc.SCRAP_MONEY, 0 ), 2 ) AS money,
            pmc.CHECK_STATUS AS checkStatus
        FROM
            project_month_costs pmc
            LEFT JOIN bm_agreement_info bai ON bai.agreement_id = pmc.AGREEMENT_ID
            LEFT JOIN calc_project_month cpm ON pmc.task_id = cpm.id
            LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id
            LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
            LEFT JOIN (
                SELECT
                pmi.type,
                pmi.agreementId,
                pmi.taskId,
                pmi.push_status,
                pmi.push_remark,
                bai.unit_id,
                bai.project_id
                FROM
                project_month_info pmi
                LEFT JOIN bm_agreement_info bai ON pmi.agreementId = bai.agreement_id
                LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
                LEFT JOIN calc_project_month cpm ON pmi.taskId = cpm.ID
                GROUP BY
                pmi.agreementId
                ) pmia ON bai.project_id = pmia.project_id AND pmc.task_id = pmia.taskId AND pmia.agreementId = pmc.AGREEMENT_ID
        WHERE
            bp.pro_id IS NOT NULL
            AND ( pmc.LEASE_MONEY > 0 OR pmc.LOST_MONEY > 0 OR pmc.REPAIR_MONEY > 0 OR pmc.SCRAP_MONEY > 0 )
        GROUP BY
            bai.project_id, bai.unit_id
        ORDER BY
            pmia.push_status
    </select>
    
    <update id="updateCostPushCheckStatus" parameterType="java.lang.String">
        update
            project_month_costs
        set
            check_status=1
        where
            ID=#{id}
    </update>

    <select id="getConsumeDataPushsList" parameterType="com.bonus.material.push.domain.CostPushBean"
            resultType="com.bonus.material.push.domain.ConsumeDataPush">
        select
            pmi.id,mmq.`NAME` as name,mm.`NAME` as type,bp.pro_id as
            projectId,bp.`NAME` as projectName,pmi.unit_name as unitName,
            pmi.num as quantity,mm.NOTAX_PRICE as netPrice,pmi.money as
            cost,pmi.time as expenseTime,#{submitUser} as submitUser,cpm.`MONTH`,
            "1" as pushStatus, bp.`CODE` as pc_no
        from
            project_month_info pmi
            LEFT JOIN calc_project_month cpm on pmi.task_id=cpm.id
            LEFT JOIN bm_project bp on pmi.PRO_ID=bp.id
            LEFT JOIN ma_machine_type mm on pmi.type_id=mm.id
            LEFT JOIN ma_machine_type mmq on mm.PARENT_ID=mmq.id
        where
            pmi.task_id =#{taskId} and
            pmi.PRO_ID=#{projectId} and
            pmi.type=2  and pmi.unit_id=#{unitId}
    </select>

    <select id="getToolDataPushList" parameterType="com.bonus.material.push.domain.CostPushBean"
            resultType="com.bonus.material.push.domain.CostPushBean">
        select pmi.id,mmq.`NAME` as typeName,mm.`NAME` as
                                    modelName,
               pmi.start_time as
                                    leaseDate,pmi.end_time as backDate,
               pmi.lease_price as
                                    leasePrice,pmi.num,pmi.money as
                                    money,pmi.time,pmi.type,bp.pro_id as
                                    projectId,
               bp.`NAME` as
                                    projectName,pmi.unit_name as
                                    unitName,cpm.`MONTH` ,
               #{submitUser} as
                                    submitUser,
               bp.`CODE` as pc_no
        from
            project_month_info pmi
                LEFT JOIN calc_project_month cpm
                          on pmi.task_id=cpm.id
                LEFT JOIN bm_project
                bp on pmi.PRO_ID=bp.id
                LEFT
                    JOIN ma_machine_type mm on
                pmi.type_id=mm.id
                LEFT JOIN ma_machine_type
                mmq on mm.PARENT_ID=mmq.id
        where pmi.task_id =#{taskId} and
            pmi.PRO_ID=#{projectId} and
            pmi.type=#{type}  and pmi.unit_id=#{unitId}
    </select>

    <update id="updateCostPushStatus" parameterType="com.bonus.material.push.domain.CostPushBean">
        update
            project_month_info
        set
            push_status=#{pushStatus},push_time=#{time},push_remark=#{pushRemark}
        where
            id = #{id}
    </update>

    <update id="updatePushAQStatus" parameterType="com.bonus.material.push.domain.CostPushBean">
        update
            project_month_info
        set
            push_status = 3
        where
            task_id=#{taskId} and pro_id=#{projectId} and unit_id=#{unitId} and type !=2
    </update>

    <update id="updatePushXHStatus" parameterType="com.bonus.material.push.domain.CostPushBean">
        update project_month_info set
            push_status=3
        where task_id=#{taskId} and pro_id=#{projectId} and unit_id=#{unitId} and type =2
    </update>

    <select id="findDetails" resultType="com.bonus.material.push.domain.CostPushBean">
        SELECT
            mmq.type_name AS typeName,
            mm.type_name AS modelName,
            pmi.leaseDate AS leaseDate,
            pmi.returnDate AS backDate,
            DATEDIFF( pmi.returnDate, pmi.leaseDate )+ 1 AS dayNum,
            pmi.leasePrice AS leasePrice,
            pmi.leaseNum,
            pmi.leaseMoney AS leaseMoney,
            pmi.push_time
        FROM
            project_month_info pmi
        LEFT JOIN bm_agreement_info bai ON pmi.agreementId = bai.agreement_id
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        LEFT JOIN ma_type mm ON pmi.typeId = mm.type_id
        LEFT JOIN ma_type mmq ON mm.PARENT_ID = mmq.type_id
        where
            pmi.taskId = #{taskId}
            and bai.project_id = #{projectId}
            and pmi.typeId = #{type}
            and bai.unit_id = #{unitId}
        <if test="typeName != null and typeName != '' ">
            and mm.`type_name` LIKE CONCAT('%',#{typeName},'%')
        </if>
        <if test="modelName != null and modelName != '' ">
            and mmq.`type_name` LIKE CONCAT('%',#{modelName},'%')
        </if>
    </select>
    <select id="getCostPushLeaseList" resultType="com.bonus.material.push.domain.CostPushBean">
        select
            pmi.id as id,
            mt2.type_name as modelName,
            mt1.type_name as typeName,
            pmi.leaseDate as leaseDate,
            pmi.returnDate as backDate,
            pmi.leasePrice as leasePrice,
            pmi.leaseNum as leaseNum,
            pmi.leaseMoney as leaseMoney,
            saa.audit_time as sltTime
        from project_month_info pmi
                 left join slt_agreement_apply saa on pmi.agreementId = sas.agreement_id
                 left join ma_type mt1 on pmi.type_id = mt1.type_id
                 left join ma_type mt2 on mt1.parent_id = mt2.type_id
        where
            pmi.agreementId = #{agreementId} and
            pmi.taskId =  #{taskId} and
            pmi.type = 1  and
            pmi.jiju_type = 2
        <if test="typeName != null and typeName != '' ">
            and mt1.type_name LIKE CONCAT('%',#{typeName},'%')
        </if>
        <if test="modelName != null and modelName != '' ">
            and mt2.type_name LIKE CONCAT('%',#{modelName},'%')
        </if>
    </select>
</mapper>