<?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.zcxk.dao.DeviceMapper">

    <sql id="Base_Column_List">
            d.id
            ,d.TENANT_ID as "tenantId"
            ,d.DEVICE_CODE as "deviceCode"
            ,d.DEVICE_NAME as "deviceName"
            ,d.DEVICE_TYPE_ID as "deviceTypeId"
            ,d.COMPANY_ORG_ID as "companyOrgId"
            ,d.DEPT_ORG_ID as "deptOrgId"
            ,d.POINT_X as "pointX"
            ,d.POINT_Y as "pointY"
            ,d.ENABLE_STATE as "enableState"
            ,d.LAST_UPDATE_TIME as "lastUpdateTime"
            ,d.ADDRESS as "address"
            ,d.REMARK as "remark"
            ,d.`STATUS` as "status"
            ,d.DATE_CREATE as "dateCreate"
            ,d.CREATE_BY as "createBy"
            ,d.DATE_UPDATE as "dateUpdate"
            ,d.UPDATE_BY as "updateBy"
            ,t.MANUFACTURER_NAME as "manufacturerName"
            ,t.MANUFACTURER_ID as "manufacturerId"
            ,t.DEVICE_TYPE as "deviceType"
            ,t.DEVICE_MODE as "deviceMode"
    </sql>
    <sql id="deviceJoins">
        left join sms_device_type t on d.DEVICE_TYPE_ID = t.id
    </sql>
    <select id="selectById" resultType="com.zcxk.dto.DeviceDto">
        select
        <include refid="Base_Column_List"/>
        from sms_device d
        <include refid="deviceJoins"/>
        where d.ID = #{id} and d.status = 1
    </select>

    <select id="selectByIds" resultType="com.zcxk.dto.DeviceDto">
        select
        <include refid="Base_Column_List"/>
        from sms_device d
        <include refid="deviceJoins"/>
        where d.status = 1
        and d.id  in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.dto.DeviceDto">
        select
        distinct
        <include refid="Base_Column_List"/>
        <if test="deviceDto.sceneId!= null and deviceDto.sceneId != 0 ">
            ,s.PARENT_SCENE_ID as "parentSceneId"
            ,s.SCENE_ID as "sceneId"
        </if>
        from sms_device d
        left join sms_device_type t on d.DEVICE_TYPE_ID = t.id
        <if test="deviceDto.sceneId!= null and deviceDto.sceneId != 0 ">
           <if test="deviceDto.searchType == null or deviceDto.searchType == 0 ">
            inner join sms_device_scene s on s.DEVICE_ID=d.ID and (s.SCENE_ID=#{deviceDto.sceneId} or s.parent_scene_id = #{deviceDto.sceneId}) and s.status=1
           </if>
            <if test="deviceDto.searchType != null and deviceDto.searchType == 1 ">
                inner join sms_device_scene s on s.DEVICE_ID=d.ID and s.SCENE_ID=#{deviceDto.sceneId} and s.status=1
            </if>
        </if>
        <if test="deviceDto.sceneIds!= null and deviceDto.sceneIds.size()>0 ">
            inner join sms_device_scene s on s.DEVICE_ID=d.ID and s.status=1 and s.SCENE_ID in
           <foreach collection="deviceDto.sceneIds" item="item" open="(" close=")" separator=",">
              #{item}
          </foreach>
        </if>
        where d.status = 1
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
            and d.tenant_id = #{deviceDto.tenantId}
        </if>
        <if test="deviceDto.condition != null and deviceDto.condition != ''">
            and (device_code like concat('%', #{deviceDto.condition},'%') or device_name like concat('%', #{deviceDto.condition},'%'))
        </if>
        <if test="deviceDto.companyOrgId != null and deviceDto.companyOrgId != 0 ">
            and d.COMPANY_ORG_ID=#{deviceDto.companyOrgId}
        </if>
        <if test="deviceDto.deptOrgId != null and deviceDto.deptOrgId != 0 ">
            and d.DEPT_ORG_ID =#{deviceDto.deptOrgId}
        </if>
        <if test="deviceDto.enableState != null ">
            and d.ENABLE_STATE =#{deviceDto.enableState}
        </if>

        <if test="deviceDto.userType!=null and deviceDto.userType!=-999 and deviceDto.userType!=-9999 and  deviceDto.programItems != null and deviceDto.programItems.size() > 0">
            <if test="deviceDto.permissonType == 5 or deviceDto.permissonType == 2">
                and ( d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="deviceDto.permissonType == 4 or deviceDto.permissonType == 3">
                and d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="deviceDto.permissonType == 1">
                and d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (d.DEPT_ORG_ID is null or d.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectListForReport" resultType="com.zcxk.dto.DeviceDto">
        select
        distinct
        <include refid="Base_Column_List"/>
        <if test="deviceDto.sceneId!= null and deviceDto.sceneId != 0 ">
            ,s.PARENT_SCENE_ID as "parentSceneId"
            ,s.SCENE_ID as "sceneId"
        </if>
        from sms_device d
        left join sms_device_type t on d.DEVICE_TYPE_ID = t.id

        <if test="deviceDto.sceneId!= null and deviceDto.sceneId != 0 ">
            <if test="deviceDto.searchType == null or deviceDto.searchType == 0 ">
                inner join sms_device_scene s on s.DEVICE_ID=d.ID and (s.SCENE_ID=#{deviceDto.sceneId} or s.parent_scene_id = #{deviceDto.sceneId}) and s.status=1
            </if>
            <if test="deviceDto.searchType != null and deviceDto.searchType == 1 ">
                inner join sms_device_scene s on s.DEVICE_ID=d.ID and s.SCENE_ID=#{deviceDto.sceneId} and s.status=1
            </if>
        </if>
        <if test="deviceDto.sceneIds!= null and deviceDto.sceneIds.size()>0 ">
            inner join sms_device_scene s on s.DEVICE_ID=d.ID and s.status=1 and s.SCENE_ID in
            <foreach collection="deviceDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        inner join sms_device_parm dp on dp.DEVICE_ID=d.id and dp.`STATUS`=1 and  dp.IS_REPORT=1
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
          and dp.TENANT_ID= #{deviceDto.tenantId}
        </if>
        <if test="deviceDto.sceneIds!= null and deviceDto.sceneIds.size()>0 ">
          and dp.SCENE_ID in
            <foreach collection="deviceDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        inner join sms_report_dimension_setting rds on rds.device_parm_id=dp.id and rds.type=1
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
          and rds.user_id= #{deviceDto.userId}
        </if>
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
          and rds.tenant_id = #{deviceDto.tenantId}
        </if>
        and rds.show_on_off=1
        where d.status = 1
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
            and d.tenant_id = #{deviceDto.tenantId}
        </if>
        <if test="deviceDto.condition != null and deviceDto.condition != ''">
            and (device_code like concat('%', #{deviceDto.condition},'%') or device_name like concat('%', #{deviceDto.condition},'%'))
        </if>
        <if test="deviceDto.companyOrgId != null and deviceDto.companyOrgId != 0 ">
            and d.COMPANY_ORG_ID=#{deviceDto.companyOrgId}
        </if>
        <if test="deviceDto.deptOrgId != null and deviceDto.deptOrgId != 0 ">
            and d.DEPT_ORG_ID =#{deviceDto.deptOrgId}
        </if>
        <if test="deviceDto.enableState != null ">
            and d.ENABLE_STATE =#{deviceDto.enableState}
        </if>

        <if test="deviceDto.userType!=null and deviceDto.userType!=-999 and deviceDto.userType!=-9999 and  deviceDto.programItems != null and deviceDto.programItems.size() > 0">
            <if test="deviceDto.permissonType == 5 or deviceDto.permissonType == 2">
                and ( d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="deviceDto.permissonType == 4 or deviceDto.permissonType == 3">
                and d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="deviceDto.permissonType == 1">
                and d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (d.DEPT_ORG_ID is null or d.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>
    <select id="selectListBySceneId" resultType="com.zcxk.dto.DeviceDto">
        select
        d.id
        ,d.DEVICE_NAME as "deviceName"
        from sms_device d
        inner join sms_device_scene s on s.DEVICE_ID=d.ID and s.SCENE_ID=#{deviceDto.sceneId} and s.status=1
        where d.status = 1 and d.ENABLE_STATE = 1
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
            and d.tenant_id = #{deviceDto.tenantId}
        </if>

        <if test="deviceDto.userType!=null and deviceDto.userType!=-999 and deviceDto.userType!=-9999 and  deviceDto.programItems != null and deviceDto.programItems.size() > 0">
            <if test="deviceDto.permissonType == 5 or deviceDto.permissonType == 2">
                and ( d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="deviceDto.permissonType == 4 or deviceDto.permissonType == 3">
                and d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="deviceDto.permissonType == 1">
                and d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (d.DEPT_ORG_ID is null or d.DEPT_ORG_ID =0)
            </if>
        </if>
    </select>


    <select id="selectListByScene" resultType="com.zcxk.dto.DeviceDto">
        select
         d.id
        ,d.TENANT_ID as "tenantId"
        ,d.DEVICE_CODE as "deviceCode"
        ,d.DEVICE_NAME as "deviceName"
        ,d.DEVICE_TYPE_ID as "deviceTypeId"
        ,d.COMPANY_ORG_ID as "companyOrgId"
        ,d.DEPT_ORG_ID as "deptOrgId"
        ,d.POINT_X as "pointX"
        ,d.POINT_Y as "pointY"
        ,d.ENABLE_STATE as "enableState"
        ,d.LAST_UPDATE_TIME as "lastUpdateTime"
        ,d.ADDRESS as "address"
        ,d.REMARK as "remark"
        ,d.`STATUS` as "status"
        ,d.DATE_CREATE as "dateCreate"
        ,d.CREATE_BY as "createBy"
        ,d.DATE_UPDATE as "dateUpdate"
        ,d.UPDATE_BY as "updateBy"
        ,t.MANUFACTURER_NAME as "manufacturerName"
        ,t.MANUFACTURER_ID as "manufacturerId"
        ,t.DEVICE_TYPE as "deviceType"
        ,t.DEVICE_MODE as "deviceMode"
        ,s.PARENT_SCENE_ID as "parentSceneId"
        ,s.SCENE_ID as "sceneId"
        ,case when s.ID is null then 0 else 1 end as "isCheck"
        from sms_device d
        left join sms_device_type t on d.DEVICE_TYPE_ID = t.id
        left join sms_device_scene s on s.DEVICE_ID=d.ID and s.SCENE_ID=#{deviceDto.sceneId} and s.status=1
        where d.status = 1
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
            and d.tenant_id = #{deviceDto.tenantId}
        </if>
        <if test="deviceDto.condition != null and deviceDto.condition != ''">
            and (device_code like concat('%', #{deviceDto.condition},'%') or device_name like concat('%', #{deviceDto.condition},'%'))
        </if>
        <if test="deviceDto.companyOrgId != null and deviceDto.companyOrgId != 0 ">
            and d.COMPANY_ORG_ID=#{deviceDto.companyOrgId}
        </if>
        <if test="deviceDto.deptOrgId != null and deviceDto.deptOrgId != 0 ">
            and d.DEPT_ORG_ID =#{deviceDto.deptOrgId}
        </if>
        <if test="deviceDto.deptOrgId == null or deviceDto.deptOrgId == 0 ">
            and d.DEPT_ORG_ID is null
        </if>

        <if test="deviceDto.enableState != null ">
            and d.ENABLE_STATE =#{deviceDto.enableState}
        </if>

        <if test="deviceDto.userType!=null and deviceDto.userType!=-999 and deviceDto.userType!=-9999 and  deviceDto.programItems != null and deviceDto.programItems.size() > 0">
            <if test="deviceDto.permissonType == 5 or deviceDto.permissonType == 2">
                and ( d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="deviceDto.permissonType == 4 or deviceDto.permissonType == 3">
                and d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="deviceDto.permissonType == 1">
                and d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (d.DEPT_ORG_ID is null or d.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>
    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO  sms_device ( TENANT_ID ,  DEVICE_CODE ,  DEVICE_NAME ,  DEVICE_TYPE_ID ,
        <if test="companyOrgId != null and companyOrgId != 0">COMPANY_ORG_ID , </if>
        <if test="deptOrgId != null and deptOrgId != 0">DEPT_ORG_ID ,</if>
        <if test="sceneId != null and sceneId != 0">SCENE_ID ,</if>
        POINT_X ,  POINT_Y ,  ENABLE_STATE ,  LAST_UPDATE_TIME ,  ADDRESS ,  REMARK ,  STATUS ,  DATE_CREATE ,  CREATE_BY ,  DATE_UPDATE ,  UPDATE_BY )
        VALUES ( #{tenantId} ,#{deviceCode} ,#{deviceName}  ,#{deviceTypeId} ,
        <if test="companyOrgId != null and companyOrgId != 0">#{companyOrgId}  , </if>
        <if test="deptOrgId != null and deptOrgId != 0"> #{deptOrgId} , </if>
        <if test="sceneId != null and sceneId != 0">#{sceneId} , </if>
        #{pointX} ,#{pointY} ,#{enableState} ,#{lastUpdateTime} ,#{address} ,#{remark} ,#{status} ,#{dateCreate} ,#{createBy} ,#{dateUpdate} ,#{updateBy})
   </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        INSERT INTO  sms_device ( TENANT_ID ,  DEVICE_CODE ,  DEVICE_NAME ,  DEVICE_TYPE_ID ,  COMPANY_ORG_ID ,  DEPT_ORG_ID ,  SCENE_ID ,  POINT_X ,  POINT_Y ,  ENABLE_STATE ,  LAST_UPDATE_TIME ,  ADDRESS ,  REMARK ,  STATUS ,  DATE_CREATE ,  CREATE_BY ,  DATE_UPDATE ,  UPDATE_BY )
        values
        <foreach collection="devices" item="item" index="index" separator=",">
            ( #{item.tenantId} ,#{item.deviceCode} ,#{item.deviceName}  ,#{item.deviceTypeId} ,#{item.companyOrgId} ,#{item.deptOrgId} ,#{item.sceneId} ,#{item.pointX} ,#{item.pointY} ,#{item.enableState} ,#{item.lastUpdateTime} ,#{item.address} ,#{item.remark} ,#{item.status} ,#{item.dateCreate} ,#{item.createBy} ,#{item.dateUpdate} ,#{item.updateBy})
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        UPDATE  sms_device
        <set>
        <if test="tenantId != null and tenantId != '' ">
            TENANT_ID  = #{tenantId},
        </if>
        <if test="deviceCode != null ">
            DEVICE_CODE  = #{deviceCode},
        </if>
        <if test="deviceName != null and deviceName != '' ">
            DEVICE_NAME  = #{deviceName},
        </if>
        <if test="deviceTypeId != null and deviceTypeId != 0">
            DEVICE_TYPE_ID  = #{deviceTypeId},
        </if>
        <if test="companyOrgId != null and companyOrgId != 0">
            COMPANY_ORG_ID  = #{companyOrgId},
        </if>
            DEPT_ORG_ID  = #{deptOrgId},
        <if test="sceneId != null  and sceneId != 0">
            SCENE_ID  = #{sceneId},
        </if>
        <if test="pointX != null ">
            POINT_X  = #{pointX},
        </if>
        <if test="pointY != null ">
            POINT_Y  = #{pointY},
        </if>
        <if test="enableState != null ">
            ENABLE_STATE  = #{enableState},
        </if>
        <if test="lastUpdateTime != null ">
            LAST_UPDATE_TIME  = #{lastUpdateTime},
        </if>
            ADDRESS  = #{address},
            REMARK  = #{remark},
        <if test="status != null ">
            STATUS  = #{status},
        </if>
        <if test="dateUpdate != null ">
            DATE_UPDATE  = #{dateUpdate},
        </if>
        <if test="updateBy != null and updateBy != ''">
            UPDATE_BY  = #{updateBy}
        </if>
        </set>
        WHERE
             ID  = #{id};
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
         UPDATE  sms_device set STATUS  = 0 where id = #{id}
    </delete>

    <!--通过主键删除-->
    <delete id="batchDelete">
         UPDATE  sms_device set STATUS  = 0
         where id  in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>

    <!-- 分页查询 -->
    <select id="selectPage" resultType="com.zcxk.dto.DeviceDto">
        select
        <include refid="Base_Column_List"/>
        <if test="deviceDto.sceneId!= null and deviceDto.sceneId != 0 ">
        ,s.PARENT_SCENE_ID as "parentSceneId"
        ,s.SCENE_ID as "sceneId"
        ,ss.SCENE_NAME as "sceneName"
        </if>
        from sms_device d
        left join sms_device_type t on d.DEVICE_TYPE_ID = t.id
        <if test="deviceDto.sceneId!= null and deviceDto.sceneId != 0 ">
            inner join sms_device_scene s on s.DEVICE_ID=d.ID and s.SCENE_ID=#{deviceDto.sceneId} and s.status=1
            left join sms_scene ss on s.SCENE_ID=ss.id
        </if>
        where d.status = 1
        <if test="deviceDto.tenantId != null and deviceDto.tenantId != '' ">
            and d.tenant_id = #{deviceDto.tenantId}
        </if>
        <if test="deviceDto.condition != null and deviceDto.condition != ''">
            and (device_code like concat('%', #{deviceDto.condition},'%') or device_name like concat('%', #{deviceDto.condition},'%'))
        </if>
        <if test="deviceDto.companyOrgId != null and deviceDto.companyOrgId != 0">
            and d.COMPANY_ORG_ID=#{deviceDto.companyOrgId}
        </if>
        <if test="deviceDto.deptOrgId != null and deviceDto.deptOrgId != 0">
            and d.DEPT_ORG_ID =#{deviceDto.deptOrgId}
        </if>
        <if test="deviceDto.enableState != null ">
            and d.ENABLE_STATE =#{deviceDto.enableState}
        </if>
        <if test="deviceDto.userType!=null and deviceDto.userType!=-999 and deviceDto.userType!=-9999 and  deviceDto.programItems != null and deviceDto.programItems.size() > 0">
            <if test="deviceDto.permissonType == 5 or deviceDto.permissonType == 2">
                and ( d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="deviceDto.permissonType == 4 or deviceDto.permissonType == 3">
                and d.DEPT_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="deviceDto.permissonType == 1">
                and d.COMPANY_ORG_ID in
                <foreach collection="deviceDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (d.DEPT_ORG_ID is null or d.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>


</mapper>