public void Post() { string Data = DataObj.GetData(); if (Data.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } JObject json = new JObject(); try { json = (JObject)JsonConvert.DeserializeObject(Data); } catch (Exception Ex) { Log.Write("[FastSetBankController]:", "【Data】" + Data, Ex); json = null; } if (json == null) { DataObj.OutError("1000"); return; } UserCard UserCard = new UserCard(); UserCard = JsonToObject.ConvertJsonToModel(UserCard, json); UserTrack UserTrack = new UserTrack(); UserTrack = JsonToObject.ConvertJsonToModel(UserTrack, json); Users baseUsers = Entity.Users.FirstOrDefault(n => n.Token == UserCard.Token); if (baseUsers == null)//用户令牌不存在 { DataObj.OutError("2004"); return; } if (baseUsers.State != 1)//用户被锁定 { DataObj.OutError("2003"); return; } if (baseUsers.CardStae != 2)//未实名认证 { DataObj.OutError("2006"); return; } FastUser FastUser = Entity.FastUser.FirstOrDefault(n => n.UId == baseUsers.Id); if (FastUser == null) { DataObj.OutError("2035"); return; } UserCard UC = Entity.UserCard.FirstOrDefault(n => n.UId == baseUsers.Id && n.Type == 1 && n.Id == UserCard.Id && n.State == 1); if (UC == null) { DataObj.OutError("1001"); return; } if (FastUser.Card == UC.Card) { DataObj.OutError("2080"); return; } string GPSRemark = "旧默认结算卡:" + FastUser.Card + " 新默认结算卡:" + UC.Card; FastUser.Card = UC.Card; FastUser.Bank = UC.Bank; FastUser.Bin = UC.Bin; Entity.SaveChanges(); //这里要增加所有通道的商户注册及绑卡操作 //============================================================= IList <FastPayWay> FastPayWayList = Entity.FastPayWay.OrderBy(n => n.Sort).ToList(); //购买商户与默认商户都需要改卡 IList <FastUserPay> FastUserPayList = Entity.FastUserPay.Where(n => n.UId == baseUsers.Id).OrderBy(n => n.PayWay).ToList(); foreach (var p in FastUserPayList) { FastPayWay FastPayWay = FastPayWayList.FirstOrDefault(n => n.Id == p.PayWay); if (FastPayWay != null) { if (FastPayWay.DllName == "MiBank" || FastPayWay.DllName == "HFPay" || FastPayWay.DllName == "ZBLHPay" || FastPayWay.DllName == "JiFuJFPay") { p.CardState = 1;//不需要验卡 } else { p.CardState = 2;//重新标识状态为待提交 } p.Bank = FastUser.Bank; p.Card = FastUser.Card; p.Bin = FastUser.Bin; BusFastPay.AddCard(FastUser, p, FastPayWay, Entity); } } Entity.SaveChanges(); //======================================= UserTrack.ENo = DataObj.ENo; UserTrack.OPType = "更换默认结算卡"; UserTrack.UserName = ""; UserTrack.Remark = GPSRemark; UserTrack.UId = FastUser.UId; UserTrack.SeavGPSLog(Entity); //======================================= FastUser.Cols = "Card,Bank,Bin"; DataObj.Data = FastUser.OutJson(); DataObj.Code = "0000"; DataObj.OutString(); }
public void Post() { string Data = DataObj.GetData(); if (Data.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } JObject json = new JObject(); try { json = (JObject)JsonConvert.DeserializeObject(Data); } catch (Exception Ex) { Log.Write("[JobPay]:", "【Data】" + Data, Ex); } if (json == null) { DataObj.OutError("1000"); return; } JobOrders JobOrders = new JobOrders(); JobOrders = JsonToObject.ConvertJsonToModel(JobOrders, json); UserTrack UserTrack = new UserTrack(); UserTrack = JsonToObject.ConvertJsonToModel(UserTrack, json); if (UserTrack.X.IsNullOrEmpty() || UserTrack.Y.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } Users Users = new Users(); Users = JsonToObject.ConvertJsonToModel(Users, json); #region 初始化与校验 if (JobOrders.TNum.IsNullOrEmpty() || JobOrders.CardId.IsNullOrEmpty() || Users.PayPwd.IsNullOrEmpty() || Users.Token.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } #region 用户验证 Users baseUsers = Entity.Users.FirstOrDefault(n => n.Token == Users.Token); if (baseUsers == null)//用户令牌不存在 { DataObj.OutError("2004"); return; } if (baseUsers.State != 1) { DataObj.OutError("2003"); return; } if (baseUsers.MiBao != 1)//未设置支付密码 { DataObj.OutError("2008"); return; } if (baseUsers.PayLock == 1)//密码错误太多次锁定 { DataObj.OutError("2050"); return; } //这里是执行指纹解锁 bool IfCheckOk = true; if (Users.PayPwd.Substring(0, 3) == "HF_") { string PassWord = Users.PayPwd; PassWord = PassWord.Replace("HF_", ""); string token = baseUsers.Token; token = token + "GoodPay"; string Md5Token = token.GetMD5().ToUpper(); string Pass = Md5Token.Substring(0, 4) + Md5Token.Substring(Md5Token.Length - 4, 4); if (Pass != PassWord) { IfCheckOk = false; } } else if (baseUsers.PayPwd != Users.PayPwd.GetPayMD5()) { //原支付密码错误 IfCheckOk = false; } if (!IfCheckOk) { //付密码错误 SysSet SysSet = Entity.SysSet.FirstOrNew(); //系统统一修改标识SAME002 baseUsers.PayErr++; if (baseUsers.PayErr >= SysSet.PayLock) { baseUsers.PayLock = 1; } Entity.SaveChanges(); Users Out = new Users(); Out.PayErr = SysSet.PayLock - baseUsers.PayErr; Out.Cols = "PayErr"; DataObj.Data = Out.OutJson(); DataObj.Code = "2010"; if (Out.PayErr == 0) { DataObj.Msg = "支付密码不正确,请明日再试或取回支付密码"; } else { DataObj.Msg = "支付密码不正确,您还可以尝试" + Out.PayErr + "次"; } DataObj.OutString(); return; } baseUsers.PayErr = 0; #endregion JobOrders baseJobOrders = this.Entity.JobOrders.Where(o => o.TNum == JobOrders.TNum && o.UId == baseUsers.Id).FirstOrDefault(); if (baseJobOrders == null) { DataObj.OutError("1001"); return; } if (baseJobOrders.State != 1) { DataObj.OutError("1001"); return; } JobItem FirstJobItem = this.Entity.JobItem.Where(o => o.TNum == baseJobOrders.TNum && o.RunType == 1).OrderBy(o => o.RunTime).FirstOrDefault(); if (FirstJobItem == null) { DataObj.OutError("1001"); return; } FirstJobItem.UserCardId = JobOrders.CardId; FirstJobItem.RunedTime = DateTime.Now; FirstJobItem.State = 2;//按Job的流程,这里需设置执行中才能进入支付 this.Entity.SaveChanges(); #endregion //======================================= UserTrack.ENo = DataObj.ENo; UserTrack.OPType = "支付还款订单"; UserTrack.GPSAddress = UserTrack.GPSAddress; UserTrack.GPSX = UserTrack.X; UserTrack.GPSY = UserTrack.Y; UserTrack.UserName = FirstJobItem.TNum; UserTrack.UId = baseUsers.Id; UserTrack.SeavGPSLog(Entity); //======================================= FirstJobItem = FirstJobItem.Pay(Entity); //0取消 1待执行 2执行中 3执行完成 4执行失败 if (FirstJobItem.State == 3) { FirstJobItem.RunTime = DateTime.Now; Entity.SaveChanges(); DataObj.OutError("0000"); } else if (FirstJobItem.State == 2) { DataObj.OutError("6028"); } else { //DataObj.Msg = FirstJobItem.Remark; DataObj.OutError("6027"); } }
public void Post() { string Data = DataObj.GetData(); if (Data.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } JObject json = new JObject(); try { json = (JObject)JsonConvert.DeserializeObject(Data); } catch (Exception Ex) { Log.Write("[FastOrderController]:", "【Data】" + Data, Ex); json = null; } if (json == null) { DataObj.OutError("1000"); return; } FastOrder InFastOrder = new FastOrder(); InFastOrder = JsonToObject.ConvertJsonToModel(InFastOrder, json); UserTrack UserTrack = new UserTrack(); UserTrack = JsonToObject.ConvertJsonToModel(UserTrack, json); if (InFastOrder.Id == 99999) { DataObj.Msg = "当前通道维护中,建议您开通收款到银行卡功能,享受更多收款通道"; DataObj.OutError("1000"); return; } if (InFastOrder.CashType.IsNullOrEmpty()) { InFastOrder.CashType = "D0"; //DataObj.OutError("1000"); //return; } decimal Amount = InFastOrder.Amoney; byte payway = InFastOrder.OType; if (Amount <= 0) { DataObj.OutError("1000"); return; } if (payway != 1 && payway != 2 && payway != 3) { DataObj.Msg = "你当前版本不支持该交易,请等待新版本发布及升级!"; DataObj.OutError("1000"); return; } Users Users = Entity.Users.FirstOrDefault(n => n.Token == InFastOrder.Token); #region 用户 if (Users == null)//用户令牌不存在 { DataObj.OutError("2004"); return; } if (Users.State != 1)//用户被锁定 { DataObj.OutError("2003"); return; } if (Users.CardStae != 2)//未实名认证 { DataObj.OutError("2006"); return; } if (Amount.IsNullOrEmpty() || payway.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } #endregion FastUser FastUser = Entity.FastUser.FirstOrDefault(o => o.UId == Users.Id); if (FastUser == null) { DataObj.OutError("2070"); return; } #region 取通道 IList <FastPayWay> FastPayWayList = null; if (InFastOrder.Id.IsNullOrEmpty()) { //这是旧逻辑,有一些旧版还在用 if (payway == 1) { FastPayWayList = Entity.FastPayWay.Where(n => n.State == 1 && n.SNum2 < Amount && n.ENum2 >= Amount && n.HasAliPay == 1).OrderBy(n => n.Sort).ToList(); } else if (payway == 2) { FastPayWayList = Entity.FastPayWay.Where(n => n.State == 1 && n.SNum < Amount && n.ENum >= Amount && n.HasWeiXin == 1).OrderBy(n => n.Sort).ToList(); } else if (payway == 3) { FastPayWayList = Entity.FastPayWay.Where(n => n.State == 1 && n.BankSNum < Amount && n.BankENum >= Amount && n.HasBank == 1).OrderBy(n => n.Sort).ToList(); } else { DataObj.OutError("1000"); return; } } else { var query = Entity.FastPayWay.Where(n => n.Id == InFastOrder.Id && n.State == 1); if (payway == 1) { query = query.Where(o => o.HasAliPay == 1 && o.SNum2 <= Amount && o.ENum2 >= Amount); } else if (payway == 2) { query = query.Where(o => o.HasWeiXin == 1 && o.SNum <= Amount && o.ENum >= Amount); } else if (payway == 3) { query = query.Where(o => o.HasBank == 1 && o.BankSNum <= Amount && o.BankENum >= Amount); } FastPayWayList = query.ToList(); } #endregion if (FastPayWayList.Count < 1) { DataObj.OutError("2079"); return; } #region 通道验证及商户进件信息验证 FastPayWay FastPayWay = null; FastUserPay FastUserPay = null; IList <FastPayWay> PayWayList = new List <FastPayWay>(); foreach (var p in FastPayWayList) { if (p.TimeType == 1)//限制时间,模式1 { DateTime STime = p.STime; DateTime ETime = p.ETime; DateTime NowSTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + STime.ToString("HH:mm:ss")); DateTime NowETime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + ETime.ToString("HH:mm:ss")); if (NowSTime <= DateTime.Now && DateTime.Now <= NowETime) { //当前时间允许交易 PayWayList.Add(p); } } else { PayWayList.Add(p); } } if (PayWayList.Count < 1) { DataObj.OutError("2071"); return; } foreach (var p in PayWayList) { FastUserPay temp = Entity.FastUserPay.FirstOrDefault(n => n.UId == Users.Id && n.PayWay == p.Id && n.MerState == 1 && n.CardState == 1 && n.BusiState == 1); if (temp != null) { FastPayWay = p; FastUserPay = temp; break; } } if (FastUserPay == null) { DataObj.OutError("2072"); return; } if (FastPayWay == null) { DataObj.OutError("2073"); return; } #endregion string[] PayConfigArr = FastPayWay.QueryArray.Split(','); #region 验证配置 if (FastPayWay.DllName == "HFPay") { if (PayConfigArr.Length != 3) { DataObj.OutError("2074"); return; } } if (FastPayWay.DllName == "HFJSPay") { if (PayConfigArr.Length != 3) { DataObj.OutError("2074"); return; } } #endregion decimal UserCost = 0;//用户 decimal BankCost = 0; decimal BankMin = 0; decimal BankMax = 0; decimal AgentCost = 0; //代理 if (payway == 1) { //支付宝 UserCost = FastUserPay.UserCost2; BankCost = FastPayWay.BankCost2; BankMin = FastPayWay.MinCost2; BankMax = FastPayWay.MaxCost2; AgentCost = FastPayWay.Cost2; } if (payway == 2)//微信 { UserCost = FastUserPay.UserCost; BankCost = FastPayWay.BankCost; BankMin = FastPayWay.MinCost; BankMax = FastPayWay.MaxCost; AgentCost = FastPayWay.Cost; } if (payway == 3)//银联 { UserCost = FastUserPay.UserCost3; BankCost = FastPayWay.BankCost3; BankMin = FastPayWay.MinCost3; BankMax = FastPayWay.MaxCost3; AgentCost = FastPayWay.Cost3; } #region 创建交易 //=======================生成订单=========================== SysAgent SysAgent = Entity.SysAgent.FirstOrNew(n => n.Id == Users.Agent); IList <SysAgent> SysAgentList = SysAgent.GetAgentsById(Entity); SysAgent TopAgent = SysAgentList.FirstOrNew(); FastOrder FastOrder = new FastOrder(); FastOrder.ComeWay = 1; FastOrder.UId = Users.Id; FastOrder.Agent = SysAgent.Id; FastOrder.PayId = string.Empty; FastOrder.OType = payway; FastOrder.PayWay = FastPayWay.Id; FastOrder.CashType = InFastOrder.CashType; FastOrder.Amoney = Amount; //用户手续费 decimal Poundage = Amount * UserCost + FastUserPay.UserCash; Poundage = Poundage.Ceiling(); FastOrder.Poundage = Poundage; //用户最终金额 FastOrder.PayMoney = FastOrder.Amoney - FastOrder.Poundage; if (FastOrder.PayMoney < 0) { DataObj.OutError("2076"); return; } FastOrder.UserRate = UserCost; FastOrder.AgentRate = AgentCost; FastOrder.SysRate = BankCost; FastOrder.UserCash = FastUserPay.UserCash; FastOrder.SysCash = FastPayWay.Cash; FastOrder.SameGet = 0; //计算手续费差 SysMoneySet SysMoneySet = Entity.SysMoneySet.FirstOrNew(); decimal PaySplit = 0;//代理商费率 PaySplit = SysAgent.GetSplit(TopAgent.Tier, Entity); decimal PayGet = Amount * PaySplit; PayGet = PayGet.Floor(); //一级代理利润 decimal AgentPayGet = PayGet; AgentPayGet = AgentPayGet.Floor(); FastOrder.AgentPayGet = 0; string AgentPath = "|"; string Split = "|"; decimal MyGet = PayGet; foreach (var p in SysAgentList) { PaySplit = SysAgent.GetSplit(p.Tier, Entity); AgentPath += p.Id + "|"; MyGet = Amount * PaySplit;//各级代理分润 MyGet = MyGet.Floor(); Split += MyGet.ToString("F2") + "|"; } FastOrder.AgentPath = AgentPath; FastOrder.Split = Split; decimal BankMoney = Amount * BankCost; if (BankMoney < BankMin) { BankMoney = BankMin; } if (BankMoney > BankMax) { BankMoney = BankMax; } BankMoney = BankMoney.Floor(); //用户手续费(含代付手续费)-代付分润-银行手续费-银行代付成本 decimal HFGet = Poundage - AgentPayGet - BankMoney - FastPayWay.Cash; FastOrder.HFGet = HFGet; FastOrder.State = 1; FastOrder.AddTime = DateTime.Now; FastOrder.PayState = 0; FastOrder.AgentState = 0; FastOrder.UserState = 0; FastOrder.CardName = FastUserPay.CardName; FastOrder.Bank = FastUserPay.Bank; FastOrder.Card = FastUserPay.Card; FastOrder.Bin = FastUserPay.Bin; Entity.FastOrder.AddObject(FastOrder); Entity.SaveChanges(); Entity.Refresh(RefreshMode.StoreWins, FastOrder); #endregion //======================================= UserTrack.ENo = DataObj.ENo; UserTrack.OPType = "到银行卡交易"; UserTrack.UserName = FastOrder.TNum; UserTrack.UId = FastOrder.UId; UserTrack.SeavGPSLog(Entity); //======================================= //=======================分润记录=========================== MyGet = PayGet; int tier = 1; foreach (var p in SysAgentList) { PaySplit = SysAgent.GetSplit(p.Tier, Entity); MyGet = Amount * PaySplit;//各级代理分润 MyGet = MyGet.Floor(); FastSplit FastSplit = new FastSplit(); FastSplit.Tnum = FastOrder.TNum; FastSplit.Profit = MyGet; FastSplit.AgentId = p.Id; FastSplit.Tier = p.Tier; FastSplit.AddTime = DateTime.Now; Entity.FastSplit.AddObject(FastSplit); tier++; } Entity.SaveChanges(); //=======================请求接口开始=========================== if (FastOrder.OType == 1 || FastOrder.OType == 2) { if (FastPayWay.DllName == "HFPay") { string NoticeUrl = NoticePath + "/PayCenter/HFPay/FastNotice.html";//后台通过地址 #region 微信&支付宝 string Action = ""; if (FastOrder.OType == 1) { Action = "AliSao"; } else if (FastOrder.OType == 2) { Action = "WxSao"; } //提交结算中心 string merId = PayConfigArr[0]; //商户号 string merKey = PayConfigArr[1]; //商户密钥 string PayWay = PayConfigArr[2]; //绑定通道 decimal money = FastOrder.Amoney * 100; string OrderMoney = money.ToString("F0");//金额,以分为单 string PostJson = "{\"action\":\"" + Action + "\",\"txnamt\":\"" + OrderMoney + "\",\"merid\":\"" + merId + "\",\"payway\":\"" + PayWay + "\",\"orderid\":\"" + FastOrder.TNum + "\",\"backurl\":\"" + NoticeUrl + "\"}"; string DataBase64 = LokFuEncode.Base64Encode(PostJson, "utf-8"); string Sign = (DataBase64 + merKey).GetMD5(); DataBase64 = HttpUtility.UrlEncode(DataBase64); string PostData = string.Format("req={0}&sign={1}", DataBase64, Sign); string HF_Url = "https://api.zhifujiekou.com/api/mpgateway"; string Ret = Utils.PostRequest(HF_Url, PostData, "utf-8"); JObject JS = new JObject(); try { JS = (JObject)JsonConvert.DeserializeObject(Ret); } catch (Exception) { FastOrder.State = 0; FastOrder.Remark = "数据请求出错"; Entity.SaveChanges(); JS = null; } if (JS != null) { string resp = JS["resp"].ToString(); Ret = LokFuEncode.Base64Decode(resp, "utf-8"); try { JS = (JObject)JsonConvert.DeserializeObject(Ret); } catch (Exception Ex) { FastOrder.State = 0; FastOrder.Remark = "JSON加载出错"; Entity.SaveChanges(); JS = null; } if (JS != null) { string respcode = JS["respcode"].ToString(); if (respcode != "00") { string respmsg = JS["respmsg"].ToString(); FastOrder.State = 0; FastOrder.Remark = respmsg; Entity.SaveChanges(); } else { if (JS["formaction"] == null) { FastOrder.State = 0; FastOrder.Remark = "接口没有返回二维码"; Entity.SaveChanges(); } else { string BankNum = JS["queryid"].ToString(); string qr_code = JS["formaction"].ToString(); FastOrder.PayId = qr_code; FastOrder.Trade = BankNum; Entity.SaveChanges(); } } } } #endregion } } else if (FastOrder.OType == 3) { if (FastPayWay.DllName == "HFPay" || FastPayWay.DllName == "HFJSPay") { #region //银联不用请求第三方,直接生成链接 FastOrder.PayId = PayPath + "/paycenter/pay-" + FastOrder.Id + ".html?sign=" + ((FastOrder.Id * 100 + 99) + "Pay").GetMD5().Substring(8, 8); Entity.SaveChanges(); #endregion } } if (FastOrder.State == 1) { if (FastOrder.PayState == 1) { if (FastOrder.UserState == 1) { FastOrder.State = 3; } else { FastOrder.State = 2; } } else { FastOrder.State = 1; } } else { FastOrder.State = 0; DataObj.OutError("1005"); return; } FastOrder.Cols = "TNum,PayId,Amoney,Poundage,State"; DataObj.Data = FastOrder.OutJson(); DataObj.Code = "0000"; DataObj.OutString(); }
public void Post() { string Data = DataObj.GetData(); if (Data.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } JObject json = new JObject(); try { json = (JObject)JsonConvert.DeserializeObject(Data); } catch (Exception Ex) { Log.Write("[JobCreate]:", "【Data】" + Data, Ex); } if (json == null) { DataObj.OutError("1000"); return; } UserTrack UserTrack = new UserTrack(); UserTrack = JsonToObject.ConvertJsonToModel(UserTrack, json); if (UserTrack.X.IsNullOrEmpty() || UserTrack.Y.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } JobSet JobSet = Entity.JobSet.FirstOrNew(); //获取配置 decimal Floated = JobSet.Floated; //浮动因子 #region 处理验证参数 if (json["token"] == null || json["money"] == null || json["card"] == null) { DataObj.OutError("1004"); return; } string Token = json["token"].ToString(); string money = json["money"].ToString(); string card = json["card"].ToString(); if (Token.IsNullOrEmpty() || money.IsNullOrEmpty() || card.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } string mymoney = ""; string number = ""; JArray dates = null; if (json["mymoney"] != null) { mymoney = json["mymoney"].ToString(); if (mymoney.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } } else if (json["number"] != null && json["dates"] != null) { number = json["number"].ToString(); dates = (JArray)json["dates"]; if (number.IsNullOrEmpty()) { DataObj.OutError("1000"); return; } } else { DataObj.OutError("1004"); return; } int Number = 0, Money = 0, MyMoney = 0, Card = 0; try { Money = Int32.Parse(money); Card = Int32.Parse(card); if (!number.IsNullOrEmpty()) { Number = Int32.Parse(number); } if (!mymoney.IsNullOrEmpty()) { MyMoney = Int32.Parse(mymoney); } }catch (Exception) { DataObj.OutError("1000"); return; } if (Money <= 0) { DataObj.Msg = "帐单金额需大于零"; DataObj.OutError("1000"); return; } if (MyMoney > Money) { DataObj.Msg = "还款金额不能大于帐单金额"; DataObj.OutError("1000"); return; } int Days = 0; DateTime[] Dates = null; if (dates != null) { Days = dates.Count; Dates = new DateTime[Days]; int Count = 0; bool NoFaile = true; foreach (var p in dates) { string date = p["date"].ToString(); try { DateTime Date = DateTime.Parse(date); Dates[Count] = Date; } catch (Exception) { NoFaile = false; } Count++; } if (!NoFaile) { DataObj.OutError("1000"); return; } } DateTime Now = DateTime.Now; DateTime Today = DateTime.Parse(Now.ToString("yyyy-MM-dd")); //这里启用智能计算 if (MyMoney > 0) { //计算最少几天还款 //如果按MyMoney付的话,控制不超的话,应该去掉手续费 decimal maxMoney = (decimal)MyMoney * (1 - JobSet.Cost) - JobSet.Cash; Days = (int)Math.Ceiling((decimal)Money * (1 + Floated) / maxMoney);//计算还款需要几天 if (Days > JobSet.MaxDay) { Days = JobSet.MaxDay;//限制最大天数 } //这里增加修正,如果笔数过多,则修正笔数 while (Money < JobSet.MinMoney * Days && maxMoney * (Days - 1) > Money) { Days = Days - 1;//减少一天还款 } if (Days < 2) { Days = 2; } Dates = new DateTime[Days]; //这里不能直接用传进来的计划金额去计算,应该总金额-传进来金额再平均 int everyDayMoney = (Money - MyMoney) / (Days - 1); int bei = (int)Math.Ceiling(everyDayMoney / JobSet.DayMoney);//超过一定金额拆分笔数 if (bei > JobSet.MaxPay) { bei = JobSet.MaxPay;//每天刷笔数过大取最大 } if (bei == 0) { bei = 1; } Number = (Days - 1) * bei + 1;//第一天只能刷一笔 Dates[0] = Today; for (var i = 1; i < Days; i++) { if (Now > Today.AddHours(23))//超过23点了 { Dates[i] = Today.AddDays(i + 1); } else { Dates[i] = Today.AddDays(i); } } } if (Number <= 0) { DataObj.OutError("1000"); return; } if (Card <= 0) { DataObj.OutError("1000"); return; } if (Days <= 0) { DataObj.OutError("1000"); return; } if (Days > JobSet.MaxDay) { DataObj.Msg = "最大还款天数为" + JobSet.MaxDay + "天"; DataObj.OutError("1000"); return; } if (Days > Number) { DataObj.Msg = "刷卡笔数必需大于等于还款天数"; DataObj.OutError("1000"); return; } int MaxPayNum = (Days - 1) * JobSet.MaxPay + 1; if (Number > MaxPayNum) { DataObj.Msg = Days + "天最多刷卡笔数为" + MaxPayNum + "笔"; DataObj.OutError("1000"); return; } decimal EP = (decimal)Money / (decimal)Number; if (EP < JobSet.MinMoney) { if (MyMoney > 0) { DataObj.Msg = "经测算每天还款金额不足" + JobSet.MinMoney.ToMoney() + "元。"; } else { DataObj.Msg = "账单金额不能低于" + (Number * JobSet.MinMoney).ToMoney() + "元"; } DataObj.OutError("1000"); return; } if (EP > JobSet.MaxMoney) { DataObj.Msg = "账单金额不能大于" + (Number * JobSet.MaxMoney).ToMoney() + "元"; DataObj.OutError("1000"); return; } decimal EM = Money / Days; if (EM < JobSet.MinMoney) { DataObj.Msg = "每天最小还款金额为" + (Days * JobSet.MinMoney).ToString("f2") + "元"; DataObj.OutError("1000"); return; } if (EM > JobSet.MaxMoney) { DataObj.Msg = "每天最大还款金额为" + JobSet.MaxMoney.ToMoney() + "元"; DataObj.OutError("1000"); return; } //数据排序,避免前端大小乱排上送数据造成错误 Array.Sort(Dates); if (!Dates.GroupBy(o => o).All(o => o.Count() <= 1)) { DataObj.Msg = "日期不能重复"; DataObj.OutError("1000"); return; } if (!Dates.All(o => o >= Today)) { DataObj.Msg = "还款日期已过期"; DataObj.OutError("1000"); return; } DateTime FirstDate = Dates.First(); DateTime LastDate = Dates.Last(); //if (FirstDate < Today) { // DataObj.Msg = "还款日期已过期"; // DataObj.OutError("1000"); // return; //} TimeSpan TS = FirstDate.Subtract(LastDate).Duration(); TimeSpan TSs = new TimeSpan(JobSet.EqDays, 0, 0, 0); if (TS > TSs) { DataObj.Msg = "还款日期跨度过大"; DataObj.OutError("1000"); return; } #endregion byte AdvCost = JobSet.AdvCost; //预收刷卡手续费 byte AdvCash = JobSet.AdvCash; //预收代付手续费 int MaxRand = JobSet.MaxRand; //最大失败次数 //Money Number DateTime[] Dates #region 获取用户信息 Users baseUsers = Entity.Users.FirstOrDefault(n => n.Token == Token); if (baseUsers == null)//用户令牌不存在 { DataObj.OutError("2004"); return; } if (baseUsers.State != 1)//用户被锁定 { DataObj.OutError("2003"); return; } if (baseUsers.CardStae != 2)//未实名认证 { DataObj.OutError("2006"); return; } if (baseUsers.MiBao != 1)//未设置支付密码 { DataObj.OutError("2008"); return; } if (baseUsers.StopPayState == 2)//禁止支付 { DataObj.OutError("6060"); return; } #endregion UserCard UserCard = Entity.UserCard.FirstOrDefault(n => n.Id == Card && n.UId == baseUsers.Id && n.Type == 2 && n.State == 1); if (UserCard == null) { DataObj.OutError("1000"); return; } UserCardOpen UserCardOpen = Entity.UserCardOpen.FirstOrDefault(n => n.State == 1 && n.CardNum == UserCard.Card && n.UId == baseUsers.Id); if (UserCardOpen == null) { DataObj.OutError("6032"); return; } //if (UserCardOpen.PayWay == 1) { // DataObj.Msg = "您的银行卡授权已过期,请删除后重新绑卡授权。"; // DataObj.OutError("6032"); // return; //} JobPayWay JobPayWay = Entity.JobPayWay.FirstOrDefault(n => n.State == 1 && n.GroupType == "Pay" && n.Id == UserCardOpen.PayWay);//从开卡记录中取通道 if (JobPayWay == null) { DataObj.OutError("6031"); return; } JobPayWay JobCashWay = Entity.JobPayWay.FirstOrDefault(n => n.State == 1 && n.GroupType == "Cash" && n.DllName == JobPayWay.DllName);//匹配出一条还款通道 if (JobCashWay == null) { DataObj.OutError("6031"); return; } bool IsRun = Entity.JobOrders.Where(o => (o.State == 2 || o.State == 3) && o.UId == baseUsers.Id && o.UserCardId == UserCard.Id).Select(o => o.UserCardId).FirstOrDefault() != 0 ? true : false; if (IsRun) { DataObj.OutError("7006"); return; } decimal Cost = JobSet.Cost; //刷卡手续费 decimal Cash = JobSet.Cash; //还款手续费 //if (baseUsers.IsVIP == 1) { // Cost = JobSet.VIPCost; // Cash = JobSet.VIPCash; //} //开始任务 RandItems RandItems = Rand.Create(Dates, Money, Number, Floated); List <jobitem> JobList = RandItems.JobList; //这里要加上验证 //============================================================ if (AdvCost == 0 || AdvCash == 0) { bool CheckOk = false; int CTimes = 0; while (!CheckOk && CTimes < MaxRand) { CheckOk = true; List <jobitem> JobListCash = JobList.Where(n => n.JobType == 2).ToList(); jobitem first = JobListCash.FirstOrDefault(); List <jobitem> JobListCashT = JobListCash.Skip(1).ToList(); foreach (var p in JobListCashT) { //只需验证还款金额 decimal AddMoney = 0; if (AdvCost == 0) { AddMoney += p.Money * Cost; AddMoney = AddMoney.Ceiling(); } if (AdvCash == 0) { AddMoney += Cash; } if (first.Money < p.Money + AddMoney) { CheckOk = false; break; } first = p; } //============================================================ if (MyMoney > 0) { jobitem firstpay = JobList.Where(n => n.JobType == 1).OrderByDescending(n => n.Money).First(); //这里检测第一笔加上手续费后大于输入金额则重新生成 decimal Poundage1, Poundage2; if (AdvCost == 1) //预收 { Poundage1 = Money * Cost; //预收总金额*手续费 } else { Poundage1 = firstpay.Money * Cost;//任务金额*手续费 } Poundage1 = Poundage1.Ceiling(); if (AdvCash == 1)//预收 { int AllPayNum = JobList.Count(n => n.JobType == 1); Poundage2 = AllPayNum * Cash;//预收总笔数*手续费 } else { Poundage2 = Cash;//第一天只有一笔交易和代付 } decimal Poundage = Poundage1 + Poundage2; if (firstpay.Money + Poundage > MyMoney) { CheckOk = false; } } //============================================================ if (!CheckOk) { //失败了重新生成 RandItems = Rand.Create(Dates, Money, Number, Floated); JobList = RandItems.JobList; } CTimes++; } ; if (!CheckOk) { DataObj.OutError("6033"); return; } } //============================================================ //生成订单 JobOrders JobOrders = new JobOrders(); JobOrders.UId = baseUsers.Id; JobOrders.Amount = 0; JobOrders.State = 0;//0取消 1待付款 2待执行 3执行中 4执行完成 5执行失败 6暂停(预留) JobOrders.AddTime = DateTime.Now; JobOrders.PayState = 0; JobOrders.PayedState = 0; JobOrders.PayWay = JobPayWay.Id; JobOrders.CashWay = JobCashWay.Id; JobOrders.AgentId = baseUsers.Agent; JobOrders.AgentState = 0; JobOrders.AgentGet = 0;//代理分润,等分润执行时写回 JobOrders.PayRate = JobPayWay.Cost; JobOrders.PayMin = JobPayWay.MinCost; JobOrders.PayMax = JobPayWay.MaxCost; JobOrders.CashRate = JobCashWay.Cost; JobOrders.CashMin = JobCashWay.MinCost; JobOrders.CashMax = JobCashWay.MaxCost; JobOrders.UPayRate = Cost; JobOrders.UPayMin = 0; JobOrders.UPayMax = 999999999; JobOrders.UCashRate = 0; JobOrders.UCashMin = Cash; JobOrders.UCashMax = 999999999; JobOrders.TotalMoney = 0; //总订单金额 JobOrders.Poundage = 0; //总手续费 JobOrders.HFGet = 0; //总成本 JobOrders.PayPoundage = 0; //交易手续费 JobOrders.CashPoundage = 0; //代付手续费 JobOrders.PayGet = 0; //交易成本 JobOrders.CashGet = 0; //代付成本 JobOrders.UserCardId = UserCard.Id; //计划对应的信用卡 JobOrders.AdvCost = JobSet.AdvCost; JobOrders.AdvCash = JobSet.AdvCash; Entity.JobOrders.AddObject(JobOrders); Entity.SaveChanges(); Entity.Refresh(RefreshMode.StoreWins, JobOrders); IList <JobItem> JobItemList = new List <JobItem>(); //============================================================ DateTime FirstRun = Today; //获取代理商分润 //SysAgent SysAgent = Entity.SysAgent.FirstOrNew(n => n.Id == baseUsers.Agent); //IList<SysAgent> SysAgentList = SysAgent.GetAgentsById(Entity); //SysAgent TopAgent = SysAgentList.FirstOrNew(); //decimal PaySplit = 0;//代理商费率 //PaySplit = SysAgent.GetSplit(TopAgent.Tier, Entity); foreach (var p in JobList) { JobItem JobItem = new JobItem(); JobItem.TNum = JobOrders.TNum; JobItem.UId = JobOrders.UId; JobItem.RunTime = p.RunTime; JobItem.RunType = p.JobType; JobItem.State = 0;//状态:0取消 1待执行 2执行中 3执行完成 4执行失败 JobItem.RunState = 0; JobItem.RunSort = 0; JobItem.AddTime = DateTime.Now; JobItem.UserCardId = UserCard.Id; DateTime NowDate = DateTime.Parse(JobItem.RunTime.ToString("yyyy-MM-dd")); decimal Poundage = 0, Poundage1 = 0, Poundage2 = 0; if (NowDate == Today) { //第一笔 if (p.JobType == 1) { //消费 if (AdvCost == 1) //预收 { Poundage1 = Money * Cost; //预收总金额*手续费 } else { Poundage1 = p.Money * Cost;//任务金额*手续费 } Poundage1 = Poundage1.Ceiling(); //2017-11-06收到新需求,每笔刷卡加金额,代付不收费用 if (AdvCash == 1)//预收 { int AllPayNum = JobList.Count(n => n.JobType == 1); Poundage2 = AllPayNum * Cash;//预收总笔数*手续费 } else { Poundage2 = Cash;//第一天只有一笔交易和代付 } Poundage = Poundage1 + Poundage2; JobItem.RunMoney = p.Money + Poundage; JobItem.Poundage = Poundage; JobItem.PayWay = JobPayWay.Id; JobItem.UserCardId = 0; } if (p.JobType == 2) { //还款 JobItem.RunMoney = p.Money; JobItem.Poundage = 0; JobItem.PayWay = JobCashWay.Id; } } else { if (p.JobType == 1) { //消费 if (AdvCost == 0) //实收 { Poundage1 = p.Money * Cost; //预收总金额 Poundage1 = Poundage1.Ceiling(); } if (AdvCash == 0)//实收 { //2017-11-06收到新需求,每笔刷卡加金额,代付不收费用 Poundage2 = Cash; //每天只有一笔代付 //==================当天第一笔要加,其它不需要加 //if (FirstRun != NowDate) { //Poundage2 = Cash; //FirstRun = NowDate; //} } Poundage = Poundage1 + Poundage2; JobItem.RunMoney = p.Money + Poundage; JobItem.Poundage = Poundage; JobItem.PayWay = JobPayWay.Id; } if (p.JobType == 2) { //还款 JobItem.RunMoney = p.Money; JobItem.Poundage = 0; JobItem.PayWay = JobCashWay.Id; } } decimal AgentPoundage = JobItem.Poundage; if (JobItem.RunType == 1) { JobItem.RunGet = p.Money * JobPayWay.Cost; if (JobItem.RunGet < JobPayWay.MinCost) { JobItem.RunGet = JobPayWay.MinCost; } if (JobItem.RunGet > JobPayWay.MaxCost) { JobItem.RunGet = JobPayWay.MaxCost; } AgentPoundage = p.Money * JobPayWay.CostAgent; if (AgentPoundage < JobPayWay.MinAgentCost) { AgentPoundage = JobPayWay.MinAgentCost; } if (AgentPoundage > JobPayWay.MaxAgentCost) { AgentPoundage = JobPayWay.MaxAgentCost; } } if (JobItem.RunType == 2) { JobItem.RunGet = p.Money * JobCashWay.Cost; if (JobItem.RunGet < JobCashWay.MinCost) { JobItem.RunGet = JobCashWay.MinCost; } if (JobItem.RunGet > JobCashWay.MaxCost) { JobItem.RunGet = JobCashWay.MaxCost; } AgentPoundage = p.Money * JobCashWay.CostAgent; if (AgentPoundage < JobCashWay.MinAgentCost) { AgentPoundage = JobCashWay.MinAgentCost; } if (AgentPoundage > JobCashWay.MaxAgentCost) { AgentPoundage = JobCashWay.MaxAgentCost; } } JobItem.RunGet = JobItem.RunGet.Ceiling();//通道成本 //AgentPoundage = AgentPoundage.Ceiling();//代理手续费 //JobItem.AgentGet = Poundage1 - AgentPoundage;//代理分润 JobItem.AgentGet = 0; JobItem.HFGet = 0; //利润=用户手续费-代理分润-通道成本 //JobItem.HFGet = JobItem.Poundage - JobItem.AgentGet - JobItem.RunGet; Entity.JobItem.AddObject(JobItem); JobItemList.Add(JobItem); } decimal TotalPoundage = JobItemList.Sum(n => n.Poundage); decimal TotalHFGet = JobItemList.Sum(n => n.HFGet); decimal TotalAgentGet = JobItemList.Sum(n => n.AgentGet); decimal TotalPoundage1 = JobItemList.Where(n => n.RunType == 1).Sum(n => n.Poundage); decimal TotalPoundage2 = JobItemList.Where(n => n.RunType == 2).Sum(n => n.Poundage); decimal TotalRunGet1 = JobItemList.Where(n => n.RunType == 1).Sum(n => n.RunGet); decimal TotalRunGet2 = JobItemList.Where(n => n.RunType == 2).Sum(n => n.RunGet); JobOrders.TotalMoney = Money + TotalPoundage; //总订单金额 JobOrders.Poundage = TotalPoundage; //总手续费 JobOrders.AgentGet = TotalAgentGet; //代理分润 JobOrders.HFGet = TotalHFGet; //利润 JobOrders.PayPoundage = TotalPoundage1; //交易手续费 JobOrders.CashPoundage = TotalPoundage2; //代付手续费 JobOrders.PayGet = TotalRunGet1; //交易成本 JobOrders.CashGet = TotalRunGet2; //代付成本 JobOrders.State = 1; //0取消 1待付款 2待执行 3执行中 4执行完成 5执行失败 6暂停(预留) JobOrders.SameGet = 0; Entity.SaveChanges(); JobOrders.Cols = JobOrders.Cols + ",Items,UsersCard,SetCost,SetCash,XFCount,AdvCost,AdvCash"; JobItemList = Entity.JobItem.Where(n => n.TNum == JobOrders.TNum).OrderBy(n => n.RunTime).ToList(); //============================================================ int RunSort = 1; foreach (var p in JobItemList) { if (RunSort == JobList.Count) { //最后一笔特别标识 p.RunSort = 999; } else { p.RunSort = RunSort; } RunSort++; } Entity.SaveChanges(); //============================================================ //这里加上修正有分问题 IList <JobItem> JobItemPay = JobItemList.Where(n => n.RunType == 1).ToList(); JobItem FirstItem = JobItemPay.First(); JobItem LastItem = JobItemPay.Last(); if (JobItemPay.Count > 1) { decimal RunAu = 0; //用于存放差额 decimal Adv = 0; //用于存放每笔调整金额 bool CanSvae = true; //用于判断是否能修正 foreach (var p in JobItemPay) { if (p.Id == FirstItem.Id) { //第一笔 if (MyMoney > p.RunMoney) { RunAu = MyMoney - p.RunMoney; p.RunMoney = MyMoney; Adv = (RunAu / (JobItemPay.Count - 1)).Ceiling();//后台平均一笔要增减金额 } } else if (p.Id == LastItem.Id) { //最后一笔 p.RunMoney = p.RunMoney - RunAu;//修正差额 if (p.RunMoney < 0) { CanSvae = false; } } else { decimal RunMoney = p.RunMoney; //任务金额 decimal Run10 = (RunMoney - Adv) * 10; //放大十倍 decimal Run10U = Math.Ceiling(Run10); //向上取整个 decimal RunMoneyU = Run10U / 10; //缩小十倍 p.RunMoney = RunMoneyU; //重新赋值 RunAu += (RunMoneyU - RunMoney); //记录差额 } } if (CanSvae) { Entity.SaveChanges(); } } //============================================================ UserTrack.UId = baseUsers.Id; UserTrack.ENo = DataObj.ENo; UserTrack.OPType = "创建还款订单"; UserTrack.GPSAddress = UserTrack.GPSAddress; UserTrack.GPSX = UserTrack.X; UserTrack.GPSY = UserTrack.Y; UserTrack.UserName = JobOrders.TNum; UserTrack.SeavGPSLog(Entity); //======================================= //最后检测整个任务是否有单笔小于1元的 if (JobItemList.Count(n => n.RunMoney < 1) > 0) { DataObj.OutError("6033"); return; } //======================================= //这里之后不能执行SaveChanges,否则很严重 foreach (var p in JobItemList) { p.State = 1; } var JobItemModelList = JobItemList.GroupBy(o => o.RunTime.Date, (x, o) => new JobItemModel { date = x, item = o.OrderBy(k => k.RunType).ThenBy(n => n.RunTime).ToList().EntityToJson(), }).OrderBy(o => o.date).ToList(); JobOrders.Items = JobItemModelList.EntityToJson(); JobOrders.XFCount = JobItemList.Where(o => o.RunType == 1).Count();; JobOrders.SetCost = JobOrders.UPayRate; JobOrders.SetCash = JobOrders.UCashMin; string UserCardStr = UserCard.OutJson(); JObject JS = new JObject(); try { JS = (JObject)JsonConvert.DeserializeObject(UserCardStr); } catch (Exception Ex) { Log.Write("[JobCreate]:", "【JsStr】" + UserCardStr, Ex); } JobOrders.UsersCard = JS; string data = JobOrders.OutJson(); data = data.Replace("\"[{", "[{").Replace("}]\"", "}]").Replace("\\", "").Replace("]\"}", "]}"); DataObj.Data = data; DataObj.Code = "0000"; DataObj.OutString(); }