<?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.admin.mapper.SmsSignMapper">
    <resultMap id="smsSignListMap" type="cn.roadm.admin.dto.SmsSpSigntureListDO">
        <id property="id" column="id"/>
        <result property="signture" column="signture"/>
        <result property="spId" column="sp_id"/>
        <result property="spName" column="sp_name"/>
        <result property="spSignExtend" column="sp_sign_extend"/>
        <result property="gid" column="gid"/>
        <result property="signGroupName" column="group_name"/>
        <result property="beginTime" column="begin_time"/>
        <result property="endTime" column="end_time"/>
        <result property="status" column="status"/>
        <result property="fixedExtend" column="fixed_extend"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap id="smsSignGroupListMap" type="cn.roadm.admin.dto.SmsSpSigntureListDO">
        <id property="id" column="id"/>
        <result property="signGroupName" column="name"/>
        <result property="createTime" column="create_time"/>
        <result property="description" column="desc"/>
        <result property="status" column="status"/>
    </resultMap>

    <select id="querySignGroupOptionsList" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as `value`, name as label
        from sms_sp_sign_group
        where status = 0
        order by create_time desc
    </select>

    <select id="listPageQuerySpSignList" resultMap="smsSignListMap">
        SELECT s.id,
               s.signture,
               s.sp_id,
               s.create_time,
               s.begin_time,
               s.end_time,
               s.status,
               sp.sp_name as sp_name,
               s.gid,
               sg.name group_name,
               s.sp_sign_extend,
               s.fixed_extend,
               s.update_time
        FROM sms_sp_signture s
                 left join sms_sp_info sp on s.sp_id = sp.id
                 left join sms_sp_sign_group sg on s.gid = sg.id
        <where>
            s.status = 1
              and sg.status = 0
            <if test="smsSpSigntureQueryVO.spId != null and smsSpSigntureQueryVO.spId != ''">
                and sp.id = #{smsSpSigntureQueryVO.spId}
            </if>
            <if test="smsSpSigntureQueryVO.sign != null and smsSpSigntureQueryVO.sign != ''">
                and s.signture like concat('%', #{smsSpSigntureQueryVO.sign}, '%')
            </if>
            <if test="smsSpSigntureQueryVO.spName != null and smsSpSigntureQueryVO.spName != ''">
                and sp.sp_name like concat('%', #{smsSpSigntureQueryVO.spName}, '%')
            </if>
            <if test="smsSpSigntureQueryVO.signGroupId != null and smsSpSigntureQueryVO.signGroupId > -1">
                and s.gid = #{smsSpSigntureQueryVO.signGroupId}
            </if>
            <if test="smsSpSigntureQueryVO.signGroupName != null and smsSpSigntureQueryVO.signGroupName != ''">
                and sg.name like concat('%', #{smsSpSigntureQueryVO.signGroupName}, '%')
            </if>
            <if test="smsSpSigntureQueryVO.startTime !=null and smsSpSigntureQueryVO.startTime !='' ">
                and s.create_time &gt;= str_to_date(#{smsSpSigntureQueryVO.startTime},'%Y-%m-%d %H:%i:%S')
            </if>
            <if test="smsSpSigntureQueryVO.endTime !=null and smsSpSigntureQueryVO.endTime !='' ">
                and s.create_time &lt; str_to_date(#{smsSpSigntureQueryVO.endTime},'%Y-%m-%d %H:%i:%S')
            </if>
        </where>
        order by create_time desc, id desc
    </select>

    <select id="listPageQuerySignGroupList" resultMap="smsSignGroupListMap">
        select id, name, create_time, `desc`, status
        from sms_sp_sign_group
        <where>
            status = #{smsSpSigntureQueryVO.status}
            <if test="smsSpSigntureQueryVO.signGroupName != null and smsSpSigntureQueryVO.signGroupName != ''">
                and name like concat('%', #{smsSpSigntureQueryVO.signGroupName}, '%')
            </if>
            <if test="smsSpSigntureQueryVO.id != null and smsSpSigntureQueryVO.id > -1">
                and id = #{smsSpSigntureQueryVO.id}
            </if>
            <if test="smsSpSigntureQueryVO.sign != null and smsSpSigntureQueryVO.sign != ''">
                and id in (select gid
                           from sms_sp_signture sss
                           where sss.status = 1
                             and sss.signture like concat('%', #{smsSpSigntureQueryVO.sign}, '%'))
            </if>
            <if test="smsSpSigntureQueryVO.spId != null">
                and find_in_set(id, (select sign_gid from sms_sp_info where id = #{smsSpSigntureQueryVO.spId}))
            </if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertSpSignGroup" useGeneratedKeys="true"  keyProperty="id"  parameterType="cn.roadm.admin.controller.vo.SmsSpSignGroupSaveVO">
        insert into sms_sp_sign_group(name,create_time) values(#{signGroupName},now())
    </insert>

    <update id="updateSignGroup" parameterType="cn.roadm.admin.controller.vo.SmsSpSignGroupSaveVO">
        update sms_sp_sign_group set name=#{signGroupName} where id=#{id}
    </update>

    <delete id="delSignGroup">
        DELETE from sms_sp_sign_group where id=#{gid}
    </delete>
    <delete id="delSignByGid">
        delete from  sms_sp_signture where gid = #{gid}
    </delete>

    <select id="getSpInfoListBySignGid" resultType="cn.roadm.admin.controller.vo.TransferSelectOptionsVO">
        select id as `key`,sign_gid as title from sms_sp_info
        where find_in_set(#{gid},sign_gid)
          and status = '1'
    </select>

    <select id="getSpSignGroupById" parameterType="integer" resultType="cn.roadm.admin.controller.vo.TransferSelectOptionsVO">
        select id as `key`,sign_gid as title from sms_sp_info
        where id =#{spid}
    </select>

    <select id="getSpInfoBySignture" resultType="cn.roadm.admin.controller.vo.TransferSelectOptionsVO">
        select id as `key`,sign_gid title from sms_sp_info
        where check_signture_bank=1
          and status = '1'
    </select>

    <update id="updateSigntureFlag" >
        update sms_sp_info set check_signture_bank=#{params.title} where id=#{params.key}
    </update>

    <update id="updateSpInfoSignIdByIds">
        update sms_sp_info sp set sign_gid=#{params.title} where sp.id=#{params.key}
    </update>

    <update id="updateSignGroupStatus">
        update sms_sp_sign_group set `status` = #{state} where id = #{gid}
    </update>

    <delete id="deleteSpSignture" parameterType="int">
        delete from sms_sp_signture  where id = #{id}
    </delete>

    <insert id="insertSpSignture" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.admin.controller.vo.SmsSpSigntureSaveVO">
        INSERT INTO sms_sp_signture (signture,sp_id,create_time,status,gid, sp_sign_extend, fixed_extend) VALUES
            (#{sign,jdbcType=VARCHAR},#{spId},now(),#{status},#{groupId},#{extendNo}, #{extendMode})
    </insert>

    <insert  id="addSpSigntureBatch"  useGeneratedKeys="true" keyProperty="id" parameterType="ArrayList">
        INSERT INTO sms_sp_signture (signture,sp_id,create_time,status,gid, sp_sign_extend, fixed_extend)
        VALUES
        <foreach collection="list" item="obj"  index="index"  separator="," >
            (#{obj.sign,jdbcType=VARCHAR},#{obj.spId},now(),#{obj.status},#{obj.groupId},#{obj.extendNo}, #{obj.extendMode})
        </foreach>
    </insert>

    <update id="updateSpSigntureInfo" parameterType="cn.roadm.admin.controller.vo.SmsSpSigntureSaveVO">
        update sms_sp_signture set signture = #{sign}, fixed_extend = #{extendMode}, sp_sign_extend = #{extendNo} where id = #{id}
    </update>

    <delete id="batchDeleteBySearch" parameterType="cn.roadm.admin.controller.vo.SmsSpSigntureQueryVO">
        delete s from  sms_sp_signture s left JOIN sms_sp_info sp  ON s.sp_id=sp.id
        left join sms_sp_sign_group sg on s.gid = sg.id
        <where>
            <if test="spId!=null and spId!=''">
                and sp.id= #{spId}
            </if>
            <if test="sign!=null and sign!=''">
                and s.signture LIKE CONCAT('%', #{sign}, '%')
            </if>
            <if test="spName!=null and spName!=''">
                and sp.sp_name LIKE CONCAT('%', #{spName}, '%')
            </if>
            <if test="signGroupId!=null and signGroupId >-1">
                and s.gid=#{signGroupId}
            </if>
            <if test="signGroupName != null and signGroupName != ''">
                and sg.name like concat('%', #{signGroupName}, '%')
            </if>
            <if test="startTime !=null and startTime !='' ">
                and s.create_time &gt;= str_to_date(#{startTime},'%Y-%m-%d %H:%i:%S')
            </if>
            <if test="endTime !=null and endTime !='' ">
                and s.create_time &lt;= str_to_date(#{endTime},'%Y-%m-%d %H:%i:%S')
            </if>
        </where>
    </delete>


    <select id="exportSign"  resultMap="smsSignListMap">
        select
        s.id,
        s.signture,
        s.sp_id,
        date_format(s.create_time, '%Y-%m-%d %H:%i:%S') as create_time,
        IFNULL(date_format(s.update_time, '%Y-%m-%d %H:%i:%S'), '') as update_time,
        s.begin_time,
        s.end_time,
        s.status,
        sp.sp_name sp_name,
        s.gid,
        sg.name group_name,
        s.sp_sign_extend,
        s.fixed_extend
        from
        sms_sp_signture s left join sms_sp_info sp on s.sp_id=sp.id left join sms_sp_sign_group sg on s.gid=sg.id
        where
        s.status = 1
        and sg.status = 0
        <if test="smsSpSigntureQueryVO.spId != null and smsSpSigntureQueryVO.spId != ''">
            and sp.id = #{smsSpSigntureQueryVO.spId}
        </if>
        <if test="smsSpSigntureQueryVO.sign != null and smsSpSigntureQueryVO.sign != ''">
            and s.signture like concat('%', #{smsSpSigntureQueryVO.sign}, '%')
        </if>
        <if test="smsSpSigntureQueryVO.spName != null and smsSpSigntureQueryVO.spName != ''">
            and sp.sp_name like concat('%', #{smsSpSigntureQueryVO.spName}, '%')
        </if>
        <if test="smsSpSigntureQueryVO.signGroupId != null and smsSpSigntureQueryVO.signGroupId > -1">
            and s.gid = #{smsSpSigntureQueryVO.signGroupId}
        </if>
        <if test="smsSpSigntureQueryVO.signGroupName != null and smsSpSigntureQueryVO.signGroupName != ''">
            and sg.name like concat('%', #{smsSpSigntureQueryVO.signGroupName}, '%')
        </if>
        <if test="smsSpSigntureQueryVO.startTime !=null and smsSpSigntureQueryVO.startTime !='' ">
            and s.create_time &gt;= str_to_date(#{smsSpSigntureQueryVO.startTime},'%Y-%m-%d %H:%i:%S')
        </if>
        <if test="smsSpSigntureQueryVO.endTime !=null and smsSpSigntureQueryVO.endTime !='' ">
            and s.create_time &lt; str_to_date(#{smsSpSigntureQueryVO.endTime},'%Y-%m-%d %H:%i:%S')
        </if>
        <if test="smsSpSigntureQueryVO.endId != null">
            and s.id &lt; #{smsSpSigntureQueryVO.endId}
        </if>
        order by id desc
        <if test="smsSpSigntureQueryVO.limit != null and smsSpSigntureQueryVO.limit > 0">
            limit #{smsSpSigntureQueryVO.limit}
        </if>
    </select>

    <update id="batchUpdateSpSignStatus">
        update sms_sp_signture
        set status = #{status}
        where id in
        <foreach collection="ids" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
</mapper>
