<?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.OemContractMapper">
  
<resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.bo.OemContract">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="code" jdbcType="VARCHAR" property="code" />
  <result column="oem_id" jdbcType="VARCHAR" property="oemId" />
  <result column="device_settlement_id" jdbcType="VARCHAR" property="deviceSettlementId" />
  <result column="install_settlement_id" jdbcType="VARCHAR" property="installSettlementId" />
  <result column="region_id" jdbcType="VARCHAR" property="regionId" />
  <result column="start_time" jdbcType="DATE" property="startTime" />
  <result column="end_time" jdbcType="DATE" property="endTime" />
  <result column="sale_account" jdbcType="VARCHAR" property="saleAccount" />
  <result column="settle_interval" jdbcType="INTEGER" property="settleInterval" />
  <result column="business_type" jdbcType="INTEGER" property="businessType" />
  <result column="standard_length" jdbcType="DECIMAL" property="standardLength" />
  <result column="tax_rate" jdbcType="DECIMAL" property="taxRate" />
  <result column="guarantee_period" jdbcType="INTEGER" property="guaranteePeriod" />
  <result column="status" jdbcType="INTEGER" property="status" />
  <result column="description" jdbcType="VARCHAR" property="description" />
  <result column="file_id" jdbcType="VARCHAR" property="fileId" />
  <result column="cams_device_enable" jdbcType="INTEGER" property="camsDeviceEnable" />
  <result column="create_account" jdbcType="VARCHAR" property="createAccount" />
  <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
  <result column="modify_account" jdbcType="VARCHAR" property="modifyAccount" />
  <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime" />
</resultMap>

  
<sql id="Base_Column_List">
  id, code, oem_id, device_settlement_id, install_settlement_id, region_id, start_time, 
  end_time, sale_account, settle_interval, business_type, standard_length, tax_rate, 
  guarantee_period, status, description, file_id, cams_device_enable, create_account, 
  create_time, modify_account, modify_time
</sql>

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

  
<insert id="insert" parameterType="com.boco.nbd.wios.manage.entity.bo.OemContract"
useGeneratedKeys="true" keyProperty="id">
  insert into t_oem_contract (id, code, oem_id, 
    device_settlement_id, install_settlement_id, 
    region_id, start_time, end_time, 
    sale_account, settle_interval, business_type, 
    standard_length, tax_rate, guarantee_period, 
    status, description, file_id, 
    cams_device_enable, create_account, create_time, 
    modify_account, modify_time)
  values (#{id,jdbcType=INTEGER}, #{code,jdbcType=VARCHAR}, #{oemId,jdbcType=VARCHAR}, 
    #{deviceSettlementId,jdbcType=VARCHAR}, #{installSettlementId,jdbcType=VARCHAR}, 
    #{regionId,jdbcType=VARCHAR}, #{startTime,jdbcType=DATE}, #{endTime,jdbcType=DATE}, 
    #{saleAccount,jdbcType=VARCHAR}, #{settleInterval,jdbcType=INTEGER}, #{businessType,jdbcType=INTEGER}, 
    #{standardLength,jdbcType=DECIMAL}, #{taxRate,jdbcType=DECIMAL}, #{guaranteePeriod,jdbcType=INTEGER}, 
    #{status,jdbcType=INTEGER}, #{description,jdbcType=VARCHAR}, #{fileId,jdbcType=VARCHAR}, 
    #{camsDeviceEnable,jdbcType=INTEGER}, #{createAccount,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, 
    #{modifyAccount,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP})
</insert>

  
<insert id="insertDynamic" parameterType="com.boco.nbd.wios.manage.entity.bo.OemContract">
  insert into t_oem_contract
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
      id,
    </if>
    <if test="code != null">
      code,
    </if>
    <if test="oemId != null">
      oem_id,
    </if>
    <if test="deviceSettlementId != null">
      device_settlement_id,
    </if>
    <if test="installSettlementId != null">
      install_settlement_id,
    </if>
    <if test="regionId != null">
      region_id,
    </if>
    <if test="startTime != null">
      start_time,
    </if>
    <if test="endTime != null">
      end_time,
    </if>
    <if test="saleAccount != null">
      sale_account,
    </if>
    <if test="settleInterval != null">
      settle_interval,
    </if>
    <if test="businessType != null">
      business_type,
    </if>
    <if test="standardLength != null">
      standard_length,
    </if>
    <if test="taxRate != null">
      tax_rate,
    </if>
    <if test="guaranteePeriod != null">
      guarantee_period,
    </if>
    <if test="status != null">
      status,
    </if>
    <if test="description != null">
      description,
    </if>
    <if test="fileId != null">
      file_id,
    </if>
    <if test="camsDeviceEnable != null">
      cams_device_enable,
    </if>
    <if test="createAccount != null">
      create_account,
    </if>
    <if test="createTime != null">
      create_time,
    </if>
    <if test="modifyAccount != null">
      modify_account,
    </if>
    <if test="modifyTime != null">
      modify_time,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="id != null">
      #{id,jdbcType=INTEGER},
    </if>
    <if test="code != null">
      #{code,jdbcType=VARCHAR},
    </if>
    <if test="oemId != null">
      #{oemId,jdbcType=VARCHAR},
    </if>
    <if test="deviceSettlementId != null">
      #{deviceSettlementId,jdbcType=VARCHAR},
    </if>
    <if test="installSettlementId != null">
      #{installSettlementId,jdbcType=VARCHAR},
    </if>
    <if test="regionId != null">
      #{regionId,jdbcType=VARCHAR},
    </if>
    <if test="startTime != null">
      #{startTime,jdbcType=DATE},
    </if>
    <if test="endTime != null">
      #{endTime,jdbcType=DATE},
    </if>
    <if test="saleAccount != null">
      #{saleAccount,jdbcType=VARCHAR},
    </if>
    <if test="settleInterval != null">
      #{settleInterval,jdbcType=INTEGER},
    </if>
    <if test="businessType != null">
      #{businessType,jdbcType=INTEGER},
    </if>
    <if test="standardLength != null">
      #{standardLength,jdbcType=DECIMAL},
    </if>
    <if test="taxRate != null">
      #{taxRate,jdbcType=DECIMAL},
    </if>
    <if test="guaranteePeriod != null">
      #{guaranteePeriod,jdbcType=INTEGER},
    </if>
    <if test="status != null">
      #{status,jdbcType=INTEGER},
    </if>
    <if test="description != null">
      #{description,jdbcType=VARCHAR},
    </if>
    <if test="fileId != null">
      #{fileId,jdbcType=VARCHAR},
    </if>
    <if test="camsDeviceEnable != null">
      #{camsDeviceEnable,jdbcType=INTEGER},
    </if>
    <if test="createAccount != null">
      #{createAccount,jdbcType=VARCHAR},
    </if>
    <if test="createTime != null">
      #{createTime,jdbcType=TIMESTAMP},
    </if>
    <if test="modifyAccount != null">
      #{modifyAccount,jdbcType=VARCHAR},
    </if>
    <if test="modifyTime != null">
      #{modifyTime,jdbcType=TIMESTAMP},
    </if>
  </trim>
</insert>

  
<update id="updateDynamic" parameterType="com.boco.nbd.wios.manage.entity.bo.OemContract">
  update t_oem_contract
  <set>
    <if test="code != null">
      code = #{code,jdbcType=VARCHAR},
    </if>
    <if test="oemId != null">
      oem_id = #{oemId,jdbcType=VARCHAR},
    </if>
    <if test="deviceSettlementId != null">
      device_settlement_id = #{deviceSettlementId,jdbcType=VARCHAR},
    </if>
    <if test="installSettlementId != null">
      install_settlement_id = #{installSettlementId,jdbcType=VARCHAR},
    </if>
    <if test="regionId != null">
      region_id = #{regionId,jdbcType=VARCHAR},
    </if>
    <if test="startTime != null">
      start_time = #{startTime,jdbcType=DATE},
    </if>
    <if test="endTime != null">
      end_time = #{endTime,jdbcType=DATE},
    </if>
    <if test="saleAccount != null">
      sale_account = #{saleAccount,jdbcType=VARCHAR},
    </if>
    <if test="settleInterval != null">
      settle_interval = #{settleInterval,jdbcType=INTEGER},
    </if>
    <if test="businessType != null">
      business_type = #{businessType,jdbcType=INTEGER},
    </if>
    <if test="standardLength != null">
      standard_length = #{standardLength,jdbcType=DECIMAL},
    </if>
    <if test="taxRate != null">
      tax_rate = #{taxRate,jdbcType=DECIMAL},
    </if>
    <if test="guaranteePeriod != null">
      guarantee_period = #{guaranteePeriod,jdbcType=INTEGER},
    </if>
    <if test="status != null">
      status = #{status,jdbcType=INTEGER},
    </if>
    <if test="description != null">
      description = #{description,jdbcType=VARCHAR},
    </if>
    <if test="fileId != null">
      file_id = #{fileId,jdbcType=VARCHAR},
    </if>
    <if test="camsDeviceEnable != null">
      cams_device_enable = #{camsDeviceEnable,jdbcType=INTEGER},
    </if>
    <if test="createAccount != null">
      create_account = #{createAccount,jdbcType=VARCHAR},
    </if>
    <if test="createTime != null">
      create_time = #{createTime,jdbcType=TIMESTAMP},
    </if>
    <if test="modifyAccount != null">
      modify_account = #{modifyAccount,jdbcType=VARCHAR},
    </if>
    <if test="modifyTime != null">
      modify_time = #{modifyTime,jdbcType=TIMESTAMP},
    </if>
  </set>
  where id = #{id,jdbcType=INTEGER}
</update>

  
<update id="update" parameterType="com.boco.nbd.wios.manage.entity.bo.OemContract">
  update t_oem_contract
  set code = #{code,jdbcType=VARCHAR},
    oem_id = #{oemId,jdbcType=VARCHAR},
    device_settlement_id = #{deviceSettlementId,jdbcType=VARCHAR},
    install_settlement_id = #{installSettlementId,jdbcType=VARCHAR},
    region_id = #{regionId,jdbcType=VARCHAR},
    start_time = #{startTime,jdbcType=DATE},
    end_time = #{endTime,jdbcType=DATE},
    sale_account = #{saleAccount,jdbcType=VARCHAR},
    settle_interval = #{settleInterval,jdbcType=INTEGER},
    business_type = #{businessType,jdbcType=INTEGER},
    standard_length = #{standardLength,jdbcType=DECIMAL},
    tax_rate = #{taxRate,jdbcType=DECIMAL},
    guarantee_period = #{guaranteePeriod,jdbcType=INTEGER},
    status = #{status,jdbcType=INTEGER},
    description = #{description,jdbcType=VARCHAR},
    file_id = #{fileId,jdbcType=VARCHAR},
    cams_device_enable = #{camsDeviceEnable,jdbcType=INTEGER},
    create_account = #{createAccount,jdbcType=VARCHAR},
    create_time = #{createTime,jdbcType=TIMESTAMP},
    modify_account = #{modifyAccount,jdbcType=VARCHAR},
    modify_time = #{modifyTime,jdbcType=TIMESTAMP}
  where id = #{id,jdbcType=INTEGER}
</update>

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

<select id="selectByCode" parameterType="java.lang.String" resultMap="BaseResultMap">
  select
	<include refid="Base_Column_List" />
  from  t_oem_contract
  where code = #{code}
</select>

<select id="selectByExample" parameterType="com.boco.nbd.wios.manage.entity.bo.OemContractBo2"
        resultType="com.boco.nbd.wios.manage.entity.bo.OemContractVo">
   select tt.*,count(tt.orderId) as orderQuantity from
   (
	   select t.*,o.name as oemName,o1.name as deviceSettlementName,
	   o2.name as installSettlementName,
	   ord.id as orderId
	   from t_oem_contract t
	   left join t_oem o on o.id=t.oem_id
	   left join t_oem o1 on o1.id=t.device_settlement_id
	   left join t_oem o2 on o2.id=t.install_settlement_Id
	   left JOIN t_order ord ON t.oem_id = ord.oem_id
	   <where>
	   	    <if test="oemId != null and oemId != ''">
	   	        and t.oem_id = #{oemId}
		    </if>
		    <if test="oemName != null and oemName != ''">
		    	<bind name="oemNameLike" value="'%' + oemName + '%'"/>
		        and o.name like #{oemNameLike}
		    </if>
		    <if test="code != null and code != ''">
		    	<bind name="codeLike" value="'%' + code + '%'"/>
		        and t.code like #{codeLike}
		    </if>
		    <if test="saleAccount != null and saleAccount != ''">
		    	<bind name="saleAccountLike" value="'%' + saleAccount + '%'"/>
		        and t.sale_account like #{saleAccountLike}
		    </if>
		    <if test="deviceSettlementName != null and deviceSettlementName != ''">
		    	<bind name="deviceSettlementNameLike" value="'%' + deviceSettlementName + '%'"/>
		        and o1.name like #{deviceSettlementNameLike}
		    </if>
		    <if test="installSettlementName != null and installSettlementName != ''">
		    	<bind name="installSettlementNameLike" value="'%' + installSettlementName + '%'"/>
		        and o2.name like #{installSettlementNameLike}
		    </if>
		    <if test="guaranteePeriod != null">
	   	        and t.guarantee_period = #{guaranteePeriod}
		    </if>
		    <if test="status != null">
	   	        and t.status = #{status}
		    </if>
		    <if test="statusInList != null">
		        and t.status in
			   <foreach collection="statusInList" item="item" index="index" open="(" close=")" separator=",">
					#{item}
				</foreach>
		    </if>
		    <if test="startTimeOrigin != null and startTimeOrigin != ''">
				and t.start_time &lt;= #{startTimeOrigin}
			</if>
			<if test="startTimeTeminal != null and startTimeTeminal != ''">
				and t.start_time &gt;= #{startTimeTeminal}
			</if>
			<if test="endTimeOrigin != null and endTimeOrigin != ''">
				and t.end_time &lt;= #{endTimeOrigin}
			</if>
			<if test="endTimeTeminal != null and endTimeTeminal != ''">
				and t.end_time &gt;= #{endTimeTeminal}
			</if>
	   </where>
	) as tt
	GROUP BY tt.id
	<choose>
      <when test="orderBy != null and orderBy==2">
        order by tt.end_time desc
      </when>
      <otherwise>
        order by tt.create_time desc
      </otherwise>
    </choose>
</select>

<select id="selectDetailById" parameterType="java.lang.Integer" resultType="com.boco.nbd.wios.manage.entity.bo.OemContractVo">
  select t.*,
  o.name as oemName,
  o.code as oemCode,
  o1.name as deviceSettlementName,
  o2.name as installSettlementName
  from t_oem_contract t
  left join t_oem o on o.id=t.oem_id
  left join t_oem o1 on o1.id=t.device_settlement_id
  left join t_oem o2 on o2.id=t.install_settlement_Id
  where t.id = #{id,jdbcType=INTEGER}
</select>

<select id="selectByStatusAndOemId" resultMap="BaseResultMap">
  select t.*
  from t_oem_contract t
  where t.oem_id = #{oemId} and t.status = #{status}
</select>

</mapper>