<?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.lease.mapper.LeaseApplyDetailsMapper">
    <resultMap type="com.bonus.material.lease.domain.LeaseApplyDetails" 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="Base_Column_List">
        <!--@mbg.generated-->
        id, parent_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by, update_time, remark, company_id
    </sql>

    <sql id="selectLeaseApplyDetailsVo">
        select
            lad.id, lad.parent_id, mt.type_id, mt.type_name, mt2.type_name as ma_type_name,
            CASE mt.manage_type
                WHEN 0 THEN
                    IFNULL(subquery0.num, 0)
                ELSE
                    IFNULL(mt.storage_num, 0)
                END as storage_num,
            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 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, lad.company_id,
            mt4.type_id as firstId,
            su.sign_url as signUrl,
            su.sign_type as signType
        from
            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'
        left join ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.del_flag = '0'
        left join ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
        left join sys_user su on su.user_id = mt3.keep_user_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
        <if test="userId != null">
            JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}
        </if>
    </sql>

    <select id="selectLeaseApplyDetailsList" parameterType="com.bonus.material.lease.domain.LeaseApplyDetails" resultMap="LeaseApplyDetailsResult">
        select
        lad.id, lad.parent_id, mt.type_id, mt.type_name, mt2.type_name as ma_type_name,
        CASE mt.manage_type
        WHEN 0 THEN
        IFNULL(subquery0.num, 0)
        ELSE
        IFNULL(mt.storage_num, 0)
        END as storage_num,
        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 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, lad.company_id,
        mt4.type_id as firstId,
        su.sign_url as signUrl,
        su.sign_type as signType
        from
        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'
        left join ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.del_flag = '0'
        left join ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
        left join sys_user su on su.user_id = mt3.keep_user_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
        <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="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="preNum != null "> and lad.pre_num = #{preNum}</if>
            <if test="auditNum != null "> and lad.audit_num = #{auditNum}</if>
            <if test="alNum != null "> and lad.al_num = #{alNum}</if>
            <if test="status != null  and status != ''"> and lad.status = #{status}</if>
            <if test="companyId != null "> and lad.company_id = #{companyId}</if>
            <if test="typeIdList != null  and typeIdList.size() > 0">
                and mt4.type_id in
                <foreach item="item" collection="typeIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by
        mt.type_id desc
    </select>

    <select id="selectLeaseApplyDetailsById" parameterType="Long" resultMap="LeaseApplyDetailsResult">
        <include refid="selectLeaseApplyDetailsVo"/>
        where lad.id = #{id}
    </select>

    <insert id="insertLeaseApplyDetails" parameterType="com.bonus.material.lease.domain.LeaseApplyDetails" useGeneratedKeys="true" keyProperty="id">
        insert into lease_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="preNum != null">pre_num,</if>
            <if test="auditNum != null">audit_num,</if>
            <if test="alNum != null">al_num,</if>
            <if test="status != null">status,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="preNum != null">#{preNum},</if>
            <if test="auditNum != null">#{auditNum},</if>
            <if test="alNum != null">#{alNum},</if>
            <if test="status != null">#{status},</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>
         </trim>
    </insert>

    <insert id="insertLeaseApplyDetailsList" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
        insert into 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>

    <update id="updateLeaseApplyDetails" parameterType="com.bonus.material.lease.domain.LeaseApplyDetails">
        update lease_apply_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="preNum != null">pre_num = #{preNum},</if>
            <if test="auditNum != null">audit_num = #{auditNum},</if>
            <if test="alNum != null">al_num = #{alNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLeaseApplyDetailsById" parameterType="Long">
        delete from lease_apply_details where id = #{id}
    </delete>

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

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

    <delete id="deletePublishTask">
        delete from lease_out_details where parent_id = #{parentId} and type_id = #{typeId}
        and publish_task = #{publishTask}
        <if test="maId != null">
            and ma_id = #{maId}
        </if>
    </delete>

    <select id="getByParentId" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        <!--@mbg.generated-->
        select
        id, parent_id as parentId, type_id as typeId, pre_num as preNum, al_num as alNum, `status`
        from lease_apply_details
        where parent_id = #{parentId}
    </select>

    <update id="updateLeaseApplyDetailsOutNum">
        UPDATE
        lease_apply_details
        SET
        al_num = IF(al_num IS NULL, #{record.outNum}, al_num + #{record.outNum}),
        <if test="record.updateBy != null and record.updateBy!= '' ">
            update_by = #{record.updateBy},
        </if>
        update_time = now(),
        status = '1'
        WHERE
        parent_id = #{record.parentId} and type_id = #{record.typeId}
    </update>

    <select id="getLeaseApplyDetails" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            ifnull( pre_num, 0 ) AS preNum,
            ifnull( audit_num, 0 ) AS auditNum,
            ifnull( al_num, 0 ) AS alNum
        FROM
            lease_apply_details
        WHERE
            parent_id = #{record.parentId}
          AND type_id = #{record.typeId}
    </select>

    <update id="updateLeaseApplyDetailsByLeaseOutRecord">
        UPDATE
            lease_apply_details
        SET
            status = '2'
        WHERE
            parent_id = #{record.parentId} and type_id = #{record.typeId}
    </update>

    <update id="updateBackDetailsOutNum">
        UPDATE
        lease_apply_details
        SET
        al_num = al_num - #{outNum},
        <if test="updateBy != null and updateBy!= '' ">
            update_by = #{updateBy},
        </if>
        update_time = now(),
        status = '1'
        WHERE
        parent_id = #{parentId} and type_id = #{typeId}
    </update>

    <update id="updateLeaseApplyPublishDetailsOutNum">
        UPDATE
        lease_apply_details
        SET
        al_num = IF(al_num IS NULL, #{alNum}, al_num + #{alNum}),
        update_time = now(),
        status = '2'
        WHERE
        id = #{id}
    </update>

    <update id="updateLeaseApplyPublishDetails">
        UPDATE
            lease_apply_details
        SET
            al_num = IF(al_num IS NULL, #{alNum}, al_num + #{alNum}),
            update_time = now(),
            status = '1'
        WHERE
            id = #{id}
    </update>

    <select id="getOutboundNum" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT id, parent_id as parentId, type_id as typeId, pre_num as preNum, al_num as alNum, `status`, remark
        FROM lease_apply_details
        WHERE id = #{id} AND (pre_num - IFNULL(al_num, 0)) > 0
    </select>

    <select id="selectMaTypeNameByParentId" resultType="java.lang.String">
        select
            GROUP_CONCAT(type_name) typeName
        from
            (
            select
                distinct lad.parent_id, mt1.type_name
            from
                lease_apply_details lad
                left join ma_type mt on lad.type_id = mt.type_id
                left join ma_type mt1 on mt.parent_id = mt1.type_id
            where
                1=1
                <if test="parentId != null">
                    and lad.parent_id = #{parentId}
                </if>
            ) t
        GROUP BY parent_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,
               mm.ma_status  as maStatus,
               mm.qr_code    as qrCode,
               case when mm.ma_status = '1' then '在库'
                           else ''
               end as statusName,
               mt.manage_type as manageType,
               mt.jiju_type as jijuType
        FROM ma_machine mm
                 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
            mm.qr_code = #{qrCode}
    </select>

    <select id="getCodeList" 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
        FROM
            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}
            and mt.type_id = #{typeId}
    </select>

    <!-- 批量获取编码详情，优化N+1查询问题 -->
    <select id="getCodeListBatch" 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
        FROM
            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_code
    </select>

    <select id="selectLeaseOutDetailsList" resultType="com.bonus.material.lease.domain.vo.LeaseOutVo">
        SELECT
            mt1.type_name AS typeName,
            mt.type_name AS typeModelName,
            mt.unit_name AS unit,
            SUM(lod.out_num) AS num,
            lod.ma_id AS maId,
            mm.ma_code AS maCode,
            mt.rated_load AS ratedLoad,
            mt.test_load AS testLoad,
            mt.holding_time AS holdingTime,
            DATE(lod.create_time) AS testTime,
            '合格' AS checkResult,
            lad.remark AS remark,
            DATE(DATE_SUB(DATE_ADD(lod.create_time, INTERVAL 1 YEAR), INTERVAL 1 DAY)) AS nextTestTime  -- 计算 nextTestTime
        FROM
            lease_out_details lod
                LEFT JOIN ma_type mt ON lod.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 mt3 ON mt1.parent_id = mt3.type_id and mt3.del_flag = '0'
                LEFT JOIN ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
                LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
                LEFT JOIN lease_apply_details lad ON lod.type_id = lad.type_id AND lad.parent_id = #{id}
                <if test="userId != null">
                  JOIN ma_type_keeper mtk ON mtk.type_id = lod.type_id AND mtk.user_id = #{userId}
                </if>
        WHERE lod.parent_id = #{id}
        <if test="typeIdList != null  and typeIdList.size() > 0">
            and mt4.type_id in
            <foreach item="item" collection="typeIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY lod.type_id, mm.ma_id
    </select>

    <select id="getInnerById" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        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,
               (lad.pre_num - IF(lad.al_num IS NULL, '0', lad.al_num)) AS outNum,
               IFNULL(lad.pre_num, 0)                                  as preNum,
               IFNULL(lad.audit_num, 0)                                as auditNum,
               IFNULL(lad.al_num, 0)                                   as alNum,
               IFNULL(lad.status, 0)                                   as status,
               mt.unit_name as unitName,
               mt.unit_value as unitValue,
               lad.create_by as createBy,
               lad.create_time as createTime,
               lad.update_by as updateBy,
               lad.update_time as updateTime,
               lad.remark as remark,
               lad.company_id as companyId,
               mt.jiju_type as jijuType,
               mt.is_check as isCheck
        from 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'
                 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.id = #{id}
    </select>

    <select id="getDetailsById" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            mt1.type_name as maTypeName,
            mt.type_name as typeName,
            mm.ma_code as maCode,
            su.nick_name as createBy,
            lod.create_time as createTime,
            lod.ma_id as maId
        FROM
            lease_out_details lod
                LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt1.type_id = mt.parent_id
                AND mt1.del_flag = '0'
                LEFT JOIN lease_apply_info lai ON lod.parent_id = lai.id
                LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
                LEFT JOIN sys_user su ON lod.create_by = su.user_id
            where lai.task_id = #{taskId} and lod.type_id = #{typeId}
        ORDER BY
            lod.create_time DESC
    </select>

    <select id="selectByMaId" resultType="com.bonus.material.lease.domain.vo.LeaseOutVo">
        SELECT
        lod.parent_id AS id,
        mt1.type_name AS typeName,
        mt.type_name AS typeModelName,
        mt.unit_name AS unit,
        lod.out_num AS num,
        mm.ma_code AS maCode,
        mt.rated_load AS ratedLoad,
        mt.test_load AS testLoad,
        mt.holding_time AS holdingTime,
        DATE(lod.create_time) AS testTime,
        '合格' AS checkResult,
        lad.remark AS remark,
        DATE(DATE_SUB(DATE_ADD(lod.create_time, INTERVAL 1 YEAR), INTERVAL 1 DAY)) AS nextTestTime  -- 计算 nextTestTime
        FROM
        lease_out_details lod
        LEFT JOIN ma_type mt ON lod.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_machine mm ON lod.ma_id = mm.ma_id
        LEFT JOIN lease_apply_details lad ON lod.type_id = lad.type_id
        LEFT JOIN purchase_check_details pcd ON lod.type_id = pcd.type_id
        WHERE lod.ma_id = #{maId}
        GROUP BY lod.type_id, mm.ma_id
        ORDER BY lod.create_time DESC
    </select>

    <select id="getDetailsPublish" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
        lpd.parent_id AS parentId,
        mt1.type_name AS maTypeName,
        mt.type_name AS typeName,
        mt.unit_name AS unitName,
        lpd.new_type AS newTypeId,
        COALESCE(lpd.new_type, lpd.type_id) AS typeId,
        IFNULL( sum(lpd.num), 0 ) AS preNum,
        lpd.publish_task AS publishTask,
        mt.manage_type AS manageType,
        CASE mt.manage_type
        WHEN 0 THEN
        IFNULL(subquery0.num, 0)
        ELSE
        IFNULL(mt.storage_num, 0)
        END as storageNum,
        mt4.type_id as firstId,
        su.sign_url as signUrl,
        su.sign_type as signType
        FROM
        lease_publish_details lpd
        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 ma_type mt3 ON mt1.parent_id = mt3.type_id and mt3.del_flag = '0'
        left join ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
        left join sys_user su on su.user_id = mt3.keep_user_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
        <if test="userId != null">
            JOIN ma_type_keeper mtk ON mtk.type_id = lpd.new_type AND mtk.user_id = #{userId}
        </if>
        WHERE
        lpd.publish_task = #{publishTask}
        <if test="keyWord != null and keyWord != ''">
            AND (mt1.type_name LIKE CONCAT(CONCAT('%', #{keyWord}), '%')
            OR mt.type_name LIKE CONCAT(CONCAT('%', #{keyWord}), '%'))
        </if>
        <if test="typeIdList != null  and typeIdList.size() > 0">
            and mt4.type_id in
            <foreach item="item" collection="typeIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        lpd.new_type
        order by
        lpd.new_type
    </select>

    <select id="selectInfo" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
        lpd.id AS id,
        lpd.parent_id AS parentId,
        mt1.type_name AS maTypeName,
        mt.type_name AS typeName,
        mt.unit_name AS unitName,
        lpd.new_type AS typeId,
        IFNULL( lpd.num, 0 ) AS preNum,
        IFNULL( lpd.num, 0 ) - IFNULL( SUM( lod.out_num ), 0 ) AS outNum,
        IFNULL( SUM( lod.out_num ), 0 ) AS alNum,
        lpd.publish_task AS publishTask,
        lpd.unit_id AS unitId,
        lpd.project_id AS projectId,
        bu.unit_name AS leaseUnit,
        bp.pro_name AS leaseProject,
        CASE mt.manage_type
        WHEN 0 THEN
        IFNULL(subquery0.num, 0)
        ELSE
        IFNULL(mt.storage_num, 0)
        END                                                 as storageNum,
        mt.jiju_type as jijuType,
        mt.is_check as isCheck
        FROM
        lease_publish_details lpd
        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 (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
        LEFT JOIN lease_out_details lod ON lpd.parent_id = lod.parent_id
        AND lpd.publish_task = lod.publish_task AND lod.type_id = lpd.new_type
        LEFT JOIN bm_unit bu ON bu.unit_id = lpd.unit_id
        LEFT JOIN bm_project bp ON bp.pro_id = lpd.project_id
        WHERE
        lpd.publish_task = #{publishTask}
        <if test="typeId != null ">
            AND lpd.new_type = #{typeId}
        </if>
        GROUP BY
        lpd.new_type
    </select>

    <select id="selectPublishDetails" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            mt1.type_name as maTypeName,
            mt.type_name as typeName,
            mm.ma_code as maCode,
            su.nick_name as createBy,
            lod.create_time as createTime,
            lod.ma_id as maId
        FROM
            lease_out_details lod
                LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt1.type_id = mt.parent_id
                AND mt1.del_flag = '0'
                LEFT JOIN lease_publish_details lpd on lod.publish_task = lpd.publish_task
                and lod.type_id = lpd.type_id
                LEFT JOIN ma_machine mm ON lod.ma_id = mm.ma_id
                LEFT JOIN sys_user su ON lod.create_by = su.user_id
        where  lod.publish_task = #{publishTask} and lod.type_id = #{typeId}
        ORDER BY
            lod.create_time DESC
    </select>

    <select id="getLeasePublishApplyDetails" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            id                   AS id,
            IFNULL( pre_num, 0 ) AS preNum,
            IFNULL( audit_num, 0 ) AS auditNum,
            IFNULL( al_num, 0 ) AS alNum,
            IFNULL( pre_num, 0 ) - IFNULL( al_num, 0 ) AS outNum
        FROM
            lease_apply_details
        WHERE
            parent_id = #{record.parentId}
          AND new_type = #{record.typeId}
    </select>
    <select id="getPendingLeaseApplyDetails" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
        parent_id AS parentId,
        type_id AS typeId,
        IFNULL( SUM( out_num ), 0 ) AS outNum
        FROM
        lease_out_details
        WHERE
        parent_id = #{parentId}
        AND type_id = #{typeId}
        <if test="publishTask != null and publishTask != ''">
            AND publish_task = #{publishTask}
        </if>
        GROUP BY
        type_id
    </select>

    <select id="getPendingCodeLeaseApplyDetails" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            parent_id AS parentId,
            type_id AS typeId,
            IFNULL( out_num, 0) AS outNum
        FROM
            lease_out_details
        WHERE
            key_id = #{keyId}
    </select>

    <select id="getOutNum" resultType="java.math.BigDecimal">
        SELECT
            IFNULL(SUM(out_num),0)
        FROM
            lease_out_details
        WHERE
            parent_id = #{parentId}
          AND type_id = #{typeId}
          <if test="publishTask != null and publishTask != ''">
              AND publish_task = #{publishTask}
          </if>
    </select>

    <select id="getLeasePublishApply" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            ifnull( sum( pre_num ), 0 ) AS preNum,
            ifnull( sum( audit_num ), 0 ) AS auditNum,
            ifnull( sum( al_num ), 0 ) AS alNum
        FROM
            lease_apply_details
        WHERE
            parent_id = #{parentId}
          AND new_type = #{typeId}
    </select>

    <select id="getLeaseMaCode" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
        parent_id AS parentId,
        type_id AS typeId,
        IFNULL( out_num, 0) AS outNum
        FROM
        lease_out_details
        WHERE
        parent_id = #{parentId} and ma_id = #{maId}
    </select>

    <select id="getPengNum" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
        parent_id AS parentId,
        type_id AS typeId,
        IFNULL( sum( num ), 0) AS outNum
        FROM
        lease_publish_details
        WHERE
        parent_id = #{parentId}
        and COALESCE(new_type, type_id) = #{typeId}
        <if test="publishTask != null and publishTask != ''">
            AND publish_task = #{publishTask}
        </if>
    </select>

    <select id="getPendLease" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            parent_id AS parentId,
            type_id AS typeId,
            IFNULL( pre_num, 0) AS outNum
        FROM
            lease_apply_details
        WHERE
            parent_id = #{parentId}
          and  type_id = #{typeId}
    </select>

</mapper>
