<?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.zehong.system.mapper.TGasStorageStationInfoMapper">

    <resultMap type="TGasStorageStationInfo" id="TGasStorageStationInfoResult">
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="stationCode"    column="station_code"    />
        <result property="director"    column="director"    />
        <result property="telNum"    column="tel_num"    />
        <result property="address"    column="address"    />
        <result property="licenceFile"    column="licence_file"    />
        <result property="licenceCode"    column="licence_code"    />
        <result property="licenceEffectiveDate"    column="licence_effective_date"    />
        <result property="licenceLimitDate"    column="licence_limit_date"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectTGasStorageStationInfoVo">
        select station_id, station_name, station_code, director, tel_num, address, licence_file, licence_code, licence_effective_date, licence_limit_date, longitude, latitude, create_time, update_time, remark from t_gas_storage_station_info
    </sql>

    <select id="selectTGasStorageStationInfoList" parameterType="TGasStorageStationInfo" resultMap="TGasStorageStationInfoResult">
        <include refid="selectTGasStorageStationInfoVo"/>
        <where>
            <if test="stationName != null  and stationName != ''"> and station_name like concat('%', #{stationName}, '%')</if>
            <if test="stationCode != null  and stationCode != ''"> and station_code = #{stationCode}</if>
            <if test="director != null  and director != ''"> and director = #{director}</if>
            <if test="telNum != null  and telNum != ''"> and tel_num = #{telNum}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="licenceFile != null  and licenceFile != ''"> and licence_file = #{licenceFile}</if>
            <if test="licenceCode != null  and licenceCode != ''"> and licence_code = #{licenceCode}</if>
            <if test="licenceEffectiveDate != null "> and licence_effective_date = #{licenceEffectiveDate}</if>
            <if test="licenceLimitDate != null "> and licence_limit_date = #{licenceLimitDate}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="createBeginTime != null and createEndTime != null">
                and create_time BETWEEN #{createBeginTime} AND #{createEndTime}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectTGasStorageStationInfoById" parameterType="Long" resultMap="TGasStorageStationInfoResult">
        <include refid="selectTGasStorageStationInfoVo"/>
        where station_id = #{stationId}
    </select>

    <insert id="insertTGasStorageStationInfo" parameterType="TGasStorageStationInfo" useGeneratedKeys="true" keyProperty="stationId">
        insert into t_gas_storage_station_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationName != null">station_name,</if>
            <if test="stationCode != null">station_code,</if>
            <if test="director != null">director,</if>
            <if test="telNum != null">tel_num,</if>
            <if test="address != null">address,</if>
            <if test="licenceFile != null">licence_file,</if>
            <if test="licenceCode != null">licence_code,</if>
            <if test="licenceEffectiveDate != null">licence_effective_date,</if>
            <if test="licenceLimitDate != null">licence_limit_date,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationName != null">#{stationName},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="director != null">#{director},</if>
            <if test="telNum != null">#{telNum},</if>
            <if test="address != null">#{address},</if>
            <if test="licenceFile != null">#{licenceFile},</if>
            <if test="licenceCode != null">#{licenceCode},</if>
            <if test="licenceEffectiveDate != null">#{licenceEffectiveDate},</if>
            <if test="licenceLimitDate != null">#{licenceLimitDate},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTGasStorageStationInfo" parameterType="TGasStorageStationInfo">
        update t_gas_storage_station_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="stationCode != null">station_code = #{stationCode},</if>
            <if test="director != null">director = #{director},</if>
            <if test="telNum != null">tel_num = #{telNum},</if>
            <if test="address != null">address = #{address},</if>
            <if test="licenceFile != null">licence_file = #{licenceFile},</if>
            <if test="licenceCode != null">licence_code = #{licenceCode},</if>
            <if test="licenceEffectiveDate != null">licence_effective_date = #{licenceEffectiveDate},</if>
            <if test="licenceLimitDate != null">licence_limit_date = #{licenceLimitDate},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where station_id = #{stationId}
    </update>

    <delete id="deleteTGasStorageStationInfoById" parameterType="Long">
        delete from t_gas_storage_station_info where station_id = #{stationId}
    </delete>

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

    <select id="baseInfoStatistics" parameterType="java.lang.Long" resultType="com.zehong.system.domain.BaseInfoStatistics">
        SELECT
          station.station_name as stationName,
          (SELECT COUNT(1) FROM t_gas_bottle_info bottle WHERE bottle.station_id = station.station_id)as totalBottle,
          (SELECT COUNT(1) FROM t_vehicle_info vehicle WHERE vehicle.station_id = station.station_id) AS totalVehicle,
          (SELECT COUNT(1) FROM t_practitioner_info pr WHERE pr.station_id = station.station_id) AS totalPractitioner
        FROM
            t_gas_storage_station_info station
        <where>
            <if test="stationId != null" >
                station.station_id = #{stationId}
            </if>
        </where>
    </select>
</mapper>
