<?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.casic.missiles.modular.system.dao.GroupMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.modular.system.model.Group">
        <id column="ID" property="id"/>
        <result column="GROUP_NAME" property="groupName"/>
        <result column="ALARM_OPEN" property="alarmOpen"/>
        <result column="DEPTID" property="deptid"/>
        <result column="TS" property="ts"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID AS id, GROUP_NAME AS groupName, ALARM_OPEN AS alarmOpen, DEPTID AS deptid, TS AS ts
    </sql>


    <select id="noGroupWellList" resultType="com.casic.missiles.modular.system.dto.BusWellInfoDto"
            parameterType="com.baomidou.mybatisplus.plugins.Page">
        SELECT
        b.id,b.well_code AS wellCode,
        b.well_name AS wellName,b.well_type AS wellType,
        b.deptid,b.POSITION,
        s.fullname AS deptName
        FROM bus_well_info b
        LEFT JOIN sys_dept s ON b.DEPTID= s.id
        WHERE b.id NOT IN (SELECT g.WELL_ID FROM bus_group_well g)
        <if test="keywords != null and keywords !=''">
            and (WELL_CODE like
            CONCAT('%',#{keywords},'%')
            OR POSITION like
            CONCAT('%',#{keywords},'%') )
        </if>
        <if test="wellType != null and wellType !=''">
            and WELL_TYPE = #{wellType}
        </if>
    </select>
    <select id="selectPageList" resultType="com.casic.missiles.modular.system.model.Group"
            parameterType="com.baomidou.mybatisplus.plugins.Page">
        SELECT
        <include refid="Base_Column_List"/>
        FROM bus_group
    </select>

    <select id="userList" resultType="com.casic.missiles.modular.system.dto.UserDTO"
            parameterType="com.baomidou.mybatisplus.plugins.Page">
        SELECT i.id,i.name,i.phone,i.deptid,d.fullname AS deptname FROM sys_user i
        RIGHT JOIN bus_group_user w ON w.user_id= i.id
        LEFT JOIN sys_dept d ON d.id=i.deptid
        <if test="groupId != null and groupId !=''">
            where  w.group_id = #{groupId}
        </if>
    </select>

    <select id="wellList" resultType="com.casic.missiles.modular.system.dto.WellDTO"
            parameterType="com.baomidou.mybatisplus.plugins.Page">
        SELECT i.id,i.well_code AS wellCode,
        i.well_name AS wellName,
        i.WELL_TYPE AS wellType,
        i.deptid,d.fullname AS deptName,
        i.position FROM bus_well_info i
        RIGHT JOIN bus_group_well w ON w.well_id= i.id
        LEFT JOIN sys_dept d ON d.id=i.deptid
        <if test="groupId != null and groupId !=''">
            where w.group_id = #{groupId}
        </if>
    </select>
</mapper>
