<?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.sgzb.app.mapper.LeaseApplyDetailsMapper">
  <resultMap id="BaseResultMap" type="com.bonus.sgzb.app.domain.LeaseApplyDetails">
    <!--@mbg.generated-->
    <!--@Table lease_apply_details-->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="parennt_id" jdbcType="INTEGER" property="parenntId" />
    <result column="type_id" jdbcType="INTEGER" property="typeId" />
    <result column="pre_num" jdbcType="FLOAT" property="preNum" />
    <result column="al_num" jdbcType="FLOAT" property="alNum" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="company_id" jdbcType="INTEGER" property="companyId" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, parennt_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by, 
    update_time, remark, company_id
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from lease_apply_details
    where id = #{id,jdbcType=INTEGER}
  </select>
    <select id="getByParentId" resultType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
      <!--@mbg.generated-->
      select
      <include refid="Base_Column_List" />
      from lease_apply_details
      where parennt_id = #{parentId}
    </select>
  <select id="selectByTaskId" resultType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
    select lad.id  from lease_apply_details lad left join lease_apply_info lai on lad.parennt_id = lai.id
    where lai.task_id = #{taskId}
  </select>
  <select id="getDetailsByApplyId" resultType="com.bonus.sgzb.app.domain.TmTask">
    SELECT
    mt2.type_name AS typeName,
    mt.type_name AS typeModelName,
    mt.type_id AS typeId,
    mm.ma_code AS maCode,
    mm.ma_id AS maId,
    sd.NAME AS maStatus
    FROM
    ma_type mt
    LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
    LEFT JOIN ma_machine mm ON mt.type_id = mm.type_id
    LEFT JOIN sys_dic sd ON sd.id = mm.ma_status
    WHERE
    mm.ma_status in ('15','16')
      AND mt.type_id = #{typeId}
      <if test="maCode != null and maCode !=''">
        AND mm.ma_code like concat('%', #{maCode}, '%')
      </if>
  </select>
    <select id="getOrderHead" resultType="com.bonus.sgzb.app.domain.TmTask">
      SELECT
        bpl.lot_name proName,
        bui.unit_name,
        tt.`code`,
        tt.update_time
      FROM
        lease_apply_info lai
          LEFT JOIN tm_task tt ON tt.task_id = lai.task_id
          LEFT JOIN tm_task_agreement tta ON tta.task_id = lai.task_id
          LEFT JOIN bm_agreement_info bai ON bai.agreement_id = tta.agreement_id
          LEFT JOIN bm_project_lot bpl ON bpl.lot_id = bai.project_id
          LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
      WHERE
        lai.id = #{parentId}
    </select>
  <select id="getOrderBody" resultType="com.bonus.sgzb.app.domain.LeaseOutDetailRecord">
    SELECT
      bpl.lot_name,
      bui.unit_name,
      lod.create_time,
      tt.`code`,
      tt.update_time,
      mt.type_name typeModelName,
      mt.unit_name,
      case WHEN mt.manage_type = '0' then '编号' else '计数' end manageTypeName,
      mt2.type_name typeName,
      lod.out_num,
      lod.remark,
      mm.ma_code maCode
    FROM
      lease_out_details lod
        LEFT JOIN lease_apply_info lai ON lai.id = lod.parent_id
        LEFT JOIN tm_task tt on tt.task_id = lai.task_id
        LEFT JOIN tm_task_agreement tta ON tta.task_id = lai.task_id
        LEFT JOIN bm_agreement_info bai ON bai.agreement_id = tta.agreement_id
        LEFT JOIN bm_project_lot bpl ON bpl.lot_id = bai.project_id
        LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
        LEFT JOIN ma_type mt on lod.type_id  = mt.type_id
        LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        LEFT JOIN ma_machine mm on lod.ma_id = mm.ma_id
    WHERE
      lod.parent_id = #{parentId} and lod.type_id = #{typeId}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from lease_apply_details
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <delete id="deleteByParentId" parameterType="java.lang.Integer">
    delete from lease_apply_details
    where parennt_id = #{parentId,jdbcType=INTEGER}
  </delete>

  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.bonus.sgzb.app.domain.LeaseApplyDetails" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into lease_apply_details (parennt_id, type_id, pre_num, 
      al_num, `status`, create_by, 
      create_time, update_by, update_time, 
      remark, company_id)
    values (#{parenntId,jdbcType=INTEGER}, #{typeId,jdbcType=INTEGER}, #{preNum,jdbcType=FLOAT}, 
      #{alNum,jdbcType=FLOAT}, #{status,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR}, 
      NOW(), #{updateBy,jdbcType=VARCHAR}, NOW(),
      #{remark,jdbcType=VARCHAR}, #{companyId,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.bonus.sgzb.app.domain.LeaseApplyDetails" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into lease_apply_details
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="parenntId != null">
        parennt_id,
      </if>
      <if test="typeId != null">
        type_id,
      </if>
      <if test="preNum != null">
        pre_num,
      </if>
      <if test="alNum != null">
        al_num,
      </if>
      <if test="status != null and status != ''">
        `status`,
      </if>
      <if test="createBy != null and createBy != ''">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null and updateBy != ''">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="remark != null and remark != ''">
        remark,
      </if>
      <if test="companyId != null">
        company_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="parenntId != null">
        #{parenntId,jdbcType=INTEGER},
      </if>
      <if test="typeId != null">
        #{typeId,jdbcType=INTEGER},
      </if>
      <if test="preNum != null">
        #{preNum,jdbcType=FLOAT},
      </if>
      <if test="alNum != null">
        #{alNum,jdbcType=FLOAT},
      </if>
      <if test="status != null and status != ''">
        #{status,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null and createBy != ''">
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null and updateBy != ''">
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null and remark != ''">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="companyId != null">
        #{companyId,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
    update lease_apply_details
    <set>
      <if test="preNum != null">
        pre_num = #{preNum,jdbcType=FLOAT},
      </if>
      <if test="alNum != null">
        al_num = #{alNum,jdbcType=FLOAT},
      </if>
      <if test="status != null and status != ''">
        `status` = #{status,jdbcType=VARCHAR},
      </if>
      <if test="updateBy != null and updateBy != ''">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null and remark != ''">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="companyId != null">
        company_id = #{companyId,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.bonus.sgzb.app.domain.LeaseApplyDetails">
    <!--@mbg.generated-->
    update lease_apply_details
    set parennt_id = #{parenntId,jdbcType=INTEGER},
      type_id = #{typeId,jdbcType=INTEGER},
      pre_num = #{preNum,jdbcType=FLOAT},
      al_num = #{alNum,jdbcType=FLOAT},
      `status` = #{status,jdbcType=VARCHAR},
      create_by = #{createBy,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      remark = #{remark,jdbcType=VARCHAR},
      company_id = #{companyId,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateBatch" parameterType="java.util.List">
    <!--@mbg.generated-->
    update lease_apply_details
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="parennt_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.parenntId,jdbcType=INTEGER}
        </foreach>
      </trim>
      <trim prefix="type_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.typeId,jdbcType=INTEGER}
        </foreach>
      </trim>
      <trim prefix="pre_num = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.preNum,jdbcType=FLOAT}
        </foreach>
      </trim>
      <trim prefix="al_num = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.alNum,jdbcType=FLOAT}
        </foreach>
      </trim>
      <trim prefix="`status` = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.status,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="create_by = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.createBy,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="create_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.createTime,jdbcType=TIMESTAMP}
        </foreach>
      </trim>
      <trim prefix="update_by = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.updateBy,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="update_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.updateTime,jdbcType=TIMESTAMP}
        </foreach>
      </trim>
      <trim prefix="remark = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.remark,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="company_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=INTEGER} then #{item.companyId,jdbcType=INTEGER}
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=INTEGER}
    </foreach>
  </update>
  <update id="updateBatchSelective" parameterType="java.util.List">
    <!--@mbg.generated-->
    update lease_apply_details
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="parennt_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.parenntId != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.parenntId,jdbcType=INTEGER}
          </if>
        </foreach>
      </trim>
      <trim prefix="type_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.typeId != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.typeId,jdbcType=INTEGER}
          </if>
        </foreach>
      </trim>
      <trim prefix="pre_num = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.preNum != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.preNum,jdbcType=FLOAT}
          </if>
        </foreach>
      </trim>
      <trim prefix="al_num = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.alNum != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.alNum,jdbcType=FLOAT}
          </if>
        </foreach>
      </trim>
      <trim prefix="`status` = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.status != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.status,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="create_by = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.createBy != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.createBy,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="create_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.createTime != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.createTime,jdbcType=TIMESTAMP}
          </if>
        </foreach>
      </trim>
      <trim prefix="update_by = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.updateBy != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.updateBy,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="update_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.updateTime != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.updateTime,jdbcType=TIMESTAMP}
          </if>
        </foreach>
      </trim>
      <trim prefix="remark = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.remark != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.remark,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="company_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.companyId != null">
            when id = #{item.id,jdbcType=INTEGER} then #{item.companyId,jdbcType=INTEGER}
          </if>
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=INTEGER}
    </foreach>
  </update>

  <insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into lease_apply_details
    (parennt_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.parenntId,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="insertOrUpdate" keyColumn="id" keyProperty="id" parameterType="com.bonus.sgzb.app.domain.LeaseApplyDetails" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into lease_apply_details
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      parennt_id,
      type_id,
      pre_num,
      al_num,
      `status`,
      create_by,
      create_time,
      update_by,
      update_time,
      remark,
      company_id,
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      #{parenntId,jdbcType=INTEGER},
      #{typeId,jdbcType=INTEGER},
      #{preNum,jdbcType=FLOAT},
      #{alNum,jdbcType=FLOAT},
      #{status,jdbcType=VARCHAR},
      #{createBy,jdbcType=VARCHAR},
      #{createTime,jdbcType=TIMESTAMP},
      #{updateBy,jdbcType=VARCHAR},
      #{updateTime,jdbcType=TIMESTAMP},
      #{remark,jdbcType=VARCHAR},
      #{companyId,jdbcType=INTEGER},
    </trim>
    on duplicate key update 
    <trim suffixOverrides=",">
      <if test="id != null">
        id = #{id,jdbcType=INTEGER},
      </if>
      parennt_id = #{parenntId,jdbcType=INTEGER},
      type_id = #{typeId,jdbcType=INTEGER},
      pre_num = #{preNum,jdbcType=FLOAT},
      al_num = #{alNum,jdbcType=FLOAT},
      `status` = #{status,jdbcType=VARCHAR},
      create_by = #{createBy,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      remark = #{remark,jdbcType=VARCHAR},
      company_id = #{companyId,jdbcType=INTEGER},
    </trim>
  </insert>
  <insert id="insertOrUpdateSelective" keyColumn="id" keyProperty="id" parameterType="com.bonus.sgzb.app.domain.LeaseApplyDetails" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into lease_apply_details
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="parenntId != null">
        parennt_id,
      </if>
      <if test="typeId != null">
        type_id,
      </if>
      <if test="preNum != null">
        pre_num,
      </if>
      <if test="alNum != null">
        al_num,
      </if>
      <if test="status != null and status != ''">
        `status`,
      </if>
      <if test="createBy != null and createBy != ''">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null and updateBy != ''">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="remark != null and remark != ''">
        remark,
      </if>
      <if test="companyId != null">
        company_id,
      </if>
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="parenntId != null">
        #{parenntId,jdbcType=INTEGER},
      </if>
      <if test="typeId != null">
        #{typeId,jdbcType=INTEGER},
      </if>
      <if test="preNum != null">
        #{preNum,jdbcType=FLOAT},
      </if>
      <if test="alNum != null">
        #{alNum,jdbcType=FLOAT},
      </if>
      <if test="status != null and status != ''">
        #{status,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null and createBy != ''">
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null and updateBy != ''">
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null and remark != ''">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="companyId != null">
        #{companyId,jdbcType=INTEGER},
      </if>
    </trim>
    on duplicate key update 
    <trim suffixOverrides=",">
      <if test="id != null">
        id = #{id,jdbcType=INTEGER},
      </if>
      <if test="parenntId != null">
        parennt_id = #{parenntId,jdbcType=INTEGER},
      </if>
      <if test="typeId != null">
        type_id = #{typeId,jdbcType=INTEGER},
      </if>
      <if test="preNum != null">
        pre_num = #{preNum,jdbcType=FLOAT},
      </if>
      <if test="alNum != null">
        al_num = #{alNum,jdbcType=FLOAT},
      </if>
      <if test="status != null and status != ''">
        `status` = #{status,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null and createBy != ''">
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null and updateBy != ''">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null and remark != ''">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="companyId != null">
        company_id = #{companyId,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
</mapper>