<?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">
<!--suppress ALL -->
<mapper namespace="com.fs.qw.mapper.QwContactWayMapper">

    <resultMap type="QwContactWay" id="QwContactWayResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />
        <result property="name"    column="name"    />
        <result property="remark"    column="remark"    />
        <result property="skipVerify"    column="skip_verify"    />
        <result property="state"    column="state"    />
        <result property="userIds"    column="user_ids"    />
        <result property="addNum"    column="add_num"    />
        <result property="deleteNum"    column="delete_num"    />
        <result property="num"    column="num"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="configId"    column="config_id"    />
        <result property="isDel"    column="is_del"    />
        <result property="createTime"    column="create_time"    />
        <result property="companyId"    column="company_id"    />
        <result property="isWelcome"    column="is_welcome"    />
        <result property="textContent"    column="text_content"    />
        <result property="imagePicUrl"    column="image_pic_url"    />
        <result property="isUserLimit"    column="is_user_limit"    />
        <result property="isSpanWelcome"    column="is_span_welcome"    />
        <result property="isCloseWelcome"    column="is_close_welcome"    />
        <result property="closeWelcomeWord"    column="close_welcome_word"    />
        <result property="isTag"    column="is_tag"    />
        <result property="tags"    column="tags"    />
        <result property="isRemark"    column="is_remark"    />
        <result property="remarkStatus"    column="remark_status"    />
        <result property="description"    column="description"    />
        <result property="isDescription"    column="is_description"    />
        <result property="spareUserIds"    column="spare_user_ids"    />
        <result property="groupId"    column="group_id"    />
        <result property="userLimitJson"    column="user_limit_json"    />
        <result property="userTimeJson"    column="user_time_json"    />
        <result property="userType"    column="user_type"    />
        <result property="welcomeJson"    column="welcome_json"    />
        <result property="corpId"    column="corp_id"    />
        <result property="informationId"    column="information_id"    />
    </resultMap>

    <sql id="selectQwContactWayVo">
        select id, type,corp_id,information_id, name,welcome_json, remark, skip_verify, state, user_ids, add_num, delete_num, num, qr_code, config_id, is_del, create_time, company_id, is_welcome, text_content, image_pic_url, is_user_limit, is_span_welcome, is_close_welcome, close_welcome_word, is_tag, tags, is_remark, remark_status, description, is_description, spare_user_ids, group_id, user_limit_json, user_time_json, user_type from qw_contact_way
    </sql>

    <select id="selectQwContactWayList" parameterType="QwContactWay" resultMap="QwContactWayResult">
        <include refid="selectQwContactWayVo"/>
        <where>
            <if test="type != null "> and type = #{type}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="skipVerify != null "> and skip_verify = #{skipVerify}</if>
            <if test="state != null  and state != ''"> and state = #{state}</if>
            <if test="userIds != null  and userIds != ''"> and user_ids = #{userIds}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="addNum != null "> and add_num = #{addNum}</if>
            <if test="deleteNum != null "> and delete_num = #{deleteNum}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="qrCode != null  and qrCode != ''"> and qr_code = #{qrCode}</if>
            <if test="configId != null  and configId != ''"> and config_id = #{configId}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="isWelcome != null "> and is_welcome = #{isWelcome}</if>
            <if test="textContent != null  and textContent != ''"> and text_content = #{textContent}</if>
            <if test="imagePicUrl != null  and imagePicUrl != ''"> and image_pic_url = #{imagePicUrl}</if>
            <if test="isUserLimit != null "> and is_user_limit = #{isUserLimit}</if>
            <if test="isSpanWelcome != null "> and is_span_welcome = #{isSpanWelcome}</if>
            <if test="isCloseWelcome != null "> and is_close_welcome = #{isCloseWelcome}</if>
            <if test="closeWelcomeWord != null  and closeWelcomeWord != ''"> and close_welcome_word = #{closeWelcomeWord}</if>
            <if test="isTag != null "> and is_tag = #{isTag}</if>
            <if test="tags != null  and tags != ''"> and tags = #{tags}</if>
            <if test="isRemark != null "> and is_remark = #{isRemark}</if>
            <if test="remarkStatus != null  and remarkStatus != ''"> and remark_status = #{remarkStatus}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="isDescription != null "> and is_description = #{isDescription}</if>
            <if test="spareUserIds != null  and spareUserIds != ''"> and spare_user_ids = #{spareUserIds}</if>
            <if test="groupId != null "> and group_id = #{groupId}</if>
            <if test="userLimitJson != null  and userLimitJson != ''"> and user_limit_json = #{userLimitJson}</if>
            <if test="userTimeJson != null  and userTimeJson != ''"> and user_time_json = #{userTimeJson}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
        </where> order by id desc
    </select>

    <select id="selectQwContactWayById" parameterType="Long" resultMap="QwContactWayResult">
        <include refid="selectQwContactWayVo"/>
        where id = #{id}
    </select>

    <insert id="insertQwContactWay" parameterType="QwContactWay" useGeneratedKeys="true" keyProperty="id">
        insert into qw_contact_way
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null">type,</if>
            <if test="name != null">name,</if>
            <if test="remark != null">remark,</if>
            <if test="skipVerify != null">skip_verify,</if>
            <if test="state != null">state,</if>
            <if test="userIds != null">user_ids,</if>
            <if test="addNum != null">add_num,</if>
            <if test="deleteNum != null">delete_num,</if>
            <if test="num != null">num,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="configId != null">config_id,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createTime != null">create_time,</if>
            <if test="companyId != null">company_id,</if>
            <if test="isWelcome != null">is_welcome,</if>
            <if test="textContent != null">text_content,</if>
            <if test="imagePicUrl != null">image_pic_url,</if>
            <if test="isUserLimit != null">is_user_limit,</if>
            <if test="isSpanWelcome != null">is_span_welcome,</if>
            <if test="isCloseWelcome != null">is_close_welcome,</if>
            <if test="closeWelcomeWord != null">close_welcome_word,</if>
            <if test="isTag != null">is_tag,</if>
            <if test="tags != null">tags,</if>
            <if test="isRemark != null">is_remark,</if>
            <if test="remarkStatus != null">remark_status,</if>
            <if test="description != null">description,</if>
            <if test="isDescription != null">is_description,</if>
            <if test="spareUserIds != null">spare_user_ids,</if>
            <if test="groupId != null">group_id,</if>
            <if test="userLimitJson != null">user_limit_json,</if>
            <if test="userTimeJson != null">user_time_json,</if>
            <if test="userType != null">user_type,</if>
            <if test="welcomeJson != null">welcome_json,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="informationId != null">information_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null">#{type},</if>
            <if test="name != null">#{name},</if>
            <if test="remark != null">#{remark},</if>
            <if test="skipVerify != null">#{skipVerify},</if>
            <if test="state != null">#{state},</if>
            <if test="userIds != null">#{userIds},</if>
            <if test="addNum != null">#{addNum},</if>
            <if test="deleteNum != null">#{deleteNum},</if>
            <if test="num != null">#{num},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="configId != null">#{configId},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="isWelcome != null">#{isWelcome},</if>
            <if test="textContent != null">#{textContent},</if>
            <if test="imagePicUrl != null">#{imagePicUrl},</if>
            <if test="isUserLimit != null">#{isUserLimit},</if>
            <if test="isSpanWelcome != null">#{isSpanWelcome},</if>
            <if test="isCloseWelcome != null">#{isCloseWelcome},</if>
            <if test="closeWelcomeWord != null">#{closeWelcomeWord},</if>
            <if test="isTag != null">#{isTag},</if>
            <if test="tags != null">#{tags},</if>
            <if test="isRemark != null">#{isRemark},</if>
            <if test="remarkStatus != null">#{remarkStatus},</if>
            <if test="description != null">#{description},</if>
            <if test="isDescription != null">#{isDescription},</if>
            <if test="spareUserIds != null">#{spareUserIds},</if>
            <if test="groupId != null">#{groupId},</if>
            <if test="userLimitJson != null">#{userLimitJson},</if>
            <if test="userTimeJson != null">#{userTimeJson},</if>
            <if test="userType != null">#{userType},</if>
            <if test="welcomeJson != null">#{welcomeJson},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="informationId != null">#{informationId},</if>
         </trim>
    </insert>

    <update id="updateQwContactWay" parameterType="QwContactWay">
        update qw_contact_way
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="name != null">name = #{name},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="skipVerify != null">skip_verify = #{skipVerify},</if>
            <if test="state != null">state = #{state},</if>
            <if test="userIds != null">user_ids = #{userIds},</if>
            <if test="addNum != null">add_num = #{addNum},</if>
            <if test="deleteNum != null">delete_num = #{deleteNum},</if>
            <if test="num != null">num = #{num},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="configId != null">config_id = #{configId},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isWelcome != null">is_welcome = #{isWelcome},</if>
            <if test="textContent != null">text_content = #{textContent},</if>
            <if test="imagePicUrl != null">image_pic_url = #{imagePicUrl},</if>
            <if test="isUserLimit != null">is_user_limit = #{isUserLimit},</if>
            <if test="isSpanWelcome != null">is_span_welcome = #{isSpanWelcome},</if>
            <if test="isCloseWelcome != null">is_close_welcome = #{isCloseWelcome},</if>
            <if test="closeWelcomeWord != null">close_welcome_word = #{closeWelcomeWord},</if>
            <if test="isTag != null">is_tag = #{isTag},</if>
            <if test="tags != null">tags = #{tags},</if>
            <if test="isRemark != null">is_remark = #{isRemark},</if>
            <if test="remarkStatus != null">remark_status = #{remarkStatus},</if>
            <if test="description != null">description = #{description},</if>
            <if test="isDescription != null">is_description = #{isDescription},</if>
            <if test="spareUserIds != null">spare_user_ids = #{spareUserIds},</if>
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="userLimitJson != null">user_limit_json = #{userLimitJson},</if>
            <if test="userTimeJson != null">user_time_json = #{userTimeJson},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="welcomeJson != null">welcome_json = #{welcomeJson},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="informationId != null">information_id = #{informationId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwContactWayById" parameterType="Long">
        delete from qw_contact_way 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.way_id=#{companyId} )t
        GROUP BY t.type
    </select>

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