<?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.chaozhanggui.dao.system.dao.TbPlussMerchantChannelStatusMapper">
  <resultMap id="BaseResultMap" type="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="channel" jdbcType="INTEGER" property="channel" />
    <result column="merchantCode" jdbcType="VARCHAR" property="merchantcode" />
    <result column="merchantId" jdbcType="VARCHAR" property="merchantid" />
    <result column="settlementType" jdbcType="VARCHAR" property="settlementtype" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="thirdStatus" jdbcType="VARCHAR" property="thirdstatus" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="createTime" jdbcType="TIMESTAMP" property="createtime" />
    <result column="updateTime" jdbcType="TIMESTAMP" property="updatetime" />
    <result column="applicationId" jdbcType="VARCHAR" property="applicationid" />
    <result column="callbackStatus" jdbcType="VARCHAR" property="callbackstatus" />
    <result column="valid" jdbcType="INTEGER" property="valid" />
    <result column="authorizationStatus" jdbcType="VARCHAR" property="authorizationstatus" />
    <result column="mercName" jdbcType="VARCHAR" property="mercname" />
    <result column="virChannelFlag" jdbcType="VARCHAR" property="virchannelflag" />
    <result column="virStatusFlag" jdbcType="VARCHAR" property="virstatusflag" />
    <result column="srcMerchantNo" jdbcType="VARCHAR" property="srcmerchantno" />
    <result column="interfaceVersion" jdbcType="VARCHAR" property="interfaceversion" />
    <result column="aliAuthorizationStatus" jdbcType="VARCHAR" property="aliauthorizationstatus" />
    <result column="wxCertUrl" jdbcType="VARCHAR" property="wxcerturl" />
    <result column="aliCertUrl" jdbcType="VARCHAR" property="alicerturl" />
    <result column="auditSuccessTime" jdbcType="TIMESTAMP" property="auditsuccesstime" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    <result column="extra" jdbcType="LONGVARCHAR" property="extra" />
  </resultMap>
  <sql id="Base_Column_List">
    id, channel, merchantCode, merchantId, settlementType, status, thirdStatus, remark, 
    createTime, updateTime, applicationId, callbackStatus, valid, authorizationStatus, 
    mercName, virChannelFlag, virStatusFlag, srcMerchantNo, interfaceVersion, aliAuthorizationStatus, 
    wxCertUrl, aliCertUrl, auditSuccessTime
  </sql>
  <sql id="Blob_Column_List">
    extra
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from tb_pluss_merchant_channel_status
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from tb_pluss_merchant_channel_status
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    insert into tb_pluss_merchant_channel_status (id, channel, merchantCode, 
      merchantId, settlementType, status, 
      thirdStatus, remark, createTime, 
      updateTime, applicationId, callbackStatus, 
      valid, authorizationStatus, mercName, 
      virChannelFlag, virStatusFlag, srcMerchantNo, 
      interfaceVersion, aliAuthorizationStatus, 
      wxCertUrl, aliCertUrl, auditSuccessTime, 
      extra)
    values (#{id,jdbcType=INTEGER}, #{channel,jdbcType=INTEGER}, #{merchantcode,jdbcType=VARCHAR}, 
      #{merchantid,jdbcType=VARCHAR}, #{settlementtype,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, 
      #{thirdstatus,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, 
      #{updatetime,jdbcType=TIMESTAMP}, #{applicationid,jdbcType=VARCHAR}, #{callbackstatus,jdbcType=VARCHAR}, 
      #{valid,jdbcType=INTEGER}, #{authorizationstatus,jdbcType=VARCHAR}, #{mercname,jdbcType=VARCHAR}, 
      #{virchannelflag,jdbcType=VARCHAR}, #{virstatusflag,jdbcType=VARCHAR}, #{srcmerchantno,jdbcType=VARCHAR}, 
      #{interfaceversion,jdbcType=VARCHAR}, #{aliauthorizationstatus,jdbcType=VARCHAR}, 
      #{wxcerturl,jdbcType=VARCHAR}, #{alicerturl,jdbcType=VARCHAR}, #{auditsuccesstime,jdbcType=TIMESTAMP}, 
      #{extra,jdbcType=LONGVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    insert into tb_pluss_merchant_channel_status
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="channel != null">
        channel,
      </if>
      <if test="merchantcode != null">
        merchantCode,
      </if>
      <if test="merchantid != null">
        merchantId,
      </if>
      <if test="settlementtype != null">
        settlementType,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="thirdstatus != null">
        thirdStatus,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="createtime != null">
        createTime,
      </if>
      <if test="updatetime != null">
        updateTime,
      </if>
      <if test="applicationid != null">
        applicationId,
      </if>
      <if test="callbackstatus != null">
        callbackStatus,
      </if>
      <if test="valid != null">
        valid,
      </if>
      <if test="authorizationstatus != null">
        authorizationStatus,
      </if>
      <if test="mercname != null">
        mercName,
      </if>
      <if test="virchannelflag != null">
        virChannelFlag,
      </if>
      <if test="virstatusflag != null">
        virStatusFlag,
      </if>
      <if test="srcmerchantno != null">
        srcMerchantNo,
      </if>
      <if test="interfaceversion != null">
        interfaceVersion,
      </if>
      <if test="aliauthorizationstatus != null">
        aliAuthorizationStatus,
      </if>
      <if test="wxcerturl != null">
        wxCertUrl,
      </if>
      <if test="alicerturl != null">
        aliCertUrl,
      </if>
      <if test="auditsuccesstime != null">
        auditSuccessTime,
      </if>
      <if test="extra != null">
        extra,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="channel != null">
        #{channel,jdbcType=INTEGER},
      </if>
      <if test="merchantcode != null">
        #{merchantcode,jdbcType=VARCHAR},
      </if>
      <if test="merchantid != null">
        #{merchantid,jdbcType=VARCHAR},
      </if>
      <if test="settlementtype != null">
        #{settlementtype,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=VARCHAR},
      </if>
      <if test="thirdstatus != null">
        #{thirdstatus,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null">
        #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="updatetime != null">
        #{updatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="applicationid != null">
        #{applicationid,jdbcType=VARCHAR},
      </if>
      <if test="callbackstatus != null">
        #{callbackstatus,jdbcType=VARCHAR},
      </if>
      <if test="valid != null">
        #{valid,jdbcType=INTEGER},
      </if>
      <if test="authorizationstatus != null">
        #{authorizationstatus,jdbcType=VARCHAR},
      </if>
      <if test="mercname != null">
        #{mercname,jdbcType=VARCHAR},
      </if>
      <if test="virchannelflag != null">
        #{virchannelflag,jdbcType=VARCHAR},
      </if>
      <if test="virstatusflag != null">
        #{virstatusflag,jdbcType=VARCHAR},
      </if>
      <if test="srcmerchantno != null">
        #{srcmerchantno,jdbcType=VARCHAR},
      </if>
      <if test="interfaceversion != null">
        #{interfaceversion,jdbcType=VARCHAR},
      </if>
      <if test="aliauthorizationstatus != null">
        #{aliauthorizationstatus,jdbcType=VARCHAR},
      </if>
      <if test="wxcerturl != null">
        #{wxcerturl,jdbcType=VARCHAR},
      </if>
      <if test="alicerturl != null">
        #{alicerturl,jdbcType=VARCHAR},
      </if>
      <if test="auditsuccesstime != null">
        #{auditsuccesstime,jdbcType=TIMESTAMP},
      </if>
      <if test="extra != null">
        #{extra,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    update tb_pluss_merchant_channel_status
    <set>
      <if test="channel != null">
        channel = #{channel,jdbcType=INTEGER},
      </if>
      <if test="merchantcode != null">
        merchantCode = #{merchantcode,jdbcType=VARCHAR},
      </if>
      <if test="merchantid != null">
        merchantId = #{merchantid,jdbcType=VARCHAR},
      </if>
      <if test="settlementtype != null">
        settlementType = #{settlementtype,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=VARCHAR},
      </if>
      <if test="thirdstatus != null">
        thirdStatus = #{thirdstatus,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null">
        createTime = #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="updatetime != null">
        updateTime = #{updatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="applicationid != null">
        applicationId = #{applicationid,jdbcType=VARCHAR},
      </if>
      <if test="callbackstatus != null">
        callbackStatus = #{callbackstatus,jdbcType=VARCHAR},
      </if>
      <if test="valid != null">
        valid = #{valid,jdbcType=INTEGER},
      </if>
      <if test="authorizationstatus != null">
        authorizationStatus = #{authorizationstatus,jdbcType=VARCHAR},
      </if>
      <if test="mercname != null">
        mercName = #{mercname,jdbcType=VARCHAR},
      </if>
      <if test="virchannelflag != null">
        virChannelFlag = #{virchannelflag,jdbcType=VARCHAR},
      </if>
      <if test="virstatusflag != null">
        virStatusFlag = #{virstatusflag,jdbcType=VARCHAR},
      </if>
      <if test="srcmerchantno != null">
        srcMerchantNo = #{srcmerchantno,jdbcType=VARCHAR},
      </if>
      <if test="interfaceversion != null">
        interfaceVersion = #{interfaceversion,jdbcType=VARCHAR},
      </if>
      <if test="aliauthorizationstatus != null">
        aliAuthorizationStatus = #{aliauthorizationstatus,jdbcType=VARCHAR},
      </if>
      <if test="wxcerturl != null">
        wxCertUrl = #{wxcerturl,jdbcType=VARCHAR},
      </if>
      <if test="alicerturl != null">
        aliCertUrl = #{alicerturl,jdbcType=VARCHAR},
      </if>
      <if test="auditsuccesstime != null">
        auditSuccessTime = #{auditsuccesstime,jdbcType=TIMESTAMP},
      </if>
      <if test="extra != null">
        extra = #{extra,jdbcType=LONGVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    update tb_pluss_merchant_channel_status
    set channel = #{channel,jdbcType=INTEGER},
      merchantCode = #{merchantcode,jdbcType=VARCHAR},
      merchantId = #{merchantid,jdbcType=VARCHAR},
      settlementType = #{settlementtype,jdbcType=VARCHAR},
      status = #{status,jdbcType=VARCHAR},
      thirdStatus = #{thirdstatus,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      createTime = #{createtime,jdbcType=TIMESTAMP},
      updateTime = #{updatetime,jdbcType=TIMESTAMP},
      applicationId = #{applicationid,jdbcType=VARCHAR},
      callbackStatus = #{callbackstatus,jdbcType=VARCHAR},
      valid = #{valid,jdbcType=INTEGER},
      authorizationStatus = #{authorizationstatus,jdbcType=VARCHAR},
      mercName = #{mercname,jdbcType=VARCHAR},
      virChannelFlag = #{virchannelflag,jdbcType=VARCHAR},
      virStatusFlag = #{virstatusflag,jdbcType=VARCHAR},
      srcMerchantNo = #{srcmerchantno,jdbcType=VARCHAR},
      interfaceVersion = #{interfaceversion,jdbcType=VARCHAR},
      aliAuthorizationStatus = #{aliauthorizationstatus,jdbcType=VARCHAR},
      wxCertUrl = #{wxcerturl,jdbcType=VARCHAR},
      aliCertUrl = #{alicerturl,jdbcType=VARCHAR},
      auditSuccessTime = #{auditsuccesstime,jdbcType=TIMESTAMP},
      extra = #{extra,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    update tb_pluss_merchant_channel_status
    set channel = #{channel,jdbcType=INTEGER},
      merchantCode = #{merchantcode,jdbcType=VARCHAR},
      merchantId = #{merchantid,jdbcType=VARCHAR},
      settlementType = #{settlementtype,jdbcType=VARCHAR},
      status = #{status,jdbcType=VARCHAR},
      thirdStatus = #{thirdstatus,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      createTime = #{createtime,jdbcType=TIMESTAMP},
      updateTime = #{updatetime,jdbcType=TIMESTAMP},
      applicationId = #{applicationid,jdbcType=VARCHAR},
      callbackStatus = #{callbackstatus,jdbcType=VARCHAR},
      valid = #{valid,jdbcType=INTEGER},
      authorizationStatus = #{authorizationstatus,jdbcType=VARCHAR},
      mercName = #{mercname,jdbcType=VARCHAR},
      virChannelFlag = #{virchannelflag,jdbcType=VARCHAR},
      virStatusFlag = #{virstatusflag,jdbcType=VARCHAR},
      srcMerchantNo = #{srcmerchantno,jdbcType=VARCHAR},
      interfaceVersion = #{interfaceversion,jdbcType=VARCHAR},
      aliAuthorizationStatus = #{aliauthorizationstatus,jdbcType=VARCHAR},
      wxCertUrl = #{wxcerturl,jdbcType=VARCHAR},
      aliCertUrl = #{alicerturl,jdbcType=VARCHAR},
      auditSuccessTime = #{auditsuccesstime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectByUserId" resultType="java.util.Map">

    SELECT
      b.userId,
      b.merchantType,
      case b.merchantType when '1' then '小微'
                          when '2' then '个体'
                          when '3' then '企业'
        end as merchantTypeName,
      b.alias,
      c.channelCode,
      c.channelName ,
      s.`status`,
      s.merchantId,
      s.updateTime

    FROM
      tb_pluss_merchant_channel_status s
        LEFT JOIN tb_pluss_merchant_base_info b ON s.merchantCode = b.merchantCode
        left join tb_pluss_merchant_channel c on s.channel=c.id
    where b.userId=#{userId} and  s.channel=#{channel}
  </select>

  <select id="getByMerchantCode" resultMap="BaseResultMap">
    select * from tb_pluss_merchant_channel_status where merchantCode=#{merchantCode}
    <choose>
      <when test="channel !=null and channel !=''">
        and channel=#{channel}
      </when>
      <otherwise>
        and channel is null
      </otherwise>
    </choose>
  </select>

  <select id="getValidData" resultMap="BaseResultMap">
    select * from tb_pluss_merchant_channel_status where merchantCode=#{merchantCode} and (`status`=3 or `status`=6) order by valid desc, id asc limit 1

  </select>
    <select id="getByMerchantCodeAndChannelType" resultType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
      select * from tb_pluss_merchant_channel_status
      where merchantCode=#{merchantCode}
        and virChannelFlag=#{virChannelFlag}
        and authorizationStatus="1"
      order by valid desc, id asc limit 1
    </select>
    <select id="selectCount" resultType="java.lang.Integer">
      SELECT COUNT( id )
      FROM tb_pluss_merchant_channel_status
      <where>
       <if test="status !=null and status !=''">
         status = #{status}
       </if>
       <if test="thirdstatus !=null and thirdstatus !=''">
         and thirdstatus = #{thirdstatus}
       </if>
      </where>
      order by id desc
    </select>
  <select id="getByMerchantCodeList"
          resultType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    select * from tb_pluss_merchant_channel_status
    where merchantCode=#{merchantCode}
    order by valid desc
    limit 1
  </select>
  <select id="selectByMerchantCode"
          resultType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    SELECT * FROM tb_pluss_merchant_channel_status
    WHERE merchantCode=#{merchantCode}
    AND channelId=#{channelId}
  </select>
  <select id="getIncomingInfo" resultType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    SELECT *
    FROM tb_pluss_merchant_channel_status
    WHERE merchantCode = #{merchantCode}
      AND status IN ('AUDIT_STATUS_SUCCESS', 'AUDIT_STATUS_DATA_EDIT')
    ORDER BY valid DESC, id ASC
      LIMIT 1;
  </select>
  <select id="getIncomingInfoAnother"
          resultType="com.chaozhanggui.dao.system.entity.TbPlussMerchantChannelStatus">
    SELECT *
    FROM merchant_channel_status
    WHERE merchant_code = #{merchantCode}
    ORDER BY valid DESC, id ASC
    LIMIT 1;
  </select>
</mapper>