<?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="cn.roadm.mapper.BeautifyPhoneMapper">

    <select id="queryBeautyPhoneList" resultType="cn.roadm.bo.BeautyPhoneDTO">
        SELECT `id`, patten_desc as  `phone_format`,phone_format as `format`,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%S') as create_time,`operator`,`state`
        FROM `sms_beauty_phone`
        <where>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                create_time BETWEEN #{startTime} and #{endTime}
            </if>
            <if test="state != null and state != -1">
                and state = #{state}
            </if>
        </where>
    </select>

    <insert id="addBeautyPhone" parameterType="cn.roadm.bo.BeautyPhoneDTO">
        INSERT into sms_beauty_phone(phone_format,create_time,operator,`state`) values (#{phone_format},#{create_time},#{operator},#{state})
    </insert>

    <update id="disableBeautyPhone" parameterType="cn.roadm.bo.BeautyPhoneDTO">
        update sms_beauty_phone set state = #{state} where id = #{id}
    </update>

    <select id="queryBeautyPhoneById" resultType="cn.roadm.bo.BeautyPhoneDTO">
        SELECT `id`,`phone_format`,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%S') as create_time,`operator`,`state`
        FROM `sms_beauty_phone` where id = #{id}
    </select>

    <select id="queryTimesLimitConfig" resultType="cn.roadm.bo.SystemConfig">
        select * from sms_system_config where config_key = "times_limit"
    </select>

    <update id="updateTimesLimitConfig" parameterType="cn.roadm.bo.TimesLimitBO">
        update sms_system_config set value = #{json} where config_key = "times_limit"
    </update>

    <select id="querySpBeautyInfoBySpId" parameterType="Integer" resultType="string">
        select phone_formats as phoneFormats from sms_sp_beauty_info where sp_id = #{spId}
    </select>

    <select id="querySpBeautyIsExitBySpId" parameterType="Integer" resultType="Integer">
        select count(1) as phoneFormats from sms_sp_beauty_info where sp_id = #{spId}
    </select>

    <select id="querySpBeautySpId" resultType="Integer">
        select sp_id from sms_sp_beauty_info
    </select>

    <insert id="insertSpBeautyInfo">
        insert into sms_sp_beauty_info (sp_id,phone_formats) value (#{spId},#{formats})
    </insert>

    <insert id="insertBatchSpBeautyInfo">
        insert into sms_sp_beauty_info  (sp_id,phone_formats) values
        <foreach collection="spIds" separator="," item="spId">
            (#{spId},#{formats})
        </foreach>
    </insert>

    <update id="updateSpBeautyInfoBySpId">
        update sms_sp_beauty_info set phone_formats = #{formats} where sp_id = #{spId}
    </update>

    <update id="updateBatchSpBeautyInfoBySpId">
        update sms_sp_beauty_info set phone_formats = #{formats} where sp_id in
        <foreach collection="spIds" open="(" close=")" separator="," item="spId">
            #{spId}
        </foreach>
    </update>

    <delete id="deleteSpBeautyInfoBySpIds" >
        delete from sms_sp_beauty_info where sp_id in
        <foreach collection="spIds" open="(" close=")" separator="," item="spId">
            #{spId}
        </foreach>
    </delete>

    <update id="updateSpBeautyInfoOfCommon">
        insert into sms_sp_beauty_info (sp_id,phone_formats)
        value (0,(select group_CONCAT(phone_format) from sms_beauty_phone where state = 0 group by state))
        ON DUPLICATE KEY UPDATE
        phone_formats = (select group_CONCAT(phone_format) from sms_beauty_phone where state = 0 group by state)
    </update>
</mapper>
