<?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.SpSignNumberReportMapper">

    <select id="listPage" resultType="cn.roadm.admin.dto.SpSignNumberReportDTO">
        select id,
               sign,
               port_sp_name   as portSpName,
               port_sp_code   as portSpCode,
               legal_person   as legalPerson,
               card_type      as cardType,
               card,
               app_name       as appName,
               date_format(createtime, '%Y-%m-%d %H:%i:%s') as createTime,
               handledby,
               handledbyCard,
               handledby_card_type as handledbyCardType,
               owner_channel  as ownerChannel
        from sp_sign_details
        <where>
            <if test="reqVO.sign != null and reqVO.sign != ''">
                and sign like concat('%', #{reqVO.sign}, '%')
            </if>
            <if test="reqVO.startTime != null and reqVO.startTime != ''">
                and createtime &gt;= str_to_date(#{reqVO.startTime}, '%Y-%m-%d')
            </if>
            <if test="reqVO.endTime != null and reqVO.endTime != ''">
                and createtime &lt; DATE_ADD(str_to_date(#{reqVO.endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        order by id desc
    </select>

    <select id="getById" resultType="cn.roadm.admin.dto.SpSignNumberReportDTO">
        select id,
               sign,
               port_sp_name   as portSpName,
               port_sp_code   as portSpCode,
               legal_person   as legalPerson,
               card_type      as cardType,
               card,
               app_name       as appName,
               date_format(createtime, '%Y-%m-%d %H:%i:%s') as createTime,
               handledby,
               handledbyCard,
               handledby_card_type as handledbyCardType,
               owner_channel  as ownerChannel
        from sp_sign_details
        where id = #{id}
    </select>

    <select id="getBySign" resultType="cn.roadm.admin.dto.SpSignNumberReportDTO">
        select id,
               sign,
               port_sp_name   as portSpName,
               port_sp_code   as portSpCode,
               legal_person   as legalPerson,
               card_type      as cardType,
               card,
               app_name       as appName,
               date_format(createtime, '%Y-%m-%d %H:%i:%s') as createTime,
               handledby,
               handledbyCard,
               handledby_card_type as handledbyCardType,
               owner_channel  as ownerChannel
        from sp_sign_details
        where sign = #{sign}
        limit 1
    </select>

    <select id="countBySignExcludingId" resultType="int">
        select count(1) from sp_sign_details
        where sign = #{sign}
        <if test="id != null">
            and id != #{id}
        </if>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into sp_sign_details (sign, port_sp_name, port_sp_code, legal_person, card_type, card, app_name,
                                    createtime, handledby, handledbyCard, handledby_card_type, owner_channel)
        values (#{sign}, #{portSpName}, #{portSpCode}, #{legalPerson}, #{cardType}, #{card}, #{appName},
                now(), #{handledby}, #{handledbyCard}, #{handledbyCardType}, #{ownerChannel})
    </insert>

    <update id="update">
        update sp_sign_details
        set sign = #{sign},
            port_sp_name = #{portSpName},
            port_sp_code = #{portSpCode},
            legal_person = #{legalPerson},
            card_type = #{cardType},
            card = #{card},
            app_name = #{appName},
            handledby = #{handledby},
            handledbyCard = #{handledbyCard},
            handledby_card_type = #{handledbyCardType},
            owner_channel = #{ownerChannel}
        where id = #{id}
    </update>

    <delete id="deleteById">
        delete from sp_sign_details where id = #{id}
    </delete>
</mapper>
