<%@language=vbscript codepage=936 %>
<%
Option Explicit
Response.buffer = True
Const PurviewLevel = 0
Const PurviewLevel_Channel = 0
Const PurviewLevel_Others = ""
%>
<!--#include file="../Conn.asp"-->
<!--#include file="../Inc/Function.asp"-->
<!--#include file="../Inc/MD5_Code.asp"-->
<!--#include file="Admin_ChkPurview.asp"-->
<%
Dim SendTo
SendTo=Trim(Request("SendTo"))
If AdminPurview = 1 Then
    PurviewPassed = True
Else
    Select Case Trim(Request("SendTo"))
    Case "Member"
        PurviewPassed = CheckPurview_Other(AdminPurview_Others, "SendSMSToMember")
    Case "Contacter"
        PurviewPassed = CheckPurview_Other(AdminPurview_Others, "SendSMSToContacter")
    Case "Consignee"
        PurviewPassed = CheckPurview_Other(AdminPurview_Others, "SendSMSToConsignee")
    Case "Other"
        PurviewPassed = CheckPurview_Other(AdminPurview_Others, "SendSMSToOther")
    End Select
End If
If PurviewPassed = False Then
    Response.Write "<br><p align='center'><font color='red' style='font-size:9pt'>对不起，你没有此项操作的权限。</font></p>"
    Response.End
End If

Dim mSendNum     '接收短信的手机号
Dim mContent     '短信内容
Dim mSendTiming  '是否定时发送，0为否（默认），1为定时
Dim mSendTime    '定时发送时间
Dim MD5String   'MD5校验字符。MD5字符是由上述几个字段加上用户密码，然后用MD5函数加密得到的字符串。字段顺序如下：
                 'SMSUserName & SMSKey & mSendNum & mContent & mSendTiming & mSendTime
Dim RecieverCount
RecieverCount=0
mContent=Trim(Request("Content"))
mSendTiming=PE_Clng(Trim(Request("SendTiming")))
Select Case SendTo
Case "SendToMember"
    mSendNum=GetReciever_Member()
Case "SendToContacter"
    mSendNum=GetReciever_Contacter()
Case "SendToConsignee"
    mSendNum=GetReciever_Consignee()
Case "SendToOther"
    mSendNum=Trim(Request("Receiver"))
    Dim arrReceiver
    arrReceiver=split(mSendNum,vbcrlf)
    RecieverCount=ubound(arrReceiver)+1
End Select
if mSendNum="" then
    FoundErr=True
    ErrMsg=ErrMsg & "<li>找不到满足条件的手机号码</li>"
End If
mSendTime=Trim(Request("SendDate")) & " " & Trim(Request("SendTime_Hour")) & ":" & Trim(Request("SendTime_Minute")) & ":00"

If mContent="" then
    FoundErr=True
    ErrMsg=ErrMsg & "<li>请输入短信内容</li>"
End If
if mSendTiming=1 and IsDate(mSendTime)=False then
    FoundErr=True
    ErrMsg=ErrMsg & "<li>定时发送时间的格式不对！</li>"
End If

if FoundErr=True then
    Response.write ErrMsg
    Response.End
end if

Dim SMSUserName,SMSKey
Dim rsConfig
Set rsConfig = Conn.Execute("select top 1 * from PE_Config")
If not(rsConfig.BOF And rsConfig.EOF) Then
    SMSUserName = rsConfig("SMSUserName")
    SMSKey = rsConfig("SMSKey")
End If
rsConfig.Close
Set rsConfig = Nothing

dim PE_MD5
set PE_MD5 = new Md5_Class
md5string = UCase(Trim(PE_MD5.MD5(SMSUserName & SMSKey & mSendNum & mContent & mSendTiming & mSendTime)))
set PE_MD5 = nothing

Dim MessageCount
MessageCount=((len(mContent) \ 70)+1) * RecieverCount
%>
<html>
<head>
<Title>短信预览</Title>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<link href='Admin_Style.css' rel='stylesheet' type='text/css'>
</head>
<body>
<table width='100%' border='0' align='center' cellpadding='2' cellspacing='1' class='border'>
  <tr class='topbg'> 
    <td height='22' colspan='10'><table width='100%'><tr class='topbg'><td align='center'><b>发 送 手 机 短 信</b></td><td width='60' align='right'><a href='http://go.powereasy.net/go.aspx?UrlID=10047' target='_blank'><img src='images/help.gif' border='0'></a></td></tr></table></td>
  </tr>
  <tr class='tdbg'>
    <td width='70' height='30'><strong>管理导航：</strong></td>
    <td><a href='Admin_SMS.asp?SendTo=Member'>给会员发送短信</a>&nbsp;|&nbsp;    <a href='Admin_SMS.asp?SendTo=Contacter'>给联系人发送短信</a>&nbsp;|&nbsp;    <a href='Admin_SMS.asp?SendTo=Consignee'>给订单中的收货人发送短信</a>&nbsp;|&nbsp;    <a href='Admin_SMS.asp?SendTo=Other'>给其他人发送短信</a>    </td>
  </tr>
</table>
<form name='message' method='post' action='http://sms.powereasy.net/MessageGate/MessageGate.aspx'>
<table width='100%' border='0' align='center' cellpadding='2' cellspacing='1' class='border'>
<tr class='title'><td colspan='2' align='center'>预 览 短 信</td></tr>
<tr class='tdbg' valign='top'>
<td width='300'>下面是根据您指定的条件查找到的接收人：<br><textarea name="SendNum" rows='20' cols='40' readonly><%= mSendNum %></textarea></td>
<td><b>短信内容：</b><br><textarea name="Content" rows='5' cols='60' readonly><%= mContent %></textarea><br><br><br><br><br><br><b>短信统计：</b><br>需要向 <%=RecieverCount%> 个号码发送 <%=MessageCount%> 条短信<br><br><b>说明：</b><br>因为每条短信不能超过70个字，所以短信数可能会大于号码数。<br>因为短信内容中的变量替换等原因，可能会导致实际发送的短信数会超过这里计算的短信数，最终结果以动易短信通平台上的实际发送数目为准。</td></tr>
<tr class='tdbg'><td colspan='2' height='50' align='center'><input type='submit' name='submit' value='提交给短信服务器'>
<input type="hidden" name="UserName" value="<%= SMSUserName %>" />
<input type="hidden" name="SendTiming" value="<%= mSendTiming %>" />
<input type="hidden" name="SendTime" value="<%= mSendTime %>" />
<input type="hidden" name="MD5String" value="<%= MD5String %>" />
<input type="hidden" name="Reserve" value="管理员ID：<%= AdminID %>" />
</td></tr></table>
</form>
</body>
</html>
<%

Private Function GetReciever_Member()
    Dim InceptType,GroupID,inceptUser
    Dim sqlUser,strReciever,strMobile
    strReciever=""
    sqlUser="select U.UserName,C.TrueName,C.Mobile,C.PHS from PE_User U left join PE_Contacter C on U.ContacterID=C.ContacterID where (C.Mobile<>'' or C.PHS<>'')"
    InceptType = PE_CLng(Trim(Request("InceptType")))
    Select Case InceptType
    Case 0  '所有会员
        
    Case 1  '指定会员组
        GroupID = ReplaceBadChar(Trim(Request("GroupID")))
        If GroupID <> 0 Then
            sqlUser = sqlUser & " and U.GroupID in (" & GroupID & ")"
        End If
    Case 2  '指定用户名
        inceptUser = Replace(ReplaceBadChar(Request("InceptUser")), ",", "','")
        sqlUser = sqlUser & " and U.UserName in ('" & inceptUser & "')"
    End Select
    sqlUser=sqlUser & " order by U.UserID"
    Dim rsUser,strMoblie
    set rsUser=conn.execute(sqlUser)
    if not(rsUser.bof and rsUser.eof) then
        strMobile=rsUser("Mobile")
        if strMobile="" then strMobile=rsUser("PHS")
        strReciever=strMobile & "," & rsUser("UserName")
        if rsUser("TrueName")<>"" then
            strReciever=strReciever & "," & rsUser("TrueName")
        end if
        RecieverCount=1
        rsUser.movenext
        do while not rsUser.eof
            strMobile=rsUser("Mobile")
            if strMobile="" then strMobile=rsUser("PHS")
            strReciever=strReciever & vbcrlf & strMobile & "," & rsUser("UserName")
            if rsUser("TrueName")<>"" then
                strReciever=strReciever & "," & rsUser("TrueName")
            end if
            RecieverCount=RecieverCount+1
            rsUser.movenext
        loop
    End if
    rsUser.Close
    set rsUser=nothing
    GetReciever_Member=strReciever
End Function

Private Function GetReciever_Contacter()
    Dim InceptType,GroupID,TrueName
    Dim sqlContacter,strReciever,strMobile
    strReciever=""
    sqlContacter="select TrueName,Mobile,PHS from PE_Contacter where (Mobile<>'' or PHS<>'')"
    InceptType = PE_CLng(Trim(Request("InceptType")))
    Select Case InceptType
    Case 0  '所有会员
        
    Case 1  '指定地区
        sqlContacter = sqlContacter & " and Country='" & ReplaceBadChar(Request("Country")) & "'"
        sqlContacter = sqlContacter & " and Province='" & ReplaceBadChar(Request("Province")) & "'"
        sqlContacter = sqlContacter & " and City='" & ReplaceBadChar(Request("City")) & "'"
    Case 2  '指定联系人
        TrueName = Replace(ReplaceBadChar(Request("TrueName")), ",", "','")
        sqlContacter = sqlContacter & " and TrueName in ('" & TrueName & "')"
    End Select
    sqlContacter=sqlContacter & " order by ContacterID desc"
    Dim rsContacter,strMoblie
    set rsContacter=conn.execute(sqlContacter)
    if not(rsContacter.bof and rsContacter.eof) then
        strMobile=rsContacter("Mobile")
        if strMobile="" then strMobile=rsContacter("PHS")
        strReciever=strMobile & "," & rsContacter("TrueName")
        RecieverCount=1
        rsContacter.movenext
        do while not rsContacter.eof
            strMobile=rsContacter("Mobile")
            if strMobile="" then strMobile=rsContacter("PHS")
            strReciever=strReciever & vbcrlf & strMobile & "," & rsContacter("TrueName")
            RecieverCount=RecieverCount+1
            rsContacter.movenext
        loop
    End if
    rsContacter.Close
    set rsContacter=nothing
    GetReciever_Contacter=strReciever
End Function

Private Function GetReciever_Consignee()
    Dim InceptType,GroupID,OrderFormID
    Dim sqlOrder,strReciever,strMobile
    strReciever=""
    sqlOrder="select OrderFormNum,ContacterName,Mobile,Phone from PE_OrderForm where (Mobile<>'' or Phone<>'')"
    InceptType = PE_CLng(Trim(Request("InceptType")))
    Select Case InceptType
    Case 0  '所有订单
        
    Case 1  '查询条件
        Dim BeginID, EndID, BeginDate, EndDate, MinMoney, MaxMoney, OrderStatus, PayStatus, DeliverStatus, OrderFormNum, ClientName, UserName, AgentName, ContacterName, Address, Phone, Mobile, Remark, ProductName
        BeginID = PE_CLng(Trim(Request("BeginID")))
        EndID = PE_CLng(Trim(Request("EndID")))
        BeginDate = Trim(Request("BeginDate"))
        EndDate = Trim(Request("EndDate"))
        MinMoney = PE_CDbl(Trim(Request("MinMoney")))
        MaxMoney = PE_CDbl(Trim(Request("MaxMoney")))
        OrderStatus = PE_CLng(Trim(Request("OrderStatus")))
        PayStatus = PE_CLng(Trim(Request("PayStatus")))
        DeliverStatus = PE_CLng(Trim(Request("DeliverStatus")))
        OrderFormNum = ReplaceBadChar(Trim(Request("OrderFormNum")))
        ClientName = ReplaceBadChar(Trim(Request("ClientName")))
        UserName = ReplaceBadChar(Trim(Request("UserName")))
        AgentName = ReplaceBadChar(Trim(Request("AgentName")))
        ContacterName = ReplaceBadChar(Trim(Request("ContacterName")))
        Address = ReplaceBadChar(Trim(Request("Address")))
        Phone = ReplaceBadChar(Trim(Request("Phone")))
        Mobile = ReplaceBadChar(Trim(Request("Mobile")))
        Remark = ReplaceBadChar(Trim(Request("Remark")))
        ProductName = ReplaceBadChar(Trim(Request("ProductName")))

        If BeginID > 0 Then
            sqlOrder = sqlOrder & " And OrderFormID>=" & BeginID
        End If
        If EndID > 0 Then
            sqlOrder = sqlOrder & " And OrderFormID<=" & EndID
        End If

        If BeginDate <> "" Then
            BeginDate = PE_CDate(BeginDate)
            If SystemDatabaseType = "SQL" Then
                sqlOrder = sqlOrder & " And InputTime>='" & BeginDate & "'"
            Else
                sqlOrder = sqlOrder & " And InputTime>=#" & BeginDate & "#"
            End If
        End If
        If EndDate <> "" Then
            EndDate = PE_CDate(EndDate)
            If SystemDatabaseType = "SQL" Then
                sqlOrder = sqlOrder & " And InputTime<='" & EndDate & "'"
            Else
                sqlOrder = sqlOrder & " And InputTime<=#" & EndDate & "#"
            End If
        End If
        If MinMoney > 0 Then
            sqlOrder = sqlOrder & " And MoneyTotal>=" & MinMoney
        End If
        If MaxMoney > 0 Then
            sqlOrder = sqlOrder & " And MoneyTotal<=" & MaxMoney
        End If
        If OrderStatus >= 0 Then
            sqlOrder = sqlOrder & " And Status=" & OrderStatus
        End If
        If PayStatus >= 0 Then
            Select Case PayStatus
            Case 0
                sqlOrder = sqlOrder & " And MoneyTotal>0 And MoneyReceipt=0"
            Case 1
                sqlOrder = sqlOrder & " And MoneyTotal>MoneyReceipt And MoneyReceipt>0"
            Case 2
                sqlOrder = sqlOrder & " And MoneyTotal<=MoneyReceipt"
            End Select
        End If
        If DeliverStatus >= 0 Then
            sqlOrder = sqlOrder & " And DeliverStatus=" & DeliverStatus
        End If
        If OrderFormNum <> "" Then
            sqlOrder = sqlOrder & " And OrderFormNum like '%" & OrderFormNum & "%'"
        End If
        If ClientName <> "" Then
            sqlOrder="select O.OrderFormNum,O.ContacterName,O.Mobile,O.Phone from PE_OrderForm O left join PE_Client C on O.ClientID=C.ClientID where (O.Mobile<>'' or O.Phone<>'')"
            sqlOrder = sqlOrder & " And C.ClientName like '%" & ClientName & "%'"
        End If
        If UserName <> "" Then
            sqlOrder = sqlOrder & " And UserName like '%" & UserName & "%'"
        End If
        If AgentName <> "" Then
            sqlOrder = sqlOrder & " And AgentName='" & AgentName & "'"
        End If
        If ContacterName <> "" Then
            sqlOrder = sqlOrder & " And ContacterName like '%" & ContacterName & "%'"
        End If
        If Address <> "" Then
            sqlOrder = sqlOrder & " And Address like '%" & Address & "%'"
        End If
        If Phone <> "" Then
            sqlOrder = sqlOrder & " And Phone like '%" & Phone & "%'"
        End If
        If Mobile <> "" Then
            sqlOrder = sqlOrder & " And Mobile like '%" & Mobile & "%'"
        End If
        If Remark <> "" Then
            sqlOrder = sqlOrder & " And Remark like '%" & Remark & "%'"
        End If
        If ProductName <> "" Then
            sqlOrder = "select O.OrderFormNum,O.ContacterName,O.Mobile,O.Phone PE_Product P inner join (Pe_OrderFormItem I inner join (PE_OrderForm O left join PE_Client C On O.ClientID = C.ClientID) on I.OrderFormID = O.OrderFormID) on P.ProductID = I.ProductID where (O.Mobile<>'' or O.Phone<>'')"
            sqlOrder = sqlOrder & " And P.ProductName like '%" & ProductName & "%'"
        End If

    Case 2  '指定订单ID
        OrderFormID = ReplaceBadChar(Trim(Request("OrderFormID")))
        if OrderFormID="" then OrderformID="0"
        sqlOrder = sqlOrder & " and OrderFormID in (" & OrderFormID & ")"
    End Select
    sqlOrder=sqlOrder & " order by OrderFormID desc"
    Dim rsOrder,strMoblie
    set rsOrder=conn.execute(sqlOrder)
    if not(rsOrder.bof and rsOrder.eof) then
        strMobile=rsOrder("Mobile")
        if strMobile="" then strMobile=rsOrder("Phone")
        strReciever=strMobile & "," & rsOrder("ContacterName") & "," & rsOrder("OrderFormNum")
        RecieverCount=1
        rsOrder.movenext
        do while not rsOrder.eof
            strMobile=rsOrder("Mobile")
            if strMobile="" then strMobile=rsOrder("Phone")
            strReciever=strReciever & vbcrlf & strMobile & "," & rsOrder("ContacterName") & "," & rsOrder("OrderFormNum")
            RecieverCount=RecieverCount+1
            rsOrder.movenext
        loop
    End if
    rsOrder.Close
    set rsOrder=nothing
    GetReciever_Consignee=strReciever
End Function


%>