1.点击用以扣分的房号
2.在后台生成订单,并调用扣除积分的方法
int gId = Convert.ToInt32(gid);
string GName = "";
var good = db.S_Goods.SingleOrDefault(p=>p.GID == gId);
if (good != null)
{
GName = good.GName;
}
//计算价格
int a = Convert.ToInt32(gcount);//数量
double b = Convert.ToDouble(UNitePrice);
var price = a * b;
S_Order pl = new S_Order();
pl.SPID = Convert.ToInt32(gid);
pl.OCount = Convert.ToInt32(gcount);
pl.OPeople = uname;
pl.Otel = uphone;
pl.OUSERID = Convert.ToInt32(uid);
pl.OCreatTime = DateTime.Now;
pl.OState = "0";//初始为未支付
pl.SPrice = Convert.ToInt32(price);
pl.SPName = GName;
string UseTime = DateTime.Now.Year + "-12-31 23:59:59";
pl.UseTime = Convert.ToDateTime(UseTime);
db.S_Order.Add(pl);
db.SaveChanges();
int ID1 = Convert.ToInt32(IDs);//房号
Jifen.JFDel(ID1,Convert.ToDecimal(price), Convert.ToInt32(uid),pl.OrderID); //扣积分的方法
3 积分扣除方法
//积分扣除
public static void JFDel(int id,decimal price,int MID,int OrderId)
{
Entities db = new Entities();
decimal Ms = new decimal();//扣分过程中已扣的分
int ID1 = Convert.ToInt32(id);//房号
DateTime Now = DateTime.Now;
DateTime Now1 = Now.AddDays(-1);
var Sml = db.S_MoneyLog.Where(p => p.HID == ID1 && p.State1 == 1 && p.State2 == 1 && p.State3 == 1 && p.State4 == 1 && p.State5 == 1&&Now>=p.StartTime&&Now1<p.EndTime);//积分卷
Sml= Sml.OrderBy(p=>p.EndTime);//按结束日期排序,先扣快过期的积分卷
foreach (var i1 in Sml)
{
if (Ms>=price) {
return;
}
var Smd = db.S_MoneyDel.Where(p => p.SMLID == i1.Id);//积分卷的扣分记录
decimal M1 = new decimal();
foreach (var i2 in Smd)
{
M1 += Convert.ToDecimal(i2.Money); //积分卷以往扣除的积分
}
if (M1 < i1.Money) //如果还有分
{
decimal M2 = Convert.ToDecimal(i1.Money) - M1;//积分卷剩余积分
if (M2 > (Convert.ToDecimal(price)-Ms)) //判断是否足够
{
S_MoneyDel smd1 = new S_MoneyDel();
smd1.SGID = OrderId;
smd1.Time = DateTime.Now;
smd1.MID = MID;
smd1.Money = Convert.ToDecimal(price)-Ms;
smd1.SMLID = i1.Id;
db.S_MoneyDel.Add(smd1);
db.SaveChanges();
Ms += Convert.ToDecimal(price);
return;
}
else //不够全扣掉
{
S_MoneyDel smd1 = new S_MoneyDel();
smd1.SGID = OrderId;
smd1.Time = DateTime.Now;
smd1.MID = MID;
smd1.Money = M2;
smd1.SMLID = i1.Id;
db.S_MoneyDel.Add(smd1);
db.SaveChanges();
Ms += M2;
}
}
}
}