/// <summary>
 /// 创建新的 Transaction 对象。
 /// </summary>
 /// <param name="transactionID">TransactionID 属性的初始值。</param>
 /// <param name="userID">UserID 属性的初始值。</param>
 public static Transaction CreateTransaction(global::System.Guid transactionID, global::System.String userID)
 {
     Transaction transaction = new Transaction();
     transaction.TransactionID = transactionID;
     transaction.UserID = userID;
     return transaction;
 }
 /// <summary>
 /// 用于向 Transactions EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet&lt;T&gt; 属性的 .Add 方法。
 /// </summary>
 public void AddToTransactions(Transaction transaction)
 {
     base.AddObject("Transactions", transaction);
 }
        /*为处方单付款*/
        public TransactionInfoEntity PayPrescription(string sPrescriptionID, string pharmacyID, string payPassword, string take) {

            DrPEDatabaseEntities DEntities = new DrPEDatabaseEntities();
            TransactionInfoEntity transactionInfoEntity = new TransactionInfoEntity();

            /*获取处方信息*/
            Guid gPrescriptionID = Guid.Empty;
            try {
                gPrescriptionID = new Guid(sPrescriptionID);
            }
            catch {
                transactionInfoEntity.ErrorMessage = "224 Wrong Prescription GUID! @Data";
                return transactionInfoEntity;
            }
            Prescription prescription = (from p in DEntities.Prescriptions
                                         where p.PrescriptionID == gPrescriptionID
                                         select p).FirstOrDefault();

            /*若处方不存在*/
            if (prescription == null) {
                transactionInfoEntity.ErrorMessage = "225 No Such Prescription! @Data";
                return transactionInfoEntity;
            }

            /*获取所属病历信息*/
            CaseHistory caseHistory = (from c in DEntities.CaseHistories
                                       where c.PrescriptionID == gPrescriptionID
                                       select c).FirstOrDefault();

            /*若处方不属于任何病历*/
            if (caseHistory == null) {
                transactionInfoEntity.ErrorMessage = "226 This Prescription Belongs to No Case! @Data";
                return transactionInfoEntity;
            }

            /*获取病历所属用户信息*/
            User user = (from u in DEntities.Users
                         where ((u.UserID == caseHistory.UserID) && (u.PayPassword == payPassword))
                         select u).FirstOrDefault();

            /*若支付密码错误*/
            if (user == null) {
                transactionInfoEntity.ErrorMessage = "227 Wrong PayPassword! @Data";
                return transactionInfoEntity;
            }

            /*判断是否已付款*/
            string sCheck = String.Format("P:{0}", gPrescriptionID.ToString());
            Transaction transaction = (from t in DEntities.Transactions
                                       where t.Status == sCheck
                                       select t).FirstOrDefault();
            if (transaction != null) {
                /*查询交易药房*/
                Pharmacy pharmacy = (from p in DEntities.Pharmacies
                                     where p.PharmacyID == transaction.PharmacyID
                                     select p).FirstOrDefault();

                /*是否与本药房交易*/
                if (pharmacy.PharmacyID == pharmacyID) {
                    /*药物是否已经领取*/
                    if (transaction.Detail == null) {
                        transactionInfoEntity.Amount = transaction.Amount;
                        transactionInfoEntity.ErrorMessage = String.Format("231 Already Paid at {0}, But Not Taken Yet! @Data", transaction.Date);

                        /*进行领取操作*/
                        transaction.Detail = "[Taken]";
                        DEntities.SaveChanges();
                    }
                    else {
                        transactionInfoEntity.Amount = transaction.Amount;
                        transactionInfoEntity.ErrorMessage = String.Format("232 Already Paid and Taken at {0}! @Data", transaction.Date);
                    }
                }
                else {
                    transactionInfoEntity.ErrorMessage = String.Format("233 Already Bought in {0}({1}) at {2}! @Data",
                                                                        pharmacy.Name, pharmacy.PharmacyID, transaction.Date);
                }
                return transactionInfoEntity;
            }

            /*获取处方费用*/
            PrescriptionCostEntity prescriptionCostEntity = GetPrescriptionCost(sPrescriptionID, pharmacyID, user.Password);
            if (prescriptionCostEntity.ErrorMessage != null) {
                transactionInfoEntity.ErrorMessage = prescriptionCostEntity.ErrorMessage;
                return transactionInfoEntity;
            }

            /*判断用户帐号是否允许支付*/
            if (user.Balance == null) {
                transactionInfoEntity.ErrorMessage = String.Format("228 User's Balance Not Available! @Data");
                return transactionInfoEntity;
            }

            /*判断用户是否有足够的余额支付*/
            if (user.Balance < prescriptionCostEntity.Amount) {
                transactionInfoEntity.ErrorMessage = String.Format("229 Needs RMB{0}, But Only RMB{1} Left! @Data", prescriptionCostEntity.Amount, user.Balance);
                return transactionInfoEntity;
            }

            /*支付密码正确,生成新交易*/
            Transaction newTransaction      = new Transaction();
            newTransaction.TransactionID    = Guid.NewGuid();
            newTransaction.UserID           = user.UserID;
            newTransaction.PharmacyID       = pharmacyID;
            newTransaction.Date             = DateTime.Now;
            newTransaction.Amount           = prescriptionCostEntity.Amount;
            newTransaction.Status           = sCheck;
            if (take != null) {
                newTransaction.Detail = "[Taken]";
            }

            /*提交修改至数据库*/
            try {
                DEntities.Transactions.AddObject(newTransaction);
                DEntities.SaveChanges();
            }
            catch {
                transactionInfoEntity.ErrorMessage = "241 GUID Conflicts! @Data";
                return transactionInfoEntity;
            }

            /*从用户帐号扣款*/
            user.Balance -= prescriptionCostEntity.Amount;
            DEntities.SaveChanges();

            /*将款项加入药房*/
            Pharmacy newPharmacy = (from ph in DEntities.Pharmacies
                                    where ph.PharmacyID == pharmacyID
                                    select ph).FirstOrDefault();
            if (newPharmacy.Balance == null) {
                newPharmacy.Balance = prescriptionCostEntity.Amount;
            }
            else {
                newPharmacy.Balance += prescriptionCostEntity.Amount;
            }
            DEntities.SaveChanges();


            /*返回交易详情*/
            transactionInfoEntity.TransactionID     = newTransaction.TransactionID;
            transactionInfoEntity.LastName          = user.LastName;
            transactionInfoEntity.FirstName         = user.FirstName;
            transactionInfoEntity.PharmacyID        = newTransaction.PharmacyID;
            transactionInfoEntity.Date              = newTransaction.Date;
            transactionInfoEntity.Amount            = newTransaction.Amount;
            transactionInfoEntity.UserBalanceThen   = user.Balance;
            if (newTransaction.Detail != null) {
                transactionInfoEntity.Action = newTransaction.Detail;
            }
            else {
                transactionInfoEntity.Action = "[Left]";
            }

            return transactionInfoEntity;
        }