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

    <select id="getChargeListPageList" resultType="cn.roadm.admin.dto.SmsSpChargeDO">
        select sp.spid as spId,
        sp.sp_name,
        sp.fee_type,
        sp.is_api_billing,
        sp.platform,
        sp.sp_saler,
        sp.month_limit,
        sp.day_limit,
        sp.saleprice as spPrice,
        a.max_num,
        a.use_num,
        a.leftover_num,
        a.effect_month
        from ( select DISTINCT id
        spid,sp_name,fee_type,platform,sp_saler,is_api_billing,sa.month_limit,sa.day_limit,sa.saleprice from sms_sp_info sp left join sms_api_setting sa on sp.id=sa.sp_id
        where sp.status!=2  and sp.is_api_billing=0
        <if test="query.spId != null">
            and sp.id=#{query.spId}
        </if>
        <if test="query.spName != null and query.spName != '' ">
            and sp.sp_name like CONCAT('%',#{query.spName},'%')
        </if>
        ) sp left join (
        select sc.sp_id,
        sum(sc.max_num) max_num,
        sum(sc.use_num) use_num,
        sum(sc.leftover_num) leftover_num,
        (select
        max(sc1.effect_month) from sms_sp_charge sc1
        <where>
            <if test="query.spId != null">
                and sc1.sp_id=#{query.spId}
            </if>
        </where>
        ) effect_month
        from sms_sp_charge sc
        <where>
            <if test="query.spId != null">
                and sc.sp_id=#{query.spId}
            </if>
        </where>
        group by sc.SP_ID
        ) a
        on a.sp_id = sp.spid
        order by sp.spid desc
    </select>

    <select id="getChargeMonthPlanCountById" parameterType="integer" resultType="long">
        select max_num from sms_sp_charge
        <where>
            <if test="spId!=null and spId>0">
                sp_id = #{spId}
            </if>
        </where>
    </select>

    <select id="getChargeCountById" parameterType="integer" resultType="integer">
        select count(id) from sms_sp_charge
        <where>
            <if test="spId!=null and spId>0">
                sp_id = #{spId}
            </if>
        </where>
    </select>

    <insert id="insertSpCharge" useGeneratedKeys="true" keyProperty="id">
        insert into sms_sp_charge
        (sp_id,max_num,use_num,leftover_num,effect_month) values
        (#{spId},#{maxNum},#{userId},#{leftoverNum},#{effectMonth})
    </insert>

    <insert id="insertSpChargeRecord" useGeneratedKeys="true" keyProperty="id" parameterType="cn.roadm.admin.table.SmsChargeRecordTable">
        insert into sms_charge_record
        (sp_id,chargenum,chargeopeartor,productid,descption,amount,price,type,chargeafternum,charge_time,charge_mode,pay_time,pay_type)
        values
        (#{spId},#{chargenum},#{chargeopeartor},1,#{descption,jdbcType=VARCHAR},#{amount},#{price},#{type},#{chargeafternum},sysdate(),#{chargeMode},#{payTime},#{payType})
    </insert>

    <update id="updateSpCharge">
        update sms_sp_charge c
        set c.max_num = #{maxNum},
        c.leftover_num = c.leftover_num + #{number}
        where c.sp_id = #{spId}
    </update>

    <select id="getChargeLeftoverNumById" resultType="long">
        select ifnull(leftover_num,0) as leftover_num from sms_sp_charge
        <where>
            <if test="spId!=null and spId>0">
                sp_id = #{spId}
            </if>
        </where>
    </select>

    <update id="updateSpLeftover">
        update sms_sp_charge
        c set c.leftover_num =c.leftover_num-#{number}
        where c.sp_id = #{spId}
    </update>

    <select id="getSalePriceBySpId" resultType="double">
        select sa.saleprice from sms_sp_info sp,sms_api_setting sa
        where sp.id=sa.sp_id
        and sp.id =#{spId}
        and sp.status=1
        limit 1
    </select>

    <update id="updateSpLeftoverAndUseCount">
        update sms_sp_charge
        c set c.leftover_num =c.leftover_num-#{number},
        c.USE_NUM = c.USE_NUM + #{number}
        where c.sp_id = #{spId}
    </update>
</mapper>
