/// <summary>
        /// 新增账单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Add(AccountRecord model)
        {
            if (model == null)
            {
                return(string.Empty);
            }
            if (string.IsNullOrEmpty(model.AccountDescription))
            {
                model.AccountDescription = string.Format("{0}{1}", model.SpendTypeText, model.ProductName);
            }

            using (DbContext db = new CRDatabase())
            {
                CTMS_SYS_USERINFO user = db.Set <CTMS_SYS_USERINFO>().Find(model.UserID);
                if (model.Balance == -1 && model.Account > user.ACCOUNT)
                {
                    throw new Exception("余额不足!");
                }
                user.ACCOUNT        += model.Balance * model.Account;
                db.Entry(user).State = EntityState.Modified;
                //Todo 对应服务次数加1

                db.Set <CTMS_ACCOUNTRECORD>().Add(ModelToEntity(model));
                db.SaveChanges();
                return(model.ID);
            }
        }
Beispiel #2
0
        private string GetUserName(CTMS_SYS_USERINFO user)
        {
            string name = string.Empty;

            if (user != null)
            {
                name = user.LOGINNAME;
            }

            return(name);
        }
Beispiel #3
0
 /// <summary>
 /// 购买会员
 /// </summary>
 /// <param name="userID"></param>
 /// <param name="MemberID"></param>
 /// <returns></returns>
 public bool BuyMember(string userID, string memberID)
 {
     using (DbContext db = new CRDatabase())
     {
         DateTime    now       = DateTime.Now;
         DateTime    startDate = now.Date;
         DateTime    endDate   = now.Date.AddYears(1).AddDays(-1);
         UserInfo    userInfo  = new UserInfoService().GetUserInfoByID(userID);
         MemberModel buyMember = new MemberBLL().GetMember(memberID);
         if (userInfo == null || buyMember == null)
         {
             throw new Exception("错误的用户ID或会员ID");
         }
         //判断余额
         if (userInfo.Account < buyMember.MEMBERPRICE.Value)
         {
             throw new Exception("余额不足,购买失败!");
         }
         //判断会员等级
         if (userInfo.Member.MEMBERLEVEL.Value >= buyMember.MEMBERLEVEL)
         {
             throw new Exception("不能购买相同或更低级别的会员!");
         }
         //1.扣钱+升级会员
         CTMS_SYS_USERINFO user = db.Set <CTMS_SYS_USERINFO>().Find(userID);
         user.ACCOUNT        -= buyMember.MEMBERPRICE.Value;
         user.MEMBERID        = buyMember.MEMBERID;
         user.MEMBERSTARTDATE = startDate;
         user.MEMBERENDDATE   = endDate;
         db.Entry(user).State = EntityState.Modified;
         //2.添加商品
         List <string> productDescList = new List <string>();
         foreach (MemberProducts memberProduct in buyMember.menberProductList)
         {
             if (!memberProduct.PRODUCTNUMBER.HasValue || Convert.ToInt32(memberProduct.PRODUCTNUMBER.Value) <= 0)
             {
                 continue;
             }
             for (int i = 0; i < Convert.ToInt32(memberProduct.PRODUCTNUMBER.Value); i++)
             {
                 db.Set <CTMS_MYPRODUCT>().Add(new CTMS_MYPRODUCT()
                 {
                     CREATEDATETIME = now,
                     CREATEUSERID   = userInfo.UserId,
                     CREATEUSERNAME = userInfo.LoginName,
                     ENDDATE        = endDate,
                     ID             = Guid.NewGuid().ToString(),
                     LOGINNAME      = userInfo.LoginName,
                     PRODUCTID      = memberProduct.PRODUCTID,
                     PRODUCTNUM     = 1,
                     STARTDATE      = startDate,
                     USERID         = userInfo.UserId
                 });
             }
             var product = db.Set <CTMS_PRODUCTS>().Find(memberProduct.PRODUCTID);
             if (product != null)
             {
                 productDescList.Add(string.Format("{0}*{1}", product.PRODUCTNAME, Convert.ToInt32(memberProduct.PRODUCTNUMBER.Value)));
             }
         }
         //3.提示记录
         db.Set <CTMS_ACCOUNTRECORD>().Add(new CTMS_ACCOUNTRECORD()
         {
             ACCOUNT            = buyMember.MEMBERPRICE.Value,
             ACCOUNTDESCRIPTION = string.Format("购买{0}", buyMember.MEMBERNAME),
             BALANCE            = -1,
             CREATEDATETIME     = now,
             CREATEUSERID       = userInfo.UserId,
             CREATEUSERNAME     = userInfo.LoginName,
             ID          = Guid.NewGuid().ToString(),
             LOGINNAME   = userInfo.LoginName,
             PRODUCTID   = "",
             PRODUCTNAME = string.Format("{0}(包含商品/服务:{1})", buyMember.MEMBERNAME, string.Join(",", productDescList)),
             //PRODUCTNAME = string.Format("{0}", buyMember.MEMBERNAME),
             SPENDTYPE = (int)SpendType.Buy,
             USERID    = userInfo.UserId,
         });
         return(db.SaveChanges() > 0);
     }
 }
Beispiel #4
0
        /// <summary>
        /// 根据ID获取我的产品
        /// </summary>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        public bool BuyProduct(string userID, string ProductID)
        {
            using (DbContext db = new CRDatabase())
            {
                DateTime now = DateTime.Now;
                if (string.IsNullOrEmpty(userID) || string.IsNullOrEmpty(ProductID))
                {
                    throw new Exception("用户ID或产品ID为空,购买失败!");
                }
                CTMS_SYS_USERINFO user    = db.Set <CTMS_SYS_USERINFO>().Find(userID);
                CTMS_PRODUCTS     product = db.Set <CTMS_PRODUCTS>().Find(ProductID);
                if (user == null || product == null)
                {
                    throw new Exception("错误的用户ID或产品ID");
                }
                if (product.SALEPRICE > user.ACCOUNT)
                {
                    throw new Exception("余额不足!");
                }

                //用户表
                decimal salePrice = product.SALEPRICE.HasValue ? product.SALEPRICE.Value : 0;
                user.ACCOUNT        -= salePrice;
                db.Entry(user).State = EntityState.Modified;

                //记录表
                CTMS_ACCOUNTRECORD accountRecord = new CTMS_ACCOUNTRECORD()
                {
                    ID                 = Guid.NewGuid().ToString(),
                    USERID             = user.USERID,
                    ACCOUNT            = salePrice,
                    ACCOUNTDESCRIPTION = string.Format("购买{0}", product.PRODUCTNAME),
                    BALANCE            = -1,
                    LOGINNAME          = user.LOGINNAME,
                    ORDERID            = "",
                    PRODUCTID          = product.PRODUCTID,
                    PRODUCTNAME        = product.PRODUCTNAME,
                    SPENDTYPE          = (int)SpendType.Buy,
                    CREATEDATETIME     = now,
                    CREATEUSERID       = user.USERID,
                    CREATEUSERNAME     = user.LOGINNAME
                };
                db.Set <CTMS_ACCOUNTRECORD>().Add(accountRecord);

                //我的产品表
                CTMS_MYPRODUCT myProduct = new CTMS_MYPRODUCT()
                {
                    ID             = Guid.NewGuid().ToString(),
                    USERID         = user.USERID,
                    LOGINNAME      = user.LOGINNAME,
                    PRODUCTID      = product.PRODUCTID,
                    PRODUCTNUM     = 1,
                    STARTDATE      = DateTime.Now.Date,
                    ENDDATE        = DateTime.Now.Date.AddYears(1).AddDays(-1),
                    CREATEUSERID   = user.USERID,
                    CREATEUSERNAME = user.LOGINNAME,
                    CREATEDATETIME = now,
                };
                db.Set <CTMS_MYPRODUCT>().Add(myProduct);

                return(db.SaveChanges() > 0);
            }
        }