<?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.settlement.mapper.SltAgreementInfoMapper">
    <resultMap type="com.bonus.material.settlement.domain.SltAgreementInfo" id="SltAgreementInfoResult">
        <result property="id"    column="id"    />
        <result property="agreementId"    column="agreement_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="maId"    column="ma_id"    />
        <result property="num"    column="num"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="status"    column="status"    />
        <result property="leaseId"    column="lease_id"    />
        <result property="backId"    column="back_id"    />
        <result property="leasePrice"    column="lease_price"    />
        <result property="buyPrice"    column="buy_price"    />
        <result property="isSlt"    column="is_slt"    />
        <result property="sltTime"    column="slt_time"    />
        <result property="companyId"    column="company_id"    />
        <result property="leaseType"    column="lease_type"    />
        <result property="trimDay"    column="trim_day"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSltAgreementInfoVo">
        select id, agreement_id, type_id, ma_id, num, start_time, end_time, status, lease_id, back_id, lease_price, buy_price, is_slt, slt_time, company_id, lease_type, trim_day, create_time, update_time from slt_agreement_info
    </sql>

    <select id="selectSltAgreementInfoList" parameterType="com.bonus.material.settlement.domain.SltAgreementInfo" resultMap="SltAgreementInfoResult">
        <include refid="selectSltAgreementInfoVo"/>
        <where>
            <if test="agreementId != null "> and agreement_id = #{agreementId}</if>
            <if test="typeId != null "> and type_id = #{typeId}</if>
            <if test="maId != null "> and ma_id = #{maId}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="leaseId != null "> and lease_id = #{leaseId}</if>
            <if test="backId != null "> and back_id = #{backId}</if>
            <if test="leasePrice != null "> and lease_price = #{leasePrice}</if>
            <if test="buyPrice != null "> and buy_price = #{buyPrice}</if>
            <if test="isSlt != null  and isSlt != ''"> and is_slt = #{isSlt}</if>
            <if test="sltTime != null "> and slt_time = #{sltTime}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="leaseType != null  and leaseType != ''"> and lease_type = #{leaseType}</if>
            <if test="trimDay != null "> and trim_day = #{trimDay}</if>
        </where>
    </select>

    <select id="selectSltAgreementInfoById" parameterType="Long" resultMap="SltAgreementInfoResult">
        <include refid="selectSltAgreementInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertSltAgreementInfo" parameterType="com.bonus.material.settlement.domain.SltAgreementInfo" useGeneratedKeys="true" keyProperty="id">
        insert into slt_agreement_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="agreementId != null">agreement_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="num != null">num,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
            <if test="leaseId != null">lease_id,</if>
            <if test="backId != null">back_id,</if>
            <if test="leasePrice != null">lease_price,</if>
            <if test="buyPrice != null">buy_price,</if>
            <if test="isSlt != null">is_slt,</if>
            <if test="sltTime != null">slt_time,</if>
            <if test="companyId != null">company_id,</if>
            <if test="leaseType != null">lease_type,</if>
            <if test="trimDay != null">trim_day,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="agreementId != null">#{agreementId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="num != null">#{num},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
            <if test="leaseId != null">#{leaseId},</if>
            <if test="backId != null">#{backId},</if>
            <if test="leasePrice != null">#{leasePrice},</if>
            <if test="buyPrice != null">#{buyPrice},</if>
            <if test="isSlt != null">#{isSlt},</if>
            <if test="sltTime != null">#{sltTime},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="leaseType != null">#{leaseType},</if>
            <if test="trimDay != null">#{trimDay},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSltAgreementInfo" parameterType="com.bonus.material.settlement.domain.SltAgreementInfo">
        update slt_agreement_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="agreementId != null">agreement_id = #{agreementId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="maId != null">ma_id = #{maId},</if>
            <if test="num != null">num = #{num},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="leaseId != null">lease_id = #{leaseId},</if>
            <if test="backId != null">back_id = #{backId},</if>
            <if test="leasePrice != null">lease_price = #{leasePrice},</if>
            <if test="buyPrice != null">buy_price = #{buyPrice},</if>
            <if test="isSlt != null">is_slt = #{isSlt},</if>
            <if test="sltTime != null">slt_time = #{sltTime},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="leaseType != null">lease_type = #{leaseType},</if>
            <if test="trimDay != null">trim_day = #{trimDay},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSltAgreementInfoById" parameterType="Long">
        delete from slt_agreement_info where id = #{id}
    </delete>

    <delete id="deleteSltAgreementInfoByIds" parameterType="String">
        delete from slt_agreement_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteSltInfo">
        DELETE FROM slt_agreement_info
        WHERE
        lease_id = #{record.parentId}
        and type_id = #{record.typeId}
        <if test="record.maId != null">
            AND ma_id = #{record.maId}
        </if>
        <if test="record.publishTask != null and record.publishTask != ''">
            AND publish_task = #{record.publishTask}
        </if>
    </delete>

    <select id="getSltAgreementInfo" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
            id AS id,
            agreement_id AS agreementId,
            type_id AS typeId,
            ma_id AS maId,
            num AS num,
            start_time AS startTime,
            end_time AS endTime,
            status AS status,
            lease_id AS leaseId,
            back_id AS backId,
            lease_price AS leasePrice,
            buy_price AS buyPrice,
            company_id AS companyId
        FROM
            slt_agreement_info
        WHERE
            lease_id = #{parentId}
          AND
            type_id = #{typeId}
          AND
            ma_id IS NULL
          AND
            status = '0'
          <if test="publishTask != null and publishTask != ''">
             AND publish_task = #{publishTask}
          </if>
          AND
            DATE(start_time) = CURDATE();
    </select>

    <select id="getSltAgreementInfo4Project" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        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 bm_agreement_info bai
        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 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>
        ORDER BY bai.create_time desc
    </select>

    <select id="getSltExam" resultType="com.bonus.material.settlement.domain.SltAgreementApply">
        select saa.id,
            saa.code,
            GROUP_CONCAT(DISTINCT sar.agreement_id) as agreementId,
            GROUP_CONCAT(DISTINCT bui.unit_name) as unitName,
            GROUP_CONCAT(DISTINCT bp.pro_name) as projectName,
            saa.cost,
            saa.status
        from slt_agreement_apply saa
            left join slt_agreement_relation sar on saa.id = sar.apply_id
            LEFT JOIN bm_agreement_info bai on sar.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
        where 1=1
        <if test="unitId != null  and unitId != ''">
            and bui.unit_id = #{unitId}
        </if>
        <if test="projectId != null  and projectId != ''">
            and bp.pro_id = #{projectId}
        </if>
        <if test="sltStatus != null  and sltStatus != ''">
            and saa.status = #{sltStatus}
        </if>
        GROUP BY saa.id
    </select>

    <update id="updSltInfo">
        update slt_agreement_info
        set num = #{num},
            update_time = now()
        where id = #{id}
    </update>

    <insert id="insSltInfo">
        insert into slt_agreement_info (agreement_id,type_id,ma_id,num,start_time,status,lease_id,lease_price,buy_price,is_slt,company_id,lease_type,create_time,publish_task)
        values (#{agreementId},#{record.typeId},#{record.maId},#{record.outNum},now(),0,#{record.parentId},#{ma.finalPrice},#{ma.buyPrice},'0',#{record.companyId},#{record.leaseType},now(),#{record.publishTask});
    </insert>

    <select id="getLeaseList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        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 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}
    </select>

    <select id="getRepairDetailsList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        select tta.agreement_id as agreementId,
            bui.unit_name as unitName,
            bp.pro_name as projectName,
            rc.id as costId,
            rc.type_id as typeId,
            rc.ma_id as maId,
            mt1.type_name as typeName,
            mt.type_name as modelName,
            mt.unit_name as mtUnitName,
            rc.repair_num as num,
            rc.costs as costs,
            case rc.part_type when '0' then '不收费' when '1' then '收费' else '' end as partType,
            case rc.repair_type when '1' then '内部维修' when '2' then '返厂维修' else '' end as repairType,
            rc.company_id as companyId,
            case rc.status when '0' then '未审核' when '1' then '已审核' when '2' then '已驳回' else '' end as repairStatus
        from repair_cost rc
            LEFT JOIN tm_task_agreement tta on rc.task_id = tta.task_id
            LEFT JOIN tm_task tt on rc.task_id = tt.task_id
            LEFT JOIN bm_agreement_info bai on tta.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 rc.type_id = mt.type_id
            left join ma_type mt1 on mt.parent_id = mt1.type_id
        where rc.status in ('0','1') and rc.repair_type in ('1','2')
        <if test="taskList != null and taskList.size() > 0">
            and rc.task_id in
            <foreach item="task" collection="taskList" open="(" separator="," close=")">
                #{task.taskId}
            </foreach>
        </if>
    </select>

    <select id="getScrapDetailsList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        select tta.agreement_id as agreementId,
        bui.unit_name as unitName,
        bp.pro_name as projectName,
        rc.id as costId,
        rc.type_id as typeId,
        rc.ma_id as maId,
        mt1.type_name as typeName,
        mt.type_name as modelName,
        mt.unit_name as mtUnitName,
        rc.repair_num as num,
        rc.costs as costs,
        case rc.part_type when '0' then '不收费' when '1' then '收费' else '' end as partType,
        case rc.repair_type when '1' then '内部维修' when '2' then '返厂维修' else '' end as repairType,
        rc.company_id as companyId,
        case rc.status when '0' then '未审核' when '1' then '已审核' when '2' then '已驳回' else '' end as repairStatus
        from repair_cost rc
        LEFT JOIN tm_task_agreement tta on rc.task_id = tta.task_id
        LEFT JOIN tm_task tt on rc.task_id = tt.task_id
        LEFT JOIN bm_agreement_info bai on tta.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 rc.type_id = mt.type_id
        left join ma_type mt1 on mt.parent_id = mt1.type_id
        where rc.status in ('0','1') and rc.repair_type = '1'
        <if test="taskList != null and taskList.size() > 0">
            and rc.task_id in
            <foreach item="task" collection="taskList" open="(" separator="," close=")">
                #{task.taskId}
            </foreach>
        </if>
    </select>

    <select id="getLoseList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        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 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="getCost" resultType="java.lang.String">
        select cost
        from slt_agreement_apply
        where id = #{id}
    </select>

    <select id="getRelations" resultType="com.bonus.material.settlement.domain.SltAgreementRelation">
        select sar.id,
               bui.unit_name as unitName,
               bp.pro_name as projectName,
               sar.apply_id as applyId,
               sar.agreement_id as agreementId,
               sar.add_cost as addCost,
               sar.sub_cost as subCost,
               sar.remark as remark,
               sar.cost as cost,
               0 as leaseCost,
               sar.repair_cost as repairCost,
               sar.scrap_cost as scrapCost,
               sar.lose_cost as loseCost
        from slt_agreement_relation sar
                 LEFT JOIN bm_agreement_info bai on sar.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
        where sar.apply_id = #{id}
    </select>

    <update id="updateRelation">
        update slt_agreement_relation
        set status = #{status}
        where apply_id = #{id}
    </update>

    <update id="updateApply">
        update slt_agreement_apply
        set status = #{status},
            auditor = #{auditor},
            audit_time = now(),
            remark = #{remark}
        where id = #{id}
    </update>

    <update id="updateMaStatus">
        update ma_machine
        set ma_status =#{status}
        where ma_id = #{maId}
    </update>

    <update id="backRejectSltCope">
        update
            slt_agreement_info sai
        join
            back_apply_info bai on sai.back_id = bai.id
        set
            sai.end_time = null, sai.back_id = null, sai.status = '0', sai.update_time = CURRENT_TIMESTAMP
        where
            bai.task_id = #{backTaskId} and sai.status = '1'
    </update>

    <select id="getRejectCount" resultType="int">
        select
        count(id)
        from slt_agreement_apply saa
        where saa.agreement_id = #{agreementId}
    </select>

    <insert id="insertSltAgreementApply" parameterType="com.bonus.material.settlement.domain.vo.SltInfoVo" useGeneratedKeys="true" keyProperty="id">
        insert into 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>

    <update id="updateBmAgreementStatus">
        update
            bm_agreement_info bai
        set
            bai.is_slt = 1, bai.update_by = #{createBy}, bai.update_time = #{updateTime}
        where
            bai.agreement_id = #{agreementId}
    </update>

    <!-- 循环插入lease -->
    <insert id="insertSltAgreementDetailLease" parameterType="com.bonus.material.settlement.domain.SltAgreementInfo">
        insert into 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>

    <!-- 循环插入repair -->
    <insert id="insertSltAgreementDetailRepair" parameterType="com.bonus.material.settlement.domain.SltAgreementInfo">
        insert into slt_agreement_details (apply_id, type_id,ma_id,slt_type,num,money,is_charge)
        values
        <foreach collection="list" item="item" separator=",">
            (#{id}, #{item.typeId}, #{item.maId},3,#{item.num},#{item.costs},
            <choose>
                <when test="item.partType == '收费'">1</when>
                <otherwise>0</otherwise>
            </choose>)
        </foreach>
    </insert>

    <!-- 循环插入scrap -->
    <insert id="insertSltAgreementDetailScrap" parameterType="com.bonus.material.settlement.domain.SltAgreementInfo">
        insert into slt_agreement_details (apply_id, type_id,ma_id,slt_type,num,money,is_charge)
        values
        <foreach collection="list" item="item" separator=",">
            (#{id}, #{item.typeId}, #{item.maId},4,#{item.num},#{item.costs},
            <choose>
                <when test="item.partType == '收费'">1</when>
                <otherwise>0</otherwise>
            </choose>)
        </foreach>
    </insert>

    <!-- 循环插入lose -->
    <insert id="insertSltAgreementDetailLose" parameterType="com.bonus.material.settlement.domain.SltAgreementInfo">
        insert into 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>

    <select id="getSltList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT saa.id as id, 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,su.nick_name as auditor,saa.audit_time as auditTime,
        case when  saa.status = '1' then '1' when saa.status = '2' then '2' when saa.status = '3' then '3' end as sltStatus
        FROM bm_agreement_info bai
        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 slt_agreement_apply saa on saa.agreement_id = bai.agreement_id
        LEFT JOIN sys_user su ON saa.auditor = su.user_id and su.del_flag = 0
        where bai.status = '1' and saa.status in ('1','2','3')
        <if test="unitId != null  and unitId != ''">
            and bui.unit_id = #{unitId}
        </if>
        <if test="projectId != null  and projectId != ''">
            and bp.pro_id = #{projectId}
        </if>
        <choose>
            <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>
        ORDER BY bai.create_time desc
    </select>

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

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

    <update id="costExame">
        update slt_agreement_apply
        <set>
            <if test="status != null and status!=''">status = #{status},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="auditor != null and auditor!=''">auditor = #{auditor},</if>
            <if test="remark != null and remark!=''">remark = #{remark}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="costDeleteDetail">
        delete from slt_agreement_details where apply_id = #{id}
    </delete>

    <update id="updateBmAgreementReject">
        update
            bm_agreement_info bai
        set
            bai.is_slt = 0, bai.update_by = #{auditor}, bai.update_time = #{auditTime}
        where
            bai.agreement_id = #{agreementId}
    </update>

    <update id="backInUseNum">
        update
        slt_agreement_info sai
        join
        back_apply_info bai on sai.back_id = bai.id
        set
        sai.end_time = null, sai.back_id = null, sai.status = '0', sai.update_time = CURRENT_TIMESTAMP
        where
        bai.task_id = #{preTaskId} and sai.status = '1'
        <if test="typeId != null">
            and sai.type_id = #{typeId}
        </if>
        <if test="maId != null">
            and sai.ma_id = #{maId}
        </if>
    </update>

    <select id="getProjectListByUnitIds" resultType="int">
        /*根据往来单位id关联协议查询工程*/
        SELECT
        bmp.pro_id AS id
        FROM
        bm_project bmp
        LEFT JOIN sys_dept sd ON sd.dept_id = bmp.imp_unit
        LEFT JOIN 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>


    <select id="getProjectList" resultType="com.bonus.common.biz.domain.ProjectTreeNode">
        /*根据往来单位id关联协议查询工程*/
        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 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 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 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>
