<?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.gic.haoban.manage.service.dao.mapper.licence.TabHaobanLicenceOrderMapper">
    <resultMap type="com.gic.haoban.manage.service.entity.licence.TabHaobanLicenceOrder" id="BaseResultMap">
        <result column="order_id" property="orderId"/>
        <result column="enterprise_id" property="enterpriseId"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId"/>
        <result column="order_type" property="orderType"/>
        <result column="external_contact_count" property="externalContactCount"/>
        <result column="base_count" property="baseCount"/>
        <result column="price" property="price"/>
        <result column="time_type" property="timeType"/>
        <result column="time_value" property="timeValue"/>
        <result column="pay_type" property="payType"/>
        <result column="order_number" property="orderNumber"/>
        <result column="voucher" property="voucher"/>
        <result column="order_status" property="orderStatus"/>
        <result column="order_status_reason" property="orderStatusReason"/>
        <result column="pay_time" property="payTime"/>
        <result column="refund_time" property="refundTime"/>
        <result column="qywx_order_id" property="qywxOrderId"/>
        <result column="qywx_order_status" property="qywxOrderStatus"/>
        <result column="qywx_order_status_reason" property="qywxOrderStatusReason"/>
        <result column="qywx_pay_time" property="qywxPayTime"/>
        <result column="qywx_refund_time" property="qywxRefundTime"/>
        <result column="reason" property="reason"/>
        <result column="delete_flag" property="deleteFlag"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="creator_id" property="creatorId"/>
        <result column="creator_name" property="creatorName"/>
        <result column="expire_time" property="expireTime"/>
        <result column="prepay_id" property="prepayId"/>
        <result column="wx_order_id" property="wxOrderId"/>
        <result column="wx_url" property="wxUrl"/>
        <result column="qywx_expire_time" property="qywxExpireTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        order_id,
	  enterprise_id,
	  wx_enterprise_id,
	  order_type,
	  external_contact_count,
	  base_count,
	  price,
	  time_type,
	  time_value,
	  pay_type,
	  order_number,
	  voucher,
	  order_status,
	  order_status_reason,
	  pay_time,
	  refund_time,
	  qywx_order_id,
	  qywx_order_status,
	  qywx_order_status_reason,
	  qywx_pay_time,
	  qywx_refund_time,
	  reason,
	  delete_flag,
	  create_time,
	  update_time,
	  creator_id,
	  creator_name,
	  expire_time,
      prepay_id,
      wx_order_id,
      wx_url,
      qywx_expire_time
    </sql>
    <!-- ===================== 新增 ======================== -->
    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.licence.TabHaobanLicenceOrder"
            useGeneratedKeys="true" keyProperty="id">
  <![CDATA[
        INSERT INTO tab_haoban_licence_order(order_id,
                                             enterprise_id,
                                             wx_enterprise_id,
                                             order_type,
                                             external_contact_count,
                                             base_count,
                                             price,
                                             time_type,
                                             time_value,
                                             pay_type,
                                             order_number,
                                             voucher,
                                             order_status,
                                             order_status_reason,
                                             pay_time,
                                             refund_time,
                                             qywx_order_id,
                                             qywx_order_status,
                                             qywx_order_status_reason,
                                             qywx_pay_time,
                                             qywx_refund_time,
                                             reason,
                                             delete_flag,
                                             create_time,
                                             update_time,
                                             creator_id,
                                             creator_name,
                                             expire_time,
                                             qywx_expire_time)
        VALUES (#{orderId,jdbcType=BIGINT},
                #{enterpriseId,jdbcType=VARCHAR},
                #{wxEnterpriseId,jdbcType=VARCHAR},
                #{orderType,jdbcType=INTEGER},
                #{externalContactCount,jdbcType=INTEGER},
                #{baseCount,jdbcType=INTEGER},
                #{price,jdbcType=INTEGER},
                #{timeType,jdbcType=INTEGER},
                #{timeValue,jdbcType=INTEGER},
                #{payType,jdbcType=INTEGER},
                #{orderNumber,jdbcType=CHAR},
                #{voucher,jdbcType=VARCHAR},
                #{orderStatus,jdbcType=INTEGER},
                #{orderStatusReason,jdbcType=VARCHAR},
                #{payTime,jdbcType=TIMESTAMP},
                #{refundTime,jdbcType=TIMESTAMP},
                #{qywxOrderId,jdbcType=VARCHAR},
                #{qywxOrderStatus,jdbcType=INTEGER},
                #{qywxOrderStatusReason,jdbcType=VARCHAR},
                #{qywxPayTime,jdbcType=TIMESTAMP},
                #{qywxRefundTime,jdbcType=TIMESTAMP},
                #{reason,jdbcType=VARCHAR},
                0,
                now(),
                now(),
                #{creatorId,jdbcType=VARCHAR},
                #{creatorName,jdbcType=VARCHAR},
                #{expireTime,jdbcType=TIMESTAMP},
                #{qywxExpireTime,jdbcType=TIMESTAMP})
        ]]>
  </insert>

    <!-- =====================删除==================== -->
    <update id="deleteById" parameterType="long">
        UPDATE tab_haoban_licence_order
        SET delete_flag = 1 ,update_time = now()
        WHERE order_id = #{orderId}
    </update>


    <!-- ==================更新 ========== -->
    <!--  <update id="update" parameterType="com.gic.haoban.manage.service.entity.licence.TabHaobanLicenceOrder">
          UPDATE tab_haoban_licence_order SET
              order_id=#{orderId,jdbcType=BIGINT},
              enterprise_id=#{enterpriseId,jdbcType=VARCHAR},
              wx_enterprise_id=#{wxEnterpriseId,jdbcType=VARCHAR},
              order_type=#{orderType,jdbcType=INTEGER},
              external_contact_count=#{externalContactCount,jdbcType=INTEGER},
              base_count=#{baseCount,jdbcType=INTEGER},
              price=#{price,jdbcType=INTEGER},
              time_type=#{timeType,jdbcType=INTEGER},
              time_value=#{timeValue,jdbcType=INTEGER},
              pay_type=#{payType,jdbcType=INTEGER},
              transaction_id=#{transactionId,jdbcType=CHAR},
              voucher=#{voucher,jdbcType=VARCHAR},
              order_status=#{orderStatus,jdbcType=INTEGER},
              order_status_reason=#{orderStatusReason,jdbcType=VARCHAR},
              pay_time=#{payTime,jdbcType=TIMESTAMP},
              refund_time=#{refundTime,jdbcType=TIMESTAMP},
              qywx_order_id=#{qywxOrderId,jdbcType=VARCHAR},
              qywx_order_status=#{qywxOrderStatus,jdbcType=INTEGER},
              qywx_order_status_reason=#{qywxOrderStatusReason,jdbcType=VARCHAR},
              qywx_pay_time=#{qywxPayTime,jdbcType=TIMESTAMP},
              qywx_refund_time=#{qywxRefundTime,jdbcType=TIMESTAMP},
              reason=#{reason,jdbcType=VARCHAR},
              delete_flag=#{deleteFlag,jdbcType=INTEGER},
              create_time=#{createTime,jdbcType=TIMESTAMP},
              update_time=#{updateTime,jdbcType=TIMESTAMP},
              creator_id=#{creatorId,jdbcType=VARCHAR},
              creator_name=#{creatorName,jdbcType=VARCHAR}
            where order_id = #{orderId}
      </update>-->

    <!-- ============ 查询=============-->
    <select id="selectById" parameterType="long" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tab_haoban_licence_order WHERE order_id = #{orderId} and delete_flag=0
    </select>


    <update id="updateLicenceOrderType">
        UPDATE tab_haoban_licence_order
        SET order_status = #{type} ,update_time = now()
        WHERE order_id = #{orderId}
          and delete_flag = 0
    </update>

    <update id="updateQywxOrderType">
        UPDATE tab_haoban_licence_order
        SET qywx_order_status = #{type} ,update_time = now()
        WHERE order_id = #{orderId}
          and delete_flag = 0
    </update>

    <update id="uploadLicenceOrderVoucher">
        UPDATE tab_haoban_licence_order
        SET voucher = #{voucher} ,update_time = now(),pay_time = now(),order_status=5
        WHERE order_id = #{orderId}
          and delete_flag = 0
    </update>

    <select id="selectByWxEnterpriseId"  resultType="java.lang.String">
        SELECT order_id
        FROM tab_haoban_licence_order
        WHERE wx_enterprise_id = #{wxEnterpriseId}
          and delete_flag = 0
          and qywx_order_status = 0 limit 1
    </select>

    <select id="getLicenceOrderPage"  resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tab_haoban_licence_order
        WHERE  delete_flag = 0
        <if test="orderNumber != null and orderNumber !='' ">
            and order_number=#{orderNumber}
        </if>
        <if test="orderStatus != null">
            and order_status=#{orderStatus}
        </if>
        <if test="qywxOrderId != null and qywxOrderId !=''">
            and qywx_order_id=#{qywxOrderId}
        </if>
        <if test="qywxOrderStatus != null and qywxOrderStatus != 0">
            and qywx_order_status=#{qywxOrderStatus}
        </if>
        <if test="qywxOrderStatus == 0">
            and (qywx_order_status=#{qywxOrderStatus} or qywx_order_status is null)
        </if>
        <if test="startTime != null and startTime !='' ">
            and create_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime !='' ">
            and create_time <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="enterpriseIdParam != null and enterpriseIdParam !='' ">
            and enterprise_id=#{enterpriseIdParam}
        </if>
        <if test="payType != null  ">
            and pay_type=#{payType}
        </if>
        <if test="wxOrderId != null and wxOrderId !='' ">
            and wx_order_id=#{wxOrderId}
        </if>
        <if test="type != null and type ==1 ">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        order by create_time desc
    </select>

    <update id="saveTransactionCode">
        UPDATE tab_haoban_licence_order
        SET
        <if test="orderNumber != null">
            order_number = #{orderNumber},
        </if>
        <if test="prepayId != null">
            prepay_id = #{prepayId},
        </if>
        <if test="wxOrderId != null">
            wx_order_id = #{wxOrderId},
        </if>
        <if test="payTime != null">
            pay_time = #{payTime},
        </if>
        <if test="wxUrl != null">
            wx_url = #{wxUrl},
        </if>
        update_time = now()
        WHERE order_id = #{orderId}
          and delete_flag = 0
    </update>

    <select id="selectByTransactionId"  resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tab_haoban_licence_order WHERE order_number = #{orderNumber} and delete_flag=0
    </select>

    <select id="selectByQywxOrderId"  resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tab_haoban_licence_order WHERE qywx_order_id = #{qywxOrderId} and delete_flag=0
    </select>

    <update id="saveQywxOrderId">
        UPDATE tab_haoban_licence_order
        SET qywx_order_id = #{qywxOrderId} ,update_time = now() ,qywx_expire_time= #{qywxExpireTime}
        WHERE order_id = #{orderId}
          and delete_flag = 0
    </update>

    <select id="getTransactionIdList"  resultMap="BaseResultMap">
        SELECT
            order_number
        FROM tab_haoban_licence_order WHERE order_number like concat (#{orderNumber},'%') and delete_flag=0
        <if test="wxEnterpriseId != null and wxEnterpriseId !='' ">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
    </select>

    <select id="getQywxOrderId"  resultMap="BaseResultMap">
        SELECT
            qywx_order_id
        FROM tab_haoban_licence_order WHERE qywx_order_id like concat (#{qywxOrderId},'%') and delete_flag=0
        <if test="wxEnterpriseId != null and wxEnterpriseId !='' ">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
    </select>

    <select id="getWxOrderId"  resultMap="BaseResultMap">
        SELECT
            wx_order_id
        FROM tab_haoban_licence_order WHERE wx_order_id like concat (#{wxOrderId},'%') and delete_flag=0
        <if test="wxEnterpriseId != null and wxEnterpriseId !='' ">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
    </select>

    <update id="saveOrderCheckReason">
        UPDATE tab_haoban_licence_order
        SET reason = #{reason} ,update_time = now(),order_status_reason = '审核未通过',order_status = #{type}
        WHERE order_id = #{orderId}
          and delete_flag = 0
    </update>

    <update id="saveCallBackTime">
        UPDATE tab_haoban_licence_order
        SET
        <if test="type == 0">
            pay_time = #{time},
        </if>
        <if test="type == 1">
            refund_time = #{time},
        </if>
        <if test="type == 2">
            qywx_pay_time = #{time},
        </if>
        <if test="type == 3">
            qywx_refund_time = #{time},
        </if>
        update_time = now()
        WHERE order_id = #{orderId}
          and delete_flag = 0
    </update>

    <select id="selectCountByWxEnterpriseId"  resultType="java.lang.Integer">
        SELECT count(*)
        FROM tab_haoban_licence_order
        WHERE wx_enterprise_id = #{wxEnterpriseId}
          and delete_flag = 0 and order_status = 1
    </select>

</mapper> 