<?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.LeaseOutDetailsMapper">
    <resultMap type="com.bonus.common.biz.domain.lease.LeaseOutDetails" id="LeaseOutDetailsResult">
        <result property="id"    column="id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="maId"    column="ma_id"    />
        <result property="outNum"    column="out_num"    />
        <result property="outType"    column="out_type"    />
        <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="carCode"    column="car_code"    />
        <result property="pushNotifications"    column="push_notifications"    />
    </resultMap>

    <sql id="selectLeaseOutDetailsVo">
        select
            lod.id, lod.parent_id, lod.type_id, lod.ma_id, lod.out_num, lod.out_type, lod.create_by, lod.create_time,
            lod.update_by, lod.update_time, lod.remark, lod.company_id, lod.car_code, lod.push_notifications
        from
            lease_out_details lod
    </sql>

    <select id="selectLeaseOutDetailsList" parameterType="com.bonus.common.biz.domain.lease.LeaseOutDetails" resultMap="LeaseOutDetailsResult">
        <include refid="selectLeaseOutDetailsVo"/>
        <where>  
            <if test="parentId != null "> and lod.parent_id = #{parentId}</if>
            <if test="typeId != null "> and lod.type_id = #{typeId}</if>
            <if test="maId != null "> and lod.ma_id = #{maId}</if>
            <if test="outNum != null "> and lod.out_num = #{outNum}</if>
            <if test="outType != null  and outType != ''"> and lod.out_type = #{outType}</if>
            <if test="companyId != null "> and lod.company_id = #{companyId}</if>
            <if test="carCode != null  and carCode != ''"> and lod.car_code = #{carCode}</if>
            <if test="pushNotifications != null "> and lod.push_notifications = #{pushNotifications}</if>
        </where>
    </select>
    
    <select id="selectLeaseOutDetailsById" parameterType="Long" resultMap="LeaseOutDetailsResult">
        <include refid="selectLeaseOutDetailsVo"/>
        where lod.id = #{id}
    </select>

    <select id="selectLeaseOutDetailsListByParentId" parameterType="com.bonus.common.biz.domain.lease.LeaseOutDetails" resultMap="LeaseOutDetailsResult">
        <include refid="selectLeaseOutDetailsVo"/>
        <where>
            lod.parent_id = #{parentId}
        </where>
    </select>

    <select id="getCountOfCodeMachine" resultType="java.lang.Integer">
        select count(mm.ma_id)
        FROM ma_type mt
                 left join ma_machine mm on mm.type_id=mt.type_id
        WHERE
            mm.type_id = #{typeId}
          and mm.ma_code is not null
          and mm.ma_status in (1)
          and mt.`level` = 4
          and mt.del_flag = '0'
    </select>

    <select id="selectByTypeId" resultType="com.bonus.material.ma.domain.Type">
        select type_id     as typeId,
               manage_type as manageType,
               storage_num as storageNum
        from ma_type
        where type_id = #{typeId}
    </select>

    <insert id="insertLeaseOutDetails" parameterType="com.bonus.common.biz.domain.lease.LeaseOutDetails" useGeneratedKeys="true" keyProperty="id">
        insert into 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>
            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>
            NOW(),
            NOW()
        </trim>
    </insert>

    <update id="updateLeaseOutDetails" parameterType="com.bonus.common.biz.domain.lease.LeaseOutDetails">
        update lease_out_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="maId != null">ma_id = #{maId},</if>
            <if test="outNum != null">out_num = #{outNum},</if>
            <if test="outType != null">out_type = #{outType},</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>
            <if test="carCode != null">car_code = #{carCode},</if>
            <if test="pushNotifications != null">push_notifications = #{pushNotifications},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLeaseOutDetailsById" parameterType="Long">
        delete from lease_out_details where id = #{id}
    </delete>

    <delete id="deleteLeaseOutDetailsByIds" parameterType="String">
        delete from lease_out_details where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteLeaseOutDetails">
        delete from lease_out_details where parent_id = #{parentId} and type_id = #{typeId}
        <if test="publishTask != null and publishTask != ''">
            and publish_task = #{publishTask}
        </if>
        <if test="maId != null">
            and ma_id = #{maId}
        </if>
    </delete>

</mapper>