<?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.fdkankan.platform.goods.mapper.ICameraDetailMapper">


    <select id="queryListByParams" resultType="com.fdkankan.platform.user.vo.ResponseCamera">
        SELECT t.id, t.child_name, t.sn_code, u.user_name,  g.name as goods_name, t.activated_time, t.create_time as inTime, i.own, i.balance,
        co.out_type, co.agent_name, o.order_sn, co.create_time as outTime, i.country,
        i.goods_id, i.company_id, com.company_name AS companyName, t.rec_status,t.wifi_name as wifiName,
        i.camera_type as cameraType, i.order_sn as orderSn from t_camera t
        INNER JOIN t_camera_detail i ON t.id = i.camera_id
        INNER JOIN t_goods g on g.id = i.goods_id
        LEFT JOIN t_user u ON i.user_id = u.id
        LEFT JOIN t_camera_out co on t.id = co.camera_id
        LEFT JOIN t_order o on o.id = co.order_id
        LEFT JOIN t_company com ON com.id = i.company_id
        WHERE 1 = 1
        <if test=" searchKey != null and searchKey !='' " >
            and (t.child_name like concat('%',#{searchKey},'%') or u.user_name  like CONCAT('%', #{searchKey},'%'))
        </if>
        <if test=" type != null and type !='' and type == 1" >
            and (t.create_time &gt;= #{startDate} or t.create_time &lt;= #{endDate})
        </if>
        <if test=" type != null and type !='' and type == 2" >
            and (co.create_time &gt;= #{startDate} or co.create_time &lt;= #{endDate})
        </if>
        <if test=" type != null and type !='' and type == 3" >
            and (ct.activated_time &gt;= #{startDate} or t.activated_time &lt;= #{endDate})
        </if>
        <if test="orderBy != null">
            order by ${orderBy}
        </if>
        <if test="start != null and pageSize!=null">
            limit ${start}, ${pageSize}
        </if>
    </select>


    <select id="queryCountByParams" resultType="integer">
        SELECT count(*) from t_camera t
        INNER JOIN t_camera_detail i ON t.id = i.camera_id
        INNER JOIN t_goods g on g.id = i.goods_id
        LEFT JOIN t_user u ON i.user_id = u.id
        LEFT JOIN t_camera_out co on t.id = co.camera_id
        LEFT JOIN t_order o on o.id = co.order_id
        LEFT JOIN t_company com ON com.id = i.company_id
        WHERE 1 = 1
        <if test=" searchKey != null and searchKey !='' " >
            and (t.child_name like concat('%',#{searchKey},'%') or u.user_name  like CONCAT('%', #{searchKey},'%'))
        </if>
        <if test=" type != null and type !='' and type == 1" >
            and (t.create_time &gt;= #{startDate} or t.create_time &lt;= #{endDate})
        </if>
        <if test=" type != null and type !='' and type == 2" >
            and (co.create_time &gt;= #{startDate} or co.create_time &lt;= #{endDate})
        </if>
        <if test=" type != null and type !='' and type == 3" >
            and (ct.activated_time &gt;= #{startDate} or t.activated_time &lt;= #{endDate})
        </if>
    </select>

    <select id="queryCompanyCameraListByParams" resultType="com.fdkankan.platform.user.vo.ResponseCamera">
        SELECT a.id, a.child_name, a.sn_code, a.rec_status, a.create_time, b.address,
        e.user_name FROM `t_camera` a LEFT JOIN `t_camera_detail` b ON a.id = b.camera_id
        LEFT JOIN `t_company` c ON b.company_id = c.id
        LEFT JOIN `t_manager` d ON c.manager_id = d.id
        LEFT JOIN `t_user` e ON b.user_id = e.id WHERE d.username = #{userName}
        <if test=" searchKey != null and searchKey !='' " >
            and a.child_name like concat('%',#{searchKey},'%')
        </if>
        <if test="orderBy != null">
            order by ${orderBy}
        </if>
        <if test="start != null and pageSize!=null">
            limit ${start}, ${pageSize}
        </if>
    </select>


    <select id="queryCompanyCameraCountByParams" resultType="integer">
        SELECT count(*) FROM `t_camera` a LEFT JOIN `t_camera_detail` b ON a.id = b.camera_id
        LEFT JOIN `t_company` c ON b.company_id = c.id
        LEFT JOIN `t_manager` d ON c.manager_id = d.id
        LEFT JOIN `t_user` e ON b.user_id = e.id WHERE d.username = #{userName}
        <if test=" searchKey != null and searchKey !='' " >
            and a.child_name like concat('%',#{searchKey},'%')
        </if>
    </select>

    <select id="cameraList" resultType="com.fdkankan.platform.user.vo.ResponseCamera">
        SELECT a.id, a.sn_code as snCode, a.activated_time as activatedTime, b.own, b.type,
        b.agent_framework_id as agentFrameworkId, b.camera_id as cameraId, b.total_space as totalSpace,
        b.used_space as usedSpace, b.user_id as userId
        FROM t_camera a LEFT JOIN t_camera_detail b on a.id = b.camera_id
        WHERE a.rec_status = 'A'
        <if test=" param.snCode != null and param.snCode !='' " >
            and a.sn_code = #{param.snCode}
        </if>
        <if test=" param.own != null and param.own !='' " >
            and b.own = #{param.own}
        </if>
        <if test=" param.agentFrameworkId != null and param.agentFrameworkId !='' " >
            and b.agent_framework_id = #{param.agentFrameworkId}
        </if>
        <if test=" param.cameraId != null and param.cameraId !='' " >
            and b.camera_id = #{param.cameraId}
        </if>
        <if test="orderBy != null">
            order by ${orderBy}
        </if>
        <if test="start != null and pageSize!=null">
            limit ${start}, ${pageSize}
        </if>
    </select>


    <select id="cameraCount" resultType="integer">
        SELECT count(*)
        FROM t_camera a LEFT JOIN t_camera_detail b on a.id = b.camera_id
        WHERE a.rec_status = 'A'
        <if test=" param.snCode != null and param.snCode !='' " >
            and a.sn_code = #{param.snCode}
        </if>
        <if test=" param.own != null and param.own !='' " >
            and b.own = #{param.own}
        </if>
        <if test=" param.agentFrameworkId != null and param.agentFrameworkId !='' " >
            and b.agent_framework_id = #{param.agentFrameworkId}
        </if>
        <if test=" param.cameraId != null and param.cameraId !='' " >
            and b.camera_id = #{param.cameraId}
        </if>
    </select>
</mapper>
