<?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.yinet.bjtj.mapper.FormBjtjMapper">
    
    <resultMap type="FormBjtj" id="FormBjtjResult">
        <result property="id"    column="ID"    />
        <result property="tbdate"    column="tbDate"    />
        <result property="tbcount"    column="tbCount"    />
        <result property="addtime"    column="addTime"    />
        <result property="adduser"    column="addUser"    />
    </resultMap>

    <sql id="selectFormBjtjVo">
        select ID, tbDate, tbCount, addTime, addUser from form_bjtj
    </sql>

    <select id="selectFormBjtjList" parameterType="FormBjtj" resultMap="FormBjtjResult">
        <include refid="selectFormBjtjVo"/>
        <where>  
            <if test="tbdate != null "> and tbDate = #{tbdate}</if>
            <if test="tbcount != null "> and tbCount = #{tbcount}</if>
            <if test="addtime != null "> and addTime = #{addtime}</if>
            <if test="adduser != null  and adduser != ''"> and addUser = #{adduser}</if>
        </where>
    </select>
    
    <select id="selectFormBjtjById" parameterType="Long" resultMap="FormBjtjResult">
        <include refid="selectFormBjtjVo"/>
        where ID = #{id}
    </select>
        
    <insert id="insertFormBjtj" parameterType="FormBjtj" useGeneratedKeys="true" keyProperty="id">
        insert into form_bjtj
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tbdate != null ">tbDate,</if>
            <if test="tbcount != null ">tbCount,</if>
            <if test="addtime != null ">addTime,</if>
            <if test="adduser != null  and adduser != ''">addUser,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tbdate != null ">#{tbdate},</if>
            <if test="tbcount != null ">#{tbcount},</if>
            <if test="addtime != null ">#{addtime},</if>
            <if test="adduser != null  and adduser != ''">#{adduser},</if>
         </trim>
    </insert>

    <update id="updateFormBjtj" parameterType="FormBjtj">
        update form_bjtj
        <trim prefix="SET" suffixOverrides=",">
            <if test="tbdate != null ">tbDate = #{tbdate},</if>
            <if test="tbcount != null ">tbCount = #{tbcount},</if>
            <if test="addtime != null ">addTime = #{addtime},</if>
            <if test="adduser != null  and adduser != ''">addUser = #{adduser},</if>
        </trim>
        where ID = #{id}
    </update>

    <delete id="deleteFormBjtjById" parameterType="Long">
        delete from form_bjtj where ID = #{id}
    </delete>

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

    <select id="selectTongjiList" parameterType="Long" resultType="java.util.Map">
        select sum(tbcount) as allBJCount,SUM((CASE WHEN year=0 THEN tbcount ELSE 0 END)) as THISYEAR_ACH ,SUM((CASE WHEN month=1 THEN tbcount ELSE 0 END)) as LASTMONTH_ACH ,SUM((CASE WHEN month=0 THEN tbcount ELSE 0 END)) as THISMONTH_ACH ,SUM((CASE WHEN day=1 THEN tbcount ELSE 0 END)) as LASTDay_ACH from (select *,(EXTRACT(YEAR FROM NOW())-EXTRACT(YEAR FROM tbDate)) as year,(EXTRACT(MONTH FROM NOW())-EXTRACT(MONTH FROM tbDate)) as month,TimeStampDiff(day, tbDate, NOW()) as day from Form_Bjtj) c
    </select>
    
</mapper>