<?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.ho.business.mapper.DeviceMapper">

    <resultMap id="BaseResultMap" type="com.ho.business.entity.Device">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="src_id" property="srcId" jdbcType="INTEGER"/>
        <result column="group_id" property="groupId" jdbcType="INTEGER"/>
        <result column="dept_id" property="deptId" jdbcType="INTEGER"/>
        <result column="station_id" property="stationId" jdbcType="INTEGER"/>
        <result column="device_name" property="deviceName" jdbcType="VARCHAR"/>
        <result column="device_type" property="deviceType" jdbcType="VARCHAR"/>
        <result column="producer" property="producer" jdbcType="VARCHAR"/>
        <result column="serial_no" property="serialNo" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <!--将json类型的device_json字段修改为如下形式-->
        <result column="device_json" property="deviceJson"
                typeHandler="com.ho.business.handler.JsonHandler"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,group_id,dept_id,src_id,pid,category,
        station_id,device_name,device_name_en,device_type,
        producer,serial_no,status,device_json,`virtual`,from_id,hide,flow_direction,producer_type,unit_type
    </sql>

    <sql id="Base_Column_List1">
        id,group_id,dept_id,src_id,pid,category,
        station_id,device_name_en deviceName,device_type,
        producer,serial_no,status,device_json,`virtual`,from_id,hide,flow_direction,producer_type,unit_type
    </sql>

    <select id="selectByInfo" resultType="com.ho.business.vo.resp.DeviceRespVO">
        select
        <include refid="Base_Column_List"/>
        from device
        <where>
            <if test="deviceReqVO.groupId != null">
                and group_id = #{deviceReqVO.groupId}
            </if>
            <if test="deviceReqVO.srcId != null">
                and src_id = #{deviceReqVO.srcId}
            </if>
            <if test="deviceReqVO.deptId != null">
                and dept_id = #{deviceReqVO.deptId}
            </if>
            <if test="deviceReqVO.stationId != null ">
                and station_id = #{deviceReqVO.stationId}
            </if>
            <if test="stationIds != null and stationIds.size() !=0">
                and station_id in
                <foreach collection="stationIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="deviceTypeList != null and deviceTypeList.size() !=0">
                and device_type in
                <foreach collection="deviceTypeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="deviceReqVO.deviceType != null">
                and device_type LIKE concat(#{deviceReqVO.deviceType},'%')
            </if>
            <if test="deviceReqVO.deviceName != null and deviceReqVO.deviceName != ''">
                and device_name LIKE concat('%',#{deviceReqVO.deviceName},'%')
            </if>
            <if test="deviceReqVO.producer != null">
                and producer = #{deviceReqVO.producer}
            </if>
            <if test="deviceReqVO.serialNo != null and deviceReqVO.serialNo != ''">
                and serial_no LIKE concat('%',#{deviceReqVO.serialNo},'%')
            </if>
            <if test="deviceReqVO.status != null">
                and status = #{deviceReqVO.status}
            </if>
            <if test="deviceReqVO.needFilter != null">
                and category > 2
            </if>
            <if test="deviceReqVO.hide != null">
                and hide = #{deviceReqVO.hide}
            </if>
        </where>
    </select>

    <select id="selectByCondition" resultType="com.ho.business.vo.resp.DeviceRespVO">
       select
        <choose>
            <when test="lang != null and lang=='en_US' ">
                <include refid="Base_Column_List1"/>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
       from device
       <where>
           <if test="stationId != null">
               and station_id = #{stationId}
           </if>
           <if test="srcId != null">
               and src_id = #{srcId}
           </if>
           <if test="srcIdList != null and srcIdList.size != 0 ">
               and src_id in
               <foreach collection="srcIdList" open="(" close=")" separator="," item="item">
                   #{item}
               </foreach>
           </if>
           <if test="noNeedFromId !=null and formIdList != null and formIdList.size != 0 ">
               and from_id not in
               <foreach collection="formIdList" open="(" close=")" separator="," item="item">
                   #{item}
               </foreach>
           </if>
           <if test="deviceType != null">
               and device_type = #{deviceType}
           </if>
           <if test="deviceTypePrefix != null">
               and device_type LIKE concat('%',#{deviceTypePrefix},'%')
           </if>
           <if test="pId != null">
               and pid = #{pId}
           </if>
           <if test="category != null">
               and category = #{category}
           </if>
           <if test="deviceIds != null and deviceIds.size != 0 ">
               and id in
               <foreach collection="deviceIds" open="(" close=")" separator="," item="item">
                   #{item}
               </foreach>
           </if>
           <if test="stationIds != null and stationIds.size != 0 ">
               and station_id in
               <foreach collection="stationIds" open="(" close=")" separator="," item="item">
                   #{item}
               </foreach>
           </if>
           <if test="statusList != null and statusList.size() !=0">
               and status in
               <foreach collection="statusList" item="item" open="(" close=")" separator=",">
                   #{item}
               </foreach>
           </if>
           <if test="deviceTypeList != null and deviceTypeList.size() !=0">
               and device_type in
               <foreach collection="deviceTypeList" item="item" open="(" close=")" separator=",">
                   #{item}
               </foreach>
           </if>
           <if test="needFilter != null">
               and category > 2
           </if>
           <if test="hide != null">
               and hide =0
           </if>
           <if test="fuzzyDeviceType != null">
               and device_type LIKE concat('%',#{fuzzyDeviceType},'%')
           </if>
           <if test="needDeviceTypeNotNull != null">
               and device_type is not null
           </if>
       </where>
    </select>


    <select id="selectBySId" resultType="com.ho.business.entity.Device">
        select
        <include refid="Base_Column_List"/>
        from device
        where src_id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectBySrcIds" resultType="com.ho.business.vo.resp.chargepile.ChargePileRespVO">
        select
        <include refid="Base_Column_List"/>
        from device
        <where>
            <if test="stationId != null ">
                and station_id = #{stationId}
            </if>
            <if test="srcIds != null and srcIds.size != 0 ">
                and src_id in
                <foreach collection="srcIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectByPrimaryKey" resultType="com.ho.business.entity.Device">
        select
        <include refid="Base_Column_List"/>
        from device
        where id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectByStationIdAndSrcId" resultType="com.ho.business.entity.Device">
        select
        <include refid="Base_Column_List"/>
        from device
        <where>
            <if test="srcId != null">
                src_id = #{srcId,jdbcType=INTEGER}
            </if>
            <if test="stationId != null">
                and station_id = #{stationId,jdbcType=INTEGER}
            </if>
        </where>

    </select>

    <select id="selectDistinctDeviceTypeByStationId" resultType="java.lang.String">
        SELECT DISTINCT device_type
        FROM `device`
        WHERE station_id = #{stationId}
          AND device_type IS NOT NULL
    </select>

    <select id="selectDevicesByStationId" resultType="com.ho.business.entity.Device">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device`
        WHERE station_id = #{stationId}
        AND device_type IS NOT NULL
    </select>

    <select id="selectDevicesByStationIds" resultType="com.ho.business.entity.Device">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device`
        <where>
            <if test="stationIds != null and stationIds.size != 0 ">
                and station_id in
                <foreach collection="stationIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectByIds" resultType="com.ho.business.entity.Device">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device`
        <where>
            <if test="deviceIds != null and deviceIds.size != 0 ">
                and id in
                <foreach collection="deviceIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectByStationIdAndSrcIds" resultType="com.ho.business.vo.resp.DeviceRespVO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device`
        <where>
            <if test="stationId != null">
                and station_id = #{stationId,jdbcType=INTEGER}
            </if>
            <if test="srcIds != null and srcIds.size != 0 ">
                and src_id in
                <foreach collection="srcIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectByIdAndSrcIdNotZero" resultType="com.ho.business.vo.resp.DeviceRespVO">
        SELECT
        <choose>
            <when test="lang != null and lang=='en_US' ">
                <include refid="Base_Column_List1"/>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        FROM `device`
        <where>
            <if test="needHide == null">
                and hide=0
            </if>
            <if test="needAccessPoint == null">
                and src_id != 0
            </if>
            <if test="stationId != null">
                and station_id = #{stationId,jdbcType=INTEGER}
            </if>
        </where>
    </select>

    <select id="selectRealDevice" resultType="com.ho.business.entity.Device">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device`
        <where>
            <if test="stationId != null">
                station_id= #{stationId,jdbcType=INTEGER}
            </if>
            <if test="virtual != null">
                and `virtual`= #{virtual,jdbcType=INTEGER}
            </if>
            <if test="category != null">
                and category= #{category}
            </if>
            <if test="fromId != null">
                and from_id = #{fromId}
            </if>
            <if test="pid != null">
                and pid = #{pid}
            </if>
            <if test="deviceType != null">
                and device_type LIKE concat('%',#{deviceType},'%')
            </if>
            <if test="deviceName != null and deviceName != ''">
                and device_name LIKE concat('%',#{deviceName},'%')
            </if>
            <if test="isFilterUnitType != null">
                and unit_type > 1
            </if>
        </where>

    </select>

    <delete id="deleteByPrimaryKey">
        delete
        from device
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.Device"
            useGeneratedKeys="true">
        insert into device
        (id, group_id, dept_id, bay_id
            , station, device_name, device_name,
         producer, serial_no, status)
        values ( #{id,jdbcType=INTEGER}, #{groupId,jdbcType=INTEGER}, #{deptId,jdbcType=INTEGER}
               , #{bayId,jdbcType=INTEGER},
               , #{station,jdbcType=INTEGER}, #{deviceName,jdbcType=VARCHAR}, #{producer,jdbcType=VARCHAR}
               , #{serialNo,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER})
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.Device"
            useGeneratedKeys="true">
        insert into device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupId != null">group_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="srcId != null">src_id,</if>
            <if test="pid != null">pid,</if>
            <if test="category != null">category,</if>
            <if test="stationId != null">station_id,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceNameEn != null">device_name_en,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="producer != null">producer,</if>
            <if test="serialNo != null">serial_no,</if>
            <if test="createTime != null">create_time,</if>
            <if test="status != null">status,</if>
            <if test="deviceJson != null">device_json,</if>
            <if test="virtual != null">`virtual`,</if>
            <if test="fromId != null">from_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupId != null">#{groupId,jdbcType=INTEGER},</if>
            <if test="deptId != null">#{deptId,jdbcType=INTEGER},</if>
            <if test="srcId != null">#{srcId,jdbcType=INTEGER},</if>
            <if test="pid != null">#{pid,jdbcType=INTEGER},</if>
            <if test="category != null">#{category,jdbcType=INTEGER},</if>
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="deviceName != null">#{deviceName,jdbcType=VARCHAR},</if>
            <if test="deviceNameEn != null">#{deviceNameEn,jdbcType=VARCHAR},</if>
            <if test="deviceType != null">#{deviceType,jdbcType=VARCHAR},</if>
            <if test="producer != null">#{producer,jdbcType=VARCHAR},</if>
            <if test="serialNo != null">#{serialNo,jdbcType=VARCHAR},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="status != null">#{status,jdbcType=INTEGER},</if>
            <if test="deviceJson != null">
                #{deviceJson,jdbcType=OTHER,typeHandler=com.ho.business.handler.JsonHandler},
            </if>
            <if test="virtual != null">#{virtual,jdbcType=INTEGER},</if>
            <if test="fromId != null">#{fromId,jdbcType=INTEGER},</if>
        </trim>
    </insert>
    <insert id="insertBatch">
        INSERT INTO device
        (group_id,dept_id,src_id,pid,category,
        station_id,device_name,device_name_en,device_type,
        serial_no,status,`virtual`,from_id,create_time)
        VALUES
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.groupId},#{item.deptId},#{item.srcId},#{item.pid}
            ,#{item.category} ,#{item.stationId},#{item.deviceName},#{item.deviceNameEn},#{item.deviceType}
            ,#{item.serialNo},#{item.status},#{item.virtual},#{item.fromId},#{item.createTime}
            )
        </foreach>

    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.Device">
        update device
        <set>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=INTEGER},
            </if>
            <if test="deptId != null">
                dept_id = #{deptId,jdbcType=INTEGER},
            </if>
            <if test="srcId != null">
                src_id = #{srcId,jdbcType=INTEGER},
            </if>
            <if test="pid != null">
                pid = #{pid,jdbcType=INTEGER},
            </if>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="category != null">
                category = #{category},
            </if>
            <if test="deviceName != null">
                device_name = #{deviceName,jdbcType=VARCHAR},
            </if>
            <if test="deviceType != null">
                device_type = #{deviceType,jdbcType=VARCHAR},
            </if>
            <if test="producer != null">
                producer = #{producer,jdbcType=VARCHAR},
            </if>
            <if test="serialNo != null">
                serial_no = #{serialNo,jdbcType=VARCHAR},
            </if>
            <if test="status!= null">
                status= #{status,jdbcType=INTEGER},
            </if>
            <if test="deviceJson != null">
                device_json = #{deviceJson,jdbcType=OTHER,typeHandler=com.ho.business.handler.JsonHandler},
            </if>
            <if test="createTime!= null">
                create_time = #{createTime},
            </if>
            <if test="updateTime!= null">
                update_time = #{updateTime},
            </if>
            <if test="address!= null">
                address = #{address},
            </if>
            <if test="virtual!= null">
                `virtual` = #{virtual},
            </if>
            <if test="fromId!= null">
                from_id = #{fromId},
            </if>
            <if test="hide!= null">
                hide = #{hide},
            </if>
            <if test="flowDirection!= null">
                flow_direction = #{flowDirection},
            </if>
            <if test="unitType!= null">
                unit_type = #{unitType,jdbcType=INTEGER},
            </if>
            <if test="producerType!= null">
                producer_type = #{producerType},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByPrimaryKey" parameterType="com.ho.business.entity.Device">
        update device
        set group_id    = #{groupId,jdbcType=INTEGER},
            dept_id     = #{deptId,jdbcType=INTEGER},
            bay_id      = #{bayId,jdbcType=INTEGER},
            station     = #{station,jdbcType=INTEGER},
            device_name = #{deviceName,jdbcType=VARCHAR},
            producer    = #{producer,jdbcType=VARCHAR},
            serial_no   = #{serialNo,jdbcType=VARCHAR},
            status      = #{status,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByStationId">
        update device
        <set>
            <if test="serialNo != null">
                serial_no = #{serialNo,jdbcType=VARCHAR},
            </if>
        </set>
        where station_id = #{stationId,jdbcType=INTEGER}
    </update>


    <delete id="deleteByList">
        delete
        from device
        <where>
            <if test="ids != null and ids.size() != 0">
                and id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </delete>

    <update id="updateDeviceList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="list" item="item" index="index" separator=";">
            update device set device_name = #{item.deviceName},device_name_en = #{item.deviceNameEn} where id = #{item.id}
        </foreach>
    </update>
</mapper>
