<?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.lightinit.hsdataportal.mapper.OrderMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdataportal.entity.Order">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="platform_type" jdbcType="VARCHAR" property="platformType" />
    <result column="user_id" jdbcType="BIGINT" property="userId" />
    <result column="order_time" jdbcType="TIMESTAMP" property="orderTime" />
    <result column="order_number" jdbcType="VARCHAR" property="orderNumber" />
    <result column="order_type" jdbcType="VARCHAR" property="orderType" />
    <result column="order_state" jdbcType="VARCHAR" property="orderState" />
    <result column="business_type" jdbcType="VARCHAR" property="businessType" />
    <result column="order_amount" jdbcType="DECIMAL" property="orderAmount" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="BIGINT" property="createdBy" />
    <result column="deleted_at" jdbcType="TIMESTAMP" property="deletedAt" />
    <result column="deleted_by" jdbcType="BIGINT" property="deletedBy" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.lightinit.hsdataportal.entity.Order">
    <result column="remark" jdbcType="LONGVARCHAR" property="remark" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, platform_type, user_id, order_time, order_number, order_type, order_state, business_type, 
    order_amount, created_at, created_by, deleted_at, deleted_by
  </sql>
  <sql id="Blob_Column_List">
    remark
  </sql>
  <select id="selectByExampleWithBLOBs" parameterType="com.lightinit.hsdataportal.entity.OrderExample" resultMap="ResultMapWithBLOBs">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from orders
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByExample" parameterType="com.lightinit.hsdataportal.entity.OrderExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from orders
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="ResultMapWithBLOBs">
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from orders
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from orders
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdataportal.entity.OrderExample">
    delete from orders
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdataportal.entity.Order">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into orders (platform_type, user_id, order_time, 
      order_number, order_type, order_state, 
      business_type, order_amount, created_at, 
      created_by, deleted_at, deleted_by, 
      remark)
    values (#{platformType,jdbcType=VARCHAR}, #{userId,jdbcType=BIGINT}, #{orderTime,jdbcType=TIMESTAMP}, 
      #{orderNumber,jdbcType=VARCHAR}, #{orderType,jdbcType=VARCHAR}, #{orderState,jdbcType=VARCHAR}, 
      #{businessType,jdbcType=VARCHAR}, #{orderAmount,jdbcType=DECIMAL}, #{createdAt,jdbcType=TIMESTAMP}, 
      #{createdBy,jdbcType=BIGINT}, #{deletedAt,jdbcType=TIMESTAMP}, #{deletedBy,jdbcType=BIGINT}, 
      #{remark,jdbcType=LONGVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdataportal.entity.Order">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into orders
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="platformType != null">
        platform_type,
      </if>
      <if test="userId != null">
        user_id,
      </if>
      <if test="orderTime != null">
        order_time,
      </if>
      <if test="orderNumber != null">
        order_number,
      </if>
      <if test="orderType != null">
        order_type,
      </if>
      <if test="orderState != null">
        order_state,
      </if>
      <if test="businessType != null">
        business_type,
      </if>
      <if test="orderAmount != null">
        order_amount,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="createdBy != null">
        created_by,
      </if>
      <if test="deletedAt != null">
        deleted_at,
      </if>
      <if test="deletedBy != null">
        deleted_by,
      </if>
      <if test="remark != null">
        remark,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="platformType != null">
        #{platformType,jdbcType=VARCHAR},
      </if>
      <if test="userId != null">
        #{userId,jdbcType=BIGINT},
      </if>
      <if test="orderTime != null">
        #{orderTime,jdbcType=TIMESTAMP},
      </if>
      <if test="orderNumber != null">
        #{orderNumber,jdbcType=VARCHAR},
      </if>
      <if test="orderType != null">
        #{orderType,jdbcType=VARCHAR},
      </if>
      <if test="orderState != null">
        #{orderState,jdbcType=VARCHAR},
      </if>
      <if test="businessType != null">
        #{businessType,jdbcType=VARCHAR},
      </if>
      <if test="orderAmount != null">
        #{orderAmount,jdbcType=DECIMAL},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        #{deletedBy,jdbcType=BIGINT},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdataportal.entity.OrderExample" resultType="java.lang.Long">
    select count(*) from orders
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update orders
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.platformType != null">
        platform_type = #{record.platformType,jdbcType=VARCHAR},
      </if>
      <if test="record.userId != null">
        user_id = #{record.userId,jdbcType=BIGINT},
      </if>
      <if test="record.orderTime != null">
        order_time = #{record.orderTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.orderNumber != null">
        order_number = #{record.orderNumber,jdbcType=VARCHAR},
      </if>
      <if test="record.orderType != null">
        order_type = #{record.orderType,jdbcType=VARCHAR},
      </if>
      <if test="record.orderState != null">
        order_state = #{record.orderState,jdbcType=VARCHAR},
      </if>
      <if test="record.businessType != null">
        business_type = #{record.businessType,jdbcType=VARCHAR},
      </if>
      <if test="record.orderAmount != null">
        order_amount = #{record.orderAmount,jdbcType=DECIMAL},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.createdBy != null">
        created_by = #{record.createdBy,jdbcType=BIGINT},
      </if>
      <if test="record.deletedAt != null">
        deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.deletedBy != null">
        deleted_by = #{record.deletedBy,jdbcType=BIGINT},
      </if>
      <if test="record.remark != null">
        remark = #{record.remark,jdbcType=LONGVARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExampleWithBLOBs" parameterType="map">
    update orders
    set id = #{record.id,jdbcType=BIGINT},
      platform_type = #{record.platformType,jdbcType=VARCHAR},
      user_id = #{record.userId,jdbcType=BIGINT},
      order_time = #{record.orderTime,jdbcType=TIMESTAMP},
      order_number = #{record.orderNumber,jdbcType=VARCHAR},
      order_type = #{record.orderType,jdbcType=VARCHAR},
      order_state = #{record.orderState,jdbcType=VARCHAR},
      business_type = #{record.businessType,jdbcType=VARCHAR},
      order_amount = #{record.orderAmount,jdbcType=DECIMAL},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=BIGINT},
      remark = #{record.remark,jdbcType=LONGVARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update orders
    set id = #{record.id,jdbcType=BIGINT},
      platform_type = #{record.platformType,jdbcType=VARCHAR},
      user_id = #{record.userId,jdbcType=BIGINT},
      order_time = #{record.orderTime,jdbcType=TIMESTAMP},
      order_number = #{record.orderNumber,jdbcType=VARCHAR},
      order_type = #{record.orderType,jdbcType=VARCHAR},
      order_state = #{record.orderState,jdbcType=VARCHAR},
      business_type = #{record.businessType,jdbcType=VARCHAR},
      order_amount = #{record.orderAmount,jdbcType=DECIMAL},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=BIGINT}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.lightinit.hsdataportal.entity.Order">
    update orders
    <set>
      <if test="platformType != null">
        platform_type = #{platformType,jdbcType=VARCHAR},
      </if>
      <if test="userId != null">
        user_id = #{userId,jdbcType=BIGINT},
      </if>
      <if test="orderTime != null">
        order_time = #{orderTime,jdbcType=TIMESTAMP},
      </if>
      <if test="orderNumber != null">
        order_number = #{orderNumber,jdbcType=VARCHAR},
      </if>
      <if test="orderType != null">
        order_type = #{orderType,jdbcType=VARCHAR},
      </if>
      <if test="orderState != null">
        order_state = #{orderState,jdbcType=VARCHAR},
      </if>
      <if test="businessType != null">
        business_type = #{businessType,jdbcType=VARCHAR},
      </if>
      <if test="orderAmount != null">
        order_amount = #{orderAmount,jdbcType=DECIMAL},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        created_by = #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        deleted_by = #{deletedBy,jdbcType=BIGINT},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=LONGVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.lightinit.hsdataportal.entity.Order">
    update orders
    set platform_type = #{platformType,jdbcType=VARCHAR},
      user_id = #{userId,jdbcType=BIGINT},
      order_time = #{orderTime,jdbcType=TIMESTAMP},
      order_number = #{orderNumber,jdbcType=VARCHAR},
      order_type = #{orderType,jdbcType=VARCHAR},
      order_state = #{orderState,jdbcType=VARCHAR},
      business_type = #{businessType,jdbcType=VARCHAR},
      order_amount = #{orderAmount,jdbcType=DECIMAL},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=BIGINT},
      remark = #{remark,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdataportal.entity.Order">
    update orders
    set platform_type = #{platformType,jdbcType=VARCHAR},
      user_id = #{userId,jdbcType=BIGINT},
      order_time = #{orderTime,jdbcType=TIMESTAMP},
      order_number = #{orderNumber,jdbcType=VARCHAR},
      order_type = #{orderType,jdbcType=VARCHAR},
      order_state = #{orderState,jdbcType=VARCHAR},
      business_type = #{businessType,jdbcType=VARCHAR},
      order_amount = #{orderAmount,jdbcType=DECIMAL},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="queryMyOrder" parameterType="com.lightinit.hsdataportal.model.OrderSearchModel" resultType="com.lightinit.hsdataportal.model.OrderListModel">
    SELECT o.id AS order_id,o.order_number,o.order_amount,o.order_state,o.order_time,ps.charge_method
    FROM orders o
    JOIN order_goods og
    ON og.order_id = o.id
    JOIN publish_setting ps
    ON ps.id = og.publish_setting_id
    <if test="platformType == '开放平台'.toString() ">
        LEFT JOIN `user` u
      ON u.id = o.user_id
      where u.id = #{userId}
    </if>
    <if test="platformType == '共享平台'.toString() ">
      LEFT JOIN `admin` a
      ON a.id = o.user_id
      where a.id = #{userId}
    </if>
    <!--/* ORDER BY o.id DESC*/-->
   <if test="page != null">
      LIMIT #{page.beginIndex},#{page.pageSize}
    </if>
  </select>

  <select id="queryMyOrderCount" parameterType="com.lightinit.hsdataportal.model.OrderSearchModel" resultType="java.lang.Long">
    SELECT count(*)
    FROM orders o
    <if test="platformType == '开放平台'.toString() ">
      LEFT JOIN `user` u
      ON u.id = o.user_id
      where u.id = #{userId}
    </if>
    <if test="platformType == '共享平台'.toString() ">
      LEFT JOIN `admin` a
      ON a.id = o.user_id
      where a.id = #{userId}
    </if>
  </select>
  <select id="isOrderGoodsCount" resultType="java.lang.Integer">
    SELECT count(*)
    FROM orders o
    LEFT JOIN order_goods og
    ON o.id = og.order_id
    WHERE og.publish_setting_id = #{settingId}
    AND o.user_id = #{userId}
    AND o.platform_type = #{platformType}
  </select>
  <select id="isOrderGoodsGuoQiTime" resultType="java.util.Date">
    SELECT og.expiried_date expiriedDate
    FROM orders o
    LEFT JOIN order_goods og
    ON o.id = og.order_id
    WHERE og.publish_setting_id = #{settingId}
    AND o.user_id = #{userId}
    AND o.platform_type = #{platformType}
    order by o.id Desc limit 0,1
  </select>

  <select id="selectBusinessType" resultType="com.lightinit.hsdataportal.entity.Order">
    SELECT o.`order_amount`,o.`business_type`
    FROM orders o WHERE o.`order_number` =#{orderNumber}
  </select>
</mapper>