<?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.AgentInfoMapper">
	<resultMap type="MenuInfo" id="menuResultMap">
        <id column="id" property="menu_id"/>
        <result column="menu_name" property="menuName"/>
        <result column="menu_url" property="menu_url"/>
        <result column="parent_id" property="parentid"/>
    </resultMap>

	<select id="listPageAgentInfo" parameterType="AgentInfo" resultType="AgentInfo">
		select id,sa_name saName,`status`,ip,DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') createtime,
		DATE_FORMAT(droptime, '%Y-%m-%d %H:%i:%s') droptime,is_lock isLock,contact_name contactName,
		contact_mdn contactMdn,saler,saler_mdn salerMdn,return_fee_type returnFeeType,is_api_billing isApiBilling,billing_mode,fail_retrun
		from sms_agent_info
		<where>
			status=1
			<if test="id!=null and id!=''">
				and id=#{id}
			</if>
			<if test="saName!=null and saName!=''">
				 and sa_name like CONCAT('%',#{saName},'%')
			</if>
			<if test="saler!=null and saler!=''">
				and saler=#{saler}
			</if>
			<if test="createtime!=null and createtime!=''">
				and createtime 	&gt; #{createtime}
			</if>
			<if test="createtime1!=null and createtime1!=''">
				and createtime 	&lt; date_sub(#{createtime1},interval -1 day)
			</if>
			<if test="billing_mode!=null and billing_mode!=''">
				and billing_mode = #{billing_mode}
			</if>
		</where>
	</select>
	
	<insert id="insertAgentInfo" parameterType="AgentInfo"  useGeneratedKeys="true" keyProperty="id">
		insert sms_agent_info(id,sa_name,ip,createtime,contact_name,contact_mdn,saler,saler_mdn,return_fee_type,is_api_billing,saleprice,billing_mode,fail_retrun, audit_strategy, custom_audit)
		VALUES(id,#{saName},#{ip},NOW(),#{contactName},#{contactMdn},#{saler},#{salerMdn},#{returnFeeType},#{isApiBilling},#{saleprice},#{billing_mode},#{fail_retrun}, #{auditStrategy}, #{customAudit})
	</insert>
	<insert id="insertAgentUserInfo" parameterType="AgentInfo">
		insert into sms_agent_user_info(sa_id,role_id,username,`password`,createtime,pawd)
		VALUES(#{saId},0,#{username},#{password},NOW(),#{pawd})
	</insert>
	<select id="getAgentUserByUsername" resultType="int">
		select count(id) from sms_agent_user_info where username =#{username}
	</select>
	<select id="getAgentUserBySaId" resultType="AgentInfo">
		select sai.id,sau.username,sau.pawd,sai.ip,
		sai.contact_name contactName,sai.contact_mdn contactMdn,
		sai.saler,sai.saler_mdn salerMdn,sai.return_fee_type returnFeeType,
		sai.is_api_billing isApiBilling,sai.sa_name saName,sai.saleprice,sai.billing_mode,sai.fail_retrun, sai.audit_strategy as auditStrategy, custom_audit as customAudit,sau.user_rights from
		sms_agent_user_info sau,sms_agent_info sai where
		sau.sa_id=sai.id and sai.id=#{id} and sai.status=1 limit 1
	</select> 
	<update id="updateAgentInfo" parameterType="AgentInfo">
		update sms_agent_info set sa_name=#{saName},ip=#{ip},contact_name=#{contactName},contact_mdn=#{contactMdn},
		saler=#{saler},saler_mdn=#{salerMdn},return_fee_type=#{returnFeeType},is_api_billing=#{isApiBilling},saleprice=#{saleprice},fail_retrun=#{fail_retrun},
        audit_strategy=#{auditStrategy}, custom_audit=#{customAudit} where id=#{id}
	</update>
	<update id="updateAgentUserInfo" parameterType="AgentInfo">
		update sms_agent_user_info set password=#{password},pawd=#{pawd} where sa_id=#{id} and role_id=0
	</update>
	
	<update id="deleteAgentInfo">
		update sms_agent_info set `status`=2,droptime=now() where id=#{id}
	</update>
	<update id="deleteAgentUserInfo">
		update sms_agent_user_info set `status`=2,droptime=now() where sa_id=#{id}
	</update>
	<update id="deleteAgentSpInfo">
		update sms_sp_info set sa_id=null where sa_id=#{id}
	</update>
	<update id="lockAgentInfo">
		update sms_agent_info set is_lock=#{isLock} where id=#{id} 
	</update>
	
	
	
	<select id="getAgentChargeBySaId" resultType="int">
		select COUNT(*) from sms_agent_charge where sa_id=#{saId}
	</select>
	<select id="getAgentCharge" resultType="cn.roadm.bo.AgentCharge">
		select id,leftover_num leftoverNum,ifnull(leftover_money,0) leftoverMoney from sms_agent_charge where sa_id=#{saId} limit 1
	</select>
	<insert id="insertAgentCharge" parameterType="cn.roadm.bo.AgentCharge">
		insert INTO sms_agent_charge(sa_id,use_num,leftover_num,effect_month,leftover_money) values(#{saId},0,#{leftoverNum},#{effectMonth},#{leftoverMoney})
	</insert>
	<update id="updateAgentChange" parameterType="cn.roadm.bo.AgentCharge">
		update sms_agent_charge set use_num=#{useNum},effect_month=#{effectMonth},
		max_num=#{maxNum},balance=#{balance},use_money=#{useMoney},leftover_money=#{leftoverMoney} where sa_id=#{saId}
	</update>
	<insert id="insertAgentRecord">
		insert INTO sms_agent_charge_record(sa_id,charge_time,chargenum,chargeopeartor,productid,amount,price,descption,type,chargeafternum,create_time,chargeaftermoney)
		VALUES(#{saId},NOW(),#{chargeNum},#{chargeOpeartor},1,#{amount},#{price},#{descption},#{type},#{chargeAfterNum},now(),#{chargeAfterMoney}) 
	</insert>
	
	<select id="listPageSaCharge" parameterType="cn.roadm.bo.AgentInfo" resultType="cn.roadm.bo.AgentCharge">
		 select sp.saId,
        sp.sa_name,
        sp.is_api_billing,
        a.max_num maxNum,
        a.use_money useMoney,
        ifnull(a.leftover_money,0) leftoverMoney,
        a.effect_month effectMonth,
        sp.billing_mode,
         ifnull(a.leftover_num,0) leftoverNum
        from ( select id
        saId,sa_name,is_api_billing,billing_mode from sms_agent_info where status!=2
        <if test="saName!=null and saName!=''"> 
            and sa_name like CONCAT('%',#{saName},'%')
         </if>
         <if test="saler!=null and saler!=''">
             and saler = #{saler}
         </if>
        ) sp left join (
        select sc.sa_id,
        sum(sc.max_num) max_num,
        sum(sc.use_money) use_money,
        sum(sc.leftover_money) leftover_money,
        sum(sc.leftover_num) leftover_num,
        (select
        max(sc1.effect_month) from sms_agent_charge sc1
        <where>
             <if test="saId!=null">
                and sc1.sa_id=#{saId}
             </if>
        </where>
        ) effect_month
        from sms_agent_charge sc
        <where>
             <if test="saId!=null">
                 and sc.sa_id=#{saId}
             </if>
        </where>
        group by sc.SA_ID
        ) a on a.sa_id = sp.saId
        <where>
        	<if test="saId!=null">
                and sp.saId=#{saId}
             </if>
        </where>
        order by sp.said desc
	</select>
	<select id="getAgentMoneyById" resultType="cn.roadm.bo.AgentCharge">
		select ifnull(leftover_money,0) leftoverMoney from sms_agent_charge where sa_id=#{id}
	</select>
	<select id="getAgentNumById" resultType="cn.roadm.bo.AgentCharge">
		select ifnull(leftover_num,0) leftoverNum from sms_agent_charge where sa_id=#{id}
	</select>
	<update id="updateAgentChargeAdd" parameterType="cn.roadm.bo.AgentCharge">
		update sms_agent_charge set leftover_money=ifnull(leftover_money,0)+#{leftoverMoney} where SA_ID=#{saId}
	</update>
	<update id="updateAgentChargeReduce" parameterType="cn.roadm.bo.AgentCharge">
		update sms_agent_charge set leftover_money=leftover_money-#{leftoverMoney} where SA_ID=#{saId}
	</update>
	
	<update id="updateAgentChargeNumAdd" parameterType="cn.roadm.bo.AgentCharge">
		update sms_agent_charge set leftover_num=ifnull(leftover_num,0)+#{leftoverNum} where SA_ID=#{saId}
	</update>
	
	<update id="updateAgentChargeNumReduce" parameterType="cn.roadm.bo.AgentCharge">
		update sms_agent_charge set LEFTOVER_NUM=leftover_num-#{leftoverNum} where SA_ID=#{saId}
	</update>
	
	<!-- 充值记录列表 -->
    <select id="listPageAgentChargeRecord" parameterType="cn.roadm.bo.AgentCharge" resultType="cn.roadm.bo.AgentCharge">
        select sd.id,
        sd.charge_time chargeTime,
        sd.sa_id saId,
        sd.chargeNum,
        sd.chargeafternum chargeAfterNum,
        sd.chargeopeartor chargeOpeartor,
        sd.amount,
        sd.type,
        sd.price,
        sd.descption,
        sp.sa_name sa_name,
        sd.chargeaftermoney chargeAfterMoney
        from
        sms_agent_charge_record sd, sms_agent_info sp
        where sp.id = sd.sa_id
        and sp.status!=2
        <if test="saId!=null and saId!=0">
            and sp.id=#{saId}
        </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="sa_name!=null and sa_name!=''">
            and sp.sa_name like CONCAT('%',#{sa_name},'%')
        </if>
        <if test="saler!=null and saler!=''">
            and sp.saler =#{saler}
        </if>
          order by sd.charge_time desc
       </select>
       
       
       <!-- 短信充值月记录总费用 -->
    <select id="getChargeAmountBymonth" parameterType="cn.roadm.bo.AgentCharge" resultType="float">
        select IFNULL(sum(sd.amount),0) amount
        from sms_agent_charge_record
        sd,sms_agent_info sp
        where sp.id=sd.sa_id and type=1 and status=1
        <if test="month!=null and month !=''">
            and date_format(sd.charge_time,'%Y-%m')=#{month}
        </if>
        <if test="sa_name!=null and sa_name !=''">
            and sp.sa_name like CONCAT('%',#{sa_name},'%')
        </if>
        <if test="saId!=null and saId>0">
            and sd.sa_id = #{saId}
        </if>
        <if test="saler!=null and saler!=''">
            and sp.saler =#{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="getChargeAmountNumBymonth" parameterType="cn.roadm.bo.AgentCharge" resultType="int">
        select IFNULL(sum(sd.CHARGENUM),0) chargeNum
        from sms_agent_charge_record
        sd,sms_agent_info sp
        where sp.id=sd.sa_id and type=3 and sp.status=1
        <if test="month!=null and month !=''">
            and date_format(sd.charge_time,'%Y-%m')=#{month}
        </if>
        <if test="sa_name!=null and sa_name !=''">
            and sp.sa_name like CONCAT('%',#{sa_name},'%')
        </if>
        <if test="saId!=null and saId>0">
            and sd.sa_id = #{saId}
        </if>
        <if test="saler!=null and saler!=''">
            and sp.saler =#{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="getAllAgentInfo" parameterType="AgentInfo" resultType="AgentInfo">
    	select id,sa_name saName from sms_agent_info where status=1
    </select>
    
    <select id="listPageParentMenu" parameterType="MenuInfo"  resultMap="menuResultMap">
        select id,menu_name,menu_url,parent_id from sms_agent_menu where parent_id =0 and status=1
        <if test="menuName!=null and menuName!=''">
            and menu_name    like CONCAT('%',#{menuName},'%') 
        </if>
         order by id asc 
    </select>
    
    <select id="listSubMenuByParentId" parameterType="int" resultMap="menuResultMap">
        select id,menu_name,menu_url,parent_id from sms_agent_menu where parent_id=#{menu_id} and status=1
    </select>
    <select id="listAllParentMenu" resultMap="menuResultMap">
        select id,menu_name,menu_url,parent_id from sms_agent_menu where parent_id =0 and status=1 order by id asc 
    </select>
     <select id="getMenuById" parameterType="int" resultMap="menuResultMap">
        select id,menu_name,menu_url,parent_id from sms_agent_menu where id=#{menu_id} 
    </select>
    
    <update id="updateMenu" parameterType="MenuInfo">
        update sms_agent_menu set 
        menu_name=#{menuName},
        menu_url=#{menu_url},
        parent_id=#{parentid} 
        where id=#{menu_id}
    </update>
    
     <insert id="insertMenu"  useGeneratedKeys="true" keyProperty="menu_id" parameterType="MenuInfo" >
        insert into sms_agent_menu (menu_name,menu_url,parent_id) values (#{menuName},#{menu_url,jdbcType=VARCHAR},#{parentid})
    </insert>
    <update id="deleteMenuById" parameterType="int">
        update  sms_agent_menu set status=2 where id=#{menu_id} or parent_id=#{menu_id}
    </update>
    <select id="exportspChargeRecord" parameterType="cn.roadm.bo.AgentCharge" resultType="Map">
        select sd.id,
        date_format( sd.charge_time,'%Y-%m-%d %H:%i:%s')
        chargeTime,
        sd.sa_id saId,
        sd.chargenum 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.sa_name
        from sms_agent_charge_record sd, sms_agent_info sp
        where sp.id = sd.sa_id
        <if test="saId!=null and saId!=0">
            and sp.id=#{saId}
        </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="sa_name!=null and sa_name!=''">
            and sp.sa_name like CONCAT('%',#{sa_name},'%')
        </if>
        order by sd.charge_time desc
    </select>
    
   <update id="editSpInfoMode" parameterType="AgentInfo">
    	update sms_api_setting set billing_mode=#{billing_mode} where SP_ID in (select id from sms_sp_info where sa_id=#{id})
    </update>
    
    <update id="editAgentMode" parameterType="AgentInfo">
    	update sms_agent_info set billing_mode=#{billing_mode} where id=#{id}
    </update> 
    <select id="listPageAgentChannelConfig" parameterType="AgentChannelConfig" resultType="AgentChannelConfig">
    	select sai.id,sai.sa_name,sac.network,sac.type,sac.mobile,sac.unicom,sac.telecom,sac.mobileGroup,sac.unicomGroup,
    	sac.telecomGroup,sac.internation
    	 from sms_agent_info sai left join sms_agent_channel_config sac on sai.id=sac.saId
    	<where>
    		sai.status=1
    		<if test="id!=null and id!=''">
    			and sai.id=#{id}
    		</if>
    		<if test="sa_name!=null and sa_name!=''">
    			and sai.sa_name like concat('%',#{sa_name},'%')
    		</if>
    		<if test="mobile!=null and mobile!=''">
    			and substring_index(mobile,",",1) = #{mobile}
    		</if>
    		<if test="unicom!=null and unicom!=''">
    			and substring_index(unicom,",",1) = #{unicom}
    		</if>
    		<if test="telecom!=null and telecom!=''">
    			and substring_index(telecom,",",1) = #{telecom}
    		</if>
    	</where>
    </select>
    <select id="agentChannelConfigById" parameterType="int" resultType="AgentChannelConfig">
    	select sai.id,sai.sa_name,sac.network,sac.type,sac.mobile,sac.unicom,sac.telecom,sac.mobileGroup,sac.unicomGroup,
    	sac.telecomGroup,sac.internation,defaultSuffix
    	 from sms_agent_info sai left join sms_agent_channel_config sac on sai.id=sac.saId where sai.id=#{id}
    </select>
    <select id="getagentChannelConfig" parameterType="int" resultType="AgentChannelConfig">
    	select sai.id,sai.sa_name,sac.network,sac.type,sac.mobile,sac.unicom,sac.telecom,sac.mobileGroup,sac.unicomGroup,
    	sac.telecomGroup,sac.internation,defaultSuffix
    	 from sms_agent_info sai inner join sms_agent_channel_config sac on sai.id=sac.saId where sai.id=#{id}
    </select>
    <insert id="insertAgentChannelConfig" parameterType="AgentChannelConfig">
    	insert into sms_agent_channel_config(mobile,unicom,telecom,saId,defaultSuffix)
    	values(#{mobile},#{unicom},#{telecom},#{id},#{defaultSuffix})
    </insert>
    <update id="updateAgentChannelConfig" parameterType="AgentChannelConfig">
    	update sms_agent_channel_config set mobile=#{mobile},unicom=#{unicom},telecom=#{telecom},defaultSuffix=#{defaultSuffix}
    	where saId=#{id}
    </update>
    
    <update id="updateAgentRights" parameterType="AgentInfo">
        update sms_agent_user_info
        set user_rights=#{user_rights} where sa_id=#{id}
    </update>
</mapper>