<?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.clz.mapper.ClzSltAgreementInfoMapper">
    <insert id="insertSltAgreementApply" useGeneratedKeys="true" keyProperty="id">
        insert into clz_slt_agreement_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="agreementId != null">agreement_id,</if>
            <if test="agreementCode != null">code,</if>
            <if test="createBy != null">creator,</if>
            <if test="createTime != null">create_time,</if>
            status,
            <if test="totalCostAll != null">cost,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="agreementId != null">#{agreementId},</if>
            <if test="agreementCode != null">#{agreementCode},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            1,
            <if test="totalCostAll != null">#{totalCostAll},</if>
        </trim>
    </insert>

    <insert id="insertSltAgreementDetailLease">
        insert into clz_slt_agreement_details (apply_id, type_id,ma_id,slt_type,num,start_time,end_time,price,money)
        values
        <foreach collection="list" item="item" separator=",">
            (#{id}, #{item.typeId}, #{item.maId},1,#{item.num},#{item.startTime},#{item.endTime},#{item.leasePrice},#{item.costs})
        </foreach>
    </insert>

    <insert id="insertSltAgreementDetailLose">
        insert into clz_slt_agreement_details (apply_id, type_id,ma_id,slt_type,num,money)
        values
        <foreach collection="list" item="item" separator=",">
            (#{id}, #{item.typeId}, #{item.maId},2,#{item.num},#{item.costs})
        </foreach>
    </insert>

    <update id="updateRejectCount">
        update clz_slt_agreement_apply
        set status = '1',
            update_time = now(),
            audit_Time = null,
            auditor = null,
            remark = null,
            cost = #{totalCostAll}
        where agreement_id = #{agreementId}
    </update>

    <select id="getSltAgreementInfo4Project" resultType="com.bonus.material.clz.domain.vo.MaterialSltAgreementInfo">
        SELECT bai.agreement_id as agreementId, bai.agreement_code as agreementCode,
               bui.unit_id as unitId,bui.unit_name as unitName, bp.pro_id as projectId , bp.pro_name as projectName,
               saa.remark,bai.protocol,saa.cost as costs,
               case when (saa.id is null or saa.status = '0') then '0' when saa.status = '1' then '1' when saa.status = '2' then '2' when saa.status = '3' then '3' end as sltStatus
        FROM clz_slt_agreement_info sai
                 LEFT JOIN clz_bm_agreement_info bai ON bai.agreement_id = sai.agreement_id
                 LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
                 LEFT JOIN bm_unit bui ON bui.unit_id = bai.unit_id
                 LEFT JOIN clz_slt_agreement_apply saa on saa.agreement_id = bai.agreement_id
        where bai.status = '1'
        <if test="unitIds != null">
            and bui.unit_id in
            <foreach item="item" index="index" collection="unitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="projectId != null  and projectId != ''">
            and bp.pro_id = #{projectId}
        </if>
        <choose>
            <when test="sltStatus == '0'.toString()">
                and saa.id is null
            </when>
            <when test="sltStatus == '1'.toString()">
                and saa.status = '1'
            </when>
            <when test="sltStatus == '2'.toString()">
                and saa.status = '2'
            </when>
            <when test="sltStatus == '3'.toString()">
                and saa.status = '3'
            </when>
        </choose>
        GROUP BY sai.agreement_id
        ORDER BY sai.create_time desc
    </select>

    <select id="getLeaseList"
            resultType="com.bonus.material.clz.domain.vo.MaterialSltAgreementInfo">
        select sai.id,
               sai.agreement_id as agreementId,
               bui.unit_name as unitName,
               bp.pro_name as projectName,
               sai.company_id as companyId,
               sai.type_id as typeId,
               sai.ma_id as maId,
               mt1.type_name as typeName,
               mt.type_name as modelName,
               mt.unit_name as mtUnitName,
               sai.lease_price as leasePrice,
               sai.num as num,
            DATE(sai.start_time) as startTime,
            DATE(sai.end_time) as endTime,
            DATEDIFF(IF(sai.end_time is null,CURDATE(),sai.end_time), sai.start_time) + 1 as leaseDays
        from clz_slt_agreement_info sai
            LEFT JOIN clz_bm_agreement_info bai on sai.agreement_id = bai.agreement_id
            LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
            LEFT JOIN bm_unit bui ON bui.unit_id = bai.unit_id
            left join ma_type mt on sai.type_id = mt.type_id
            left join ma_type mt1 on mt.parent_id = mt1.type_id
        where sai.agreement_id = #{agreementId}
    </select>

    <select id="getLoseList" resultType="com.bonus.material.clz.domain.vo.MaterialSltAgreementInfo">
        select sai.id,
               sai.agreement_id as agreementId,
               bui.unit_name as unitName,
               bp.pro_name as projectName,
               sai.company_id as companyId,
               sai.type_id as typeId,
               sai.ma_id as maId,
               mt1.type_name as typeName,
               mt.type_name as modelName,
               mt.unit_name as mtUnitName,
               sai.buy_price as buyPrice,
               sai.num as num,
               sai.start_time as startTime,
               sai.end_time as endTime,
               DATEDIFF(sai.end_time, sai.start_time) + 1 as leaseDays
        from clz_slt_agreement_info sai
                 LEFT JOIN bm_agreement_info bai on sai.agreement_id = bai.agreement_id
                 LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
                 LEFT JOIN bm_unit bui ON bui.unit_id = bai.unit_id
                 left join ma_type mt on sai.type_id = mt.type_id
                 left join ma_type mt1 on mt.parent_id = mt1.type_id
        where sai.agreement_id = #{agreementId} and sai.end_time is null
    </select>

    <select id="getReductionList" resultType="com.bonus.material.settlement.domain.SltAgreementReduce">
        SELECT mt2.type_name   as typeName,
               mt.type_name   as modeName,
               mt.type_name    as modelName,
               mt.unit_name    as unitName,
               srd.lease_price as leasePrice,
               srd.reduce_num  as reduceNum,
               srd.start_time  as startTime,
               srd.end_time    as endTime,
               srd.days        as days,
               srd.lease_money as leaseMoney,
               sra.remark
        FROM clz_slt_reduce_apply sra
                 INNER JOIN clz_slt_reduce_details srd on sra.id = srd.apply_id
                 LEFT JOIN ma_type mt on mt.type_id = srd.type_id
                 LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        WHERE sra.agreement_id = #{agreementId}
    </select>

    <select id="getRejectCount" resultType="java.lang.Integer">
        select
            count(id)
        from clz_slt_agreement_apply saa
        where saa.agreement_id = #{agreementId}
    </select>

    <select id="getRejectId" resultType="java.lang.Long">
        select
            id
        from clz_slt_agreement_apply saa
        where saa.agreement_id = #{agreementId}
    </select>

    <!-- 根据往来单位id关联协议查询工程 -->
    <select id="getProjectListByUnitIds" resultType="int">
        SELECT
            bmp.pro_id AS id
        FROM
            bm_project bmp
            LEFT JOIN sys_dept sd ON sd.dept_id = bmp.imp_unit
            LEFT JOIN clz_bm_agreement_info bai ON bai.project_id = bmp.pro_id AND bai.`status` = '1'
            LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id AND bu.del_flag = '0'
        WHERE
            sd.del_flag = '0' AND sd.`status` = '0' AND bmp.del_flag = '0'
            <if test="unitId != null">
                AND bu.unit_id = #{unitId}
            </if>
    </select>

    <!-- 根据往来单位id关联协议查询工程 -->
    <select id="getProjectList" resultType="com.bonus.common.biz.domain.ProjectTreeNode">
        SELECT
            *
        FROM
            (
            SELECT
                bmp.pro_id AS id,
                bmp.pro_name AS name,
                concat( 'gs', bmp.imp_unit ) AS parentId,
                2 AS level
            FROM
                bm_project bmp
            LEFT JOIN sys_dept sd ON sd.dept_id = bmp.imp_unit
            LEFT JOIN clz_bm_agreement_info bai ON bai.project_id = bmp.pro_id AND bai.`status` = '1'
            LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id AND bu.del_flag = '0'
            WHERE
                sd.del_flag = '0'
                AND sd.`status` = '0'
                AND bmp.del_flag = '0'
                <if test="projectId != null">
                    AND bmp.pro_id = #{projectId}
                </if>
            UNION
            SELECT DISTINCT
                concat( 'gs', sd.dept_id ) AS id,
                sd.dept_name AS proName,
                '0' AS parentId,
                1 AS level
            FROM
                sys_dept sd
            LEFT JOIN bm_project bmp ON sd.dept_id = bmp.imp_unit
            LEFT JOIN clz_bm_agreement_info bai ON bai.project_id = bmp.pro_id AND bai.`status` = '1'
            LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id
            WHERE
                bmp.pro_id IS NOT NULL
                AND sd.del_flag = '0'
                AND sd.`status` = '0'
                AND bmp.del_flag = '0'
            <if test="projectId != null">
                AND bmp.pro_id = #{projectId}
            </if>
        ) a
        ORDER BY
        level
    </select>

    <select id="getAgreementInfoById" resultType="com.bonus.material.common.domain.vo.AgreementVo">
        SELECT
            agreement_id AS agreementId,
            agreement_code AS agreementCode,
            is_slt AS isSlt
        FROM
            clz_bm_agreement_info
        WHERE
            unit_id IN
            <foreach item="item" index="index" collection="unitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
            AND project_id = #{projectId} AND status = '1'
    </select>
</mapper>