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

    <resultMap type="TGasBottleInfo" id="TGasBottleInfoResult">
        <result property="bottleId"    column="bottle_id"    />
        <result property="stationId"    column="station_id"    />
        <result property="bottleCode"    column="bottle_code"    />
        <result property="myselfNum"    column="myself_num"    />
        <result property="produceUnit"    column="produce_unit"    />
        <result property="produceDate"    column="produce_date"    />
        <result property="produceCode"    column="produce_code"    />
        <result property="bottleStatus"    column="bottle_status"    />
        <result property="lastChargeDate"    column="last_charge_date"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="chargeMedium"    column="charge_medium"    />
        <result property="bottleSpecs"    column="bottle_specs"    />
        <result property="ratedPresure"    column="rated_presure"    />
        <result property="bottleCapacity"    column="bottle_capacity"    />
        <result property="wallThickness"    column="wall_thickness"    />
        <result property="tare"    column="tare"    />
        <result property="useRegisterCode"    column="use_register_code"    />
        <result property="registerDate"    column="register_date"    />
        <result property="lastCheckDate"    column="last_check_date"    />
        <result property="nextCheckDate"    column="next_check_date"    />
        <result property="scrapDate"    column="scrap_date"    />
        <result property="currentStatus"    column="current_status"    />
        <result property="emptyType"    column="empty_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remark"    column="remark"    />
        <result property="stationName" column="station_name"/>
    </resultMap>

    <sql id="selectTGasBottleInfoVo">

        SELECT
            bottle.bottle_id,
            bottle.station_id,
            bottle.bottle_code,
            bottle.myself_num,
            bottle.produce_unit,
            bottle.produce_date,
            bottle.produce_code,
            bottle.bottle_status,
            bottle.last_charge_date,
            bottle.longitude,
            bottle.latitude,
            bottle.charge_medium,
            bottle.bottle_specs,
            bottle.rated_presure,
            bottle_capacity,
            bottle.wall_thickness,
            bottle.tare,
            bottle.use_register_code,
            bottle.register_date,
            bottle.last_check_date,
            bottle.next_check_date,
            bottle.scrap_date,
            bottle.current_status,
            bottle.empty_type,
            bottle.create_time,
            bottle.update_time,
            bottle.is_del,
            bottle.remark,
           (SELECT station_name FROM t_gas_storage_station_info info WHERE info.station_id = bottle.station_id) as station_name
        FROM
            t_gas_bottle_info bottle
    </sql>

    <select id="selectTGasBottleInfoList" parameterType="TGasBottleInfo" resultMap="TGasBottleInfoResult">
        <include refid="selectTGasBottleInfoVo"/>
        <where>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="bottleCode != null  and bottleCode != ''"> and bottle.bottle_code = #{bottleCode}</if>
            <if test="myselfNum != null  and myselfNum != ''"> and bottle.myself_num = #{myselfNum}</if>
            <if test="produceUnit != null  and produceUnit != ''"> and bottle.produce_unit = #{produceUnit}</if>
            <if test="produceDate != null "> and bottle.produce_date = #{produceDate}</if>
            <if test="produceBeginTime != null and produceEndTime">and bottle.produce_date between #{produceBeginTime} and #{produceEndTime}</if>
            <if test="produceCode != null  and produceCode != ''"> and bottle.produce_code = #{produceCode}</if>
            <if test="bottleStatus != null  and bottleStatus != ''"> and bottle.bottle_status = #{bottleStatus}</if>
            <if test="lastChargeDate != null "> and bottle.last_charge_date = #{lastChargeDate}</if>
            <if test="longitude != null "> and bottle.longitude = #{longitude}</if>
            <if test="latitude != null "> and bottle.latitude = #{latitude}</if>
            <if test="chargeMedium != null  and chargeMedium != ''"> and bottle.charge_medium = #{chargeMedium}</if>
            <if test="bottleSpecs != null  and bottleSpecs != ''"> and bottle.bottle_specs = #{bottleSpecs}</if>
            <if test="ratedPresure != null  and ratedPresure != ''"> and bottle.rated_presure = #{ratedPresure}</if>
            <if test="bottleCapacity != null  and bottleCapacity != ''"> and bottle.bottle_capacity = #{bottleCapacity}</if>
            <if test="wallThickness != null  and wallThickness != ''"> and bottle.wall_thickness = #{wallThickness}</if>
            <if test="tare != null  and tare != ''"> and bottle.tare = #{tare}</if>
            <if test="useRegisterCode != null  and useRegisterCode != ''"> and bottle.use_register_code = #{useRegisterCode}</if>
            <if test="registerDate != null "> and bottle.register_date = #{registerDate}</if>
            <if test="lastCheckDate != null "> and bottle.last_check_date = #{lastCheckDate}</if>
            <if test="nextCheckDate != null "> and bottle.next_check_date = #{nextCheckDate}</if>
            <if test="scrapDate != null "> and bottle.scrap_date = #{scrapDate}</if>
            <if test="currentStatus != null  and currentStatus != ''"> and bottle.current_status = #{currentStatus}</if>
            <if test="emptyType != null  and emptyType != ''"> and bottle.empty_type = #{emptyType}</if>
            <if test="isDel != null  and isDel != ''"> and bottle.is_del = #{isDel}</if>
        </where>
        order by bottle.bottle_id desc
    </select>

    <select id="selectTGasBottleInfoById" parameterType="Long" resultMap="TGasBottleInfoResult">
        <include refid="selectTGasBottleInfoVo"/>
        where bottle.bottle_id = #{bottleId}
    </select>

    <insert id="insertTGasBottleInfo" parameterType="TGasBottleInfo" useGeneratedKeys="true" keyProperty="bottleId">
        insert into t_gas_bottle_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="bottleCode != null">bottle_code,</if>
            <if test="myselfNum != null">myself_num,</if>
            <if test="produceUnit != null">produce_unit,</if>
            <if test="produceDate != null">produce_date,</if>
            <if test="produceCode != null">produce_code,</if>
            <if test="bottleStatus != null">bottle_status,</if>
            <if test="lastChargeDate != null">last_charge_date,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="chargeMedium != null">charge_medium,</if>
            <if test="bottleSpecs != null">bottle_specs,</if>
            <if test="ratedPresure != null">rated_presure,</if>
            <if test="bottleCapacity != null">bottle_capacity,</if>
            <if test="wallThickness != null">wall_thickness,</if>
            <if test="tare != null">tare,</if>
            <if test="useRegisterCode != null">use_register_code,</if>
            <if test="registerDate != null">register_date,</if>
            <if test="lastCheckDate != null">last_check_date,</if>
            <if test="nextCheckDate != null">next_check_date,</if>
            <if test="scrapDate != null">scrap_date,</if>
            <if test="currentStatus != null">current_status,</if>
            <if test="emptyType != null">empty_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId},</if>
            <if test="bottleCode != null">#{bottleCode},</if>
            <if test="myselfNum != null">#{myselfNum},</if>
            <if test="produceUnit != null">#{produceUnit},</if>
            <if test="produceDate != null">#{produceDate},</if>
            <if test="produceCode != null">#{produceCode},</if>
            <if test="bottleStatus != null">#{bottleStatus},</if>
            <if test="lastChargeDate != null">#{lastChargeDate},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="chargeMedium != null">#{chargeMedium},</if>
            <if test="bottleSpecs != null">#{bottleSpecs},</if>
            <if test="ratedPresure != null">#{ratedPresure},</if>
            <if test="bottleCapacity != null">#{bottleCapacity},</if>
            <if test="wallThickness != null">#{wallThickness},</if>
            <if test="tare != null">#{tare},</if>
            <if test="useRegisterCode != null">#{useRegisterCode},</if>
            <if test="registerDate != null">#{registerDate},</if>
            <if test="lastCheckDate != null">#{lastCheckDate},</if>
            <if test="nextCheckDate != null">#{nextCheckDate},</if>
            <if test="scrapDate != null">#{scrapDate},</if>
            <if test="currentStatus != null">#{currentStatus},</if>
            <if test="emptyType != null">#{emptyType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTGasBottleInfo" parameterType="TGasBottleInfo">
        update t_gas_bottle_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="bottleCode != null">bottle_code = #{bottleCode},</if>
            <if test="myselfNum != null">myself_num = #{myselfNum},</if>
            <if test="produceUnit != null">produce_unit = #{produceUnit},</if>
            <if test="produceDate != null">produce_date = #{produceDate},</if>
            <if test="produceCode != null">produce_code = #{produceCode},</if>
            <if test="bottleStatus != null">bottle_status = #{bottleStatus},</if>
            <if test="lastChargeDate != null">last_charge_date = #{lastChargeDate},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="chargeMedium != null">charge_medium = #{chargeMedium},</if>
            <if test="bottleSpecs != null">bottle_specs = #{bottleSpecs},</if>
            <if test="ratedPresure != null">rated_presure = #{ratedPresure},</if>
            <if test="bottleCapacity != null">bottle_capacity = #{bottleCapacity},</if>
            <if test="wallThickness != null">wall_thickness = #{wallThickness},</if>
            <if test="tare != null">tare = #{tare},</if>
            <if test="useRegisterCode != null">use_register_code = #{useRegisterCode},</if>
            <if test="registerDate != null">register_date = #{registerDate},</if>
            <if test="lastCheckDate != null">last_check_date = #{lastCheckDate},</if>
            <if test="nextCheckDate != null">next_check_date = #{nextCheckDate},</if>
            <if test="scrapDate != null">scrap_date = #{scrapDate},</if>
            <if test="currentStatus != null">current_status = #{currentStatus},</if>
            <if test="emptyType != null">empty_type = #{emptyType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where bottle_id = #{bottleId}
    </update>

    <delete id="deleteTGasBottleInfoById" parameterType="Long">
        delete from t_gas_bottle_info where bottle_id = #{bottleId}
    </delete>

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

    <select id="bottleStatistics" parameterType="java.lang.Long" resultType="com.zehong.system.domain.BottleStatistics">
        select
          (select station_name from t_gas_storage_station_info info where info.station_id = final.station_id) as stationName,
          sum(final.totalNum) as totalNum,
          sum(final.normalNum) as normalNum,
          sum(final.overNum) as overNum,
          sum(final.junkNum) as junkNum
        from(
            select
                base.station_id,
            count(1) as totalNum,
                case bottle_status when '1' then count(1) else 0 end as normalNum,
                case bottle_status when '2' then count(1) else 0 end as overNum,
                case bottle_status when '3' then count(1) else 0 end as junkNum
            from
                t_gas_bottle_info base
            <where>
                <if test="stationId != null">
                    base.station_id = #{stationId}
                </if>
            </where>
            group by
                base.station_id,base.bottle_status
        )final
        group by final.station_id
    </select>

    <!--查询气瓶详细信息-->
    <select id="getInf" resultMap="TGasBottleInfoResult"  parameterType="java.lang.String">
        <include refid="selectTGasBottleInfoVo"/>
        where bottle.bottle_id = #{bottleId}
    </select>

    <select id="getInfn" resultMap="TGasBottleInfoResult"  parameterType="java.lang.String">
        <include refid="selectTGasBottleInfoVo"/>
        where bottle.bottle_code = #{bottleId}
    </select>
</mapper>
