<?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.MaterialLeaseInfoMapper">
    <resultMap type="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo" id="LeaseApplyInfoResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="taskId"    column="task_id"    />
        <result property="leasePerson"    column="lease_person"    />
        <result property="relPhone"    column="phone"    />
        <result property="type"    column="type"    />
        <result property="companyAuditBy"    column="company_audit_by"    />
        <result property="companyAuditTime"    column="company_audit_time"    />
        <result property="companyAuditRemark"    column="company_audit_remark"    />
        <result property="deptAuditBy"    column="dept_audit_by"    />
        <result property="deptAuditTime"    column="dept_audit_time"    />
        <result property="deptAuditRemark"    column="dept_audit_remark"    />
        <result property="directAuditBy"    column="direct_audit_by"    />
        <result property="directAuditTime"    column="direct_audit_time"    />
        <result property="directAuditRemark"    column="direct_audit_remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="directId"    column="direct_id"    />
        <result property="leaseType"    column="lease_type"    />
        <result property="estimateLeaseTime"    column="estimate_lease_time"    />
        <result property="costBearingParty"    column="cost_bearing_party"    />
        <result property="leaseProject" column="pro_name" />
        <result property="leaseProjectId" column="project_id" />
        <result property="leaseUnit" column="unit_name" />
        <result property="leaseUnitId" column="unit_id" />
        <result property="agreementCode" column="agreement_code" />
        <result property="leaseSignUrl" column="lease_sign_url" />
        <result property="leaseSignType" column="lease_sign_type" />
        <result property="agreementId" column="agreement_id" />
        <result property="externalId" column="external_id" />
        <result property="impUnit" column="imp_unit" />
        <result property="teamLeaderIdCard" column="bzz_idcard" />
    </resultMap>

    <resultMap type="com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails" id="LeaseApplyDetailsResult">
        <result property="id"    column="id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="typeName"    column="type_name"    />
        <result property="maTypeName"    column="ma_type_name"    />
        <result property="unitName"    column="unit_name"    />
        <result property="unitValue"    column="unit_value"    />
        <result property="storageNum"    column="storage_num"    />
        <result property="preNum"    column="pre_num"    />
        <result property="auditNum"    column="audit_num"    />
        <result property="alNum"    column="al_num"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectLeaseApplyInfoVo">
        select
            lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,
            lai.company_audit_time, lai.company_audit_remark, lai.dept_audit_by, lai.dept_audit_time,
            lai.dept_audit_remark, lai.direct_audit_by, lai.direct_audit_time, lai.direct_audit_remark,
            lai.create_by, lai.create_time, lai.update_by, lai.update_time, lai.remark, lai.company_id,
            lai.direct_id, lai.lease_type, lai.estimate_lease_time, lai.cost_bearing_party, lai.lease_sign_url, lai.lease_sign_type,
            lai.project_id as proId, bp.pro_name as projectName, tt.task_status as taskStatus,
            sda.dict_label as taskStatusName,
            lai.team_id as teamId,
            bt.unit_name as teamName,
            IFNULL(sum(lad.pre_num),0) as preCountNum,
            IFNULL(lod.num,0) as alNum,
            IFNULL(sum(lad.pre_num),0) - IFNULL(lod.num,0) as waitCountNum,
            GROUP_CONCAT(DISTINCT mt1.type_name) as maTypeNames,
            bp.contract_part as contractPart,
            sd.dept_name as impUnitName,
            tta.agreement_id,
            bp.external_id,
            bp.imp_unit,
            bt.bzz_idcard,
            bai.unit_id,
            bai.project_id as projectId,
            bt.link_man as relName,
            CASE WHEN lai.lease_sign_url IS NOT NULL
            THEN 0
            ELSE 1
            END as isElectronicSign
        from
            clz_lease_apply_info lai
                left join tm_task tt on lai.task_id = tt.task_id
                left join tm_task_agreement tta ON tt.task_id = tta.task_id
                left join clz_bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
                left join clz_lease_apply_details lad on lai.id = lad.parent_id
                LEFT JOIN (SELECT IFNULL( sum(out_num ), 0) AS num, parent_id from clz_lease_out_details
                WHERE is_finished = 1 GROUP BY parent_id) lod ON lai.id = lod.parent_id
                left join bm_project bp on bp.pro_id = lai.project_id
                left join bm_unit bt on bt.unit_id = lai.team_id
                left join sys_dept sd on sd.dept_id = bp.imp_unit
                left join sys_dict_data sda on tt.task_status = sda.dict_value
                and sda.dict_type = 'clz_lease_apply_task_status'
                left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
                left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
    </sql>

    <sql id="selectLeaseApplyDetailsVo">
        select
        lad.id, lad.parent_id, mt.type_id, mt.type_name, mt2.type_name as ma_type_name,
        mt.manage_type as manageType,
        IFNULL(lad.pre_num,0) as pre_num,
        IFNULL(lad.audit_num,0) as audit_num,
        IFNULL(lad.al_num,0) as al_num,
        IFNULL(lad.status,0) as status, mt.unit_name ,mt.unit_value,
        lad.create_by, lad.create_time, lad.update_by, lad.update_time, lad.remark as remark, lad.company_id
        from
        clz_lease_apply_details lad
        left join
        ma_type mt on lad.type_id = mt.type_id and  mt.`level` = '4' and mt.del_flag = '0'
        left join
        ma_type mt2 on mt2.type_id = mt.parent_id and mt2.`level` = '3' and mt2.del_flag = '0'
        <if test="userId != null">
            JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}
        </if>
    </sql>

    <insert id="insertLeaseApplyInfo" parameterType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo" useGeneratedKeys="true" keyProperty="id">
        insert into clz_lease_apply_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="taskId != null">task_id,</if>
            <if test="leasePerson != null">lease_person,</if>
            <if test="phone != null">phone,</if>
            <if test="type != null">type,</if>
            <if test="companyAuditBy != null">company_audit_by,</if>
            <if test="companyAuditTime != null">company_audit_time,</if>
            <if test="companyAuditRemark != null">company_audit_remark,</if>
            <if test="deptAuditBy != null">dept_audit_by,</if>
            <if test="deptAuditTime != null">dept_audit_time,</if>
            <if test="deptAuditRemark != null">dept_audit_remark,</if>
            <if test="directAuditBy != null">direct_audit_by,</if>
            <if test="directAuditTime != null">direct_audit_time,</if>
            <if test="directAuditRemark != null">direct_audit_remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="directId != null">direct_id,</if>
            <if test="leaseType != null">lease_type,</if>
            <if test="estimateLeaseTime != null">estimate_lease_time,</if>
            <if test="costBearingParty != null">cost_bearing_party,</if>
            <if test="teamId != null">team_id,</if>
            <if test="proId != null">project_id,</if>
            <if test="leaseStyle != null">lease_style,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="leasePerson != null">#{leasePerson},</if>
            <if test="phone != null">#{phone},</if>
            <if test="type != null">#{type},</if>
            <if test="companyAuditBy != null">#{companyAuditBy},</if>
            <if test="companyAuditTime != null">#{companyAuditTime},</if>
            <if test="companyAuditRemark != null">#{companyAuditRemark},</if>
            <if test="deptAuditBy != null">#{deptAuditBy},</if>
            <if test="deptAuditTime != null">#{deptAuditTime},</if>
            <if test="deptAuditRemark != null">#{deptAuditRemark},</if>
            <if test="directAuditBy != null">#{directAuditBy},</if>
            <if test="directAuditTime != null">#{directAuditTime},</if>
            <if test="directAuditRemark != null">#{directAuditRemark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="directId != null">#{directId},</if>
            <if test="leaseType != null">#{leaseType},</if>
            <if test="estimateLeaseTime != null">#{estimateLeaseTime},</if>
            <if test="costBearingParty != null">#{costBearingParty},</if>
            <if test="teamId != null">#{teamId},</if>
            <if test="proId != null">#{proId},</if>
            <if test="leaseStyle != null">#{leaseStyle},</if>
        </trim>
    </insert>
    <insert id="insertLeaseOutDetails" useGeneratedKeys="true" keyProperty="id">
        insert into clz_lease_out_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId!= null">
                parent_id,
            </if>
            <if test="typeId!= null">
                type_id,
            </if>
            <if test="maId!= null">
                ma_id,
            </if>
            <if test="outNum!= null">
                out_num,
            </if>
            <if test="outType!= null">
                out_type,
            </if>
            <if test="createBy!= null">
                create_by,
            </if>
            <if test="updateBy!= null">
                update_by,
            </if>
            <if test="remark!= null">
                remark,
            </if>
            <if test="companyId!= null">
                company_id,
            </if>
            <if test="carCode!= null">
                car_code,
            </if>
            <if test="publishTask != null and publishTask != ''">
                publish_task,
            </if>
            <if test="isFinished!= null">
                is_finished,
            </if>
            create_time,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId!= null">
                #{parentId},
            </if>
            <if test="typeId!= null">
                #{typeId},
            </if>
            <if test="maId!= null">
                #{maId},
            </if>
            <if test="outNum!= null">
                #{outNum},
            </if>
            <if test="outType!= null">
                #{outType},
            </if>
            <if test="createBy!= null">
                #{createBy},
            </if>
            <if test="updateBy!= null">
                #{updateBy},
            </if>
            <if test="remark!= null">
                #{remark},
            </if>
            <if test="companyId!= null">
                #{companyId},
            </if>
            <if test="carCode!= null">
                #{carCode},
            </if>
            <if test="publishTask != null and publishTask != ''">
                #{publishTask},
            </if>
            <if test="isFinished!= null">
                #{isFinished},
            </if>
            NOW(),
            NOW()
        </trim>
    </insert>

    <insert id="insertLeaseApplyDetailsList" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
        insert into clz_lease_apply_details
        (parent_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by,
        update_time, remark, company_id)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.parentId,jdbcType=INTEGER}, #{item.typeId,jdbcType=INTEGER}, #{item.preNum,jdbcType=INTEGER},
            #{item.alNum,jdbcType=INTEGER}, #{item.status,jdbcType=VARCHAR}, #{item.createBy,jdbcType=VARCHAR},
            NOW(), #{item.updateBy,jdbcType=VARCHAR}, NOW(),
            #{item.remark,jdbcType=VARCHAR}, #{item.companyId,jdbcType=INTEGER})
        </foreach>
    </insert>

    <insert id="insSltInfo">
        insert into clz_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, lease_style)
        values (#{agreementId},#{record.typeId},#{record.maId},#{record.outNum},now(),0,#{record.parentId},#{ma.finalPrice},#{ma.buyPrice},'0',#{record.companyId},#{record.leaseType},now(),#{record.publishTask}, #{record.leaseStyle});
    </insert>

    <insert id="insertLeaseApplyDetails">
        insert into clz_lease_apply_details
        (parent_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by,
         update_time, remark, company_id)
        values (#{parentId}, #{typeId}, #{preNum},#{alNum}, #{status}, #{createBy},NOW(), #{updateBy}, NOW(),
                #{remark}, #{companyId})
    </insert>

    <update id="updateLeaseApplyInfo">
        update clz_lease_apply_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="leasePerson != null">lease_person = #{leasePerson},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="type != null">`type` = #{type},</if>
            <if test="companyAuditBy != null">company_audit_by = #{companyAuditBy},</if>
            <if test="companyAuditTime != null">company_audit_time = #{companyAuditTime},</if>
            <if test="companyAuditRemark != null">company_audit_remark = #{companyAuditRemark},</if>
            <if test="deptAuditBy != null">dept_audit_by = #{deptAuditBy},</if>
            <if test="deptAuditTime != null">dept_audit_time = #{deptAuditTime},</if>
            <if test="deptAuditRemark != null">dept_audit_remark = #{deptAuditRemark},</if>
            <if test="directAuditBy != null">direct_audit_by = #{directAuditBy},</if>
            <if test="directAuditTime != null">direct_audit_time = #{directAuditTime},</if>
            <if test="directAuditRemark != null">direct_audit_remark = #{directAuditRemark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = now(),
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="directId != null">direct_id = #{directId},</if>
            <if test="leaseType != null">lease_type = #{leaseType},</if>
            <if test="estimateLeaseTime != null">estimate_lease_time = #{estimateLeaseTime},</if>
            <if test="costBearingParty != null">cost_bearing_party = #{costBearingParty},</if>
            <if test="teamId != null">team_id = #{teamId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateLeaseApplyInfoSign">
        update clz_lease_apply_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="leaseSignUrl != null and leaseSignUrl != '' ">lease_sign_url = #{leaseSignUrl},</if>
            <if test="leaseSignType != null">lease_sign_type = #{leaseSignType},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

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

    <update id="updateLeaseApplyDetails">
        UPDATE
        clz_lease_apply_details
        SET
        pre_num = pre_num + #{record.outNum},
        al_num = al_num + #{record.outNum},
        update_time = now()
        WHERE
        parent_id = #{record.parentId} and type_id = #{record.typeId}
    </update>

    <update id="updateLeaseOutDetails">
        UPDATE
        clz_lease_out_details
        SET
        out_num = out_num + #{record.outNum},
        update_time = now()
        WHERE
        parent_id = #{record.parentId} and type_id = #{record.typeId}
    </update>

    <delete id="deleteLeaseApplyDetailsByParentIds">
        delete from clz_lease_apply_details where parent_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteById">
        delete from clz_lease_out_details where parent_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteLeaseApplyInfoByIds">
        delete from clz_lease_apply_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectLeaseApplyInfoList" parameterType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo" resultMap="LeaseApplyInfoResult">
        <include refid="selectLeaseApplyInfoVo"/>
        <where>
            <if test="taskStatus != null"> and tt.task_status = #{taskStatus}</if>
            <if test="code != null  and code != ''"> and lai.code = #{code}</if>
            <if test="taskId != null "> and lai.task_id = #{taskId}</if>
            <if test="leasePerson != null  and leasePerson != ''"> and lai.lease_person = #{leasePerson}</if>
            <if test="phone != null  and phone != ''"> and lai.phone = #{phone}</if>
            <if test="statusList != null  and statusList.size() > 0">
                and tt.task_status in
                <foreach item="item" collection="statusList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                <![CDATA[ AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
            </if>
            <if test="leaseType != null  and leaseType != ''"> and lai.lease_type = #{leaseType}</if>
            and lai.lease_style = '1'
            <if test="idCard != null and idCard != ''">
                AND bt.bzz_idcard = #{idCard}
            </if>
            <if test="projectIdList != null  and projectIdList.size() > 0">
                and bp.external_id in
                <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY lai.id
        ORDER BY tt.create_time desc
    </select>

    <select id="getTotalList" resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo">
        select
        lai.id, lai.code as code, lai.lease_person as leasePerson,
        lai.create_by, lai.create_time as leaseTime, lai.update_by, lai.update_time, lai.remark, lai.company_id,
        lai.lease_sign_url as leaseSignUrl, lai.lease_sign_type as leaseSignType,
        lai.material_man, lai.confirm_time, lai.is_confirm, lai.confirm_remark,
        bu.unit_name as leaseUnit, bp.pro_name as leaseProject, tt.task_status as taskStatus,
        sda.dict_label as taskStatusName,
        IFNULL(sum(lad.pre_num),0) as preCountNum,
        IFNULL(sum(lad.al_num),0) as alNum,
        GROUP_CONCAT(DISTINCT mt1.type_name) as maTypeNames,
        tt.task_type as taskType,
        bp.external_id AS externalId,
        bu.bzz_idcard AS idCard,
        bp.imp_unit AS impUnit
        from
        lease_apply_info lai
        left join tm_task tt on lai.task_id = tt.task_id
        left join lease_apply_details lad on lai.id = lad.parent_id
        left join tm_task_agreement tta on lai.task_id = tta.task_id
        left join bm_agreement_info bai on tta.agreement_id = bai.agreement_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 sys_dept sd on sd.dept_id = bp.imp_unit
        left join sys_dict_data sda on tt.task_status = sda.dict_value
        and sda.dict_type = 'lease_task_status'
        left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        <if test="userId != null">
            JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}
        </if>
        <where>
            <if test="statusList != null  and statusList.size() > 0">
                and tt.task_status in
                <foreach item="item" collection="statusList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="projectIdList != null  and projectIdList.size() > 0">
                and bp.external_id in
                <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                <![CDATA[ AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
            </if>
            and tt.task_type = '2'
        </where>
        GROUP BY lai.id
        ORDER BY tt.create_time desc
    </select>

    <select id="getDetailsList" resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo">
        SELECT
            mt.type_name as modelCode,
            mt2.type_name AS typeCode,
            mt.manage_type AS manageType,
            IFNULL( lod.out_num, 0 ) AS preCountNum,
            mt.unit_name as unitNames,
            mm.ma_code as maCode
        FROM
            lease_out_details lod
                LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
                LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
                AND mt.`level` = '4' AND mt.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                AND mt2.`level` = '3' AND mt2.del_flag = '0'
                LEFT JOIN lease_apply_info lai ON lod.parent_id = lai.id
        WHERE
            lai.`code` = #{code}
    </select>

    <select id="selectLeaseApplyInfoById" parameterType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo" resultMap="LeaseApplyInfoResult">
        <include refid="selectLeaseApplyInfoVo"/>
        where lai.id = #{id}
    </select>
    <select id="selectLeaseApplyDetailsList" parameterType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails" resultMap="LeaseApplyDetailsResult">
        <include refid="selectLeaseApplyDetailsVo"/>
        <where>
            <if test="parentId != null "> and lad.parent_id = #{parentId}</if>
            <if test="keyword != null and keyword != ''">
                AND (mt.type_name LIKE CONCAT('%', #{keyword}, '%')
                OR mt2.type_name LIKE CONCAT('%', #{keyword}, '%'))
            </if>
            <if test="typeId != null "> and lad.type_id = #{typeId}</if>
            <if test="status != null  and status != ''"> and lad.status = #{status}</if>
        </where>
    </select>

    <select id="selectLeaseOutList"
            resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails">
        SELECT
            mt1.type_name AS maTypeName,
            mt.type_name AS typeName,
            mt.unit_name AS unitName,
            SUM( IFNULL(lod.out_num, 0) ) AS preNum,
            mt.manage_type AS manageType,
            mt.type_id AS typeId,
            lod.parent_id AS parentId
        FROM
            clz_lease_out_details lod
                LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        WHERE
            lod.parent_id = #{id}
        GROUP BY
            mt.type_id
    </select>

    <select id="getCodeList" resultType="com.bonus.common.biz.domain.lease.MaterialLeaseMaCodeDto">
        SELECT
            mt.type_id as typeId,
            mt1.type_name as materialName,
            mt.type_name as materialModel,
            mm.ma_id as maId,
            mm.ma_code as maCode,
            '在用' as maStatus,
            mm.ma_status as status,
            su.sign_type as signType,
            su.sign_url as signUrl
        FROM
            clz_lease_out_details lod
                LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
                LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN sys_user su ON lod.create_by = su.nick_name
        WHERE
            lod.parent_id = #{id}
          and mt.type_id = #{typeId}
    </select>

    <!-- 批量获取编码详情，优化N+1查询问题 -->
    <select id="getCodeListBatch" resultType="com.bonus.common.biz.domain.lease.MaterialLeaseMaCodeDto">
        SELECT
            mt.type_id as typeId,
            mt1.type_name as materialName,
            mt.type_name as materialModel,
            mm.ma_id as maId,
            mm.ma_code as maCode,
            '在用' as maStatus,
            mm.ma_status as status
        FROM
            clz_lease_out_details lod
                LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
                LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
        WHERE
            lod.parent_id = #{id}
            <if test="typeIds != null and typeIds.size() > 0">
                AND mt.type_id IN
                <foreach collection="typeIds" item="typeId" open="(" separator="," close=")">
                    #{typeId}
                </foreach>
            </if>
        ORDER BY mt.type_id, mm.ma_id
    </select>

    <!-- 批量查询设备是否已被领料 -->
    <select id="getUsedMaIdsBatch" resultType="java.lang.Long">
        SELECT DISTINCT csa.ma_id
        FROM clz_slt_agreement_info csa
        WHERE csa.status = '0'
        AND csa.ma_id IN
        <foreach collection="maIds" item="maId" open="(" separator="," close=")">
            #{maId}
        </foreach>
    </select>

    <select id="getOutNum" resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails">
        SELECT
        lad.parent_id as parentId,
        lad.type_id as typeId,
        COALESCE(lad.pre_num, 0) AS preNum,
        COALESCE(SUM(lod.out_num), 0) AS alNum,
        GREATEST(COALESCE(lad.pre_num, 0) - COALESCE(SUM(lod.out_num), 0), 0) AS outNum,
        mt1.type_name as maTypeName,
        mt.type_name as typeName,
        mt.manage_type as manageType,
        '0' as status,
        mt.unit_name as unitName,
        CASE mt.manage_type
        WHEN 0 THEN
        IFNULL(subquery0.num, 0)
        ELSE
        IFNULL(mt.storage_num, 0)
        END as storageNum
        FROM
        clz_lease_apply_details lad
        LEFT JOIN clz_lease_out_details lod ON lad.parent_id = lod.parent_id
        AND lad.type_id = lod.type_id
        LEFT JOIN ma_type mt ON lad.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        LEFT JOIN (SELECT mt.type_id,
        mt2.type_name AS             typeName,
        mt.type_name  AS             typeModelName,
        count(mm.ma_id) num
        FROM ma_machine mm
        LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE mm.ma_code is not null and mm.ma_status in (1)
        GROUP BY mt.type_id) AS subquery0 ON subquery0.type_id = mt.type_id
        WHERE
        lad.parent_id = #{id}
        <if test="typeId != null and typeId != ''">
            AND lad.type_id  = #{typeId}
        </if>
        GROUP BY
        lad.parent_id,
        lad.type_id;
    </select>

    <select id="getOutNumList"
            resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails">
        SELECT
            lod.parent_id as parentId,
            lod.type_id as typeId,
            0 AS preNum,
            COALESCE(SUM(lod.out_num), 0) AS alNum,
            0 AS outNum,
            mt1.type_name as maTypeName,
            mt.type_name as typeName,
            mt.manage_type as manageType,
            mt.unit_name as unitName,
            '1' as status
        FROM
            clz_lease_out_details lod
                LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        WHERE
            lod.parent_id = #{id}
        GROUP BY
            lod.type_id
    </select>

    <select id="getOutDetailsById"
            resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails">
        SELECT
            lad.id as id,
            lad.parent_id as parentId,
            mt.type_id as typeId,
            mt.type_name as typeName,
            mt2.type_name AS maTypeName,
            CASE
                mt.manage_type
                WHEN 0 THEN
                    IFNULL( subquery0.num, 0 ) ELSE IFNULL( mt.storage_num, 0 )
                END AS storageNum,
            mt.manage_type AS manageType,
            mt.unit_name as unitName,
            mt.unit_value as unitValue,
            SUM(lad.out_num) as outNum,
            IFNULL( lod.pre_num, 0 ) as preNum,
            lod.remark as remark
        FROM
            clz_lease_out_details lad
                LEFT JOIN clz_lease_apply_details lod ON lad.parent_id = lod.parent_id
                AND lad.type_id = lod.type_id
                LEFT JOIN ma_type mt ON lad.type_id = mt.type_id
                AND mt.`level` = '4' AND mt.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                AND mt2.`level` = '3' AND mt2.del_flag = '0'
                LEFT JOIN (
                SELECT
                    mt.type_id,
                    mt2.type_name AS typeName,
                    mt.type_name AS typeModelName,
                    count( mm.ma_id ) num
                FROM
                    ma_machine mm
                        LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                WHERE
                    mm.ma_code IS NOT NULL
                  AND mm.ma_status IN ( 1 )
                GROUP BY
                    mt.type_id
            ) AS subquery0 ON subquery0.type_id = mt.type_id
        WHERE
            lad.parent_id = #{id}
        GROUP BY mt.type_id
    </select>

    <select id="getLeaseInfo" resultType="com.bonus.material.clz.domain.lease.LeaseApplyDetailsInfo">
        SELECT
        lad.id AS id,
        lad.parent_id AS parentId,
        mt.type_id AS typeId,
        mt.type_name AS typeName,
        mt2.type_name AS maTypeName,
        mt.manage_type AS manageType,
        (lad.pre_num - IF( lad.al_num IS NULL, '0', lad.al_num )) AS outNum,
        IFNULL( lad.pre_num, 0 ) AS preNum,
        IFNULL( lad.al_num, 0 ) AS alNum,
        IFNULL( lad.STATUS, 0 ) AS STATUS,
        mt.unit_name AS unitName,
        mt.unit_value AS unitValue,
        lai.create_time as leaseDate,
        MAX(lod.create_time) as outDate,
        lai.create_by as leasePerson,
        GROUP_CONCAT(DISTINCT lod.create_by) as outPerson,
        lai.`code` as leaseCode,
        bt.unit_name as teamName,
        bp.pro_name as projectName,
        bp.external_id AS externalId,
        bt.bzz_idcard AS idCard,
        bp.imp_unit AS impUnit
        FROM
        clz_lease_apply_info lai
        LEFT JOIN clz_lease_apply_details lad ON lai.id = lad.parent_id
        LEFT JOIN ma_type mt ON lad.type_id = mt.type_id
        AND mt.`level` = '4' AND mt.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        AND mt2.`level` = '3' AND mt2.del_flag = '0'
        LEFT JOIN clz_lease_out_details lod  on lai.id = lod.parent_id
        LEFT JOIN bm_unit bt ON lai.team_id = bt.unit_id
        LEFT JOIN bm_project bp ON bp.pro_id = lai.project_id
        WHERE lai.lease_style = '1'
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="projectIdList != null  and projectIdList.size() > 0">
            and bp.external_id in
            <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        lai.`code`,mt.type_id
    </select>

    <select id="getInfoByQrcode" resultType="com.bonus.common.biz.domain.lease.LeaseOutDetails">
        SELECT
        mt1.type_name as typeName,
        mt.type_name  as typeModelName,
        mm.ma_id      as maId,
        mm.ma_code    as maCode,
        mm.type_id    as typeId,
        '1'           as maStatus,
        mm.qr_code    as qrCode,
        '在库'         as statusName,
        mt.manage_type as manageType
        FROM ma_machine mm
        LEFT JOIN slt_agreement_info sai ON mm.ma_id = sai.ma_id
        AND mm.type_id = sai.type_id
        LEFT JOIN bm_agreement_info bai ON sai.agreement_id = bai.agreement_id
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
        LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
        AND mt.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        AND mt1.del_flag = '0'
        where
        bu.type_id = '36'
        AND bp.pro_id = #{proId}
        AND sai.`status` = '0' and mm.qr_code = #{qrCode}
        <if test="typeId != null and typeId != ''">
            and mt.type_id = #{typeId}
        </if>
    </select>

    <select id="getTaskId" resultType="java.lang.String">
        select task_id
        from clz_lease_apply_info
        where id = #{parentId}
    </select>

    <select id="getLeaseInfoDetails" resultType="com.bonus.material.clz.domain.vo.lease.LeaseTotalInfo">
        SELECT
        lod.id AS id,
        lod.parent_id AS parentId,
        mt.type_id AS typeId,
        mt2.type_name AS maTypeName,
        mt.type_name AS typeName,
        mt.manage_type AS manageType,
        mt.unit_name AS unitName,
        SUM( lod.out_num ) AS outNum,
        lod.remark AS remark,
        lai.create_time as leaseDate,
        lod.create_time as outDate,
        bt.unit_name as teamName,
        bp.external_id as externalId,
        bp.pro_name as projectName,
        lai.lease_person as leasePerson,
        su.nick_name as outPerson,
        lai.`code` as code,
        bp.imp_unit as impUnit,
        bt.bzz_idcard as idCard
        FROM
        clz_lease_out_details lod
        LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
        AND mt.`level` = '4'
        AND mt.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        AND mt2.`level` = '3'
        AND mt2.del_flag = '0'
        LEFT JOIN clz_lease_apply_info lai ON lod.parent_id = lai.id
        LEFT JOIN bm_unit bt on lai.team_id = bt.unit_id
        LEFT JOIN bm_project bp ON lai.project_id = bp.pro_id
        LEFT JOIN sys_user su on su.user_id=lod.create_by
        WHERE
        lod.is_finished = '1'
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="projectIdList != null  and projectIdList.size() > 0">
            and bp.external_id in
            <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (
            mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            lai.`code` like concat('%', #{keyWord}, '%') or
            lai.lease_person like concat('%', #{keyWord}, '%') or
            lod.create_by like concat('%', #{keyWord}, '%') or
            bt.unit_name like concat('%', #{keyWord}, '%') or
            bp.pro_name like concat('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY
        mt.type_id,lai.team_id,lai.project_id,lai.`code`
        ORDER BY
        lai.create_time DESC
    </select>

    <select id="getTotalInfo" resultType="com.bonus.material.clz.domain.vo.lease.LeaseTotalInfo">
        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="getSecondLeaseInfo" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        mt.type_id as typeId,
        mt1.type_name as materialName,
        mt.type_name as typeName,
        mm.ma_id as maId,
        mm.ma_code as maCode,
        su.nick_name as outPerson,
        lod.create_time as outTime
        FROM
        clz_lease_out_details lod
        LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
        LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
        AND mt.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        AND mt1.del_flag = '0'
        LEFT JOIN sys_user su on su.user_id=lod.create_by
        WHERE
        lod.parent_id = #{parentId}
        and mt.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            mt1.type_name LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            OR mt.type_name LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            OR mm.ma_code LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            OR lod.create_by LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            )
        </if>
        ORDER BY
        lod.create_time DESC
    </select>

    <select id="getSltAgreementInfo"
            resultType="com.bonus.material.clz.domain.vo.MaterialSltAgreementInfo">
        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
        clz_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>
    </select>

    <select id="getUseTypeTree" resultType="com.bonus.common.biz.domain.TypeTreeNode">
        SELECT
            mt.type_id as typeId,
            mt1.type_name as materialName,
            mt.type_name as typeName,
            mt.parent_id as parentId,
            mt.unit_name as unitName,
            mt.unit_value as unitValue,
            mt.manage_type as manageType,
            SUM( CASE WHEN sai.agreement_id = #{agreementId} AND sai.STATUS = '0' THEN sai.num ELSE 0 END ) AS num,
            mt.LEVEL as level
        FROM
            ma_type mt
                LEFT JOIN clz_slt_agreement_info sai ON mt.type_id = sai.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        WHERE
            EXISTS ( SELECT 1 FROM clz_slt_agreement_info sai2 WHERE sai2.type_id = mt.type_id AND sai2.agreement_id = #{agreementId}
                                                                 AND sai.STATUS = '0' AND IFNULL(sai.lease_type,0) = 0 AND sai2.num > 0)
        GROUP BY
            mt.type_id
    </select>

    <select id="getAgreementId" resultType="java.lang.Long">
        SELECT
            tt.agreement_id AS agreementId
        FROM
            clz_lease_apply_info ml
                LEFT JOIN tm_task_agreement tt ON ml.task_id = tt.task_id
        WHERE
            ml.id = #{id}
    </select>
    <select id="selectInfoById"
            resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo">
        SELECT
        agreement_id AS agreementId,
        type_id AS typeId,
        ma_id AS maId
        FROM
        clz_slt_agreement_info
        WHERE
        status = '0'
        AND type_id = #{typeId}
        AND ma_id = #{maId}
    </select>

    <select id="getMachineById" resultType="com.bonus.material.clz.domain.vo.MaterialMaCodeVo">
        SELECT
            mm.ma_id AS maId,
            mm.ma_code AS maCode,
            '在库' AS maStatusName,
            mt1.type_name AS materialName,
            mt.type_id AS typeId,
            mt.type_name AS typeName,
            mt2.type_name AS materialType
        FROM
            slt_agreement_info sai
                LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
                and mm.type_id = sai.type_id
                LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
                AND mt2.del_flag = '0'
                LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
                LEFT JOIN bm_unit bu ON ba.unit_id = bu.unit_id and bu.del_flag = '0'
        WHERE
              sai.status = '0'
          AND bu.type_id = '36'
          AND mt.type_id = #{typeId}
          AND ba.project_id = #{proId}
        <if test="maCode != null and maCode != ''">
            AND mm.ma_code LIKE CONCAT(CONCAT('%', #{maCode}), '%')
        </if>
        GROUP BY mm.ma_id
    </select>

    <select id="getUseMachineById" resultType="com.bonus.material.clz.domain.vo.MaterialMaCodeVo">
        SELECT
            mm.ma_id AS maId,
            mm.ma_code AS maCode,
            '在用' AS maStatusName,
            mt1.type_name AS materialName,
            mt.type_id AS typeId,
            mt.type_name AS typeName,
            mt2.type_name AS materialType
        FROM
            clz_slt_agreement_info sai
                LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
                LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
                AND mt2.del_flag = '0'
                LEFT JOIN clz_bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
        WHERE
            sai.status = '0'
          AND mt.type_id = #{typeId}
          AND ba.project_id = #{proId}
        GROUP BY mm.ma_id
    </select>

    <select id="selectPublishList" resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo">
        SELECT
        lai.id AS id,
        lai.code AS code,
        lai.create_by AS createBy,
        lai.create_time AS createTime,
        sd.dept_name AS impUnitName,
        bp.contract_part AS contractPart,
        lpd.lease_person AS leasePerson,
        lpd.phone AS phone,
        lpd.unit_id AS leaseUnitId,
        lpd.project_id AS leaseProjectId,
        bu.unit_name AS leaseUnit,
        bp.pro_name AS leaseProject,
        bai.agreement_code AS agreementCode,
        IFNULL( sum( lpd.num ), 0 ) AS preCountNum,
        IFNULL(lod.num, 0) AS alNum,
        GROUP_CONCAT( DISTINCT mt1.type_name ) AS maTypeNames,
        lpd.publish_task AS publishTask,
        lai.task_id AS taskId,
        lpd.lease_sign_url AS leaseSignUrl,
        lpd.lease_sign_type AS leaseSignType,
        tt.task_type AS taskType,
        bp.external_id AS externalId,
        bu.bzz_idcard AS idCard,
        bp.imp_unit AS impUnit
        FROM
        lease_publish_details lpd
        LEFT JOIN lease_apply_info lai ON lai.id = lpd.parent_id
        LEFT JOIN tm_task tt ON lai.task_id = tt.task_id
        LEFT JOIN (SELECT IFNULL( sum(out_num ), 0) AS num, parent_id, publish_task from lease_out_details
        GROUP BY parent_id, publish_task) lod ON lpd.parent_id = lod.parent_id
        AND lpd.publish_task = lod.publish_task
        LEFT JOIN bm_unit bu ON bu.unit_id = lpd.unit_id
        LEFT JOIN bm_project bp ON bp.pro_id = lpd.project_id
        LEFT JOIN sys_dept sd ON sd.dept_id = bp.imp_unit
        LEFT JOIN ma_type mt ON lpd.new_type = mt.type_id
        AND mt.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        AND mt1.del_flag = '0'
        LEFT JOIN bm_agreement_info bai ON lpd.unit_id = bai.unit_id
        AND lpd.project_id = bai.project_id
        <if test="userId != null">
            JOIN ma_type_keeper mtk ON mtk.type_id = lpd.type_id AND mtk.user_id = #{userId}
        </if>
        where 1 = 1
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[ AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="id != null ">
            AND lai.id = #{id}
        </if>
        <if test="publishTask != null and publishTask != ''">
            AND lpd.publish_task = #{publishTask}
        </if>
        <if test="projectIdList != null  and projectIdList.size() > 0">
            and bp.external_id in
            <foreach item="item" collection="projectIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        lpd.publish_task
        ORDER BY
        lpd.create_time DESC
    </select>

    <select id="getStorageNum" resultType="java.math.BigDecimal">
        SELECT
            IFNULL(SUM(num), 0)
        FROM
            slt_agreement_info
        WHERE
            agreement_id = #{agreementId}
          AND type_id = #{typeId}
    </select>

    <select id="getAgreementIdByProId" resultType="com.bonus.material.basic.domain.BmAgreementInfo">
        SELECT
            bai.agreement_id as agreementId,
            bai.agreement_code as agreementCode
        FROM
            clz_bm_agreement_info bai
            LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        WHERE
               bai.project_id = #{proId}
    </select>

    <select id="getAgreeId" resultType="com.bonus.material.basic.domain.BmAgreementInfo">
        SELECT
            bai.agreement_id as agreementId,
            bai.agreement_code as agreementCode
        FROM
            clz_bm_agreement_info bai
            LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        WHERE
            bai.project_id = #{proId}
            AND bai.unit_id = #{teamId}
    </select>

    <select id="getInfoByQrcodeClz" resultType="com.bonus.common.biz.domain.lease.LeaseOutDetails">
        SELECT
        mt1.type_name as typeName,
        mt.type_name  as typeModelName,
        mm.ma_id      as maId,
        mm.ma_code    as maCode,
        mm.type_id    as typeId,
        mm.qr_code    as qrCode,
        mt.manage_type as manageType
        FROM ma_machine mm
        LEFT JOIN clz_slt_agreement_info sai ON mm.ma_id = sai.ma_id
        and mm.type_id = sai.type_id
        LEFT JOIN ma_type mt ON mm.type_id = mt.type_id AND mt.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id AND mt1.del_flag = '0'
        where
        sai.status = '0'
        and mm.qr_code = #{qrCode}
        <if test="typeId != null and typeId != ''">
            and mt.type_id = #{typeId}
        </if>
    </select>
    <select id="selectId" resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo">
        SELECT
        id as  id,
        task_id as taskId
        FROM
        clz_lease_apply_info
        WHERE
        task_id = #{taskId}
    </select>
    <select id="selectDetailsById" resultType="com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails">
        SELECT
        id as id,
        parent_id as parentId,
        type_id as typeId,
        pre_num as num
        FROM
        clz_lease_apply_details
        WHERE
        parent_id = #{id}
        and type_id = #{typeId}
    </select>

    <select id="selectInfoByTypeId" resultType="com.bonus.common.biz.domain.lease.LeaseOutDetails">
        SELECT
            id as id,
            parent_id as parentId,
            type_id as typeId,
            out_num as outNum
        FROM
            clz_lease_out_details
        WHERE
            parent_id = #{id}
          and type_id = #{typeId}
    </select>
    <select id="getList" resultType="com.bonus.common.biz.domain.lease.MaterialLeaseMaCodeDto">
        SELECT
            su.sign_type AS signType,
            su.sign_url AS signUrl
        FROM
            sys_user su
            LEFT JOIN clz_lease_out_details lod ON lod.create_by = su.user_id
        WHERE
            lod.parent_id = #{id}
          AND lod.type_id = #{typeId}
          AND su.sign_type IS NOT NULL
          AND su.sign_url IS NOT NULL
    </select>
    <select id="getTaskIds" resultType="java.lang.Long">
        SELECT
            task_id
        FROM
            clz_lease_apply_info
        WHERE
        id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>