<?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="cn.pluss.platform.mapper.DeviceSignUnbindMapper">

    <insert id="saveDeviceSignUnbind" parameterType="cn.pluss.platform.entity.DeviceSignUnbind">
        insert into tb_pluss_device_sign_unbind(orderNo, merchantCode, merchantName, mchId, status, soleCode,
                                                venderModel, sn, createDt, venderCode, reason)
        VALUES (#{orderNo}, #{merchantCode}, #{merchantName}, #{mchId}, #{status}, #{soleCode}, #{venderModel}, #{sn},
                #{createDt}, #{venderCode}, #{reason})
    </insert>

    <insert id="saveDeviceSignUnbindBatch">
        insert into tb_pluss_device_sign_unbind(orderNo,merchantCode,merchantName,mchId,status) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{orderNo},#{merchantCode},#{merchantName},#{mchId},#{status} )
        </foreach>
    </insert>

    <select id="queryDeviceSignUnbind" parameterType="cn.pluss.platform.entity.DeviceSignUnbind"
            resultType="cn.pluss.platform.entity.DeviceSignUnbind">
        SELECT * from tb_pluss_device_sign_unbind
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="mchId!= null">
                and mchId = #{mchId}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>

            <if test="soleCode!= null">
                and soleCode = #{soleCode}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="sn!= null">
                and sn = #{sn}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="reason!= null">
                and reason = #{reason}
            </if>
        </where>
    </select>

    <select id="queryDeviceSignUnbindList" parameterType="cn.pluss.platform.entity.DeviceSignUnbind"
            resultType="cn.pluss.platform.entity.DeviceSignUnbind">
        SELECT * from tb_pluss_device_sign_unbind
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="mchId!= null">
                and mchId = #{mchId}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="soleCode!= null">
                and soleCode = #{soleCode}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="sn!= null">
                and sn = #{sn}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="reason!= null">
                and reason = #{reason}
            </if>
        </where>
    </select>

    <update id="updateDeviceSignUnbind" parameterType="cn.pluss.platform.entity.DeviceSignUnbind">
        update tb_pluss_device_sign_unbind
        <set>
            <if test="orderNo!= null">
                orderNo = #{orderNo},
            </if>
            <if test="merchantCode!= null">
                merchantCode = #{merchantCode},
            </if>
            <if test="merchantName!= null">
                merchantName = #{merchantName},
            </if>
            <if test="mchId!= null">
                mchId = #{mchId},
            </if>
            <if test="status!= null">
                status = #{status},
            </if>
            <if test="soleCode!= null">
                soleCode = #{soleCode},
            </if>
            <if test="venderModel!= null">
                venderModel = #{venderModel},
            </if>
            <if test="sn!= null">
                sn = #{sn},
            </if>
            <if test="createDt!= null">
                createDt = #{createDt},
            </if>
            <if test="venderCode!= null">
                venderCode = #{venderCode},
            </if>
            <if test="reason!= null">
                reason = #{reason}
            </if>
        </set>
        where id=#{id}
    </update>

    <delete id="deleteDeviceSignUnbind" parameterType="cn.pluss.platform.entity.DeviceSignUnbind">
        DELETE
        FROM tb_pluss_device_sign_unbind
        where id = #{id}
    </delete>

    <select id="queryDeviceSignUnbindPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.entity.DeviceSignUnbind">
        SELECT su.*,mbi.userId as userId from tb_pluss_device_sign_unbind su
        left join tb_pluss_merchant_base_info mbi on su.merchantCode = mbi.merchantCode
        <where>
            <if test="id!= null">
                and su.id = #{id}
            </if>
            <if test="userId != null">
                and mbi.userId = #{userId}
            </if>
            <if test="orderNo!= null and orderNo!=''">
                and su.orderNo LIKE CONCAT('%',#{orderNo},'%')
            </if>
            <if test="merchantCode!= null">
                and su.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null and merchantName!=''">
                and su.merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="mchId!= null">
                and su.mchId = #{mchId}
            </if>
            <if test="status!= null and status!=''">
                and su.status = #{status}
            </if>
            <if test="soleCode!= null">
                and su.soleCode = #{soleCode}
            </if>
            <if test="venderModel!= null">
                and su.venderModel = #{venderModel}
            </if>
            <if test="sn!= null">
                and su.sn = #{sn}
            </if>
            <if test="createDt!= null">
                and su.createDt = #{createDt}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(su.createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="endTime!= null and endTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(su.createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="venderCode!= null">
                and su.venderCode = #{venderCode}
            </if>
            <if test="reason!= null">
                and su.reason = #{reason}
            </if>
        </where>
        order by su.id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryDeviceSignUnbindPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_device_sign_unbind su
        left join tb_pluss_merchant_base_info mbi on su.merchantCode = mbi.merchantCode
        <where>
            <if test="id!= null">
                and su.id = #{id}
            </if>
            <if test="userId != null">
                and mbi.userId = #{userId}
            </if>
            <if test="orderNo!= null and orderNo!=''">
                and su.orderNo LIKE CONCAT('%',#{orderNo},'%')
            </if>
            <if test="merchantCode!= null">
                and su.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null and merchantName!=''">
                and su.merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="mchId!= null">
                and su.mchId = #{mchId}
            </if>
            <if test="status!= null and status!=''">
                and su.status = #{status}
            </if>
            <if test="soleCode!= null">
                and su.soleCode = #{soleCode}
            </if>
            <if test="venderModel!= null">
                and su.venderModel = #{venderModel}
            </if>
            <if test="sn!= null">
                and su.sn = #{sn}
            </if>
            <if test="createDt!= null">
                and su.createDt = #{createDt}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(su.createDt, '%Y-%m-%d %H:%i:%S')>=DATE_FORMAT(#{startTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="endTime!= null and endTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(su.createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>
            <if test="venderCode!= null">
                and su.venderCode = #{venderCode}
            </if>
            <if test="reason!= null">
                and su.reason = #{reason}
            </if>
        </where>
    </select>


    <select id="selectPageWithMerchantInfo" resultType="cn.pluss.platform.entity.DeviceSignUnbind">
        SELECT su.*,mbi.userId as userId
        FROM tb_pluss_device_sign_unbind su
        LEFT JOIN tb_pluss_merchant_base_info mbi on su.merchantCode = mbi.merchantCode
    </select>

</mapper>
