using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
///MyBankService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class MyBankService : System.Web.Services.WebService {

    public MyBankService () {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

   public enum PayResults
    { success,notEnoughMoney,invalidAccount}

    MyBankDataContext db = new MyBankDataContext();

    [WebMethod]
    //查询账户余额
    public string   Amount(string cid, string pwd)
    {
        var result = from a in db.Accounts
                     where a.customerId == cid && a.customerPassword==pwd
                     select a;
        if (result == null)// 账号不存在，返回非法账号
        {
            return "账号密码错误！";
        }
        foreach (var g in result)
        {
            return "账户余额："+g.balance.ToString();
        }
        return "账户不存在";
    }


    [WebMethod]
    public PayResults pay(string cid, string pwd, decimal amount)
    {
        var result=from a in db.Accounts
                   where a.customerId == cid && a.stateId==1
                   select a;
        if (result == null)// 账号不存在，返回非法账号
        {
            return PayResults.invalidAccount;            
        }
        foreach (var g in result )
        {
            if (g.customerPassword!=pwd)//密码不对，返回非法账号
            {
                return PayResults.invalidAccount;                
            }
            if (g.balance < amount)//账户余额不足
            {
                return PayResults.notEnoughMoney;                
            }
            g.balance -= amount;
        }
       
        db.SubmitChanges();
        return PayResults.success;       
    }
    
}

