<?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.SupplierContractMapper">
  
<resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.bo.SupplierContract">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="code" jdbcType="VARCHAR" property="code" />
  <result column="supplier_id" jdbcType="VARCHAR" property="supplierId" />
  <result column="region_id" jdbcType="VARCHAR" property="regionId" />
  <result column="contact_name" jdbcType="VARCHAR" property="contactName" />
  <result column="contact_phone" jdbcType="VARCHAR" property="contactPhone" />
  <result column="address" jdbcType="VARCHAR" property="address" />
  <result column="sign_date" jdbcType="DATE" property="signDate" />
  <result column="start_time" jdbcType="DATE" property="startTime" />
  <result column="end_time" jdbcType="DATE" property="endTime" />
  <result column="settle_interval" jdbcType="INTEGER" property="settleInterval" />
  <result column="business_type" jdbcType="INTEGER" property="businessType" />
  <result column="location_id" jdbcType="VARCHAR" property="locationId" />
  <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="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, supplier_id, region_id, contact_name, contact_phone, address, sign_date, 
  start_time, end_time, settle_interval, business_type, location_id, tax_rate, guarantee_period, 
  status, description, create_account, create_time, modify_account, modify_time
</sql>

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

  
<insert id="insert" parameterType="com.boco.nbd.wios.manage.entity.bo.SupplierContract"
useGeneratedKeys="true" keyProperty="id">
  insert into t_supplier_contract (id, code, supplier_id, 
    region_id, contact_name, contact_phone, 
    address, sign_date, start_time, 
    end_time, settle_interval, business_type, 
    location_id, tax_rate, guarantee_period, 
    status, description, create_account, 
    create_time, modify_account, modify_time
    )
  values (#{id,jdbcType=INTEGER}, #{code,jdbcType=VARCHAR}, #{supplierId,jdbcType=VARCHAR}, 
    #{regionId,jdbcType=VARCHAR}, #{contactName,jdbcType=VARCHAR}, #{contactPhone,jdbcType=VARCHAR}, 
    #{address,jdbcType=VARCHAR}, #{signDate,jdbcType=DATE}, #{startTime,jdbcType=DATE}, 
    #{endTime,jdbcType=DATE}, #{settleInterval,jdbcType=INTEGER}, #{businessType,jdbcType=INTEGER}, 
    #{locationId,jdbcType=VARCHAR}, #{taxRate,jdbcType=DECIMAL}, #{guaranteePeriod,jdbcType=INTEGER}, 
    #{status,jdbcType=INTEGER}, #{description,jdbcType=VARCHAR}, #{createAccount,jdbcType=VARCHAR}, 
    #{createTime,jdbcType=TIMESTAMP}, #{modifyAccount,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP}
    )
</insert>

  
<insert id="insertDynamic" parameterType="com.boco.nbd.wios.manage.entity.bo.SupplierContract">
  insert into t_supplier_contract
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
      id,
    </if>
    <if test="code != null">
      code,
    </if>
    <if test="supplierId != null">
      supplier_id,
    </if>
    <if test="regionId != null">
      region_id,
    </if>
    <if test="contactName != null">
      contact_name,
    </if>
    <if test="contactPhone != null">
      contact_phone,
    </if>
    <if test="address != null">
      address,
    </if>
    <if test="signDate != null">
      sign_date,
    </if>
    <if test="startTime != null">
      start_time,
    </if>
    <if test="endTime != null">
      end_time,
    </if>
    <if test="settleInterval != null">
      settle_interval,
    </if>
    <if test="businessType != null">
      business_type,
    </if>
    <if test="locationId != null">
      location_id,
    </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="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="supplierId != null">
      #{supplierId,jdbcType=VARCHAR},
    </if>
    <if test="regionId != null">
      #{regionId,jdbcType=VARCHAR},
    </if>
    <if test="contactName != null">
      #{contactName,jdbcType=VARCHAR},
    </if>
    <if test="contactPhone != null">
      #{contactPhone,jdbcType=VARCHAR},
    </if>
    <if test="address != null">
      #{address,jdbcType=VARCHAR},
    </if>
    <if test="signDate != null">
      #{signDate,jdbcType=DATE},
    </if>
    <if test="startTime != null">
      #{startTime,jdbcType=DATE},
    </if>
    <if test="endTime != null">
      #{endTime,jdbcType=DATE},
    </if>
    <if test="settleInterval != null">
      #{settleInterval,jdbcType=INTEGER},
    </if>
    <if test="businessType != null">
      #{businessType,jdbcType=INTEGER},
    </if>
    <if test="locationId != null">
      #{locationId,jdbcType=VARCHAR},
    </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="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.SupplierContract">
  update t_supplier_contract
  <set>
    <if test="code != null">
      code = #{code,jdbcType=VARCHAR},
    </if>
    <if test="supplierId != null">
      supplier_id = #{supplierId,jdbcType=VARCHAR},
    </if>
    <if test="regionId != null">
      region_id = #{regionId,jdbcType=VARCHAR},
    </if>
    <if test="contactName != null">
      contact_name = #{contactName,jdbcType=VARCHAR},
    </if>
    <if test="contactPhone != null">
      contact_phone = #{contactPhone,jdbcType=VARCHAR},
    </if>
    <if test="address != null">
      address = #{address,jdbcType=VARCHAR},
    </if>
    <if test="signDate != null">
      sign_date = #{signDate,jdbcType=DATE},
    </if>
    <if test="startTime != null">
      start_time = #{startTime,jdbcType=DATE},
    </if>
    <if test="endTime != null">
      end_time = #{endTime,jdbcType=DATE},
    </if>
    <if test="settleInterval != null">
      settle_interval = #{settleInterval,jdbcType=INTEGER},
    </if>
    <if test="businessType != null">
      business_type = #{businessType,jdbcType=INTEGER},
    </if>
    <if test="locationId != null">
      location_id = #{locationId,jdbcType=VARCHAR},
    </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="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.SupplierContract">
  update t_supplier_contract
  set code = #{code,jdbcType=VARCHAR},
    supplier_id = #{supplierId,jdbcType=VARCHAR},
    region_id = #{regionId,jdbcType=VARCHAR},
    contact_name = #{contactName,jdbcType=VARCHAR},
    contact_phone = #{contactPhone,jdbcType=VARCHAR},
    address = #{address,jdbcType=VARCHAR},
    sign_date = #{signDate,jdbcType=DATE},
    start_time = #{startTime,jdbcType=DATE},
    end_time = #{endTime,jdbcType=DATE},
    settle_interval = #{settleInterval,jdbcType=INTEGER},
    business_type = #{businessType,jdbcType=INTEGER},
    location_id = #{locationId,jdbcType=VARCHAR},
    tax_rate = #{taxRate,jdbcType=DECIMAL},
    guarantee_period = #{guaranteePeriod,jdbcType=INTEGER},
    status = #{status,jdbcType=INTEGER},
    description = #{description,jdbcType=VARCHAR},
    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_supplier_contract
  where id = #{id,jdbcType=INTEGER}
</select>

<select id="selectByCode" parameterType="java.lang.String" resultMap="BaseResultMap">
  select
	<include refid="Base_Column_List" />
  from  t_supplier_contract
  where code = #{code}
</select>
  
<select id="selectByExample" parameterType="com.boco.nbd.wios.manage.entity.bo.SupplierContractBo"
        resultType="com.boco.nbd.wios.manage.entity.bo.SupplierContractVo">
   select tt.*,count(tt.orderId) as orderQuantity from
   (
	   select t.*,s.name as supplierName,
	   ord.id as orderId
	   from t_supplier_contract t
	   left join t_supplier s on s.id=t.supplier_id
	   left JOIN t_order ord ON t.supplier_id = ord.install_supplier_id
	   <where>
	   	    <if test="supplierId != null and supplierId != ''">
	   	        and t.supplier_id = #{supplierId}
		    </if>
		    <if test="code != null and code != ''">
		    	<bind name="codeLike" value="'%' + code + '%'"/>
		        and t.code like #{codeLike}
		    </if>
		    <if test="supplierName != null and supplierName != ''">
		    	<bind name="supplierNameLike" value="'%' + supplierName + '%'"/>
		        and s.name like #{supplierNameLike}
		    </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="taxRate != null">
	   	        and t.tax_rate = #{taxRate}
		    </if>
		    <if test="orderBy != null and orderBy==1">
		    	order by t.create_time desc
		    </if>
		    <if test="orderBy != null and orderBy==2">
		    	order by t.end_time desc
		    </if>
	   </where>
	) as tt
	GROUP BY tt.id
    order by tt.create_time desc
</select>

<select id="selectByRegionId"
        resultType="com.boco.nbd.wios.manage.entity.bo.SupplierContract">
   select t.* from t_supplier_contract t
   left join t_supplier s on s.id=t.supplier_id
   <where>
   		s.status=1
   		and t.`status`=2
        and
	   <foreach collection="regionIdList" item="item" index="index" open="(" close=")">
	   		<if test="index>0">
   	         or
	   		</if>
		    FIND_IN_SET(#{item},t.region_id) 
	   </foreach>
	   <if test="status != null">
   	         and t.status = #{status}
	   </if>
       <if test="checkStatus != null">
         and s.check_status = #{checkStatus}
       </if>
   </where>
</select>


<select id="selectEffectiveContract" resultType="com.boco.nbd.wios.manage.entity.bo.SupplierContract">
  select t.*
  from t_supplier_contract t
  <where>
    <if test="supplierId != null and supplierId != ''">
      and t.supplier_id = #{supplierId}
    </if>
    <if test="regionId != null and regionId != ''">
      and FIND_IN_SET(#{regionId}, t.region_id)
    </if>
    <if test="status != null">
      and t.status = #{status}
    </if>
  </where>
</select>
</mapper>