<?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.pj.project.tb_fee_statistics.TbFeeStatisticsMapper">

	<!-- 增 [G] -->
	<insert id="add">
		insert into 
		tb_fee_statistics (id, fee_type, item_type_id, item_type_name, year, month, day_time, num, tax_money, tax_rate, no_tax_money, pay_type)
		values (#{id}, #{feeType}, #{itemTypeId}, #{itemTypeName}, #{year}, #{month}, #{dayTime}, #{num}, #{taxMoney}, #{taxRate}, #{noTaxMoney}, #{payType})
	</insert>

	<!-- 删 -->
	<delete id="delete">
		delete from tb_fee_statistics 
		where id = #{id}
	</delete>

	<!-- 改 [G] -->
	<update id="update">
		update tb_fee_statistics set
		id = #{id}, 
		fee_type = #{feeType},
		item_type_id = #{itemTypeId}, 
		item_type_name = #{itemTypeName}, 
		year = #{year}, 
		month = #{month}, 
		day_time = #{dayTime}, 
		num = #{num}, 
		tax_money = #{taxMoney}, 
		tax_rate = #{taxRate}, 
		no_tax_money = #{noTaxMoney}, 
		pay_type = #{payType}
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, fee_type, fee_type_id, fee_type_name, item_type_id, item_type_name, year, month, day_time, num, tax_money, tax_rate, no_tax_money, pay_type from tb_fee_statistics  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.tb_fee_statistics.TbFeeStatistics">
		<result property="id" column="id" />
		<result property="feeType" column="fee_type" />
		<result property="itemTypeId" column="item_type_id" />
		<result property="itemTypeName" column="item_type_name" />
		<result property="year" column="year" />
		<result property="month" column="month" />
		<result property="dayTime" column="day_time" />
		<result property="num" column="num" />
		<result property="taxMoney" column="tax_money" />
		<result property="taxRate" column="tax_rate" />
		<result property="noTaxMoney" column="no_tax_money" />
		<result property="payType" column="pay_type" />
        <result property="statisticsMoney" column="statistics_money" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from tb_fee_statistics 
	</sql>
	
	<!-- 查 - 根据id -->
	<select id="getById" resultMap="model">
		<include refid="select_sql"></include>
		where id = #{id}
	</select>
	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
			<if test=' this.has("id") '> and id = #{id} </if>
			<if test=' this.has("feeType") '> and fee_type = #{feeType} </if>
			<if test=' this.has("itemTypeId") '> and item_type_id = #{itemTypeId} </if>
			<if test=' this.has("itemTypeName") '> and item_type_name = #{itemTypeName} </if>
			<if test=' this.has("year") '> and year = #{year} </if>
			<if test=' this.has("month") '> and month = #{month} </if>
			<if test=' this.has("dayTime") '> and day_time = #{dayTime} </if>
			<if test=' this.has("num") '> and num = #{num} </if>
			<if test=' this.has("taxMoney") '> and tax_money = #{taxMoney} </if>
			<if test=' this.has("taxRate") '> and tax_rate = #{taxRate} </if>
			<if test=' this.has("noTaxMoney") '> and no_tax_money = #{noTaxMoney} </if>
			<if test=' this.has("payType") '> and pay_type = #{payType} </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id desc </when>
			<when test='sortType == 2'> fee_type desc </when>
			<when test='sortType == 5'> item_type_id desc </when>
			<when test='sortType == 6'> item_type_name desc </when>
			<when test='sortType == 7'> year desc </when>
			<when test='sortType == 8'> month desc </when>
			<when test='sortType == 9'> day_time desc </when>
			<when test='sortType == 10'> num desc </when>
			<when test='sortType == 11'> tax_money desc </when>
			<when test='sortType == 12'> tax_rate desc </when>
			<when test='sortType == 13'> no_tax_money desc </when>
			<when test='sortType == 14'> pay_type desc </when>
			<otherwise> id desc </otherwise>
		</choose>
	</select>

    <select id="getMonth" resultMap="model">
        select month, fee_type,  sum(num) as num, sum(tax_money) as statistics_money
        from tb_fee_statistics
        <where>
            <if test=' this.has("feeType") '> and fee_type = #{feeType} </if>
            <if test=' this.has("month") '> and month = #{month} </if>
        </where>
        group by month, fee_type
        order by month desc
    </select>

    <select id="getYear" resultMap="model">
        select year, fee_type,  sum(num) as num, sum(tax_money) as statistics_money
        from tb_fee_statistics
        <where>
            <if test=' this.has("feeType") '> and fee_type = #{feeType} </if>
            <if test=' this.has("year") '> and year = #{year} </if>
        </where>
        group by year, fee_type
        order by year desc
    </select>

    <!-- 查 - 根据id -->
    <select id="getByDayAndFeeType" resultMap="model">
        <include refid="select_sql"></include>
        where day_time = #{day} and fee_type = #{feeType}
    </select>
	
	
	
	

</mapper>
