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

    <resultMap type="TAirChargeRecord" id="TAirChargeRecordResult">
        <result property="chargeRecordId"    column="charge_record_id"    />
        <result property="stationId"    column="station_id"    />
        <result property="bottleId"    column="bottle_id"    />
        <result property="chargeOperator"    column="charge_operator"    />
        <result property="chargeMeasure"    column="charge_measure"    />
        <result property="chargeDate"    column="charge_date"    />
        <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"/>
        <result property="bottleCode" column="bottle_code"/>
        <result property="bottleCapacity" column="bottle_capacity"/>
        <result property="bottleStatus" column="bottle_status"/>
        <result property="chargeOperatorName" column="charge_operator_name"/>
    </resultMap>

    <sql id="selectTAirChargeRecordVo">
        SELECT
            charge.charge_record_id,
            charge.station_id,
            charge.bottle_id,
            charge.charge_operator,
            charge.charge_measure,
            charge.charge_date,
            charge.create_time,
            charge.update_time,
            charge.is_del,
            charge.remark,
            station.station_name,
            bottle.bottle_code,
            bottle.bottle_capacity,
            bottle.bottle_status,
            pr.name AS charge_operator_name
        FROM
            t_air_charge_record charge
        LEFT JOIN t_gas_storage_station_info station ON station.station_id = charge.station_id
        LEFT JOIN t_gas_bottle_info bottle ON bottle.bottle_id = charge.bottle_id
        LEFT JOIN t_practitioner_info pr ON pr.practitioner_id = charge.charge_operator
    </sql>

    <select id="selectTAirChargeRecordList" parameterType="TAirChargeRecord" resultMap="TAirChargeRecordResult">
        <include refid="selectTAirChargeRecordVo"/>
        <where>
            <if test="stationId != null "> and charge.station_id = #{stationId}</if>
            <if test="bottleId != null "> and charge.bottle_id = #{bottleId}</if>
            <if test="chargeOperator != null "> and charge.charge_operator = #{chargeOperator}</if>
            <if test="chargeMeasure != null  and chargeMeasure != ''"> and charge.charge_measure = #{chargeMeasure}</if>
            <if test="chargeDate != null "> and charge.charge_date = #{chargeDate}</if>
            <if test="isDel != null  and isDel != ''"> and charge.is_del = #{isDel}</if>
            <if test="chargeBeginTime != null and chargeEndTime != null">and charge.charge_date BETWEEN #{chargeBeginTime} and #{chargeEndTime}</if>
            <if test="bottleCode != null">and bottle.bottle_code like concat('%', #{bottleCode}, '%')</if>
            <if test="stationName != null">and station.station_name like concat('%', #{stationName}, '%')</if>
        </where>
        order by   charge.charge_record_id desc
    </select>

    <select id="selectTAirChargeRecordById" parameterType="Long" resultMap="TAirChargeRecordResult">
        <include refid="selectTAirChargeRecordVo"/>
        where charge.charge_record_id = #{chargeRecordId}
    </select>

    <insert id="insertTAirChargeRecord" parameterType="TAirChargeRecord" useGeneratedKeys="true" keyProperty="chargeRecordId">
        insert into t_air_charge_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="bottleId != null">bottle_id,</if>
            <if test="chargeOperator != null">charge_operator,</if>
            <if test="chargeMeasure != null">charge_measure,</if>
            <if test="chargeDate != null">charge_date,</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="bottleId != null">#{bottleId},</if>
            <if test="chargeOperator != null">#{chargeOperator},</if>
            <if test="chargeMeasure != null">#{chargeMeasure},</if>
            <if test="chargeDate != null">#{chargeDate},</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="updateTAirChargeRecord" parameterType="TAirChargeRecord">
        update t_air_charge_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="bottleId != null">bottle_id = #{bottleId},</if>
            <if test="chargeOperator != null">charge_operator = #{chargeOperator},</if>
            <if test="chargeMeasure != null">charge_measure = #{chargeMeasure},</if>
            <if test="chargeDate != null">charge_date = #{chargeDate},</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 charge_record_id = #{chargeRecordId}
    </update>

    <delete id="deleteTAirChargeRecordById" parameterType="Long">
        delete from t_air_charge_record where charge_record_id = #{chargeRecordId}
    </delete>

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


    <select id="airChargeStationStatistics" parameterType="com.zehong.system.domain.vo.AirChargeStatisticsVo" resultType="com.zehong.system.domain.AirChargeStationStatistics">
        select
          station.station_name as stationName,
          count(air.charge_record_id)as bottleNum,
          sum(air.charge_measure) as chargeMeasure
        from
            t_air_charge_record air
        left join t_gas_storage_station_info station on station.station_id = air.station_id
        left join t_gas_bottle_info bottle on bottle.bottle_id = air.bottle_id
        left join t_practitioner_info pr ON pr.practitioner_id = air.charge_operator
        <where>
            <if test="stationId != null">
                and air.station_id = #{stationId}
            </if>
            <if test="operator != null">
                and pr.name like concat('%', #{operator}, '%')
            </if>
            <if test="operateBeginTime != null and operateEndTime != null" >
                and air.charge_date between #{operateBeginTime} and #{operateEndTime}
            </if>
        </where>
        group by air.station_id
    </select>

    <select id="airChargeOperatorStatistics" parameterType="com.zehong.system.domain.vo.AirChargeStatisticsVo" resultType="com.zehong.system.domain.AirChargeOperatorStatistics">
        select
          station.station_name as stationName,
          count(air.charge_record_id)as bottleNum,
          sum(air.charge_measure) as chargeMeasure,
          pr.name as operator
        from
            t_air_charge_record air
        left join t_gas_storage_station_info station on station.station_id = air.station_id
        left join t_gas_bottle_info bottle on bottle.bottle_id = air.bottle_id
        left join t_practitioner_info pr ON pr.practitioner_id = air.charge_operator
        <where>
            <if test="stationId != null">
                and air.station_id = #{stationId}
            </if>
            <if test="operator != null">
                and pr.name like concat('%', #{operator}, '%')
            </if>
            <if test="operateBeginTime != null and operateEndTime != null" >
                and air.charge_date between #{operateBeginTime} and #{operateEndTime}
            </if>
        </where>
        group by air.station_id,air.charge_operator
    </select>
</mapper>
