<?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.SpInfoMapper">
    <sql id="spInfoColumns">
        sp.id,sp.sp_name,sp.ip,sp.status,sp.sp_name,sp.createtime,sp.mobile_prefix,sp.mobile_suffix,sp.mobile_num,sp.unicom_prefix,sp.unicom_suffix,sp.unicom_num,sp.telecom_prefix,sp.telecom_suffix,sp.telecom_num,sp.report_url,sp.reply_url,sp.is_lock,sp.is_audit_word,
        sp.sp_type,sp.contact_name,sp.contact_mdn,sp.sp_saler,sp.sp_saler_mdn,sp.droptime,sp.sp_desc,sp.platfrom,sp.is_api,sp.return_fee_type
    </sql>


    <resultMap type="SpInfo" id="spInfoResultMap">
        <id column="id" property="spid"/>
        <result column="sp_name" property="sp_name"/>
        <result column="password" property="password"/>
        <result column="ip" property="ip"/>
        <result column="status" property="status"/>
        <result column="createtime" property="createTime"/>
        <result column="fee_type" property="feetype"/>
        <result column="mobile_prefix" property="mobile_prefix"/>
        <result column="mobile_suffix" property="mobile_suffix"/>
        <result column="mobile_num" property="mobile_num"/>
        <result column="unicom_prefix" property="unicom_prefix"/>
        <result column="unicom_suffix" property="unicom_suffix"/>
        <result column="unicom_num" property="unicom_num"/>
        <result column="telecom_prefix" property="telecom_prefix"/>
        <result column="telecom_suffix" property="telecom_suffix"/>
        <result column="telecom_num" property="telecom_num"/>

        <result column="mms_mobile_prefix" property="mms_mobile_prefix"/>
        <result column="mms_mobile_suffix" property="mms_mobile_suffix"/>
        <result column="mms_mobile_num" property="mms_mobile_num"/>
        <result column="mms_unicom_prefix" property="mms_unicom_prefix"/>
        <result column="mms_unicom_suffix" property="mms_unicom_suffix"/>
        <result column="mms_unicom_num" property="mms_unicom_num"/>
        <result column="mms_telecom_prefix" property="mms_telecom_prefix"/>
        <result column="mms_telecom_suffix" property="mms_telecom_suffix"/>
        <result column="mms_telecom_num" property="mms_telecom_num"/>
        <result column="batch_api_sms" property="batch_api_sms"/>
        <result column="fixed_length" property="fixed_length"/>
        <result column="zt_ms" property="zt_ms"/>
        <result column="report_url" property="report_url"/>
        <result column="reply_url" property="reply_url"/>
        <result column="push_flag" property="push_flag"/>
        <result column="is_lock" property="is_lock"/>
        <result column="username" property="username"/>
        <result column="is_audit_word" property="is_audit_word"/>
        <result column="error_count" property="error_count"/>
        <result column="contact_name" property="spContact_name"/>
        <result column="contact_mdn" property="spContact_mdn"/>
        <result column="sp_saler" property="sp_saler"/>
        <result column="sp_saler_mdn" property="sp_saler_mdn"/>
        <result column="droptime" property="droptime"/>
        <result column="sp_channelconfig" property="sp_channelconfig"/>
        <result column="sp_mms_channelconfig" property="sp_mms_channelconfig"/>
        <result column="sp_audit_type" property="audit_type"/>
        <result column="sp_type" property="sp_type"/>
        <result column="samemdnmaxnum" property="samemdnmaxnum"/>
        <result column="minsendtotal" property="minsendtotal"/>
        <result column="no_limit_mdn" property="no_limit_mdn"/>
        <result column="return_type" property="return_type"/>
        <result column="sp_audit_number" property="sp_audit_number"/>
        <result column="saleprice" property="saleprice"/>
        <result column="email" property="email"/>
        <result column="sp_desc" property="sp_desc"/>
        <result column="platform" property="platform"/>
        <result column="content_type" property="content_type"/>
        <result column="is_need_signture" property="is_need_signture"/>
        <result column="groupid" property="groupid"/>
        <result column="samecontentmaxnum" property="same_content_max_num"/>
        <result column="is_flowcode" property="is_flowcode"/>
        <result column="is_api" property="is_api"/>
        <result column="return_fee_type" property="return_fee_type"/>
        <result column="begin_send_time" property="beginSendTime"/>
        <result column="end_send_time" property="endSendTime"/>
        <result column="send_type" property="sendType"/>
        <result column="plan_send_time" property="planSendTime"/>
        <result column="is_send_priority" property="is_send_priority"/>
        <result column="send_priority" property="send_priority"/>
        <result column="business_license" property="business_license"/>
        <result column="identity_front" property="identity_front"/>
        <result column="identity_side" property="identity_side"/>
        <result column="check_signture_bank" property="check_signture_bank"/>
        <result column="is_contract" property="isContract"/>
        <result column="fail_retrun" property="failRetrun"/>
        <result column="product_id" property="productId"/>
    </resultMap>

	<resultMap type="SpInfo" id="spInfoAndSpcharge">
        <result column="spid" property="spid"/>
        <result column="sp_name" property="sp_name"/>
        <result column="fee_type" property="feetype"/>
        <result column="effect_month" property="effect_month"/>
        <result column="is_api_billing" property="is_api_billing"/>
        <association property="spCharge" column="spid" javaType="SpCharge">
            <id column="spid" property="sp_id"/>
            <result column="max_num" property="max_num"/>
            <result column="use_num" property="use_num"/>
            <result column="leftover_num" property="leftover_num"/>
            <result column="id" property="id"/>
        </association>
    </resultMap>
    
    <resultMap type="SpInfo" id="spInfoAndSpmoney">
        <result column="spid" property="spid"/>
        <result column="sp_name" property="sp_name"/>
        <result column="fee_type" property="feetype"/>
        <result column="effect_month" property="effect_month"/>
        <result column="is_api_billing" property="is_api_billing"/>
        <result column="money" property="money"/>
        <result column="use_money" property="useMoney"/>
    </resultMap>
    
     <resultMap type="ChargeRecord" id="ChargeRecordResult">
        <id column="id" property="id"/>
        <result column="sp_name" property="sp_name"/>
        <result column="chargenum" property="chargeNum"/>
        <result column="chargeopeartor" property="operator"/>
        <result column="amount" property="amount"/>
        <result column="price" property="price"/>
        <result column="sp_id" property="sp_id"/>
        <result column="type" property="type"/>
        <result column="descption" property="descption"/>
        <result column="platform" property="platform"/>
        <result column="sa_id" property="sa_id"/>
    </resultMap>

    <select id="listPageSpInfo" parameterType="SpInfo" resultMap="spInfoResultMap">
        select sp.id,sp.sp_name,sp.ip, sp.status,
        sp.international_prefix,sp.international_suffix,sp.international_num,
        sp.fee_type,sp.createtime,sp.mobile_prefix,sp.mobile_suffix,sp.mobile_num,sp.unicom_prefix,sp.unicom_suffix,sp.unicom_num,sp.telecom_prefix,sp.telecom_suffix,sp.telecom_num,
        sp.mms_mobile_prefix,sp.mms_mobile_suffix,sp.mms_mobile_num,sp.mms_unicom_prefix,sp.mms_unicom_suffix,sp.mms_unicom_num,sp.mms_telecom_prefix,sp.mms_telecom_suffix,sp.mms_telecom_num,
        sp.report_url,sp.reply_url,sp.is_lock,
        sp.is_audit_word,sp.contact_name,sp.contact_mdn,sp.sp_saler,sp.sp_saler_mdn,sp.droptime,sp.sp_desc,sp.platform
        ,sp.is_api_billing,sp.fail_retrun failRetrun,is_contract isContract,product_id productId
        from sms_sp_info sp
        where sp.status=1 and sp.sa_id=#{saId}
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="platform!=null and platform!='' and platform >0">
            and sp.platform =#{platform}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="spid!=null and spid!=0">
            and sp.id=#{spid}
        </if>
        <if test="createTime!=null and createTime!='' ">
            and sp.createtime&gt;=#{createTime}
        </if>
        <if test="createTime1!=null and createTime1!='' ">
            and sp.createtime&lt;=DATE_ADD(#{createTime1},INTERVAL 1 DAY)
        </if>
        order by createtime desc
    </select>

	<select id="listAllSaler" parameterType="int" resultMap="spInfoResultMap">
      select si.id,
         si.user_name,
         si.role_id,
         si.name sp_saler,
         sr.role_name,
         sr.id
         from  sms_user_info si,sms_user_role sr
         where si.role_id=sr.id and sr.id = 42 ;
    </select>

    
    <select id="getSystemTitle" parameterType="String"  resultType="String">
        select a.value from sms_system_config a
        where a.config_key = #{key}
               limit 1
    </select>    
    
    
    <update id="updateSpInfo" parameterType="SpInfo">
        update sms_sp_info set
        sp_name=#{sp_name,jdbcType=VARCHAR},
        platform=#{platform,jdbcType=VARCHAR},
        <!--    username=#{username,jdbcType=VARCHAR},
            password=#{password,jdbcType=VARCHAR}, -->
        is_audit_word=#{is_audit_word},
        contact_name=#{spContact_name,jdbcType=VARCHAR},
        contact_mdn=#{spContact_mdn,jdbcType=VARCHAR},
        ip=#{ip,jdbcType=VARCHAR},
        sp_type=#{sp_type},
        report_url=#{report_url,jdbcType=VARCHAR},
        reply_url=#{reply_url,jdbcType=VARCHAR},
        sp_saler=#{sp_saler,jdbcType=VARCHAR},
        sp_saler_mdn=#{sp_saler_mdn,jdbcType=VARCHAR},
        is_need_signture=#{is_need_signture,jdbcType=VARCHAR},
        check_signture_bank=#{check_signture_bank,jdbcType=VARCHAR},
        signature=#{signature,jdbcType=VARCHAR},
        groupid=#{groupid,jdbcType=VARCHAR},
        is_api=#{is_api,jdbcType=INTEGER},
        return_fee_type=#{return_fee_type,jdbcType=INTEGER},
        is_audit_black=#{is_audit_black,jdbcType=INTEGER},
        template_word_policy=#{template_word_policy,jdbcType=INTEGER},
        is_api_billing=#{is_api_billing,jdbcType=INTEGER},
        grade=#{grade,jdbcType=VARCHAR},
        sensitive_word_type=#{sensitive_word_type,jdbcType=VARCHAR},
        verification_code_free_review=#{verification_code_free_review},
        is_send_priority=#{is_send_priority,jdbcType=INTEGER},
        is_web=#{is_web,jdbcType=INTEGER},
        send_priority=#{send_priority,jdbcType=INTEGER},
        business_license=#{business_license,jdbcType=VARCHAR},
        identity_front=#{identity_front,jdbcType=VARCHAR},
        identity_side=#{identity_side,jdbcType=VARCHAR},
        url_detect=#{url_detect,jdbcType=INTEGER},
        is_contract=#{isContract},
        fail_retrun=#{failRetrun}
        
        where id=#{spid}
    </update>
    
    <update id="updateSpSetting" parameterType="SpInfo">
        update
        sms_api_setting c
        set c.sp_audit_type = #{audit_type},content_type=#{content_type,jdbcType=VARCHAR} 
        ,send_province=#{send_province,jdbcType=VARCHAR}
        ,c.saleprice = #{saleprice}
        where
        c.sp_id = #{spid}
    </update>
    
    <update id="updateSpUserInfo" parameterType="SpInfo">
        update sms_sp_user_info s
        <trim prefix="set" suffixOverrides=",">
            <if test="username!=null">s.username=#{username}</if>
            <if test="password!=null">,s.password=#{password},s.pawd=#{pawd}</if>
            <if test="web_password!=null">,s.web_password=#{web_password},s.web_pawd=#{web_pawd}</if>
        </trim>
        where s.sp_id=#{spid}
        and s.role_id=0
    </update>
    <insert id="insertSpinfo" useGeneratedKeys="true" keyProperty="spid" parameterType="SpInfo">
        insert into sms_sp_info
        (sp_name,ip,report_url,reply_url,is_lock,is_audit_word,
        contact_name,contact_mdn,sp_saler,sp_saler_mdn,sp_type,
        platform,is_need_signture,check_signture_bank,groupid,signature,createtime,is_api,
        return_fee_type,template_word_policy,
        is_api_billing,is_audit_black,grade,sensitive_word_type,verification_code_free_review,
        is_send_priority,is_web,send_priority,business_license,identity_front,identity_side
        ,url_detect,is_contract,fail_retrun,sa_id)
        values
        (#{sp_name},#{ip,jdbcType=VARCHAR},#{report_url,jdbcType=VARCHAR},#{reply_url,jdbcType=VARCHAR},1,#{is_audit_word},
        #{spContact_name,jdbcType=VARCHAR},#{spContact_mdn,jdbcType=VARCHAR},#{sp_saler,jdbcType=VARCHAR},#{sp_saler_mdn,jdbcType=VARCHAR}, #{sp_type},
        #{platform},#{is_need_signture},#{check_signture_bank},#{groupid},#{signature,jdbcType=VARCHAR},sysdate(),
        #{is_api},#{return_fee_type,jdbcType=INTEGER},#{template_word_policy,jdbcType=INTEGER},
        #{is_api_billing,jdbcType=INTEGER},
        #{is_audit_black,jdbcType=INTEGER},
        #{grade,jdbcType=VARCHAR},
        #{sensitive_word_type,jdbcType=VARCHAR},
        #{verification_code_free_review,jdbcType=INTEGER},
        #{is_send_priority,jdbcType=INTEGER},
        #{is_web,jdbcType=INTEGER},
        #{send_priority,jdbcType=INTEGER},
        #{business_license,jdbcType=VARCHAR},
        #{identity_front,jdbcType=VARCHAR},
        #{identity_side,jdbcType=VARCHAR},
        #{url_detect,jdbcType=INTEGER},
        #{isContract,jdbcType=INTEGER},
        #{failRetrun,jdbcType=INTEGER},
        #{saId,jdbcType=INTEGER})
    </insert>
    
     <insert id="insertSpUserInfo" useGeneratedKeys="true" keyProperty="userid" parameterType="SpInfo">
        insert into
        sms_sp_user_info
        (sp_id, role_id, username, web_password, password, web_pawd, pawd, createtime)
        values
        (#{spid},0,#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},#{pawd,jdbcType=VARCHAR},#{pawd,jdbcType=VARCHAR},sysdate())
    </insert>
    
    <insert id="insertSpSetting" parameterType="SpInfo">
        insert into
        sms_api_setting
        (sp_id,sp_audit_type,return_type,content_type,send_province)
        values(#{spid},#{audit_type,jdbcType=VARCHAR},#{return_type},#{content_type},#{send_province})
    </insert>
    
        <select id="getSpinfoById" parameterType="int" resultMap="spInfoResultMap">
        select
        sp.id,sp.sp_name,sp.ip,sp.status,sp.fee_type,sp.createtime
        ,sp.mms_mobile_prefix,sp.mms_mobile_suffix,sp.mms_mobile_num,sp.mms_unicom_prefix,sp.mms_unicom_suffix,sp.mms_unicom_num,sp.mms_telecom_prefix,sp.mms_telecom_suffix,sp.mms_telecom_num
        ,sp.mobile_prefix,sp.mobile_suffix,sp.mobile_num,sp.unicom_prefix,sp.unicom_suffix,sp.unicom_num,sp.telecom_prefix,sp.telecom_suffix,sp.telecom_num
        
        ,sa.report_url,sa.reply_url,sa.push_flag,sp.is_lock,sp.is_audit_word,sp.is_audit_black,sp.template_word_policy,sp.is_api_billing,sp.sp_type,
        su.username,su.web_pawd password,sp.contact_name,sp.contact_mdn,sp.sp_saler,sp.signature,sp.sp_saler_mdn,sp.droptime,sp.sp_channelconfig,sa.sp_audit_type,
        sa.samemdnmaxnum,
        sa.minsendtotal,
        sa.no_limit_mdn,
        sa.return_type,
        sa.saleprice,
        sa.saleprice display_saleprice,
        sa.sp_audit_number,
        sa.isvalidbome,
        sa.isreportnow,
        sa.samecontentmaxnum,
        sa.is_flowcode,
        sp.sp_desc,
        sp.platform,
        sa.content_type,
        sp.is_need_signture,
        sp.check_signture_bank,
        sp.groupid,
        sp.is_api,
        sp.is_web,
        sp.url_detect,
        
        sp.return_fee_type,
        sa.begin_send_time,
        sa.end_send_time,
        sa.send_type,
        sa.plan_send_time,
        su.id userid,
        sp.grade,
        sa.samemdn_nday,
        sa.limit_days,
        sa.samemdn_threeday,
        sa.samemdn_oneday,
        sa.samemdn_oneminute,
        sa.samemdn_onehour,
        sp.sensitive_word_type,
        sp.verification_code_free_review,
        sp.is_send_priority,
        sa.push_buyun,
        sa.push_receiver,
        sa.push_pswd,
        sa.samemsg_nminute,
        sa.samemsg_nnum,
        
        sa.batch_api_sms,
        sa.fixed_length,
        sa.zt_ms,
        sp.send_priority,
        sa.send_province,
        
        sp.business_license,
        sp.identity_front,
        sp.identity_side,
        sp.is_contract isContract,
        sp.fail_retrun failRetrun
        from
        sms_sp_info sp,sms_api_setting sa,sms_sp_user_info su
        where sp.id=sa.sp_id
        and su.sp_id=sp.id
        and su.role_id=0
        and sp.id =#{spid}
        and sp.status=1
        limit 1
    </select>
   	<select id="getSpCountByUsername" parameterType="SpInfo"
            resultType="int">
            select count(1) from sms_sp_info s
            <where>
                <if test="username!=null and username!=''">
                    s.username = #{username}
                </if>

                and s.status=1
            </where>
     </select>
     
     
     <update id="deleteSpInfo" parameterType="int">
        update sms_sp_info set
        status=2,droptime=sysdate() where id=#{spid}
    </update>
    
     <update id="updateSpInfostatus" parameterType="map">
        update sms_sp_info
        set is_lock=#{isLosk} where id=#{spid}
    </update>
    
     <update id="updateSpInfoDescById">
        update sms_sp_info set
        sp_desc=#{sp_desc,jdbcType=VARCHAR} where id=#{spid}
    </update>
    
    
    <select id="listPageSpCharge" parameterType="SpInfo"
            resultMap="spInfoAndSpmoney">
        select sp.id spid,sp.sp_name,sp.is_api_billing,sm.money,sm.use_money from sms_sp_info sp left join sms_sp_money sm on sp.id=sm.spid 
        <where>
        	sp.sa_id=#{saId}
        	<if test="spid!=null and spid!=''">
        		and sp.id=#{spid}
        	</if>
        	<if test="sp_name!=null and sp_name!=''">
        		and sp.sp_name like CONCAT('%',#{sp_name},'%')
        	</if>
        </where> 
    </select>
    
    <select id="getChargeCountById" parameterType="SpCharge" resultType="int">
        select count(id) from sms_sp_charge
        <where>
            <if test="sp_id!=null and sp_id>0">
                sp_id = #{sp_id}
            </if>
        </where>
    </select>
    
     <select id="getAgentChargeById" resultType="SpCharge">
        select id,sa_id,ifnull(leftover_money,0) leftoverMoney from sms_agent_charge
        <where>
            <if test="sa_id!=null and sa_id>0">
                sa_id = #{sa_id}
            </if>
        </where>
        limit 1
    </select>
    
    <update id="updateAgentCharge" parameterType="SpCharge">
    	update sms_agent_charge set leftover_money=#{leftoverMoney} where sa_id=#{sa_id}
    </update>
    <insert id="insertSpCharge" useGeneratedKeys="true" keyProperty="id" parameterType="SpCharge">
        insert into sms_sp_charge
        (sp_id,max_num,use_num,leftover_num,effect_month) values
        (#{sp_id},#{max_num},#{use_num},#{leftover_num},#{effect_month})
    </insert>
    
     <insert id="insertSpChargeRecord" useGeneratedKeys="true" keyProperty="id" parameterType="ChargeRecord">
        insert into sms_charge_record
        (sp_id,chargeopeartor,productid,descption,amount,type,chargeaftermoney,charge_time)
        values
        (#{sp_id},#{operator,jdbcType=VARCHAR},1,#{descption,jdbcType=VARCHAR},#{amount},#{type},#{chargeAfterMoney},sysdate())
    </insert>
    <update id="updateSpCharge" parameterType="SpCharge">
        update sms_sp_charge c
        set c.max_num = #{max_num},
        c.leftover_num = c.leftover_num + #{chargeNum}
        <!--         where c.effect_month = date_format(sysdate(),'%Y-%m') -->
        where c.sp_id = #{sp_id}
    </update>
    
    <select id="getChargeLeftoverNumById" parameterType="SpCharge" resultType="int">
        select leftover_num from sms_sp_charge
        <where>
            <if test="sp_id!=null and sp_id>0">
                sp_id = #{sp_id}
            </if>
        </where>
    </select>
    
    <update id="updateSpLeftover" parameterType="SpCharge">
        update sms_sp_charge
        c set c.leftover_num =c.leftover_num-#{max_num}
        <!--         where c.effect_month = date_format(sysdate(),'%Y-%m') -->
        where c.sp_id = #{sp_id}
    </update>
    
    <select id="getSpIdBySaId" parameterType="SpInfo" resultType="int">
    	select id from sms_sp_info where sa_id=#{saId}
    </select>
    
     <select id="getChargeAmountBymonth" parameterType="ChargeRecord" resultType="float">
        select IFNULL(sum(sd.chargenum),0) chargeNum
        from sms_charge_record
        sd,sms_sp_info sp
        where sp.id=sd.sp_id and sp.sa_id=#{sa_id}
        <if test="month!=null and month !=''">
            and date_format(sd.charge_time,'%Y-%m')=#{month}
        </if>
        <if test="sp_name!=null and sp_name !=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_id!=null and sp_id>0">
            and sd.sp_id = #{sp_id}
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>
        <if test="createTime!=null and createTime!=''">
            and sd.charge_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            sd.charge_time&lt;=DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>

    </select>
    
     <!-- 充值记录列表 -->
    <select id="listPageChargeRecord" parameterType="ChargeRecord" resultMap="ChargeRecordResult">
        select sd.id,
        sd.charge_time,
        sd.sp_id,
        sd.chargenum,
        sd.chargeafternum,
        sd.chargeopeartor,
        sd.amount,
        sd.type,
        sd.price,
        sd.price display_price,
        sd.descption,
        sp.sp_name,
        sp.platform,
        sd.chargeaftermoney chargeAfterMoney
        from
        sms_charge_record sd, sms_sp_info sp
        where sp.id = sd.sp_id and sp.sa_id=#{sa_id}
        and sp.status!=2
        <if test="platform!=null and platform!=0">
            and sp.platform = #{platform}
        </if>
        <if test="sp_id!=null and sp_id!=0">
            and sp.id=#{sp_id}
        </if>
        <if test="month!=null and month!=''">
            and date_format(sd.charge_time ,'%Y-%m')=#{month}
        </if>
        <if test="createTime!=null and createTime!=''">
            and sd.charge_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            sd.charge_time&lt;=DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_saler!=null and sp_saler!=''">
            and sp.sp_saler =#{sp_saler}
        </if>

        order by sd.charge_time desc
    </select>
    
    <select id="exportspChargeRecord" parameterType="ChargeRecord" resultType="Map">
        select sd.id,
        date_format( sd.charge_time,'%Y-%m-%d %H:%i:%s')
        charge_time,
        sd.sp_id,
        sd.chargenum,
        sd.chargeopeartor,
        sd.amount,
        <!--         decode(sd.type,1,'充值',2,'扣减')type, -->
        case
        when sd.type=1 then '充值'
        when sd.type=2 then '扣减'
        else '其他类型'
        end as type,
        sd.price,
        sd.descption,
        sp.sp_name
        from sms_charge_record sd, sms_sp_info sp
        where sp.id = sd.sp_id and sp.sa_id=#{sa_id}
        <if test="sp_id!=null and sp_id!=0">
            and sp.id=#{sp_id}
        </if>
        <if test="month!=null and month!=''">
            and date_format(sd.charge_time ,'%Y-%m')=#{month}
        </if>
        <if test="createTime!=null and createTime!=''">
            and sd.charge_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            sd.charge_time&lt;=DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        order by sd.charge_time desc
    </select>
    
    
    <select id="getMoneyCountById" parameterType="SpCharge" resultType="int">
        select count(id) from sms_sp_money
        <where>
            <if test="sp_id!=null and sp_id>0">
                spid = #{sp_id}
            </if>
        </where>
    </select>
    <insert id="insertMoney">
    	insert into sms_sp_money(spid,money,use_money) values(#{spId},#{money},0)
    </insert>
    
     <update id="updateSpMoney" parameterType="cn.roadm.bo.SpMoney">
        update sms_sp_money c
        set c.money = c.money + #{money}
        <!--         where c.effect_month = date_format(sysdate(),'%Y-%m') -->
        where c.spid = #{spId}
    </update>
    <select id="getMoneyById" parameterType="cn.roadm.bo.SpMoney" resultType="cn.roadm.bo.SpMoney">
        select money from sms_sp_money
        <where>
            <if test="spId!=null and spId>0">
                spid = #{spId}
            </if>
        </where>
    </select>
    
    <update id="updateSpMoneyReduce" parameterType="cn.roadm.bo.SpMoney">
        update sms_sp_money
        c set c.money =c.money-#{money}
        <!--         where c.effect_month = date_format(sysdate(),'%Y-%m') -->
        where c.spid = #{spId}
    </update>
    
    
	<select id="getSpPriceById" parameterType="int" resultType="cn.roadm.bo.SpPrice">
    	select id,name,lt_price ltPrice,yd_price ydPrice,dx_price dxPrice,DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') cretetime,type from sms_price where id=#{id}  
    </select>
</mapper>