<?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.MerchantDeviceMapper">

    <insert id="saveMerchantDevice" parameterType="cn.pluss.platform.entity.MerchantDevice">
        insert into tb_pluss_merchant_device(name, type, introDesc, status, createDt, merchantCode, userId, sn,
                                             venderModel, merchantName, venderCode, orderNo)
        VALUES (#{name}, #{type}, #{introDesc}, #{status}, now(), #{merchantCode}, #{userId}, #{sn}, #{venderModel},
                #{merchantName}, #{venderCode}, #{orderNo})
    </insert>

    <insert id="saveMerchantDeviceBatch">
        insert into tb_pluss_merchant_device(name,type,introDesc,status,createDt,merchantCode,userId,sn,orderNo) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{name},#{type},#{introDesc},#{status},now(),#{merchantCode},#{userId},#{sn},#{orderNo} )
        </foreach>
    </insert>

    <select id="queryMerchantDevice" parameterType="cn.pluss.platform.entity.MerchantDevice"
            resultType="cn.pluss.platform.entity.MerchantDevice">
        SELECT * from tb_pluss_merchant_device
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null">
                and name = #{name}
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="introDesc!= null">
                and introDesc = #{introDesc}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="sn!= null">
                and sn = #{sn}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
        </where>
    </select>

    <select id="queryMerchantDeviceList" parameterType="cn.pluss.platform.entity.MerchantDevice"
            resultType="cn.pluss.platform.entity.MerchantDevice">
        SELECT * from tb_pluss_merchant_device
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null">
                and name = #{name}
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="introDesc!= null">
                and introDesc = #{introDesc}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="sn!= null">
                and sn = #{sn}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
        </where>
    </select>

    <update id="updateMerchantDevice" parameterType="cn.pluss.platform.entity.MerchantDevice">
        update tb_pluss_merchant_device
        <set>
            <if test="name!= null">
                name = #{name},
            </if>
            <if test="type!= null">
                type = #{type},
            </if>
            <if test="introDesc!= null">
                introDesc = #{introDesc},
            </if>
            <if test="status!= null">
                status = #{status},
            </if>
            <if test="createDt!= null">
                createDt = #{createDt},
            </if>
            <if test="merchantCode!= null">
                merchantCode = #{merchantCode},
            </if>
            <if test="userId!= null">
                userId = #{userId},
            </if>
            <if test="sn!= null">
                sn = #{sn},
            </if>
            <if test="venderModel!= null">
                venderModel = #{venderModel},
            </if>
            <if test="merchantName!= null">
                merchantName = #{merchantName},
            </if>
            <if test="venderCode!= null">
                venderCode = #{venderCode}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
        </set>
        where id=#{id}
    </update>

    <delete id="deleteMerchantDevice" parameterType="cn.pluss.platform.entity.MerchantDevice">
        DELETE
        FROM tb_pluss_merchant_device
        where id = #{id}
    </delete>

    <select id="queryMerchantDevicePage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.entity.MerchantDevice">
        SELECT * from tb_pluss_merchant_device
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null and name!=''">
                and name like CONCAT("%",#{name},"%")
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="introDesc!= null">
                and introDesc = #{introDesc}
            </if>
            <if test="status!= null and status!=''">
                and status = #{status}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="sn!= null">
                and sn = #{sn}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
        </where>
        order by id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryMerchantDevicePageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_merchant_device
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null and name!=''">
                and name like CONCAT("%",#{name},"%")
            </if>
            <if test="type!= null">
                and type = #{type}
            </if>
            <if test="introDesc!= null">
                and introDesc = #{introDesc}
            </if>
            <if test="status!= null and status!=''">
                and status = #{status}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="userId!= null">
                and userId = #{userId}
            </if>
            <if test="sn!= null">
                and sn = #{sn}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
        </where>
    </select>

    <select id="selectMerchantDevicePage" resultType="cn.pluss.platform.vo.DeviceVO">
        SELECT distinct md.id as id,mbi.alias as alias,pd.venderModel as venderModel,md.merchantName as
        merchantName,pd.name as name,pd.deviceLogo as deviceLogo,pd.introDesc as introDesc,dsu.soleCode as
        soleCode,dsu.orderNo as orderNo,dsu.status as status,
        md.createDt as createDt,md.userId as userId
        FROM tb_pluss_merchant_device md
        LEFT JOIN tb_pluss_device pd ON md.venderCode=pd.venderCode
        LEFT JOIN tb_pluss_device_sign_unbind dsu ON dsu.merchantCode=md.merchantCode
        LEFT JOIN tb_pluss_merchant_base_info mbi on md.merchantCode = mbi.merchantCode
        ${ew.customSqlSegment}
    </select>

    <select id="showMerchantDevicePage" resultType="cn.pluss.platform.vo.DeviceVO">
        SELECT distinct md.id as id,mbi.alias as alias,pd.venderModel as venderModel,md.merchantName as
        merchantName,pd.name as name,pd.deviceLogo as deviceLogo,pd.introDesc as introDesc,dsu.soleCode as
        soleCode,dsu.orderNo as orderNo,dsu.status as status,
        md.createDt as createDt,md.userId as userId
        FROM tb_pluss_merchant_device md
        LEFT JOIN tb_pluss_device pd ON md.venderCode=pd.venderCode
        LEFT JOIN tb_pluss_device_sign_unbind dsu ON dsu.merchantCode=md.merchantCode
        LEFT JOIN tb_pluss_merchant_base_info mbi on md.merchantCode = mbi.merchantCode
        <where>
            <if test="userId != null">
                and md.userId=#{userId}
            </if>
            <if test="alias!=null">
                and mbi.alias like concat('%',#{alias},'%')
            </if>
            <if test="merchantCode!=null">
                and md.merchantCode=#{merchantCode}
            </if>
            <if test="name!=null and name!=''">
                and pd.name like concat('%',#{name},'%')
            </if>
            <if test="merchantName!=null and merchantName!=''">
                and md.merchantName like concat('%',#{merchantName},'%')
            </if>
        </where>
        order by md.id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="showMerchantDevicePageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(*) from(
        SELECT distinct md.id as id,mbi.alias as alias,pd.venderModel as venderModel,md.merchantName,pd.name as
        name,pd.deviceLogo as deviceLogo,pd.introDesc as introDesc,dsu.soleCode as soleCode,dsu.orderNo as
        orderNo,dsu.status as status,md.createDt as createDt
        FROM tb_pluss_merchant_device md
        LEFT JOIN tb_pluss_device pd ON md.venderCode=pd.venderCode LEFT JOIN tb_pluss_device_sign_unbind dsu ON
        dsu.merchantCode=md.merchantCode LEFT JOIN tb_pluss_merchant_base_info mbi on md.merchantCode = mbi.merchantCode
        <where>
            <if test="userId!=null">
                and md.userId=#{userId}
            </if>
            <if test="alias!=null">
                and mbi.alias like concat('%',#{alias},'%')
            </if>
            <if test="merchantCode!=null">
                and md.merchantCode=#{merchantCode}
            </if>
            <if test="name!=null and name!=''">
                and pd.name like concat('%',#{name},'%')
            </if>
            <if test="merchantName!=null and merchantName!=''">
                and md.merchantName like concat('%',#{merchantName},'%')
            </if>
        </where>
        ) as showMerchant
    </select>
</mapper>
