<?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.XhpcTradebillPaymentCheckRecordMapper">
    <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_by" jdbcType="BIGINT" property="createBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_by" jdbcType="BIGINT" property="updateBy"/>
        <result column="upload_id" jdbcType="INTEGER" property="uploadId"/>
        <result column="order_type" jdbcType="SMALLINT" property="orderType"/>
        <result column="payment_channel" jdbcType="SMALLINT" property="paymentChannel"/>
        <result column="platform_order_number" jdbcType="BIGINT" property="platformOrderNumber"/>
        <result column="platform_pay_number" jdbcType="VARCHAR" property="platformPayNumber"/>
        <result column="platform_user_id" jdbcType="VARCHAR" property="platformUserId"/>
        <result column="platform_amount" jdbcType="DECIMAL" property="platformAmount"/>
        <result column="platform_pay_time" jdbcType="TIMESTAMP" property="platformPayTime"/>
        <result column="payment_pay_number" jdbcType="VARCHAR" property="paymentPayNumber"/>
        <result column="payment_user_id" jdbcType="VARCHAR" property="paymentUserId"/>
        <result column="payment_amount" jdbcType="DECIMAL" property="paymentAmount"/>
        <result column="payment_pay_time" jdbcType="TIMESTAMP" property="paymentPayTime"/>
        <result column="status" jdbcType="SMALLINT" property="status"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
    </resultMap>
    <sql id="Base_Column_List">
        id, create_time, create_by, update_time, update_by, upload_id, order_type, payment_channel,
    platform_order_number, platform_pay_number, platform_user_id, platform_amount, platform_pay_time,
    payment_pay_number, payment_user_id, payment_amount, payment_pay_time, `status`, remark, tenant_id
    </sql>


    <select id="selectByParams" resultType="java.util.Map">
        select
        <include refid="Base_Column_List"/>
        from xhpc_tradebill_payment_check_record
        <where>
            <if test="params.uploadId != null and params.uploadId != ''">
                and upload_id = #{params.uploadId}
            </if>
            <if test="params.paymentChannel == 1">
                and payment_channel = 1 and order_type=1
            </if>
            <if test="params.paymentChannel == 2">
                and payment_channel = 1 and order_type=2
            </if>
            <if test="params.paymentChannel == 3">
                and payment_channel = 2 and order_type=1
            </if>
            <if test="params.paymentChannel == 4">
                and payment_channel = 2 and order_type=2
            </if>
            <if test="params.checkStatus != null and params.checkStatus != ''">
                and status = #{params.checkStatus}
            </if>
        </where>
    </select>
    <select id="selectListByParams"
            resultType="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain">
        select
        p.*, cu.user_name as 'create_name', uu.user_name as 'update_name',
        case when p.status=0 then '未对账' when p.status=1 then '对账错误' when p.status=2 then '对账正确' else '未知' end as
        'status_name'
        from xhpc_tradebill_payment_check_record p
        left join sys_user cu on cu.user_id=p.create_by
        left join sys_user uu on uu.user_id=p.update_by
        <where>
            <if test="params.uploadId!=null and params.uploadId!='' and params.uploadId!=0">
                and p.upload_id = #{params.uploadId}
            </if>
            <if test="params.orderNumber != null and params.orderNumber !=''">
                and p.payment_pay_number like concat('%', #{params.orderNumber},'%')
            </if>
            <if test="params.status != null and params.status !=''">
                and p.status=#{params.status}
            </if>
            <if test="params.tenantId != null and params.tenantId !=''">
                and p.tenant_id=#{params.tenantId}
            </if>
        </where>
        order by p.upload_id desc, status asc
    </select>

    <select id="selectTotalGroupbyStatus" resultType="java.util.Map">
        select
        status, count(id) as 'total'
        from xhpc_tradebill_payment_check_record
        <where>
            <if test="params.uploadId!=null and params.uploadId!='' and params.uploadId!=0">
                and upload_id = #{params.uploadId}
            </if>
            <if test="params.status!=null and params.status!=''">
                and status = #{params.status}
            </if>
            <if test="params.paymentPayNumber!=null and params.paymentPayNumber!=''">
                and payment_pay_number like concat('%', #{params.orderNumber},'%')
            </if>
            <if test="params.tenantId!=null and params.tenantId!=''">
                and tenant_id=#{params.tenantId}
            </if>
        </where>
        group by status
    </select>


    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from xhpc_tradebill_payment_check_record
        where id = #{id}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete
        from xhpc_tradebill_payment_check_record
        where id = #{id}
    </delete>

    <insert id="insert" keyColumn="id" keyProperty="id"
            parameterType="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain" useGeneratedKeys="true">
        insert into xhpc_tradebill_payment_check_record (create_time, create_by, update_time,
                                                         update_by, upload_id, order_type,
                                                         payment_channel, platform_order_number, platform_pay_number,
                                                         platform_user_id, platform_amount, platform_pay_time,
                                                         payment_pay_number, payment_user_id, payment_amount,
                                                         payment_pay_time, `status`, remark, tenant_id)
        values (now(), #{createBy}, now(),
                #{updateBy}, #{uploadId}, #{orderType},
                #{paymentChannel}, #{platformOrderNumber},
                #{platformPayNumber},
                #{platformUserId}, #{platformAmount},
                #{platformPayTime},
                #{paymentPayNumber}, #{paymentUserId},
                #{paymentAmount},
                #{paymentPayTime}, #{status}, #{remark}, #{tenantId})
    </insert>

    <insert id="insertCheckList" keyColumn="id" keyProperty="id"
            parameterType="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain" useGeneratedKeys="true">
        insert into xhpc_tradebill_payment_check_record (create_time, create_by, update_time,
        update_by, upload_id, order_type,
        payment_channel, platform_order_number, platform_pay_number,
        platform_user_id, platform_amount, platform_pay_time,
        payment_pay_number, payment_user_id, payment_amount,
        payment_pay_time, `status`, remark, tenant_id) values
        <foreach collection="list" item="check" index="index" separator=",">
            (now(), #{check.createBy}, now(),
            #{check.updateBy}, #{check.uploadId},
            #{check.orderType},
            #{check.paymentChannel}, #{check.platformOrderNumber},
            #{check.platformPayNumber},
            #{check.platformUserId}, #{check.platformAmount},
            #{check.platformPayTime},
            #{check.paymentPayNumber}, #{check.paymentUserId},
            #{check.paymentAmount},
            #{check.paymentPayTime}, #{check.status},
            #{check.remark}, #{check.tenantId})
        </foreach>
    </insert>


    <insert id="insertSelective" keyColumn="id" keyProperty="id"
            parameterType="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain" useGeneratedKeys="true">
        insert into xhpc_tradebill_payment_check_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            create_time,
            <if test="createBy != null">
                create_by,
            </if>
            update_time,
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="uploadId != null">
                upload_id,
            </if>
            <if test="orderType != null">
                order_type,
            </if>
            <if test="paymentChannel != null">
                payment_channel,
            </if>
            <if test="platformOrderNumber != null">
                platform_order_number,
            </if>
            <if test="platformPayNumber != null">
                platform_pay_number,
            </if>
            <if test="platformUserId != null">
                platform_user_id,
            </if>
            <if test="platformAmount != null">
                platform_amount,
            </if>
            <if test="platformPayTime != null">
                platform_pay_time,
            </if>
            <if test="paymentPayNumber != null">
                payment_pay_number,
            </if>
            <if test="paymentUserId != null">
                payment_user_id,
            </if>
            <if test="paymentAmount != null">
                payment_amount,
            </if>
            <if test="paymentPayTime != null">
                payment_pay_time,
            </if>
            <if test="status != null">
                `status`,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="tenantId != null">
                tenant_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            now(),
            <if test="createBy != null">
                #{createBy},
            </if>
            now(),
            <if test="updateBy != null">
                #{updateBy},
            </if>
            <if test="uploadId != null">
                #{uploadId},
            </if>
            <if test="orderType != null">
                #{orderType},
            </if>
            <if test="paymentChannel != null">
                #{paymentChannel},
            </if>
            <if test="platformOrderNumber != null">
                #{platformOrderNumber},
            </if>
            <if test="platformPayNumber != null">
                #{platformPayNumber},
            </if>
            <if test="platformUserId != null">
                #{platformUserId},
            </if>
            <if test="platformAmount != null">
                #{platformAmount},
            </if>
            <if test="platformPayTime != null">
                #{platformPayTime},
            </if>
            <if test="paymentPayNumber != null">
                #{paymentPayNumber},
            </if>
            <if test="paymentUserId != null">
                #{paymentUserId},
            </if>
            <if test="paymentAmount != null">
                #{paymentAmount},
            </if>
            <if test="paymentPayTime != null">
                #{paymentPayTime},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="remark != null">
                #{remark},
            </if>
            <if test="tenantId != null">
                #{tenantId},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective"
            parameterType="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain">
        update xhpc_tradebill_payment_check_record
        <set>
            update_time = now(),
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
            <if test="uploadId != null">
                upload_id = #{uploadId},
            </if>
            <if test="orderType != null">
                order_type = #{orderType},
            </if>
            <if test="paymentChannel != null">
                payment_channel = #{paymentChannel},
            </if>
            <if test="platformOrderNumber != null">
                platform_order_number = #{platformOrderNumber},
            </if>
            <if test="platformPayNumber != null">
                platform_pay_number = #{platformPayNumber},
            </if>
            <if test="platformUserId != null">
                platform_user_id = #{platformUserId},
            </if>
            <if test="platformAmount != null">
                platform_amount = #{platformAmount},
            </if>
            <if test="platformPayTime != null">
                platform_pay_time = #{platformPayTime},
            </if>
            <if test="paymentPayNumber != null">
                payment_pay_number = #{paymentPayNumber},
            </if>
            <if test="paymentUserId != null">
                payment_user_id = #{paymentUserId},
            </if>
            <if test="paymentAmount != null">
                payment_amount = #{paymentAmount},
            </if>
            <if test="paymentPayTime != null">
                payment_pay_time = #{paymentPayTime},
            </if>
            <if test="status != null">
                `status` = #{status},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain">
        update xhpc_tradebill_payment_check_record
        set update_time           = now(),
            update_by             = #{updateBy},
            upload_id             = #{uploadId},
            order_type            = #{orderType},
            payment_channel       = #{paymentChannel},
            platform_order_number = #{platformOrderNumber},
            platform_pay_number   = #{platformPayNumber},
            platform_user_id      = #{platformUserId},
            platform_amount       = #{platformAmount},
            platform_pay_time     = #{platformPayTime},
            payment_pay_number    = #{paymentPayNumber},
            payment_user_id       = #{paymentUserId},
            payment_amount        = #{paymentAmount},
            payment_pay_time      = #{paymentPayTime},
            `status`              = #{status},
            remark                = #{remark}
        where id = #{id}
    </update>

    <update id="updateStatusByPk" parameterType="com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain">
        update xhpc_tradebill_payment_check_record
        set update_time = now(),
            update_by   = #{updateBy},
            `status`    = #{status},
            remark      = #{remark}
        where id = #{id}
    </update>


</mapper>