<?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.DeviceCallMapper">

    <resultMap id="BaseResultMap" type="com.ho.business.entity.DeviceCall">
        <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_type,
        producer,serial_no,status,device_json
    </sql>

    <select id="selectByInfo" resultType="com.ho.business.vo.resp.DeviceRespVO">
        select
        <include refid="Base_Column_List"/>
        from device_call
        <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 device_name LIKE concat('%',#{deviceReqVO.deviceName},'%')
            </if>
            <if test="deviceReqVO.producer != null">
                and producer = #{deviceReqVO.producer}
            </if>
            <if test="deviceReqVO.serialNo != null">
                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>
        </where>
    </select>

    <select id="selectByCondition" resultType="com.ho.business.vo.resp.DeviceRespVO">
        select
        <include refid="Base_Column_List"/>
        from device_call
        <where>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="srcId != null">
                and src_id = #{srcId}
            </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>
        </where>
    </select>


    <select id="selectBySId" resultType="com.ho.business.entity.DeviceCall">
        select
        <include refid="Base_Column_List"/>
        from device_call
        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_call
        <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.DeviceCall">
        select
        <include refid="Base_Column_List"/>
        from device_call
        where id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectByStationIdAndSrcId" resultType="com.ho.business.entity.DeviceCall">
        select
        <include refid="Base_Column_List"/>
        from device_call
        <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_call`
        WHERE station_id = #{stationId}
          AND device_type IS NOT NULL
    </select>

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

    <select id="selectDevicesByStationIds" resultType="com.ho.business.entity.DeviceCall">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device_call`
        <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_call`
        <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="selectByIdAndSrcIdNotZero" resultType="com.ho.business.vo.resp.DeviceRespVO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device_call`
        where
            hide = 0
        <if test="needAccessPoint == null">
            and src_id != 0
        </if>

        <if test="stationId != null">
            and station_id = #{stationId,jdbcType=INTEGER}
        </if>
    </select>

    <select id="selectByStationIdAndSrcIds" resultType="com.ho.business.vo.resp.DeviceRespVO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device_call`
        <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>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from device_call
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.Device"
            useGeneratedKeys="true">
        insert into device_call
        (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_call
        <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="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>
        </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="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>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.Device">
        update device_call
        <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>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByPrimaryKey" parameterType="com.ho.business.entity.Device">
        update device_call
        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>
</mapper>
