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

    <insert id="saveDeviceMerchantBuy" parameterType="cn.pluss.platform.entity.DeviceMerchantBuy">
        insert into tb_pluss_device_merchant_buy(orderNo, transNo, merchantCode, merchantName, venderModel, status,
                                                 payCode, payName, createDt, transDt, refundNo, refundDt, expressNo,
                                                 provinces, address, orderAmount, quantity, linkName, linkPhone,
                                                 venderCode, email, snNo,logistNo)
        VALUES (#{orderNo}, #{transNo}, #{merchantCode}, #{merchantName}, #{venderModel}, #{status}, #{payCode},
                #{payName}, now(), #{transDt}, #{refundNo}, #{refundDt}, #{expressNo}, #{provinces}, #{address},
                #{orderAmount}, #{quantity}, #{linkName}, #{linkPhone}, #{venderCode}, #{email}, #{snNo},#{logistNo})
    </insert>

    <insert id="saveDeviceMerchantBuyBatch">
        insert into
        tb_pluss_device_merchant_buy(orderNo,transNo,merchantCode,merchantName,venderModel,status,payCode,payName,createDt,transDt,refundNo,snNo,logistNo)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{orderNo},#{transNo},#{merchantCode},#{merchantName},#{venderModel},#{status},#{payCode},#{payName},#{createDt},#{transDt},#{refundNo},#{snNo},#{logistNo})
        </foreach>
    </insert>

    <select id="queryDeviceMerchantBuy" parameterType="cn.pluss.platform.entity.DeviceMerchantBuy"
            resultType="cn.pluss.platform.entity.DeviceMerchantBuy">
        SELECT * from tb_pluss_device_merchant_buy
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
            <if test="transNo!= null">
                and transNo = #{transNo}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="payCode!= null">
                and payCode = #{payCode}
            </if>
            <if test="payName!= null">
                and payName = #{payName}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="transDt!= null">
                and transDt = #{transDt}
            </if>
            <if test="refundNo!= null">
                and refundNo = #{refundNo}
            </if>
            <if test="refundDt!= null">
                and refundDt = #{refundDt}
            </if>
            <if test="expressNo!= null">
                and expressNo = #{expressNo}
            </if>
            <if test="provinces!= null">
                and provinces = #{provinces}
            </if>
            <if test="address!= null">
                and address = #{address}
            </if>
            <if test="orderAmount!= null">
                and orderAmount = #{orderAmount}
            </if>

            <if test="quantity!= null">
                and quantity = #{quantity}
            </if>
            <if test="linkName!= null">
                and linkName = #{linkName}
            </if>
            <if test="linkPhone!= null">
                and linkPhone = #{linkPhone}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="email!= null">
                and email = #{email}
            </if>
            <if test="actStatus != null">
                and actStatus = #{actStatus}
            </if>
            <if test="snNo != null">
                and snNo = #{snNo}
            </if>

            <if test="logistNo != null">
                and logistNo = #{logistNo}
            </if>

        </where>
    </select>

    <select id="queryDeviceMerchantBuyList" parameterType="cn.pluss.platform.entity.DeviceMerchantBuy"
            resultType="cn.pluss.platform.entity.DeviceMerchantBuy">
        SELECT * from tb_pluss_device_merchant_buy
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="orderNo!= null">
                and orderNo = #{orderNo}
            </if>
            <if test="transNo!= null">
                and transNo = #{transNo}
            </if>
            <if test="merchantCode!= null">
                and merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null">
                and merchantName = #{merchantName}
            </if>
            <if test="venderModel!= null">
                and venderModel = #{venderModel}
            </if>
            <if test="status!= null">
                and status = #{status}
            </if>
            <if test="payCode!= null">
                and payCode = #{payCode}
            </if>
            <if test="payName!= null">
                and payName = #{payName}
            </if>
            <if test="createDt!= null">
                and createDt = #{createDt}
            </if>
            <if test="startTime != null">
                and createDt > #{startTime}
            </if>
            <if test="endTime != null">
                and createDt <![CDATA[ <= ]]> #{endTime}
            </if>
            <if test="transDt!= null">
                and transDt = #{transDt}
            </if>
            <if test="refundNo!= null">
                and refundNo = #{refundNo}
            </if>
            <if test="refundDt!= null">
                and refundDt = #{refundDt}
            </if>
            <if test="expressNo!= null">
                and expressNo = #{expressNo}
            </if>
            <if test="provinces!= null">
                and provinces = #{provinces}
            </if>
            <if test="address!= null">
                and address = #{address}
            </if>
            <if test="orderAmount!= null">
                and orderAmount = #{orderAmount}
            </if>
            <if test="quantity!= null">
                and quantity = #{quantity}
            </if>
            <if test="linkName!= null">
                and linkName = #{linkName}
            </if>
            <if test="linkPhone!= null">
                and linkPhone = #{linkPhone}
            </if>
            <if test="venderCode!= null">
                and venderCode = #{venderCode}
            </if>
            <if test="email!= null">
                and email = #{email}
            </if>
            <if test="actStatus!= null">
                and actStatus = #{actStatus}
            </if>
            <if test="snNo!= null">
                and snNo = #{snNo}
            </if>
            <if test="logistNo!= null">
                and logistNo = #{logistNo}
            </if>

        </where>
    </select>

    <update id="updateDeviceMerchantBuy" parameterType="cn.pluss.platform.entity.DeviceMerchantBuy">
        update tb_pluss_device_merchant_buy
        <set>
            <if test="orderNo!= null">
                orderNo = #{orderNo},
            </if>
            <if test="transNo!= null">
                transNo = #{transNo},
            </if>
            <if test="merchantCode!= null">
                merchantCode = #{merchantCode},
            </if>
            <if test="merchantName!= null">
                merchantName = #{merchantName},
            </if>
            <if test="venderModel!= null">
                venderModel = #{venderModel},
            </if>
            <if test="status!= null">
                status = #{status},
            </if>
            <if test="payCode!= null">
                payCode = #{payCode},
            </if>
            <if test="payName!= null">
                payName = #{payName},
            </if>
            <if test="createDt!= null">
                createDt = #{createDt},
            </if>
            <if test="transDt!= null">
                transDt = #{transDt},
            </if>
            <if test="refundNo!= null">
                refundNo = #{refundNo},
            </if>
            <if test="refundDt!= null">
                refundDt = #{refundDt},
            </if>

            <if test="expressNo!= null">
                expressNo = #{expressNo},
            </if>
            <if test="provinces!= null">
                provinces = #{provinces},
            </if>
            <if test="address!= null">
                address = #{address},
            </if>
            <if test="orderAmount!= null">
                orderAmount = #{orderAmount},
            </if>
            <if test="quantity!= null">
                quantity = #{quantity},
            </if>
            <if test="linkName!= null">
                linkName = #{linkName},
            </if>
            <if test="linkPhone!= null">
                linkPhone = #{linkPhone},
            </if>
            <if test="venderCode!= null">
                venderCode = #{venderCode},
            </if>
            <if test="email!= null">
                email = #{email},
            </if>
            <if test="actStatus!= null">
                actStatus = #{actStatus},
            </if>
            <if test="snNo!= null">
                snNo = #{snNo},
            </if>
            <if test="logistNo!= null">
                logistNo = #{logistNo}
            </if>

        </set>
        where id=#{id}
    </update>

    <delete id="deleteDeviceMerchantBuy" parameterType="cn.pluss.platform.entity.DeviceMerchantBuy">
        DELETE
        FROM tb_pluss_device_merchant_buy
        where id = #{id}
    </delete>

    <select id="queryDeviceMerchantBuyPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.entity.DeviceMerchantBuy">
        SELECT dmb.*,mbi.alias,doi.userId, dg.`name`,doi.deviceType
        from tb_pluss_device_merchant_buy dmb
        LEFT JOIN tb_pluss_merchant_base_info mbi on mbi.merchantCode = dmb.merchantCode
        LEFT JOIN tb_pluss_device_order_info doi ON doi.orderNo = dmb.orderNo
        LEFT JOIN tb_pluss_qr_code qc on dmb.orderNo = qc.orderNo
        LEFT JOIN tb_pluss_device_detail dd ON dd.type = doi.deviceType AND dd.`code` = doi.`code`
        LEFT JOIN tb_pluss_device_goods dg ON dg.`code` = dd.`code`
        <where>
            <if test="id!= null">
                and dmb.id = #{id}
            </if>
            <if test="alias!=null">
                and mbi.alias like concat("%",#{alias},"%")
            </if>
            <if test="userId != null and userId != ''">
                and mbi.userId = #{userId}
            </if>
            <if test="orderNo!= null and orderNo!=''">
                and dmb.orderNo LIKE CONCAT('%',#{orderNo},'%')
            </if>
            <if test="transNo!= null and transNo!=''">
                and dmb.transNo LIKE CONCAT('%',#{transNo},'%')
            </if>
            <if test="merchantCode!= null">
                and dmb.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null and merchantName!=''">
                and dmb.merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="venderModel!= null">
                and dmb.venderModel = #{venderModel}
            </if>
            <if test="status!= null and status!=''">
                and dmb.status = #{status}
            </if>
            <if test="payCode!= null">
                and dmb.payCode = #{payCode}
            </if>
            <if test="payName!= null">
                and dmb.payName = #{payName}
            </if>
            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(dmb.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(dmb.createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>

            <if test="createDt!= null">
                and dmb.createDt = #{createDt}
            </if>
            <if test="transDt!= null">
                and dmb.transDt = #{transDt}
            </if>
            <if test="refundNo!= null">
                and dmb.refundNo = #{refundNo}
            </if>
            <if test="refundDt!= null">
                and dmb.refundDt = #{refundDt}
            </if>
            <if test="expressNo!= null">
                and dmb.expressNo = #{expressNo}
            </if>
            <if test="provinces!= null">
                and dmb.provinces = #{provinces}
            </if>
            <if test="address!= null">
                and dmb.address = #{address}
            </if>
            <if test="orderAmount!= null">
                and dmb.orderAmount = #{orderAmount}
            </if>
            <if test="quantity!= null">
                and dmb.quantity = #{quantity}
            </if>
            <if test="linkName!= null">
                and dmb.linkName = #{linkName}
            </if>
            <if test="linkPhone!= null">
                and dmb.linkPhone = #{linkPhone}
            </if>
            <if test="venderCode!= null">
                and dmb.venderCode = #{venderCode}
            </if>
            <if test="email!= null">
                and dmb.email = #{email}
            </if>
            <if test="actStatus != null ">
                and dmb.actStatus = #{actStatus}
            </if>
            <if test="snNo != null">
                and dmb.snNo = #{snNo}
            </if>

        </where>
        order by id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryDeviceMerchantBuyPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_device_merchant_buy dmb LEFT JOIN tb_pluss_merchant_base_info mbi on
        mbi.merchantCode = dmb.merchantCode
        <where>
            <if test="id!= null">
                and dmb.id = #{id}
            </if>
            <if test="alias!= null">
                and mbi.alias like concat("%",#{alias},"%")
            </if>

            <if test="orderNo!= null and orderNo!=''">
                and dmb.orderNo LIKE CONCAT('%',#{orderNo},'%')
            </if>
            <if test="transNo!= null and transNo!=''">
                and dmb.transNo LIKE CONCAT('%',#{transNo},'%')
            </if>
            <if test="merchantCode!= null">
                and dmb.merchantCode = #{merchantCode}
            </if>
            <if test="merchantName!= null and merchantName!=''">
                and dmb.merchantName LIKE CONCAT('%',#{merchantName},'%')
            </if>
            <if test="venderModel!= null">
                and dmb.venderModel = #{venderModel}
            </if>
            <if test="status!= null and status!=''">
                and dmb.status = #{status}
            </if>
            <if test="payCode!= null">
                and dmb.payCode = #{payCode}
            </if>
            <if test="payName!= null">
                and dmb.payName = #{payName}
            </if>

            <if test="startTime!= null and startTime!=''">
                <![CDATA[
                            and  DATE_FORMAT(dmb.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(dmb.createDt, '%Y-%m-%d %H:%i:%S')<=DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%i:%S')
                         ]]>
            </if>

            <if test="createDt!= null">
                and dmb.createDt = #{createDt}
            </if>
            <if test="transDt!= null">
                and dmb.transDt = #{transDt}
            </if>
            <if test="refundNo!= null">
                and dmb.refundNo = #{refundNo}
            </if>
            <if test="refundDt!= null">
                and dmb.refundDt = #{refundDt}
            </if>
            <if test="expressNo!= null">
                and dmb.expressNo = #{expressNo}
            </if>
            <if test="provinces!= null">
                and dmb.provinces = #{provinces}
            </if>
            <if test="address!= null">
                and dmb.address = #{address}
            </if>
            <if test="orderAmount!= null">
                and dmb.orderAmount = #{orderAmount}
            </if>
            <if test="quantity!= null">
                and dmb.quantity = #{quantity}
            </if>
            <if test="linkName!= null">
                and dmb.linkName = #{linkName}
            </if>
            <if test="linkPhone!= null">
                and dmb.linkPhone = #{linkPhone}
            </if>
            <if test="venderCode!= null">
                and dmb.venderCode = #{venderCode}
            </if>
            <if test="email!= null">
                and dmb.email = #{email}
            </if>
            <if test="actStatus != null ">
                and dmb.actStatus = #{actStatus}
            </if>
            <if test="snNo != null">
                and dmb.snNo = #{snNo}
            </if>

        </where>
    </select>

    <select id="deviceOrderShowPage" parameterType="java.util.Map"
            resultType="cn.pluss.platform.vo.DeviceMerchantBuyVO">
        SELECT dmb.orderNo as orderNo,(dmb.orderAmount / dmb.quantity) as unitPrice,dmb.transNo as
        transNo,dmb.orderAmount as orderAmount,dmb.quantity as quantity,dmb.merchantName as merchantName,dmb.venderModel
        as venderModel,dmb.payCode as payCode,dmb.payName as payName,
        dmb.createDt as createDt,dmb.transDt as transDt,dmb.provinces as provinces,dmb.address as address,dmb.linkName
        aslinkName,dmb.linkPhone as linkPhone,dmb.expressNo as expressNo,
        dmb.actStatus as actStatus,dmb.snNo as snNo,
        IFNULL(pd.deviceLogo, dg.deviceLogo) as deviceLogo,
        IFNULL(pd.introDesc, dg.introDesc) as introDesc,
        IFNULL(pd.aisleModel, dd.aisleModel) as aisleModel,
        IFNULL(pd.`name`, dg.`name`) as name,
        IFNULL(pd.venderName, dd.`venderName`) as venderName,
        doi.orderType
        FROM tb_pluss_device_merchant_buy dmb
        LEFT JOIN tb_pluss_device_order_info doi ON dmb.orderNo = doi.orderNo
        LEFT JOIN tb_pluss_device pd ON dmb.venderCode=pd.venderCode
        LEFT JOIN tb_pluss_merchant_base_info mbi ON dmb.merchantCode = mbi.merchantCode
        LEFT JOIN tb_pluss_user_app ua ON doi.userId = ua.userId
        LEFT JOIN tb_pluss_user_app ua2 ON mbi.userId = ua2.userId
        LEFT JOIN tb_pluss_device_detail dd ON dd.code = doi.code AND dd.type = doi.deviceType
        LEFT JOIN tb_pluss_device_goods dg ON dg.code = doi.code
        <!-- 此处防止有的非商户订单 -->
        WHERE (ua.userId = #{userId} OR ua2.userId = #{userId})
        <if test="status!=null and status!=''">
            and dmb.`status`= #{status}
        </if>
        order by dmb.id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="deviceOrderShowPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(dmb.id)
        FROM tb_pluss_device_merchant_buy dmb
        LEFT JOIN tb_pluss_device pd ON dmb.venderCode=pd.venderCode
        LEFT JOIN tb_pluss_merchant_base_info mbi ON dmb.merchantCode=mbi.merchantCode
        LEFT JOIN tb_pluss_user_app ua ON mbi.userId=ua.userId
        WHERE ua.userId=#{userId}
        <if test="status!=null and status!=''">
            and dmb.`status`=#{status}
        </if>
    </select>

    <select id="selectDeviceMerchantBuyPage" resultType="cn.pluss.platform.entity.DeviceMerchantBuy">
        SELECT dmb.*, mbi.alias, doi.userId, dg.`name`, doi.deviceType
        from tb_pluss_device_merchant_buy dmb
                 LEFT JOIN tb_pluss_merchant_base_info mbi on mbi.merchantCode = dmb.merchantCode
                 LEFT JOIN tb_pluss_device_order_info doi ON doi.orderNo = dmb.orderNo
                 LEFT JOIN tb_pluss_device_detail dd ON dd.type = doi.deviceType AND dd.`code` = doi.`code`
                 LEFT JOIN tb_pluss_device_goods dg ON dg.`code` = dd.`code`
            ${ew.customSqlSegment}
    </select>

</mapper>
