<?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.company.mapper.CompanySmsLogsMapper">
    
    <resultMap type="CompanySmsLogs" id="CompanySmsLogsResult">
        <result property="logsId"    column="logs_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="tempId"    column="temp_id"    />
        <result property="tempCode"    column="temp_code"    />
        <result property="phone"    column="phone"    />
        <result property="content"    column="content"    />
        <result property="createTime"    column="create_time"    />
        <result property="sendTime"    column="send_time"    />
        <result property="status"    column="status"    />
        <result property="mid"    column="mid"    />
        <result property="stat"    column="stat"    />
        <result property="replyContent"    column="reply_content"    />
        <result property="number"    column="number"    />

    </resultMap>

    <sql id="selectCompanySmsLogsVo">
        select logs_id, company_id, company_user_id, customer_id, temp_id, temp_code, phone, content, create_time, send_time, status,mid,stat,reply_content,number from company_sms_logs
    </sql>


    
    <select id="selectCompanySmsLogsById" parameterType="Long" resultMap="CompanySmsLogsResult">
        <include refid="selectCompanySmsLogsVo"/>
        where logs_id = #{logsId}
    </select>
        
    <insert id="insertCompanySmsLogs" parameterType="CompanySmsLogs" useGeneratedKeys="true" keyProperty="logsId">
        insert into company_sms_logs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="tempId != null">temp_id,</if>
            <if test="tempCode != null">temp_code,</if>
            <if test="phone != null">phone,</if>
            <if test="content != null">content,</if>
            <if test="createTime != null">create_time,</if>
            <if test="sendTime != null">send_time,</if>
            <if test="status != null">status,</if>
            <if test="mid != null">mid,</if>
            <if test="stat != null">stat,</if>
            <if test="replyContent != null">reply_content,</if>
            <if test="number != null">number,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="tempId != null">#{tempId},</if>
            <if test="tempCode != null">#{tempCode},</if>
            <if test="phone != null">#{phone},</if>
            <if test="content != null">#{content},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="sendTime != null">#{sendTime},</if>
            <if test="status != null">#{status},</if>
            <if test="mid != null">#{mid},</if>
            <if test="stat != null">#{stat},</if>
            <if test="replyContent != null">#{replyContent},</if>
            <if test="number != null">#{number},</if>
         </trim>
    </insert>

    <update id="updateCompanySmsLogs" parameterType="CompanySmsLogs">
        update company_sms_logs
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="tempId != null">temp_id = #{tempId},</if>
            <if test="tempCode != null">temp_code = #{tempCode},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="content != null">content = #{content},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="sendTime != null">send_time = #{sendTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="mid != null">mid = #{mid},</if>
            <if test="stat != null">stat = #{stat},</if>
            <if test="replyContent != null">reply_content = #{replyContent},</if>
            <if test="number != null">number = #{number},</if>
        </trim>
        where logs_id = #{logsId}
    </update>

    <delete id="deleteCompanySmsLogsById" parameterType="Long">
        delete from company_sms_logs where logs_id = #{logsId}
    </delete>

    <delete id="deleteCompanySmsLogsByIds" parameterType="String">
        delete from company_sms_logs where logs_id in 
        <foreach item="logsId" collection="array" open="(" separator="," close=")">
            #{logsId}
        </foreach>
    </delete>
    <select id="selectSmsLogsCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT  count(t.company_user_id ) AS smsCount,count( IF ( t.status =1,1, NULL ) ) AS successCount,  t.type
        FROM
        (
        SELECT DATE_FORMAT( s.create_time, #{sqlDateFormat} ) AS type,  s.company_user_id,s.status
        FROM
        company_sms_logs  AS s
        WHERE
        (DATE_FORMAT(s.create_time,#{sqlDateFormat}) between #{beginTime} and #{finalTime})
        AND s.company_user_id IN
        <foreach item="item" collection="userIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        ) t
        GROUP BY t.type
    </select>
</mapper>