<?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.fs.qw.mapper.QwInformationMapper">

    <resultMap type="QwInformation" id="QwInformationResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="voidId"    column="void_id"    />
        <result property="type"    column="type"    />
        <result property="addNum"    column="add_num"    />
        <result property="num"    column="num"    />
        <result property="deleteNum"    column="delete_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectQwInformationVo">
        select id, name, void_id, type, add_num, num, delete_num, create_time, update_time from qw_information
    </sql>

    <select id="selectQwInformationList" parameterType="QwInformation" resultMap="QwInformationResult">
        <include refid="selectQwInformationVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="voidId != null  and voidId != ''"> and void_id = #{voidId}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="addNum != null "> and add_num = #{addNum}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="deleteNum != null "> and delete_num = #{deleteNum}</if>
        </where>
    </select>

    <select id="selectQwInformationById" parameterType="Long" resultMap="QwInformationResult">
        <include refid="selectQwInformationVo"/>
        where id = #{id}
    </select>

    <insert id="insertQwInformation" parameterType="QwInformation">
        insert into qw_information
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="voidId != null">void_id,</if>
            <if test="type != null">type,</if>
            <if test="addNum != null">add_num,</if>
            <if test="num != null">num,</if>
            <if test="deleteNum != null">delete_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="voidId != null">#{voidId},</if>
            <if test="type != null">#{type},</if>
            <if test="addNum != null">#{addNum},</if>
            <if test="num != null">#{num},</if>
            <if test="deleteNum != null">#{deleteNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateQwInformation" parameterType="QwInformation">
        update qw_information
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="voidId != null">void_id = #{voidId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="addNum != null">add_num = #{addNum},</if>
            <if test="num != null">num = #{num},</if>
            <if test="deleteNum != null">delete_num = #{deleteNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwInformationById" parameterType="Long">
        delete from qw_information where id = #{id}
    </delete>
    <select id="selectQwWayStatisticsCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT  count( IF ( t.tt =2,1, null ) ) AS deleteNum,count( IF ( t.tt =1,1, NULL ) ) AS addNum, (count( IF ( t.tt =1,1, NULL ) ) - count( IF ( t.tt =2,1, null ) )) as num ,t.type
        FROM
            (
                SELECT DATE_FORMAT( l.create_time, #{sqlDateFormat} ) AS type, l.type as tt
                FROM
                    qw_contact_way_logs  AS l
                WHERE
                    (DATE_FORMAT(l.create_time,#{sqlDateFormat}) between #{beginTime} and #{finalTime})
                  AND l.information_id=#{companyId} )t
        GROUP BY t.type
    </select>
    <delete id="deleteQwInformationByIds" parameterType="String">
        delete from qw_information where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
