<?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.zm.order.tool.server.dao.OrderUserRelationDao">
  <resultMap id="BaseResultMap" type="com.zm.order.tool.server.model.OrderUserRelation">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="biz_type" jdbcType="INTEGER" property="bizType" />
    <result column="order_type" jdbcType="INTEGER" property="orderType" />
    <result column="order_sub_type" jdbcType="INTEGER" property="orderSubType" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="user_id" jdbcType="BIGINT" property="userId" />
    <result column="root_id" jdbcType="BIGINT" property="rootId" />
    <result column="is_root" jdbcType="INTEGER" property="isRoot" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" />
    <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
  </resultMap>

  <sql id="tb">
    order_user_relation
  </sql>

  <sql id="cols_all">
    id,
    <include refid="cols_exclude_id"/>
  </sql>

  <sql id="cols_exclude_id">
    biz_type, order_type, order_sub_type, order_no, user_id, root_id, is_root, created_at, updated_at, is_deleted
  </sql>

  <sql id="values">
    #{id}, #{bizType}, #{orderType}, #{orderSubType}, #{orderNo}, #{userId}, #{rootId}, #{isRoot}, now(),now(), 0
  </sql>

  <insert id="insert" parameterType="com.zm.order.tool.server.model.OrderUserRelation">
    insert into
    <include refid="tb"/>
    (<include refid="cols_all"/>)
    values
    (<include refid="values"/>)
  </insert>

  <select id="selectOne" parameterType="map" resultMap="BaseResultMap">
    select 
    <include refid="cols_all" />
    from
    <include refid="tb" />
    <where>
      <if test="orderNo != null and orderNo != ''">
        AND order_no = #{orderNo}
      </if>
      <if test="userId != null">
        AND user_id = #{userId}
      </if>
      <if test="rootId != null">
        AND root_id = #{rootId}
      </if>
    </where>
    limit 1
  </select>

  <select id="selectByOrderNo" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all"/>
    from
    <include refid="tb"/>
    where order_no = #{orderNo}
    and user_id = #{userId}
    limit 1
  </select>

  <select id="selectRoot" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all" />
    from
    <include refid="tb" />
    <where>
      order_no = #{orderNo} and is_root = 0
    </where>
    limit 1
  </select>

  <select id="selectList" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all" />
    from
    <include refid="tb" />
    <where>
      <if test="orderNo != null and orderNo != ''">
        AND order_no = #{orderNo}
      </if>
      <if test="userId != null">
        AND user_id = #{userId}
      </if>
      <if test="rootId != null">
        AND root_id = #{rootId}
      </if>
      <if test="excludeUserId != null">
        AND user_id != #{excludeUserId}
      </if>
    </where>
    <if test="start != null">
      limit ${start}, ${pageSize}
    </if>
  </select>

  <select id="selectListByOrderNo" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all" />
    from
    <include refid="tb" />
    <where>
        order_no = #{orderNo} and is_deleted = 0
    </where>
  </select>

  <update id="deleteByKey" parameterType="java.lang.Long">
    update order_user_relation set is_deleted = -1,updated_at = now() where id = #{id}
  </update>

  <insert id="insertList" parameterType="list">
    insert into
    <include refid="tb"/>
    (<include refid="cols_all"/>)
    values
    <foreach collection="list" item="relation" index="index" separator=",">
      (
      #{relation.id}, #{relation.bizType}, #{relation.orderType}, #{relation.orderSubType}, #{relation.orderNo},
      #{relation.userId}, #{relation.rootId}, #{relation.isRoot}, now(), now(), 0
      )
    </foreach>
  </insert>


</mapper>