<?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.xhpc.activity.mapper.XhpcClearingCheckoutMapper">
  <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcClearingCheckoutDomain">
    <id column="clearing_checkout_id" jdbcType="BIGINT" property="clearingCheckoutId" />
    <result column="clearing_bank_id" jdbcType="BIGINT" property="clearingBankId" />
    <result column="apply_amount" jdbcType="DECIMAL" property="applyAmount" />
      <result column="apply_power_amount" property="applyPowerAmount" />
      <result column="apply_service_amount" property="applyServiceAmount" />
    <result column="handling_fee" jdbcType="DECIMAL" property="handlingFee" />
      <result column="bank_fee" jdbcType="DECIMAL" property="bankFee" />
    <result column="received_amount" jdbcType="DECIMAL" property="receivedAmount" />
      <result column="received_power_amount" property="receivedPowerAmount" />
      <result column="received_service_amount" property="receivedServiceAmount" />
    <result column="status" jdbcType="INTEGER" property="status" />
      <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="operator_id" jdbcType="BIGINT" property="operatorId" />
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="check_by" jdbcType="VARCHAR" property="checkBy" />
    <result column="check_time" jdbcType="TIMESTAMP" property="checkTime" />
  </resultMap>
  <sql id="Base_Column_List">
      cc.clearing_checkout_id, cc.clearing_bank_id, cc.apply_amount, cc.apply_power_amount, cc.apply_service_amount, cc.handling_fee,
    cc.received_amount, cc.bank_fee, cc.del_flag, cc.received_power_amount, cc.received_service_amount,
    cc.`status`, cc.operator_id, cc.tenant_id, cc.create_by, cc.create_time, cc.update_by, cc.update_time, cc.check_by, cc.check_time
  </sql>


  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultType="com.xhpc.activity.domain.XhpcClearingCheckoutDomain">
    select
    <include refid="Base_Column_List" />, xo.name as 'operator_name'
    from xhpc_clearing_checkout cc
    left join xhpc_operator xo on xo.operator_id = cc.operator_id
    where cc.clearing_checkout_id = #{clearingCheckoutId,jdbcType=BIGINT}
  </select>



  <select id="selectByParams" resultType="com.xhpc.activity.domain.XhpcClearingCheckoutDomain">
    select
    <include refid="Base_Column_List" />,
           xo.name as 'operator_name',
           xt.tenant_name
    from xhpc_clearing_checkout cc
    left join xhpc_operator xo on xo.operator_id = cc.operator_id
    left join xhpc_tenant xt on xo.tenant_id=xt.tenant_id
    where cc.del_flag =0
    <if test="params.status!=null and params.status!=''">
      and cc.status=#{params.status}
    </if>
    <if test="params.tenantId!=null and params.tenantId!=''">
      and cc.tenant_id=#{params.tenantId}
    </if>
    <if test="params.operatorId!=null and params.operatorId!='' ">
      and cc.operator_id=#{params.operatorId}
    </if>
    <if test="params.startTime!=null and params.startTime!=''">
      and cc.create_time <![CDATA[ >= ]]> #{params.startTime}
    </if>
    <if test="params.endTime!=null and params.endTime!=''">
      and cc.create_time <![CDATA[ <= ]]> #{params.endTime}
    </if>
      <if test="params.type==1">
          and cc.operator_id in (select operator_id from xhpc_charging_station where charging_station_id in(select charging_station_id from xhpc_charging_station where operator_id=#{params.logOperatorId}))
      </if>
      <if test="params.type==2">
          and cc.operator_id in (select operator_id from xhpc_charging_station where charging_station_id in(select charging_station_id from xhpc_user_privilege where user_id=#{params.logOperatorId}))
      </if>
  </select>



    <select id="selectCheckoutMapByParams" resultType="map">
        select
        xt.tenant_id as 'tenantId',
        xt.tenant_name as 'tenantName',
        xo.operator_id as 'operatorId',
        xo.name as 'operatorName',
        IFNULL(ho.unClearingAmount,0) as 'unClearingAmount',
        IFNULL(cho.cashAmount,0) as 'cashAmount',
        IFNULL(cc1.applyAmount,0) as 'applyAmount',
        IFNULL(cc2.cashedAmount,0) as 'cashedAmount'
        from  xhpc_operator xo
        left join (SELECT operator_id, sum(received_amount) as 'applyAmount' from xhpc_clearing_checkout  WHERE `status`=1 and del_flag=0 GROUP BY operator_id) cc1 on xo.operator_id = cc1.operator_id
        left join (SELECT operator_id, sum(received_amount) as 'cashedAmount' from xhpc_clearing_checkout  WHERE `status`=6 and del_flag=0 GROUP BY operator_id) cc2 on xo.operator_id = cc2.operator_id
        left join xhpc_tenant xt on xo.tenant_id=xt.tenant_id
        LEFT JOIN (
        select
        ifnull(sum(ho.act_power_price + ho.act_service_price), 0) as 'unClearingAmount',
        ct.operator_id as 'operatorId'
        from xhpc_clearing_history_order as ho
        left join xhpc_charging_station as ct on ct.charging_station_id = ho.charging_station_id
        left join xhpc_operator as op on op.operator_id = ct.operator_id
        where ho.check_status not in (1,2) and ho.del_flag=0
        GROUP BY ct.operator_id
        ) ho on ho.operatorId = xo.operator_id
        LEFT JOIN (
        select
        ifnull(sum(ho.act_power_price + ho.act_service_price), 0) as 'cashAmount',
        ho.operator_id as 'operatorId'
        from xhpc_clearing_history_order as ho
        where ho.check_status=2 and ho.del_flag=0 and ho.clearing_checkout_id is null GROUP BY ho.operator_id
        ) cho on cho.operatorId= xo.operator_id
        where xo.del_flag = 0
        <if test="params.tenantId!=null and params.tenantId!=''">
            and xo.tenant_id=#{params.tenantId}
        </if>
        <if test="params.operatorId!=null and params.operatorId!='' ">
            and xo.operator_id=#{params.operatorId}
        </if>
    </select>


    <select id="selectAmountByStatus" resultType="java.math.BigDecimal">
        select ifnull(sum(received_amount), 0) from xhpc_clearing_checkout where del_flag =0 and operator_id=#{operatorId} and status=#{status}
    </select>


  <select id="selectOrderMapByCheckoutIds" resultType="com.xhpc.activity.vo.CheckoutBankVo">
    select
        'RMB' as 'curreyType',
        date_format(now(), '%Y%m%d') as 'date',
        '' as 'detailFlag',
        cc.clearing_checkout_id as 'index',
        '' as 'payBank',
        '' as 'payBankAccount',
        '' as 'payBankName',
        cb.bank_name as 'receiveBank',
        '' as 'receiveProvice',
        '' as 'receiveCity',
        '' as 'receiveCode',
        cb.bank_no as 'receiveAccount',
        cb.operator_account as 'receiveName',
        sum(cc.received_amount) as 'amount',
        '' as 'purposes',
        '' as 'remark',
        '0' as 'transType',
        '' as 'mobile',
        '' as 'customeIndex',
        '' as 'applyCheckNo'
    from xhpc_clearing_checkout cc
    left join xhpc_clearing_bank cb on cb.clearing_bank_id=cc.clearing_bank_id
    where find_in_set(cc.clearing_checkout_id, #{checkoutIds})
    group by cc.clearing_bank_id, cc.operator_id
  </select>


    <select id="selectMapGroupByStatus" resultType="java.util.Map">
        SELECT `status`, GROUP_CONCAT(clearing_checkout_id) as 'ids' from xhpc_clearing_checkout
        WHERE del_flag=0 and find_in_set(clearing_checkout_id, #{checkoutIds})
        GROUP BY status
    </select>


    <select id="selectIdsByStatusAndIds" resultType="java.lang.String">
        SELECT GROUP_CONCAT(clearing_checkout_id) as 'ids' from xhpc_clearing_checkout
        WHERE del_flag=0 and find_in_set(clearing_checkout_id, #{checkoutIds}) and status=#{status}
    </select>


    <update id="deleteLogicByPrimaryKey" parameterType="java.lang.Long">
    update xhpc_clearing_checkout set del_flag=2
    where clearing_checkout_id = #{clearingCheckoutId,jdbcType=BIGINT}
  </update>


  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from xhpc_clearing_checkout
    where clearing_checkout_id = #{clearingCheckoutId,jdbcType=BIGINT}
  </delete>


  <insert id="insert" keyColumn="clearing_checkout_id" keyProperty="clearingCheckoutId" parameterType="com.xhpc.activity.domain.XhpcClearingCheckoutDomain" useGeneratedKeys="true">
    insert into xhpc_clearing_checkout (clearing_bank_id, apply_amount, apply_power_amount, apply_service_amount,
                                        handling_fee, bank_fee,
      received_amount, received_power_amount, received_service_amount, del_flag, `status`, operator_id, tenant_id,
      create_by, create_time,
    update_by,update_time, check_by, check_time)
    values (#{clearingBankId,jdbcType=BIGINT}, #{applyAmount,jdbcType=DECIMAL}, #{applyPowerAmount}, #{applyServiceAmount},
            #{handlingFee,jdbcType=DECIMAL}, #{bankFee},
      #{receivedAmount,jdbcType=DECIMAL}, #{receivedPowerAmount}, #{receivedServiceAmount}, 0, #{status}, #{operatorId}, #{tenantId,jdbcType=VARCHAR},
      #{createBy,jdbcType=VARCHAR}, sysdate(), #{updateBy,jdbcType=VARCHAR},
            sysdate(), #{checkBy}, sysdate())
  </insert>


  <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcClearingCheckoutDomain">
    update xhpc_clearing_checkout
    set clearing_bank_id = #{clearingBankId,jdbcType=BIGINT},
      apply_amount = #{applyAmount,jdbcType=DECIMAL},
        apply_power_amount=#{applyPowerAmount},
        apply_service_amount=#{applyServiceAmount},
      handling_fee = #{handlingFee,jdbcType=DECIMAL},
        bank_fee = #{bankFee},
      received_amount = #{receivedAmount,jdbcType=DECIMAL},
        received_power_amount=#{receivedPowerAmount},
        received_service_amount=#{receivedServiceAmount}
      `status` = #{status,jdbcType=INTEGER},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = sysdate()
    where clearing_checkout_id = #{clearingCheckoutId,jdbcType=BIGINT}
  </update>


  <update id="checkByPrimaryKey">
    update xhpc_clearing_checkout
    set `status` = #{status,jdbcType=INTEGER},
        check_by = #{updateBy,jdbcType=VARCHAR},
        check_time = #{updateTime,jdbcType=TIMESTAMP}
    where clearing_checkout_id = #{clearingCheckoutId,jdbcType=BIGINT}
  </update>


  <update id="updateStatusByPrimaryKey">
    update xhpc_clearing_checkout
    set `status` = #{status,jdbcType=INTEGER},
        update_by = #{updateBy,jdbcType=VARCHAR},
        update_time = sysdate()
    where clearing_checkout_id = #{clearingCheckoutId,jdbcType=BIGINT}
  </update>


    <update id="updateStatusByIds">
        update xhpc_clearing_checkout
        set `status` = #{status}
        where find_in_set(clearing_checkout_id, #{checkoutIds})
    </update>
</mapper>