<?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.OemMapper">

<resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.bo.Oem">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="code" jdbcType="VARCHAR" property="code" />
  <result column="short_name" jdbcType="VARCHAR" property="shortName" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="status" jdbcType="INTEGER" property="status" />
  <result column="type" jdbcType="INTEGER" property="type" />
  <result column="wb_account_id" jdbcType="INTEGER" property="wbAccountId" />
  <result column="region_id" jdbcType="INTEGER" property="regionId" />
  <result column="manual_dispatch_order" jdbcType="INTEGER" property="manualDispatchOrder" />
  <result column="organization_code" jdbcType="VARCHAR" property="organizationCode" />
  <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="file_id" jdbcType="VARCHAR" property="fileId" />
  <result column="parent_id" jdbcType="INTEGER" property="parentId" />
  <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, short_name, name, status, type, wb_account_id, region_id, manual_dispatch_order,
  organization_code, contact_name, contact_phone, address, file_id, parent_id, description,
  create_account, create_time, modify_account, modify_time
</sql>


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


<insert id="insert" parameterType="com.boco.nbd.wios.manage.entity.bo.Oem">
  insert into t_oem (id, code, short_name,
    name, status, type,
    wb_account_id, region_id, manual_dispatch_order,
    organization_code, contact_name, contact_phone,
    address, file_id, parent_id,
    description, create_account, create_time,
    modify_account, modify_time)
  values (#{id,jdbcType=INTEGER}, #{code,jdbcType=VARCHAR}, #{shortName,jdbcType=VARCHAR},
    #{name,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{type,jdbcType=INTEGER},
    #{wbAccountId,jdbcType=INTEGER}, #{regionId,jdbcType=INTEGER}, #{manualDispatchOrder,jdbcType=INTEGER},
    #{organizationCode,jdbcType=VARCHAR}, #{contactName,jdbcType=VARCHAR}, #{contactPhone,jdbcType=VARCHAR},
    #{address,jdbcType=VARCHAR}, #{fileId,jdbcType=VARCHAR}, #{parentId,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.Oem">
  insert into t_oem
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
      id,
    </if>
    <if test="code != null">
      code,
    </if>
    <if test="shortName != null">
      short_name,
    </if>
    <if test="name != null">
      name,
    </if>
    <if test="status != null">
      status,
    </if>
    <if test="type != null">
      type,
    </if>
    <if test="wbAccountId != null">
      wb_account_id,
    </if>
    <if test="regionId != null">
      region_id,
    </if>
    <if test="manualDispatchOrder != null">
      manual_dispatch_order,
    </if>
    <if test="organizationCode != null">
      organization_code,
    </if>
    <if test="contactName != null">
      contact_name,
    </if>
    <if test="contactPhone != null">
      contact_phone,
    </if>
    <if test="address != null">
      address,
    </if>
    <if test="fileId != null">
      file_id,
    </if>
    <if test="parentId != null">
      parent_id,
    </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="shortName != null">
      #{shortName,jdbcType=VARCHAR},
    </if>
    <if test="name != null">
      #{name,jdbcType=VARCHAR},
    </if>
    <if test="status != null">
      #{status,jdbcType=INTEGER},
    </if>
    <if test="type != null">
      #{type,jdbcType=INTEGER},
    </if>
    <if test="wbAccountId != null">
      #{wbAccountId,jdbcType=INTEGER},
    </if>
    <if test="regionId != null">
      #{regionId,jdbcType=INTEGER},
    </if>
    <if test="manualDispatchOrder != null">
      #{manualDispatchOrder,jdbcType=INTEGER},
    </if>
    <if test="organizationCode != null">
      #{organizationCode,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="fileId != null">
      #{fileId,jdbcType=VARCHAR},
    </if>
    <if test="parentId != null">
      #{parentId,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.Oem">
  update t_oem
  <set>
    <if test="code != null">
      code = #{code,jdbcType=VARCHAR},
    </if>
    <if test="shortName != null">
      short_name = #{shortName,jdbcType=VARCHAR},
    </if>
    <if test="name != null">
      name = #{name,jdbcType=VARCHAR},
    </if>
    <if test="status != null">
      status = #{status,jdbcType=INTEGER},
    </if>
    <if test="type != null">
      type = #{type,jdbcType=INTEGER},
    </if>
    <if test="wbAccountId != null">
      wb_account_id = #{wbAccountId,jdbcType=INTEGER},
    </if>
    <if test="regionId != null">
      region_id = #{regionId,jdbcType=INTEGER},
    </if>
    <if test="manualDispatchOrder != null">
      manual_dispatch_order = #{manualDispatchOrder,jdbcType=INTEGER},
    </if>
    <if test="organizationCode != null">
      organization_code = #{organizationCode,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="fileId != null">
      file_id = #{fileId,jdbcType=VARCHAR},
    </if>
    <if test="parentId != null">
      parent_id = #{parentId,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.Oem">
  update t_oem
  set code = #{code,jdbcType=VARCHAR},
    short_name = #{shortName,jdbcType=VARCHAR},
    name = #{name,jdbcType=VARCHAR},
    status = #{status,jdbcType=INTEGER},
    type = #{type,jdbcType=INTEGER},
    wb_account_id = #{wbAccountId,jdbcType=INTEGER},
    region_id = #{regionId,jdbcType=INTEGER},
    manual_dispatch_order = #{manualDispatchOrder,jdbcType=INTEGER},
    organization_code = #{organizationCode,jdbcType=VARCHAR},
    contact_name = #{contactName,jdbcType=VARCHAR},
    contact_phone = #{contactPhone,jdbcType=VARCHAR},
    address = #{address,jdbcType=VARCHAR},
    file_id = #{fileId,jdbcType=VARCHAR},
    parent_id = #{parentId,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_oem
  where id = #{id,jdbcType=INTEGER}
</select>

<select id="selectSelfAndChildren" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  select *
  from  t_oem
  where id = #{id} or parent_id=#{id}
</select>

<select id="selectByExample" parameterType="com.boco.nbd.wios.manage.entity.bo.OemBo"
        resultType="com.boco.nbd.wios.manage.entity.bo.OemVo">
   <if test="hasContract != null"><!-- 是否签了合同 -->
   select tt.* from
   (
   </if>
	   select oem.*,
	   <if test="type != null and type == 2"><!-- 经销商 -->
	       po.name as parentOemName,
	   </if>
	   <if test="hasContract != null"><!-- 是否签了合同 -->
	       oc.id as validContractId,
	   </if>
	   a.account as accountName from t_oem oem
	   left join wb_account a on a.id=oem.wb_account_id
	   <if test="type != null and type == 2"><!-- 经销商 -->
	       left join t_oem po on po.id=oem.parent_id
	   </if>
	   <if test="hasContract != null"><!-- 是否签了合同 -->
	       left join t_oem_contract oc on oc.oem_id=oem.id
	       <if test="hasContract == 1">
	       	and oc.status=2
	       </if>
	   </if>
	   <where>
	   		<if test="id != null">
		        and oem.id = #{id}
		    </if>
		    <if test="idAndPId != null">
		        and (oem.id = #{idAndPId} or oem.parent_id=#{idAndPId})
		    </if>
		    <if test="code != null and code !=''">
		        <bind name="codeLike" value="'%' + code + '%'"/>
		        and oem.code like #{codeLike}
		    </if>
		    <if test="name != null and name != ''">
		        <bind name="nameLike" value="'%' + name + '%'"/>
		        and oem.name like #{nameLike}
		    </if>
		    <if test="type != null">
		        and oem.type = #{type}
		    </if>
		    <if test="status != null">
		        and oem.status = #{status}
		    </if>
		    <if test="organizationCode != null and organizationCode != ''">
		        <bind name="organizationCodeLike" value="'%' + organizationCode + '%'"/>
		        and oem.organization_code like #{organizationCodeLike}
		    </if>
		    <if test="regionId != null">
		        and oem.region_id = #{regionId}
		    </if>
		    <if test="parentId != null">
		        and oem.parent_id = #{parentId}
		    </if>
	   </where>
	   order by oem.create_time desc
	<if test="hasContract != null"><!-- 是否签了合同 -->
	) as tt
	</if>
	<if test="hasContract != null and hasContract==0"><!-- 未签合同 -->
	where tt.validContractId is null
	group by tt.id
	</if>
	<if test="hasContract != null and hasContract!=0"><!-- 签了合同 -->
	where tt.validContractId is not null
	group by tt.id
	</if>
</select>

<select id="selectOnly" resultType="com.boco.nbd.wios.manage.entity.bo.Oem">
	select * from t_oem
	<where>
	    <if test="code != null and code !=''">
	        or code = #{code}
	    </if>
	    <if test="name != null and name != ''">
	        or name = #{name}
	    </if>
   </where>
</select>

<select id="selectByName" parameterType="java.lang.String" resultMap="BaseResultMap">
  select *
  from  t_oem
  where name = #{name} and status = 1
</select>

<select id="selectByNameAndCode" resultMap="BaseResultMap">
  select * from t_oem
	<where>
		status = 1
	    <if test="code != null and code !=''">
	        and code = #{code}
	    </if>
	    <if test="name != null and name != ''">
	        and name = #{name}
	    </if>
   </where>
</select>

<select id="selectByAccountId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  select *
  from  t_oem
  where wb_account_id = #{accountId} and status = 1
</select>

<select id="selectAll" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
     from t_oem
    <where>
        <if test="status != null">
            and status = #{status}
        </if>
    </where>
</select>

<select id="selectAllOem" resultType="com.boco.nbd.wios.manage.entity.bo.Oem">
     select DISTINCT a.id,a.name,a.code,a.short_name shortName from t_oem a
        inner join t_oem_contract b on a.id=b.oem_id and b.status=2
    where type=1 and a.status=1
</select>

</mapper>