<?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.sckw.slope.detection.dao.mysql.KwsProjectMapper">
    <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.mysql.KwsProject">
        <!--@mbg.generated-->
        <!--@Table kws_project-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="mountain_id" jdbcType="VARCHAR" property="mountainId"/>
        <result column="company_id" jdbcType="VARCHAR" property="companyId"/>
        <result column="mountain_name" jdbcType="VARCHAR" property="mountainName"/>
        <result column="platform_name" jdbcType="VARCHAR" property="platformName"/>
        <result column="platform_address" jdbcType="VARCHAR" property="platformAddress"/>
        <result column="design_company" jdbcType="VARCHAR" property="designCompany"/>
        <result column="design_contacts" jdbcType="VARCHAR" property="designContacts"/>
        <result column="design_phone" jdbcType="VARCHAR" property="designPhone"/>
        <result column="build_company" jdbcType="VARCHAR" property="buildCompany"/>
        <result column="build_contacts" jdbcType="VARCHAR" property="buildContacts"/>
        <result column="build_phone" jdbcType="VARCHAR" property="buildPhone"/>
        <result column="project_type" jdbcType="VARCHAR" property="projectType"/>
        <result column="project_no" jdbcType="VARCHAR" property="projectNo"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="create_by" jdbcType="BIGINT" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="BIGINT" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id,
        `name`,
        mountain_id,
        project_type,
        project_no,
        mountain_name,
        platform_name,
        platform_address,
        design_company,
        design_contacts,
        design_phone,
        build_company,
        build_contacts,
        build_phone,
        remark,
        `status`,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag,
        company_id
    </sql>
    <select id="selectByProjectParam" resultType="com.sckw.slope.detection.model.vo.ProjectVo">
        select
        <include refid="Base_Column_List">
        </include>
        from kws_project
        where del_flag = 0
        <if test="companyId != null and companyId != ''">
            and company_id = #{companyId,jdbcType=VARCHAR}
        </if>
        <if test="projectQueryDto.name != null and projectQueryDto.name != ''">
            and name like concat('%', #{projectQueryDto.name}, '%')
        </if>
        order by create_time desc
    </select>

    <select id="selectDeviceByProjectId" resultType="com.sckw.slope.detection.model.vo.ProjectDeviceVO">
        SELECT a.id           AS projectId,
               a.`name`       AS projectName,
               c.id           AS id,
               c.`name`       AS NAME,
               d.device_type  AS deviceType,
               d.manufacturer AS manufacturer,
               d.`name`       AS modelName,
               c.create_time  AS createTime,
               e.part_name    as partName
        FROM kws_project a
                 LEFT JOIN kws_project_device b
                           ON a.id = b.project_id and a.mountain_id = b.mountain_id
                 LEFT JOIN kws_device c ON b.device_id = c.id and c.mountain_id = b.mountain_id
                 LEFT JOIN kws_device_model d ON c.model_id = d.id and d.mountain_id = c.mountain_id
                 LEFT JOIN kws_device_model_part e ON e.device_model_id = d.id and d.mountain_id = c.mountain_id
        WHERE a.del_flag = 0
          AND b.del_flag = 0
          AND c.del_flag = 0
          AND d.del_flag = 0
        <if test="mountainId != null and mountainId != ''">
            AND c.mountain_id = #{mountainId}
        </if>
        <if test="projectId != null  and projectId != ''">
            AND a.id = #{projectId}
        </if>
        <if test="deviceId != null and deviceId != ''">
            AND c.id = #{deviceId}
        </if>
        <if test="type != null and type != ''">
            and e.part_name = #{type}
        </if>
    </select>

    <select id="selectDeviceList" resultType="com.sckw.slope.detection.model.vo.ProjectDeviceVO">
        SELECT DISTINCT e.id           AS deviceRelationId,
                        e.device_id    AS parentId,
                        a.id           AS projectId,
                        a.`name`       AS projectName,
                        c.id           AS id,
                        c.`name`       AS NAME,
                        c.alias        AS alias,
                        d.device_type  AS deviceType,
                        d.manufacturer AS manufacturer,
                        d.`name`       AS deviceModelName,
                        c.create_time  AS createTime
        FROM kws_project a
                 LEFT JOIN kws_project_device b
                           ON a.id = b.project_id
                 LEFT JOIN kws_device c ON b.device_id = c.id
                 LEFT JOIN kws_device_model d ON c.model_id = d.id
                 LEFT JOIN kws_device_relation e ON e.relate_device_id = c.id
        WHERE a.del_flag = 0
          AND b.del_flag = 0
          AND c.del_flag = 0
          AND d.del_flag = 0
          AND e.del_flag = 0
        <if test="deviceQuery.projectId != null and deviceQuery.projectId != ''">
            AND a.id = #{deviceQuery.projectId}
        </if>
        <if test="deviceQuery.manufacturer != null and deviceQuery.manufacturer != ''">
            and d.manufacturer like concat('%', #{deviceQuery.manufacturer}, '%')
        </if>
        <if test="deviceQuery.deviceType != null and deviceQuery.deviceType != ''">
            and d.device_type like concat('%', #{deviceQuery.deviceType}, '%')
        </if>
        <if test="deviceQuery.deviceModel != null and deviceQuery.deviceModel != ''">
            and d.name like concat('%', #{deviceQuery.deviceModel}, '%')
        </if>
        <if test="deviceQuery.name != null and deviceQuery.name != ''">
            and c.name like concat('%', #{deviceQuery.name}, '%')
        </if>
        order by b.create_time desc
    </select>

    <select id="selectDeviceNotBindList" resultType="com.sckw.slope.detection.model.vo.ProjectDeviceVO">
        SELECT a.id           AS projectId,
               a.`name`       AS projectName,
               c.id           AS id,
               c.`status`     as status,
               c.`name`       AS NAME,
               d.device_type  AS deviceType,
               d.manufacturer AS manufacturer,
               d.`name`       AS deviceModelName,
               c.create_time  AS createTime
        FROM kws_project a
                 LEFT JOIN kws_device c ON a.mountain_id = c.mountain_id
                 LEFT JOIN kws_device_model d ON c.model_id = d.id
        WHERE a.del_flag = 0
          AND c.del_flag = 0
          AND d.del_flag = 0
          AND c.relevance_level = 0
          AND c.id NOT in (SELECT DISTINCT a.relate_device_id
                           FROM kws_device_relation a
                                    LEFT JOIN kws_project b ON a.mountain_id = b.mountain_id)
          AND c.id NOT in (SELECT DISTINCT a.device_id
                           FROM kws_device_relation a
                                    LEFT JOIN kws_project b ON a.mountain_id = b.mountain_id)
        <if test="deviceQuery.projectId != null and deviceQuery.projectId != ''">
            AND a.id = #{deviceQuery.projectId}
        </if>
        <if test="deviceQuery.manufacturer != null and deviceQuery.manufacturer != ''">
            and d.manufacturer like concat('%', #{deviceQuery.manufacturer}, '%')
        </if>
        <if test="deviceQuery.deviceType != null and deviceQuery.deviceType != ''">
            and d.device_type like concat('%', #{deviceQuery.deviceType}, '%')
        </if>
        <if test="deviceQuery.deviceModel != null and deviceQuery.deviceModel != ''">
            and d.name like concat('%', #{deviceQuery.deviceModel}, '%')
        </if>
        <if test="deviceQuery.name != null and deviceQuery.name != ''">
            and c.name like concat('%', #{deviceQuery.name}, '%')
        </if>
    </select>

    <select id="selectNotBindDeviceList" resultType="com.sckw.slope.detection.model.vo.ProjectDeviceVO">
        SELECT a.id           AS id,
               a.`status`     AS STATUS,
               a.`name`       AS NAME,
               a.create_time  AS createTime,
               d.device_type  AS deviceType,
               d.manufacturer AS manufacturer,
               d.`name`       AS deviceModelName
        FROM kws_device a
                 LEFT JOIN kws_device_model d ON d.id = a.model_id
        WHERE a.mountain_id = d.mountain_id
          and a.del_flag = 0
          AND a.del_flag = 0
          AND d.del_flag = 0
          AND a.relevance_level = 0
        <if test="deviceQuery.manufacturer != null and deviceQuery.manufacturer != ''">
            and d.manufacturer like concat('%'
                , #{deviceQuery.manufacturer}
                , '%')
        </if>
        <if test="deviceQuery.deviceType != null and deviceQuery.deviceType != ''">
            and d.device_type like concat('%'
                , #{deviceQuery.deviceType}
                , '%')
        </if>
        <if test="deviceQuery.deviceModel != null and deviceQuery.deviceModel != ''">
            and d.name like concat('%'
                , #{deviceQuery.deviceModel}
                , '%')
        </if>
        <if test="deviceQuery.name != null and deviceQuery.name != ''">
            and a.name like concat('%'
                , #{deviceQuery.name}
                , '%')
        </if>
        <if test="headerData.companyId != null and headerData.companyId != ''">
            and a.mountain_id =#{headerData.companyId}
        </if>
    </select>

    <select id="selectListData" resultMap="BaseResultMap">
        select *
        from kws_project
        <where>
            <if test="ids != null and ids.size() > 0">
                and id in
                <foreach collection="ids" close=")" open="(" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="delFlag != null">
                and del_flag = #{delFlag}
            </if>
        </where>
    </select>

    <select id="selectListByType" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List">
        </include>
        from kws_project
        <where>
            <if test="projectType != null and projectType != ''">
            </if>
            and project_type like concat(#{projectType}, '%')
        </where>
    </select>
</mapper>