<?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.zksy.WaterSupply.WaterPipeInfo.mapper.WaterPipeInfoMapper">

    <resultMap id="BaseResultMap" type="com.zksy.WaterSupply.WaterPipeInfo.domain.WaterPipeInfo">
        <id column="id" property="id"/>
        <result column="pipe_code" property="pipeCode"/>
        <result column="pipe_name" property="pipeName"/>
        <result column="pipe_material" property="pipeMaterial"/>
        <result column="pipe_diameter" property="pipeDiameter"/>
        <result column="pipe_length" property="pipeLength"/>
        <result column="start_point" property="startPoint"/>
        <result column="end_point" property="endPoint"/>
        <result column="laying_year" property="layingYear"/>
        <result column="pressure_rating" property="pressureRating"/>
        <result column="status" property="status"/>
        <result column="remark" property="remark"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, pipe_code, pipe_name, pipe_material, pipe_diameter, pipe_length,
        start_point, end_point, laying_year, pressure_rating, status, remark,
        del_flag, create_by, create_time, update_by, update_time
    </sql>

    <select id="selectPipeList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM water_pipe_info
        WHERE del_flag = '0'
        <if test="pipeCode != null and pipeCode != ''">
            AND pipe_code LIKE CONCAT('%', #{pipeCode}, '%')
        </if>
        <if test="pipeName != null and pipeName != ''">
            AND pipe_name LIKE CONCAT('%', #{pipeName}, '%')
        </if>
        <if test="pipeMaterial != null and pipeMaterial != ''">
            AND pipe_material = #{pipeMaterial}
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <if test="startTime != null">
            AND create_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND create_time &lt;= #{endTime}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="statPipeByStatus" resultType="java.util.Map">
        SELECT
        status,
        COUNT(*) AS pipeCount
        FROM water_pipe_info
        WHERE del_flag = '0'
        GROUP BY status
    </select>

    <select id="statPipeByMaterial" resultType="java.util.Map">
        SELECT
        pipe_material AS pipeMaterial,
        COUNT(*) AS pipeCount,
        SUM(pipe_length) AS totalLength
        FROM water_pipe_info
        WHERE del_flag = '0'
        GROUP BY pipe_material
    </select>

    <select id="getLatestPipe" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM water_pipe_info
        WHERE del_flag = '0'
        ORDER BY create_time DESC
        LIMIT 1
    </select>

    <select id="getPipeOverallStatistics" resultType="java.util.Map">
        SELECT
        COUNT(*) AS totalPipeCount,
        SUM(pipe_length) AS totalLength,
        SUM(CASE WHEN status = '0' THEN 1 ELSE 0 END) AS runningCount,
        SUM(CASE WHEN status = '1' THEN 1 ELSE 0 END) AS stopCount
        FROM water_pipe_info
        WHERE del_flag = '0'
    </select>

    <select id="selectDeletedList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM water_pipe_info
        WHERE del_flag = '2'
        <if test="pipeCode != null and pipeCode != ''">
            AND pipe_code LIKE CONCAT('%', #{pipeCode}, '%')
        </if>
        <if test="pipeName != null and pipeName != ''">
            AND pipe_name LIKE CONCAT('%', #{pipeName}, '%')
        </if>
        <if test="pipeMaterial != null and pipeMaterial != ''">
            AND pipe_material = #{pipeMaterial}
        </if>
        <if test="startTime != null">
            AND update_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND update_time &lt;= #{endTime}
        </if>
        ORDER BY update_time DESC
    </select>

    <delete id="deleteByIdsPhysically">
        DELETE FROM water_pipe_info WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="restoreByIds">
        UPDATE water_pipe_info
        SET del_flag = '0',
        update_by = #{updateBy},
        update_time = CURRENT_TIMESTAMP
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
