<?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
        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
    </sql>

    <select id="selectLeaseApplyDetailsList" parameterType="com.bonus.material.lease.domain.LeaseApplyDetails" 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="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>
        </where>
    </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>

    <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>

    <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>
</mapper>
