<?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.OperationRecordMapper">
    <select id="selectOperationRecordOne" parameterType="java.util.Map"
            resultType="cn.pluss.platform.entity.OperationRecord">
        SELECT pd.`name` as dictName, pui.loginName AS loginName, por.*, pms.merchantName AS merchantName
        FROM tb_pluss_operation_record por
                 LEFT JOIN tb_pluss_dict pd
                           ON por.dictValue = pd.`value`
                 LEFT JOIN tb_pluss_user_info pui
                           ON por.operator = pui.id
                 LEFT JOIN tb_pluss_merchant_store pms
                           ON por.userId = pms.id
        where por.userId = #{id}
        ORDER BY por.createTime DESC limit #{pageSize}
        offset #{offset}
    </select>

    <select id="selectParticularsCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM tb_pluss_operation_record por
                 LEFT JOIN tb_pluss_dict pd
                           ON por.dictValue = pd.`value`
                 LEFT JOIN tb_pluss_user_info pui
                           ON por.operator = pui.id
                 LEFT JOIN tb_pluss_merchant_store pms
                           ON por.userId = pms.id
        where por.userId = #{id}
    </select>

    <select id="selectVOPage" resultType="cn.pluss.platform.vo.OperationRecordVO">
        SELECT opr.*,
               opr.extension ->> '$.isp' isp, opr.extension ->> '$.city' city, opr.extension ->> '$.province' province
                , opr.extension ->> '$.country' country, opr.extension ->> '$.county' county
                , ua.userName, mbi.address, t.count, IF(opr.extension ->> '$.country' IS NULL, -1, IF(opr.extension ->> '$.country' = '中国', '1', '0')) isChina
        FROM tb_pluss_operation_record opr
            LEFT JOIN tb_pluss_user_app ua
        ON opr.userId = ua.userId
            LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.userId = opr.userId
            LEFT JOIN (
            SELECT ip , COUNT (DISTINCT opr.userId) count FROM tb_pluss_operation_record opr
            LEFT JOIN tb_pluss_user_app ua ON ua.userId = opr.userId WHERE ua.userType != 'staff' GROUP BY ip
            ) t ON t.ip = opr.ip
            ${ew.customSqlSegment}
    </select>

    <select id="selectUserSameIpCountPage" resultType="cn.pluss.platform.vo.OperationRecordVO">
        SELECT DISTINCT opr.remark
                      , opr.ip
                      , t.count
                      , opr.createTime
                      , opr.extension ->> '$.city' city
                      , opr.extension ->> '$.province' province
                      , opr.extension ->> '$.county' county
        FROM tb_pluss_operation_record opr LEFT JOIN (
            SELECT ip, COUNT(DISTINCT opr.userId) `count` FROM tb_pluss_operation_record opr
            LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.userId = opr.userId
            LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantCode = mbi.merchantCode
            WHERE mcs.`status` = '3' AND mbi.userId = #{userId}
            GROUP BY ip
            ) t
        ON opr.ip = t.ip
        WHERE opr.userId = #{userId}
        ORDER BY opr.createTime DESC
    </select>

</mapper>