<?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.hw.ems.base.mapper.BasePublicUserMapper">

    <resultMap type="BasePublicUser" id="BasePublicUserResult">
        <result property="id"    column="id"    />
        <result property="userName"    column="user_name"    />
        <result property="area"    column="area"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="percentage"    column="percentage"    />
        <result property="publicExpend" column="publicExpend" />
        <collection  property="monitors"   javaType="java.util.List"        resultMap="monitorResult" />
    </resultMap>


    <resultMap type="BaseMonitorInfo" id="BaseMonitorInfoResult">
        <result property="objid"    column="objid"    />
        <result property="parentId"    column="parent_id"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="monitorAddr"    column="monitor_addr"    />
        <result property="monitorType"    column="monitor_type"    />
        <result property="monitorStatus"    column="monitor_status"    />
        <result property="collectDeviceId"    column="collect_device_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="grade"    column="grade"    />
        <result property="buildId"    column="build_id"    />
        <result property="subentryId"    column="subentry_id"    />
        <result property="businessId"    column="business_id"    />
        <result property="meterTypeId"    column="meter_type_id"    />
        <result property="meterId"    column="meter_id"    />
        <result property="correctValue"    column="correct_value"    />
        <result property="pt"    column="pt"    />
        <result property="ct"    column="ct"    />
        <result property="isAmmeter"    column="is_ammeter"    />
        <result property="isKeyMonitor"    column="is_key_monitor"    />
        <result property="isCircuit"    column="is_circuit"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="parentName" column="parent_name" />
        <result property="publicShareType" column="public_share_type" />
    </resultMap>

    <resultMap id="monitorResult" type="MonitorVo">
        <id     property="monitorId"   column="monitor_id"     />
        <result property="monitorName" column="monitor_name"   />
        <result property="publicShareType" column="public_share_type"   />
        <result property="monitorType" column="monitor_type"   />
    </resultMap>


    <sql id="selectBasePublicUserVo">
        select id, user_name,area, create_by, create_time, update_by, update_time, remark from base_public_user
    </sql>

    <select id="selectBasePublicUserList" parameterType="BasePublicUser" resultMap="BasePublicUserResult">
        <include refid="selectBasePublicUserVo"/>
        <where>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
        </where>
    </select>

    <select id="selectPublicUserList" parameterType="BasePublicUser" resultMap="BasePublicUserResult">
        SELECT id,
        bmp.monitor_id,
        bmi.monitor_name,
        bmp.public_share_type,
        bmi.monitor_type,
        user_name,
        area,
        0    publicExpend,
        remark,
        0 as percentage
        FROM base_public_user bpu
        LEFT JOIN base_monitor_public bmp ON bmp.public_share_id = bpu.id
        LEFT JOIN base_monitor_info bmi ON bmp.monitor_id = bmi.monitor_id
        <where>
            AND id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
    </select>

    <select id="selectBasePublicUserById" parameterType="Long" resultMap="BasePublicUserResult">
        SELECT
            bpu.id,
            bpu.user_name,
            bpu.create_by,
            bpu.create_time,
            bpu.update_by,
            bpu.update_time,
            bpu.remark,
            bpu.area,
            bmi.monitor_id,
            bmi.monitor_name
        FROM
            base_public_user bpu
                LEFT JOIN base_monitor_public bmp ON bmp.public_share_id = bpu.id
                LEFT JOIN base_monitor_info bmi ON bmp.monitor_id = bmi.monitor_id
        where bpu.id = #{id}
    </select>
    <select id="selectlistChild" resultType="com.hw.ems.base.domain.vo.MonitorVo">
        SELECT bmp.monitor_id   AS   monitorId,
        bmi.monitor_name AS   monitorName,
        bmp.public_share_type publicShareType
        FROM base_monitor_public bmp
        LEFT JOIN base_monitor_info bmi ON bmp.monitor_id = bmi.monitor_id
        <where>
            <if test="id != null">bmp.public_share_id = #{id}</if>
        </where>
    </select>
    <select id="selectDistinctPublicMonitor"  resultMap="BaseMonitorInfoResult">
        select bmp.monitor_id monitor_id, count(1) correct_value from
        base_monitor_public bmp
        where bmp.public_share_type = 0
        <if test="ids != null">
         and   bmp.public_share_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        </if>
        group by bmp.monitor_id
    </select>

    <select id="queryPublicMonitorDnbExpend" resultType="java.math.BigDecimal">
        SELECT COALESCE(sum(expend),0) FROM report_point_dnb WHERE
        <if test="ids != null and ids.length > 0">
        monitor_id in
        <foreach item="item" collection="ids" open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
        <if test="startTime != null and startTime != ''">
            AND begin_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND begin_time &lt;= #{endTime}
        </if>
    </select>

    <select id="queryPublicMonitorWaterExpend" resultType="java.math.BigDecimal">
        SELECT COALESCE(sum(expend),0) FROM report_point_water WHERE
        monitor_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND  date_format(begin_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
        AND date_format(begin_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
    </select>

    <insert id="insertBasePublicUser" parameterType="BasePublicUser" useGeneratedKeys="true" keyProperty="id">
        insert into base_public_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null">user_name,</if>
            <if test="monitorId != null">monitor_id,</if>
            <if test="area != null and area != ''">area,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userName != null">#{userName},</if>
            <if test="monitorId != null">#{monitorId},</if>
            <if test="area != null and area != ''">#{area},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateBasePublicUser" parameterType="BasePublicUser">
        update base_public_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="userName != null">user_name = #{userName},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="area != null">area = #{area},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBasePublicUserById" parameterType="Long">
        delete from base_public_user where id = #{id}
    </delete>

    <delete id="deleteBasePublicUserByIds" parameterType="String">
        delete from base_public_user where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
