<?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.yijia.station.mapper.StationClassStructureMapper">
    
    <resultMap type="StationClassStructure" id="StationClassStructureResult">
        <result property="id"    column="id"    />
        <result property="stationId"    column="station_id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="oilGun"    column="oil_gun"    />
        <result property="oilName"    column="oil_name"    />
        <result property="consumerId"    column="consumer_id"    />
        <result property="consumer"    column="consumer"    />
        <result property="amt"    column="amt"    />
        <result property="stationName"    column="station_name"    />
        <result property="orderLiters"    column="order_liters"    />
        <result property="payType"    column="pay_type"    />
        <result property="payWay"    column="pay_way"    />
        <result property="payDate"    column="pay_date"    />
        <result property="oilPersonnel"    column="oil_personnel"    />
        <result property="orderType"    column="order_type"    />
        <result property="oilType"    column="oil_type"    />
        <result property="printNum"    column="print_num"    />
        <result property="classStructureNo"    column="class_structure_no"    />
        <result property="classStartDate"    column="class_start_date"    />
        <result property="classStructureDate"    column="class_structure_date"    />
        <result property="classStructureMan"    column="class_structure_man"    />
        <result property="oils"    column="oils"    />
    </resultMap>

    <sql id="selectStationClassStructureVo">
        select id, station_id, order_no, oil_gun, oil_name, consumer_id, consumer, amt, station_name, order_liters, pay_type, pay_way, pay_date, oil_personnel, order_type, oil_type, print_num, class_structure_no, class_start_date, class_structure_date, class_structure_man, oils from station_class_structure
    </sql>

    <select id="selectStationClassStructureList" parameterType="StationClassStructure" resultMap="StationClassStructureResult">
        <include refid="selectStationClassStructureVo"/>
        <where>  
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="orderNo != null  and orderNo != ''"> and order_no = #{orderNo}</if>
            <if test="oilGun != null  and oilGun != ''"> and oil_gun = #{oilGun}</if>
            <if test="oilName != null  and oilName != ''"> and oil_name like concat('%', #{oilName}, '%')</if>
            <if test="consumerId != null "> and consumer_id = #{consumerId}</if>
            <if test="consumer != null  and consumer != ''"> and consumer = #{consumer}</if>
            <if test="amt != null  and amt != ''"> and amt = #{amt}</if>
            <if test="stationName != null  and stationName != ''"> and station_name like concat('%', #{stationName}, '%')</if>
            <if test="orderLiters != null  and orderLiters != ''"> and order_liters = #{orderLiters}</if>
            <if test="payType != null  and payType != ''"> and pay_type = #{payType}</if>
            <if test="payWay != null  and payWay != ''"> and pay_way = #{payWay}</if>
            <if test="payDate != null "> and pay_date = #{payDate}</if>
            <if test="oilPersonnel != null  and oilPersonnel != ''"> and oil_personnel = #{oilPersonnel}</if>
            <if test="orderType != null  and orderType != ''"> and order_type = #{orderType}</if>
            <if test="oilType != null  and oilType != ''"> and oil_type = #{oilType}</if>
            <if test="printNum != null "> and print_num = #{printNum}</if>
            <if test="classStructureNo != null  and classStructureNo != ''"> and class_structure_no = #{classStructureNo}</if>
            <if test="classStartDate != null "> and class_start_date = #{classStartDate}</if>
            <if test="classStructureDate != null "> and class_structure_date = #{classStructureDate}</if>
            <if test="classStructureMan != null  and classStructureMan != ''"> and class_structure_man = #{classStructureMan}</if>
            <if test="oils != null  and oils != ''"> and oils = #{oils}</if>
        </where>
    </select>

    <select id="listPersonnelStructure" parameterType="StationClassStructure" resultMap="StationClassStructureResult">
        SELECT a.oil_personnel,b.gun_no as oil_gun,ROUND(SUM(a.amt),2) as amt,ROUND(SUM(order_liters),2) as order_liters,COUNT(order_no) as order_no
        from station_class_structure a JOIN station_personnel b on a.oil_personnel =b.personnel_name
        and a.station_id = b.station_id
        where a.station_id =1
        GROUP BY a.oil_personnel,b.gun_no
    </select>

    <select id="listOilStructure" parameterType="StationClassStructure" resultMap="StationClassStructureResult">
       	SELECT  a.oil_name,GROUP_CONCAT(DISTINCT oil_gun) as oil_gun,ROUND(SUM(a.amt),2) as amt,ROUND(SUM(order_liters),2) as order_liters,COUNT(order_no) as order_no
        from station_class_structure a
        where a.station_id =1
        GROUP BY a.oil_name
    </select>
    <select id="selectStationClassStructureMax" parameterType="StationClassStructure" resultMap="StationClassStructureResult">
        select id, station_id, order_no, oil_gun, oil_name, consumer_id, consumer, amt, station_name, order_liters, pay_type, pay_way, pay_date, oil_personnel, order_type, oil_type, print_num, class_structure_no, class_structure_date, class_structure_man, oils
         from station_class_structure
       	 where id = (
		SELECT MAX(id) from station_class_structure where station_id= #{stationId})
    </select>
    <select id="selectStationClassStructureById" parameterType="Long" resultMap="StationClassStructureResult">
        <include refid="selectStationClassStructureVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertStationClassStructure" parameterType="StationClassStructure" useGeneratedKeys="true" keyProperty="id">
        insert into station_class_structure
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="oilGun != null">oil_gun,</if>
            <if test="oilName != null">oil_name,</if>
            <if test="consumerId != null">consumer_id,</if>
            <if test="consumer != null">consumer,</if>
            <if test="amt != null">amt,</if>
            <if test="stationName != null">station_name,</if>
            <if test="orderLiters != null">order_liters,</if>
            <if test="payType != null">pay_type,</if>
            <if test="payWay != null">pay_way,</if>
            <if test="payDate != null">pay_date,</if>
            <if test="oilPersonnel != null">oil_personnel,</if>
            <if test="orderType != null">order_type,</if>
            <if test="oilType != null">oil_type,</if>
            <if test="printNum != null">print_num,</if>
            <if test="classStructureNo != null">class_structure_no,</if>
            <if test="classStartDate != null">class_start_date,</if>
            <if test="classStructureDate != null">class_structure_date,</if>
            <if test="classStructureMan != null">class_structure_man,</if>
            <if test="oils != null">oils,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="oilGun != null">#{oilGun},</if>
            <if test="oilName != null">#{oilName},</if>
            <if test="consumerId != null">#{consumerId},</if>
            <if test="consumer != null">#{consumer},</if>
            <if test="amt != null">#{amt},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="orderLiters != null">#{orderLiters},</if>
            <if test="payType != null">#{payType},</if>
            <if test="payWay != null">#{payWay},</if>
            <if test="payDate != null">#{payDate},</if>
            <if test="oilPersonnel != null">#{oilPersonnel},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="oilType != null">#{oilType},</if>
            <if test="printNum != null">#{printNum},</if>
            <if test="classStructureNo != null">#{classStructureNo},</if>
            <if test="classStartDate != null">#{classStartDate},</if>
            <if test="classStructureDate != null">#{classStructureDate},</if>
            <if test="classStructureMan != null">#{classStructureMan},</if>
            <if test="oils != null">#{oils},</if>
         </trim>
    </insert>

    <update id="updateStationClassStructure" parameterType="StationClassStructure">
        update station_class_structure
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="oilGun != null">oil_gun = #{oilGun},</if>
            <if test="oilName != null">oil_name = #{oilName},</if>
            <if test="consumerId != null">consumer_id = #{consumerId},</if>
            <if test="consumer != null">consumer = #{consumer},</if>
            <if test="amt != null">amt = #{amt},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="orderLiters != null">order_liters = #{orderLiters},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="payWay != null">pay_way = #{payWay},</if>
            <if test="payDate != null">pay_date = #{payDate},</if>
            <if test="oilPersonnel != null">oil_personnel = #{oilPersonnel},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="oilType != null">oil_type = #{oilType},</if>
            <if test="printNum != null">print_num = #{printNum},</if>
            <if test="classStructureNo != null">class_structure_no = #{classStructureNo},</if>
            <if test="classStartDate != null">class_start_date = #{classStartDate},</if>
            <if test="classStructureDate != null">class_structure_date = #{classStructureDate},</if>
            <if test="classStructureMan != null">class_structure_man = #{classStructureMan},</if>
            <if test="oils != null">oils = #{oils},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStationClassStructureById" parameterType="Long">
        delete from station_class_structure where id = #{id}
    </delete>

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