<?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.jsowell.pile.mapper.PileSimInfoMapper">

    <resultMap type="com.jsowell.pile.domain.PileSimInfo" id="PileSimInfoResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="iccid"    column="iccid"    />
        <result property="status"    column="status"    />
        <result property="simSupplier"    column="sim_supplier"    />
        <result property="totalData"    column="total_data"    />
        <result property="surplusData"    column="surplus_data"    />
        <result property="expireTime"    column="expire_time"    />
        <result property="operator"    column="operator"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectPileSimInfoVo">
        select id, name, iccid, status, sim_supplier, total_data, surplus_data, expire_time, operator, create_by, create_time, update_by, update_time, del_flag from pile_sim_info
    </sql>

    <select id="selectPileSimInfoList" parameterType="com.jsowell.pile.domain.PileSimInfo" resultMap="PileSimInfoResult">
        <include refid="selectPileSimInfoVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="iccid != null  and iccid != ''"> and iccid = #{iccid}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="simSupplier != null  and simSupplier != ''"> and sim_supplier = #{simSupplier}</if>
            <if test="totalData != null  and totalData != ''"> and total_data = #{totalData}</if>
            <if test="surplusData != null  and surplusData != ''"> and surplus_data = #{surplusData}</if>
            <if test="expireTime != null "> and expire_time = #{expireTime}</if>
            <if test="operator != null  and operator != ''"> and operator = #{operator}</if>
        </where>
    </select>

    <select id="selectPileSimInfoById" parameterType="Long" resultMap="PileSimInfoResult">
        <include refid="selectPileSimInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertPileSimInfo" parameterType="com.jsowell.pile.domain.PileSimInfo">
        insert into pile_sim_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="iccid != null">iccid,</if>
            <if test="status != null">status,</if>
            <if test="simSupplier != null">sim_supplier,</if>
            <if test="totalData != null">total_data,</if>
            <if test="surplusData != null">surplus_data,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="operator != null">operator,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="iccid != null">#{iccid},</if>
            <if test="status != null">#{status},</if>
            <if test="simSupplier != null">#{simSupplier},</if>
            <if test="totalData != null">#{totalData},</if>
            <if test="surplusData != null">#{surplusData},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="operator != null">#{operator},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updatePileSimInfo" parameterType="com.jsowell.pile.domain.PileSimInfo">
        update pile_sim_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="iccid != null">iccid = #{iccid},</if>
            <if test="status != null">status = #{status},</if>
            <if test="simSupplier != null">sim_supplier = #{simSupplier},</if>
            <if test="totalData != null">total_data = #{totalData},</if>
            <if test="surplusData != null">surplus_data = #{surplusData},</if>
            <if test="expireTime != null">expire_time = #{expireTime},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePileSimInfoById" parameterType="Long">
        delete from pile_sim_info where id = #{id}
    </delete>

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

    <sql id="Base_Column_List">
        id, name, iccid, status, sim_supplier, total_data, surplus_data, expire_time, operator, create_by, create_time,
        update_by, del_flag
    </sql>

    <select id="querySimCardInfoByPileSn" resultType="com.jsowell.pile.vo.web.SimCardInfoVO">
        SELECT
            t1.id,
            t2.sn as pileSn,
            t1.iccid as iccId,
            t1.STATUS as simCardStatus,
            t1.sim_supplier as simSupplier,
            t1.total_data as totalData,
            t1.surplus_data as surplusData,
            t1.expire_time as expireTime,
            t1.operator
        from pile_sim_info t1 join pile_basic_info t2 on t1.id = t2.sim_id
        where t1.del_flag = '0'
          and t2.sn = #{pileSn,jdbcType=VARCHAR}
    </select>

    <select id="selectSimInfoByIccIds" resultMap="PileSimInfoResult">
        select
        <include refid="Base_Column_List"/>
        from pile_sim_info
        where del_flag = '0'
        and iccid in
        <foreach collection="iccIds" open="(" separator="," close=")" item="iccId">
            #{iccId,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="getBasicInfoByIccId" resultMap="PileSimInfoResult">
        select
        <include refid="Base_Column_List"/>
        from pile_sim_info
        where del_flag = '0'
        and iccid = #{iccId,jdbcType=VARCHAR}
    </select>

    <select id="getSimInfoList" resultType="com.jsowell.pile.vo.web.SimCardInfoVO">
        SELECT
            t2.sn as pileSn,
            t2.sim_id as id,
            t1.iccid as iccId,
            t1.NAME as name,
            t1.sim_supplier as simSupplier,
            t1.STATUS as simCardStatus,
            t1.total_data as totalData,
            t1.surplus_data as surplusData,
            t1.expire_time as expireTime,
            t1.operator
        from pile_sim_info t1 left join pile_basic_info t2 on t1.id = t2.sim_id
        where t1.del_flag = '0'
        <if test="dto.pileSn != null and dto.pileSn != ''">
            and t2.sn = #{dto.pileSn,jdbcType=VARCHAR}
        </if>
        <if test="dto.simSupplier != null and dto.simSupplier != ''">
            and t1.sim_supplier = #{dto.simSupplier,jdbcType=VARCHAR}
        </if>
        <if test="dto.iccId != null and dto.iccId != ''">
            and t1.iccid = #{dto.iccId,jdbcType=VARCHAR}
        </if>
        <if test="dto.expiredTime != null and dto.expiredTime != ''">
            and t1.expire_time = #{dto.expiredTime,jdbcType=VARCHAR}
        </if>

    </select>
</mapper>