<?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.boco.nbd.wios.manage.mapper.def.SettlementMapper">
  
<resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.settlement.po.Settlement">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="settlement_id" jdbcType="VARCHAR" property="settlementId" />
  <result column="start_date" jdbcType="DATE" property="startDate" />
  <result column="end_date" jdbcType="DATE" property="endDate" />
  <result column="target_id" jdbcType="INTEGER" property="targetId" />
  <result column="type" jdbcType="INTEGER" property="type" />
  <result column="total_price" jdbcType="DECIMAL" property="totalPrice" />
  <result column="total_price_with_tax" jdbcType="DECIMAL" property="totalPriceWithTax" />
  <result column="total_tax" jdbcType="DECIMAL" property="totalTax" />
  <result column="total_price_actual" jdbcType="DECIMAL" property="totalPriceActual" />
  <result column="total_tax_actual" jdbcType="DECIMAL" property="totalTaxActual" />
  <result column="status" jdbcType="INTEGER" property="status" />
  <result column="remark" jdbcType="VARCHAR" property="remark" />
  <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
  <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  <result column="update_account" jdbcType="INTEGER" property="updateAccount" />
  <result column="create_account" jdbcType="INTEGER" property="createAccount" />
</resultMap>

  
<sql id="Base_Column_List">
  id, settlement_id, start_date, end_date, target_id, type, total_price, total_price_with_tax, 
  total_tax, total_price_actual, total_tax_actual, status, remark, create_time, update_time, 
  update_account, create_account
</sql>

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

  
<insert id="insert" parameterType="com.boco.nbd.wios.manage.entity.settlement.po.Settlement">
  insert into t_settlement (id, settlement_id, start_date, 
    end_date, target_id, type, 
    total_price, total_price_with_tax, total_tax, 
    total_price_actual, total_tax_actual, status, 
    remark, create_time, update_time, 
    update_account, create_account)
  values (#{id,jdbcType=INTEGER}, #{settlementId,jdbcType=VARCHAR}, #{startDate,jdbcType=DATE}, 
    #{endDate,jdbcType=DATE}, #{targetId,jdbcType=INTEGER}, #{type,jdbcType=INTEGER}, 
    #{totalPrice,jdbcType=DECIMAL}, #{totalPriceWithTax,jdbcType=DECIMAL}, #{totalTax,jdbcType=DECIMAL}, 
    #{totalPriceActual,jdbcType=DECIMAL}, #{totalTaxActual,jdbcType=DECIMAL}, #{status,jdbcType=INTEGER}, 
    #{remark,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
    #{updateAccount,jdbcType=INTEGER}, #{createAccount,jdbcType=INTEGER})
</insert>

  
<insert id="insertDynamic" parameterType="com.boco.nbd.wios.manage.entity.settlement.po.Settlement">
  insert into t_settlement
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
      id,
    </if>
    <if test="settlementId != null">
      settlement_id,
    </if>
    <if test="startDate != null">
      start_date,
    </if>
    <if test="endDate != null">
      end_date,
    </if>
    <if test="targetId != null">
      target_id,
    </if>
    <if test="type != null">
      type,
    </if>
    <if test="totalPrice != null">
      total_price,
    </if>
    <if test="totalPriceWithTax != null">
      total_price_with_tax,
    </if>
    <if test="totalTax != null">
      total_tax,
    </if>
    <if test="totalPriceActual != null">
      total_price_actual,
    </if>
    <if test="totalTaxActual != null">
      total_tax_actual,
    </if>
    <if test="status != null">
      status,
    </if>
    <if test="remark != null">
      remark,
    </if>
    <if test="createTime != null">
      create_time,
    </if>
    <if test="updateTime != null">
      update_time,
    </if>
    <if test="updateAccount != null">
      update_account,
    </if>
    <if test="createAccount != null">
      create_account,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="id != null">
      #{id,jdbcType=INTEGER},
    </if>
    <if test="settlementId != null">
      #{settlementId,jdbcType=VARCHAR},
    </if>
    <if test="startDate != null">
      #{startDate,jdbcType=DATE},
    </if>
    <if test="endDate != null">
      #{endDate,jdbcType=DATE},
    </if>
    <if test="targetId != null">
      #{targetId,jdbcType=INTEGER},
    </if>
    <if test="type != null">
      #{type,jdbcType=INTEGER},
    </if>
    <if test="totalPrice != null">
      #{totalPrice,jdbcType=DECIMAL},
    </if>
    <if test="totalPriceWithTax != null">
      #{totalPriceWithTax,jdbcType=DECIMAL},
    </if>
    <if test="totalTax != null">
      #{totalTax,jdbcType=DECIMAL},
    </if>
    <if test="totalPriceActual != null">
      #{totalPriceActual,jdbcType=DECIMAL},
    </if>
    <if test="totalTaxActual != null">
      #{totalTaxActual,jdbcType=DECIMAL},
    </if>
    <if test="status != null">
      #{status,jdbcType=INTEGER},
    </if>
    <if test="remark != null">
      #{remark,jdbcType=VARCHAR},
    </if>
    <if test="createTime != null">
      #{createTime,jdbcType=TIMESTAMP},
    </if>
    <if test="updateTime != null">
      #{updateTime,jdbcType=TIMESTAMP},
    </if>
    <if test="updateAccount != null">
      #{updateAccount,jdbcType=INTEGER},
    </if>
    <if test="createAccount != null">
      #{createAccount,jdbcType=INTEGER},
    </if>
  </trim>
</insert>

  
<update id="updateDynamic" parameterType="com.boco.nbd.wios.manage.entity.settlement.po.Settlement">
  update t_settlement
  <set>
    <if test="settlementId != null">
      settlement_id = #{settlementId,jdbcType=VARCHAR},
    </if>
    <if test="startDate != null">
      start_date = #{startDate,jdbcType=DATE},
    </if>
    <if test="endDate != null">
      end_date = #{endDate,jdbcType=DATE},
    </if>
    <if test="targetId != null">
      target_id = #{targetId,jdbcType=INTEGER},
    </if>
    <if test="type != null">
      type = #{type,jdbcType=INTEGER},
    </if>
    <if test="totalPrice != null">
      total_price = #{totalPrice,jdbcType=DECIMAL},
    </if>
    <if test="totalPriceWithTax != null">
      total_price_with_tax = #{totalPriceWithTax,jdbcType=DECIMAL},
    </if>
    <if test="totalTax != null">
      total_tax = #{totalTax,jdbcType=DECIMAL},
    </if>
    <if test="totalPriceActual != null">
      total_price_actual = #{totalPriceActual,jdbcType=DECIMAL},
    </if>
    <if test="totalTaxActual != null">
      total_tax_actual = #{totalTaxActual,jdbcType=DECIMAL},
    </if>
    <if test="status != null">
      status = #{status,jdbcType=INTEGER},
    </if>
    <if test="remark != null">
      remark = #{remark,jdbcType=VARCHAR},
    </if>
    <if test="createTime != null">
      create_time = #{createTime,jdbcType=TIMESTAMP},
    </if>
    <if test="updateTime != null">
      update_time = #{updateTime,jdbcType=TIMESTAMP},
    </if>
    <if test="updateAccount != null">
      update_account = #{updateAccount,jdbcType=INTEGER},
    </if>
    <if test="createAccount != null">
      create_account = #{createAccount,jdbcType=INTEGER},
    </if>
  </set>
  where id = #{id,jdbcType=INTEGER}
</update>

  
<update id="update" parameterType="com.boco.nbd.wios.manage.entity.settlement.po.Settlement">
  update t_settlement
  set settlement_id = #{settlementId,jdbcType=VARCHAR},
    start_date = #{startDate,jdbcType=DATE},
    end_date = #{endDate,jdbcType=DATE},
    target_id = #{targetId,jdbcType=INTEGER},
    type = #{type,jdbcType=INTEGER},
    total_price = #{totalPrice,jdbcType=DECIMAL},
    total_price_with_tax = #{totalPriceWithTax,jdbcType=DECIMAL},
    total_tax = #{totalTax,jdbcType=DECIMAL},
    total_price_actual = #{totalPriceActual,jdbcType=DECIMAL},
    total_tax_actual = #{totalTaxActual,jdbcType=DECIMAL},
    status = #{status,jdbcType=INTEGER},
    remark = #{remark,jdbcType=VARCHAR},
    create_time = #{createTime,jdbcType=TIMESTAMP},
    update_time = #{updateTime,jdbcType=TIMESTAMP},
    update_account = #{updateAccount,jdbcType=INTEGER},
    create_account = #{createAccount,jdbcType=INTEGER}
  where id = #{id,jdbcType=INTEGER}
</update>

  
<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  select
	<include refid="Base_Column_List" />
  from  t_settlement
  where id = #{id,jdbcType=INTEGER}
</select>

<select id="selectBySettlementId" parameterType="java.lang.String" resultMap="BaseResultMap">
  select *
  from  t_settlement
  where settlement_id = #{settlementId}
</select>

<select id="selectByExample" parameterType="com.boco.nbd.wios.manage.entity.settlement.po.Settlement"
        resultType="com.boco.nbd.wios.manage.entity.settlement.vo.SettlementVo">
   select t.*,o.name as targetName from t_settlement t
   <if test="type==1">
   	left join t_oem o on o.id = t.target_id
   </if>
   <if test="type==2">
   	left join t_supplier o on o.id = t.target_id
   </if>
   <where>
	    <if test="settlementId != null and settlementId !=''">
	        and t.settlement_id = #{settlementId}
	    </if>
	    <if test="targetId != null ">
	        and t.target_id = #{targetId}
	    </if>
	    <if test="type != null ">
	        and t.type = #{type}
	    </if>
	    <if test="status != null">
	        and t.status = #{status}
	    </if>
   </where>
   order by t.create_time desc
</select>

  <select id="getSettlementByCondition" resultType="com.boco.nbd.wios.manage.entity.settlement.po.Settlement">
      SELECT
        <include refid="Base_Column_List" />
      FROM
      t_settlement
      WHERE
      `status` = 1
      <if test="list != null ">
        AND target_id IN
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
          #{item}
        </foreach>
      </if>

      <if test="type != null ">
        AND type = #{type}
      </if>

      <if test="startDate != null and endDate != null">
        AND (
        (
        start_date &lt;= #{startDate}
        AND end_date &gt;= #{startDate}
        )
        OR (
        start_date &lt;= #{endDate}
        AND end_date &gt;= #{endDate}
        )
        OR (
        start_date &gt;= #{startDate}
        AND end_date &lt;= #{endDate}
        )
        )
      </if>
  </select>
</mapper>