1012 lines
61 KiB
C#
1012 lines
61 KiB
C#
using CRM.Core.BLL.Application.Order;
|
||
using CRM.Core.BLL.Base;
|
||
using CRM.Core.BLL.Csvr;
|
||
using CRM.Core.BLL.EventBus.Events;
|
||
using CRM.Core.Common.Layui;
|
||
using CRM.Core.DAL;
|
||
using CRM.Core.DTO;
|
||
using CRM.Core.DTO.Ord;
|
||
using CRM.Core.Model.Entity;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using WX.CRM.Common;
|
||
|
||
namespace CRM.Core.BLL.Wx
|
||
{
|
||
public class WX_SzzyOrderDeposit_BL : DbContextRepository<WX_SzzyOrderDeposit>
|
||
{
|
||
private readonly Bas_CompanyVirtual_BL _company = new Bas_CompanyVirtual_BL();
|
||
private readonly Csvr_Message_BL _msg = new Csvr_Message_BL();
|
||
private readonly Wx_SzzyOrderPayUseLog_BL _payuselog = new Wx_SzzyOrderPayUseLog_BL();
|
||
private WX_SZZYORDER_BL _order = new WX_SZZYORDER_BL();
|
||
|
||
public List<OrderDepositView> GetDepositByUmid(string Umid, Laypage pg) {
|
||
using (var db = new zxdContext())
|
||
{
|
||
if (string.IsNullOrEmpty(Umid)) return new List<OrderDepositView>();
|
||
var query = from p in db.WX_SzzyOrderPay
|
||
join d in db.WX_SzzyOrderDeposit on p.depositid equals d.id into deposit
|
||
from dep in deposit.DefaultIfEmpty()
|
||
join o in db.WX_SZZYORDER on p.orderid equals o.ORDERID into order
|
||
from so in order.DefaultIfEmpty()
|
||
join c in db.RES_CUSTOMER on so.RESID equals c.RESID into cus
|
||
from cu in cus.DefaultIfEmpty()
|
||
where cu.UMID == Umid
|
||
select new OrderDepositView()
|
||
{
|
||
depositid= p.depositid,
|
||
umid = cu.UMID,
|
||
deptname=dep.deptname,
|
||
orderid=so.ORDERID,
|
||
needpay= p.needpay,
|
||
arrivalpay= so.ARRIVALPAY,
|
||
payprice= dep.payprice,
|
||
payname = p.payname,
|
||
paytypename=p.paytypename,
|
||
payno=p.payno,
|
||
remark=p.remark,
|
||
paydate=p.paydate
|
||
};
|
||
int skip = (pg.page - 1) * pg.limit;
|
||
pg.count = query.Count();
|
||
var reslut= query.OrderByDescending(x => x.paydate).Skip(skip).Take(pg.limit).ToList();
|
||
//var company = db.BAS_COMPANY.ToList();
|
||
//foreach (var item in reslut)
|
||
//{
|
||
// item.deptname = company.FirstOrDefault(m => m.COMPANYCODE.Split(',').Contains(item.deptname))?.COMPANYNAME;
|
||
//}
|
||
|
||
return reslut;
|
||
}
|
||
}
|
||
public int Add(OrderDepositDto dto, int? channel)
|
||
{
|
||
var model = new WX_SzzyOrderDeposit(dto.resid, dto.paytype, dto.paytypename, dto.paydate, dto.payprice, dto.payname, dto.remark, dto.payno, dto.isuse, dto.companycode, channel, dto.tradeno)
|
||
{
|
||
id = new SEQUENCES_BL().Seq_OrderDeposit()
|
||
};
|
||
if (dto.creator.HasValue)
|
||
{
|
||
model.creator = dto.creator.Value;
|
||
}
|
||
if (!string.IsNullOrEmpty(dto.creatorname))
|
||
{
|
||
model.creatorname = dto.creatorname;
|
||
}
|
||
|
||
using (var db = new zxdContext())
|
||
{
|
||
using (var trans = db.Database.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
if (!string.IsNullOrEmpty(dto.orderid))
|
||
{
|
||
var idsArr = dto.orderid.Split(',');
|
||
foreach (var arr in idsArr)
|
||
{
|
||
var orderid = Convert.ToInt32(arr[0]);
|
||
var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == orderid);
|
||
var pay = new WX_SzzyOrderPay(orderid, order != null ? order.NEEDPAY.Value : 0, dto.paytype, dto.paytypename, dto.paydate, dto.payprice, dto.payname, dto.remark, dto.payno, dto.companycode, channel, dto.tradeno);
|
||
if (dto.creator.HasValue)
|
||
{
|
||
pay.creator = dto.creator.Value;
|
||
}
|
||
if (!string.IsNullOrEmpty(dto.creatorname))
|
||
{
|
||
pay.creatorname = dto.creatorname;
|
||
}
|
||
|
||
pay.depositid = model.id;
|
||
|
||
db.WX_SzzyOrderPay.Add(pay);
|
||
}
|
||
}
|
||
|
||
db.WX_SzzyOrderDeposit.Add(model);
|
||
|
||
db.SaveChanges();
|
||
trans.Commit();
|
||
|
||
#region 发送通知
|
||
try
|
||
{
|
||
var comcode = _company.GetInfoByChannel(channel.Value);
|
||
_msg.PushNewsMsg("DJTZ", $"订金【{model.id}】已经【提交】,请您审核!", comcode == null ? "事业部" : comcode.CompanyName, model.id.ToString(), comcode == null ? model.companycode : comcode.CompanyCode);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error("发送通知出错:" + e.ToString());
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex);
|
||
trans.Rollback();
|
||
return 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
return model.id;
|
||
}
|
||
/// <summary>
|
||
/// 订金审核
|
||
/// </summary>
|
||
/// <param name="orderDeposit"></param>
|
||
/// <param name="userId"></param>
|
||
/// <param name="userName"></param>
|
||
/// <param name="type">1:普通审核 2:二维码到款,然后自动审核</param>
|
||
/// <param name="needAutomaticOpen">true:调用自动开通订单审核(默认) false:不调用自动开通订单审核</param>
|
||
/// <returns></returns>
|
||
public retMsg Audit(WX_SzzyOrderDeposit orderDeposit, int userId, string userName, int type = 1, bool needAutomaticOpen = true)
|
||
{
|
||
using (var client = RedisManager.GetClient())
|
||
{
|
||
var mdeposit = this.Get(m => m.id == orderDeposit.id);
|
||
using (var datalock = client.AcquireLock("DataLock:" + mdeposit.resid, TimeSpan.FromSeconds(10)))
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
using (var trans = db.Database.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var deposit = db.WX_SzzyOrderDeposit.FirstOrDefault(p => p.id == orderDeposit.id);
|
||
|
||
OrderAndPayInfoEvent eventInfo = new OrderAndPayInfoEvent() { depositList = new List<OrderAndPayInfo_Deposit>(), orderList = new List<OrderAndPayInfo_Order>(), payList = new List<OrderAndPayInfo_Pay>() };
|
||
|
||
|
||
|
||
var orderpays = db.WX_SzzyOrderPay.Where(p => p.depositid == orderDeposit.id && p.isdelete == 0 && p.payprice == deposit.payprice).OrderBy(m => m.paydate).ToList();
|
||
var orderpays2 = db.WX_SzzyOrderPay.Where(p => p.depositid == orderDeposit.id && p.isdelete == 0 && p.payprice > deposit.payprice).OrderBy(m => m.paydate).ToList();
|
||
var orderpays3 = db.WX_SzzyOrderPay.Where(p => p.depositid == orderDeposit.id && p.isdelete == 0 && p.payprice < deposit.payprice).OrderBy(m => m.paydate).ToList();
|
||
orderpays.AddRange(orderpays2);
|
||
orderpays.AddRange(orderpays3);
|
||
|
||
//else
|
||
//{
|
||
// var orderPay = new WX_SzzyOrderPay(orderDeposit.id, orderDeposit.payprice, orderDeposit.paytype, orderDeposit.paytypename, orderDeposit.paydate, orderDeposit.payprice, orderDeposit.payname, orderDeposit.remark, orderDeposit.payno, orderDeposit.companycode, orderDeposit.channel, orderDeposit.tradeno)
|
||
// {
|
||
// auditstatus = orderDeposit.auditstatus,
|
||
// auditor = orderDeposit.auditor,
|
||
// auditorname = orderDeposit.auditorname,
|
||
// audittime = orderDeposit.audittime,
|
||
// creator = orderDeposit.creator,
|
||
// creatorname = orderDeposit.creatorname
|
||
// };
|
||
// db.WX_SzzyOrderPay.Add(orderPay);
|
||
//}
|
||
|
||
decimal useprice = 0;
|
||
if (orderpays != null && orderpays.Count() > 0)
|
||
useprice = orderpays.Sum(p => p.payprice);
|
||
|
||
|
||
if (deposit.isdelete.HasValue && deposit.isdelete.Value == 1)
|
||
{
|
||
return new retMsg { result = false, retcode = 500, retmsg = "isDelete" };
|
||
}
|
||
deposit.isdelete = 0;
|
||
deposit.lastprice = deposit.payprice;//剩余可用金额,应该直接等于支付金额
|
||
deposit.auditstatus = 1;
|
||
deposit.auditor = userId;
|
||
deposit.auditorname = userName;
|
||
|
||
if (type == 2)//自动确认的,需要将支付日期和流水填上
|
||
{
|
||
deposit.paydate = orderDeposit.paydate;
|
||
deposit.payno = orderDeposit.payno;
|
||
}
|
||
|
||
|
||
db.SaveChanges();
|
||
|
||
|
||
var lastpriceLJ = db.WX_SzzyOrderDeposit.Where(m => m.resid == deposit.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);
|
||
|
||
//金额入账+(此处没毛病)
|
||
var addLog = new Wx_SzzyOrderPayUseLog(deposit.resid, deposit.orderid, deposit.id, deposit.paytype, deposit.paytypename, deposit.payno, deposit.payprice.Value, deposit.audittime ?? DateTime.Now, deposit.channel.Value, 1, lastpriceLJ, "到账审核");
|
||
db.Wx_SzzyOrderPayUseLog.Add(addLog);
|
||
db.SaveChanges();
|
||
|
||
|
||
//减去金额(这里有毛病,使用金额可能超出订单的金额)
|
||
//deposit.useprice += useprice;
|
||
//deposit.lastprice -= useprice;
|
||
if (!deposit.audittime.HasValue)
|
||
{
|
||
deposit.audittime = DateTime.Now;
|
||
}
|
||
db.SaveChanges();
|
||
|
||
|
||
//金额被使用记录日志
|
||
if (orderpays != null && orderpays.Any())
|
||
{
|
||
var orderPyas = orderpays.GroupBy(m => m.orderid);
|
||
foreach (var pays in orderPyas)//根据订单ID进行
|
||
{
|
||
var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == pays.Key);
|
||
int updateCount = 0;//修改次数
|
||
//对pay循环,并循环使用订金信息
|
||
foreach (var aa in pays)
|
||
{
|
||
var item = db.WX_SzzyOrderPay.FirstOrDefault(m => m.id == aa.id);
|
||
if (order.ARRIVALPAY >= order.NEEDPAY || deposit.lastprice <= 0)//订单已付金额>=应付,直接跳出,不应该在做使用了
|
||
{
|
||
item.auditstatus = -1;//使用的金额已经够了,那么需要把多余的使用记录删除掉
|
||
item.rejectremark = "订单不需要更多金额了。";
|
||
item.audittime = DateTime.Now;
|
||
item.auditorname = "系统";
|
||
db.SaveChanges();
|
||
SendInfoFormat(eventInfo, null, null, item);//将pay信息formrt
|
||
continue;
|
||
}
|
||
var stillNeedPay = (order.NEEDPAY ?? 0) - (order.ARRIVALPAY ?? 0);//还需付款
|
||
|
||
if (type == 2)//自动确认的,需要将支付日期和流水填上
|
||
{
|
||
item.paydate = orderDeposit.paydate;
|
||
item.payno = orderDeposit.payno;
|
||
}
|
||
decimal[] threaPrice = { stillNeedPay, item.payprice, (deposit.lastprice ?? 0) };
|
||
decimal miniPrice = threaPrice.Min();
|
||
if (miniPrice == 0)
|
||
{
|
||
//item.auditstatus = 0;//使用的金额已经够了,那么需要把多余的使用记录删除掉
|
||
//item.isdelete = 1;
|
||
|
||
item.auditstatus = -1;//使用的金额已经够了,那么需要把多余的使用记录删除掉
|
||
item.rejectremark = "订单不需要更多金额了。";
|
||
item.audittime = DateTime.Now;
|
||
item.auditorname = "系统";
|
||
db.SaveChanges();
|
||
SendInfoFormat(eventInfo, null, null, item);//将pay信息formrt
|
||
continue;
|
||
}
|
||
|
||
item.payprice = miniPrice;
|
||
|
||
item.auditstatus = 1;
|
||
item.auditor = userId;
|
||
item.auditorname = userName;
|
||
item.audittime = deposit.audittime;
|
||
item.checkpaytime = deposit.checkpaytime;
|
||
item.checkreslut = orderDeposit.checkreslut;
|
||
//金额被使用,添加一条 减金额流水
|
||
lastpriceLJ -= item.payprice;
|
||
deposit.useprice += item.payprice;//订金中 使用金额累加
|
||
deposit.lastprice -= item.payprice;//可用余额,累减
|
||
|
||
order.ARRIVALPAY = (order.ARRIVALPAY ?? 0) + item.payprice;//订单已付金额。
|
||
var log = new Wx_SzzyOrderPayUseLog(deposit.resid, item.orderid, item.depositid ?? 0, item.paytype, item.paytypename, item.payno, -item.payprice, item.audittime ?? DateTime.Now, item.channel.Value, 2, lastpriceLJ, "金额使用");
|
||
//_payuselog.Add(log);
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
db.SaveChanges();
|
||
updateCount++;
|
||
SendInfoFormat(eventInfo, null, null, item);//将pay信息formrt
|
||
}
|
||
//对订单信息进行修改
|
||
if (updateCount > 0)//有pay更改,那么需要修改订单信息,并且同步
|
||
{
|
||
if (order.ARRIVALPAY >= order.NEEDPAY)
|
||
{
|
||
order.ORDERSTATUS = "200";
|
||
order.ORDERSTATUSNAME = "已支付";
|
||
}
|
||
else
|
||
{
|
||
order.ORDERSTATUS = "190";
|
||
order.ORDERSTATUSNAME = "已提交支付";
|
||
}
|
||
var payDate = pays.OrderByDescending(p => p.paydate).First().paydate;
|
||
order.ARRIVALTIME = payDate;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
db.SaveChanges();
|
||
SendInfoFormat(eventInfo, null, order, null);//将订金信息formrt
|
||
|
||
#region 平台一到账状态回调
|
||
if (order.CHANNEL >= 25000 && order.CHANNEL <= 26199)
|
||
{
|
||
Task.Run(() => {
|
||
new OrderService().CallBackPayPT1(order.SZZYORDERID, deposit.paydate, deposit.payprice);
|
||
});
|
||
}
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
if ((order.ARRIVALPAY ?? 0) == 0)
|
||
{
|
||
order.ORDERSTATUS = "180";
|
||
order.ORDERSTATUSNAME = "新订单";
|
||
db.SaveChanges();
|
||
SendInfoFormat(eventInfo, null, order, null);//将订金信息formrt
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
#region 废弃(已改成再上面,直接在使用金额中判断订单是否完成支付)
|
||
////_payuselog.Add(addLog);//审核通过后,金额入库
|
||
//if (orderpays != null && orderpays.Any())
|
||
//{
|
||
// var orderids = orderpays.Select(p => p.orderid).Distinct().ToList();
|
||
// foreach (var orderid in orderids)
|
||
// {
|
||
// //单个订单的支付信息
|
||
// var orderpayList = db.WX_SzzyOrderPay.Where(p => p.orderid == orderid && p.isdelete == 0 && p.auditstatus == 1);
|
||
// var arrivalPay = orderpayList.Sum(p => p.payprice);
|
||
// var payDate = orderpayList.OrderByDescending(p => p.paydate).First().paydate;
|
||
|
||
// var oid = Convert.ToDecimal(orderid);
|
||
// var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == oid);
|
||
// if (order != null)
|
||
// {
|
||
// order.ARRIVALPAY = order.ARRIVALPAY.HasValue ? order.ARRIVALPAY : 0;//如果为空,需要赋值0
|
||
// order.ARRIVALPAY = arrivalPay;//需要加上本次的 使用金额再进行判断
|
||
// if (order.ARRIVALPAY >= order.NEEDPAY)
|
||
// {
|
||
// order.ORDERSTATUS = "200";
|
||
// order.ORDERSTATUSNAME = "已支付";
|
||
// }
|
||
// else
|
||
// {
|
||
// order.ORDERSTATUS = "190";
|
||
// order.ORDERSTATUSNAME = "已提交支付";
|
||
// }
|
||
|
||
// order.ARRIVALTIME = payDate;
|
||
// }
|
||
// }
|
||
//}
|
||
#endregion
|
||
|
||
db.SaveChanges();
|
||
|
||
trans.Commit();
|
||
|
||
//Common.EventBus.EventBus.Instance.Publish(new DepositOrderEvent(deposit.id, deposit.auditstatus, deposit.auditor.Value, deposit.auditorname, deposit.audittime.Value, deposit.isuse, deposit.companycode, deposit.payno, deposit.paydate));
|
||
SendInfoFormat(eventInfo, deposit, null, null);//将订金信息formrt
|
||
Common.EventBus.EventBus.Instance.Publish(eventInfo);
|
||
|
||
#region 调用自动判断是否开通程序,异步操作
|
||
if (needAutomaticOpen)//需要允许自动开通的,才能够去调用开通
|
||
{
|
||
Task.Run(() =>
|
||
{
|
||
new OrderService().CanIOpenByDepositAuditOrUse(new int[] { deposit.id }, userId, userName);
|
||
});
|
||
}
|
||
#endregion
|
||
|
||
return new retMsg { result = true, retcode = 200, retmsg = "审核成功" };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex);
|
||
trans.Rollback();
|
||
return new retMsg { result = false, retcode = 500, retmsg = "出现错误:" + ex.ToString() };
|
||
}
|
||
finally
|
||
{
|
||
trans.Dispose();
|
||
datalock.Dispose();
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 需要通知到坐席端的数据format
|
||
/// </summary>
|
||
/// <param name="eventInfo"></param>
|
||
/// <param name="deposit"></param>
|
||
/// <param name="order"></param>
|
||
/// <param name="item"></param>
|
||
public static void SendInfoFormat(OrderAndPayInfoEvent eventInfo, WX_SzzyOrderDeposit deposit, WX_SZZYORDER order, WX_SzzyOrderPay item)
|
||
{
|
||
if (deposit != null)
|
||
{
|
||
if (eventInfo.depositList == null)
|
||
eventInfo.depositList = new List<OrderAndPayInfo_Deposit>();
|
||
eventInfo.depositList.Add(new OrderAndPayInfo_Deposit
|
||
{
|
||
Auditor = deposit.auditor,
|
||
AuditorName = deposit.auditorname,
|
||
AuditStatus = deposit.auditstatus,
|
||
AuditTime = deposit.audittime,
|
||
FrozenPrice = deposit.frozenprice,
|
||
Id = deposit.id,
|
||
IsDelete = deposit.isdelete,
|
||
LastPrice = deposit.lastprice,
|
||
PayType = deposit.paytype,
|
||
PayTypeName = deposit.paytypename,
|
||
UsePrice = deposit.useprice
|
||
|
||
});//订金信息需要同步
|
||
eventInfo.channel = deposit.channel.Value;//通过渠道推送
|
||
}
|
||
if (order != null)
|
||
{
|
||
|
||
if (eventInfo.orderList == null)
|
||
eventInfo.orderList = new List<OrderAndPayInfo_Order>();
|
||
eventInfo.orderList.Add(new OrderAndPayInfo_Order
|
||
{
|
||
Arrivalpay = order.ARRIVALPAY,
|
||
Arrivaltime = order.ARRIVALTIME,
|
||
CName = order.CNAME,
|
||
OrderId = order.ORDERID,
|
||
OrderStatus = order.ORDERSTATUS,
|
||
OrderStatusName = order.ORDERSTATUSNAME,
|
||
ResId = order.RESID,
|
||
FinalPay = order.FINALPAY
|
||
|
||
});//需要同步至坐席
|
||
}
|
||
if (item != null)
|
||
{
|
||
if (eventInfo.payList == null)
|
||
eventInfo.payList = new List<OrderAndPayInfo_Pay>();
|
||
eventInfo.payList.Add(new OrderAndPayInfo_Pay
|
||
{
|
||
auditor = item.auditor,
|
||
paytypename = item.paytypename,
|
||
auditorname = item.auditorname,
|
||
auditstatus = item.auditstatus,
|
||
audittime = item.audittime,
|
||
channel = item.channel,
|
||
checkpaytime = item.checkpaytime,
|
||
checkreslut = item.checkreslut,
|
||
companycode = item.companycode,
|
||
creator = item.creator,
|
||
creatorname = item.creatorname,
|
||
depositid = item.depositid,
|
||
id = item.id,
|
||
isdelete = item.isdelete,
|
||
needpay = item.needpay,
|
||
orderid = item.orderid,
|
||
paydate = item.paydate,
|
||
payname = item.payname,
|
||
payno = item.payno,
|
||
payprice = item.payprice,
|
||
paytype = item.paytype,
|
||
rejectremark = item.rejectremark,
|
||
remark = item.remark,
|
||
tradeno = item.tradeno
|
||
|
||
});//需要同步至坐席
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 驳回,或者取消使用
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <param name="status">-1:驳回审核 0:取消使用</param>
|
||
/// <param name="rejectremark"></param>
|
||
/// <param name="userId"></param>
|
||
/// <param name="userName"></param>
|
||
/// <returns></returns>
|
||
public retMsg Reject(int id, int? status, string rejectremark, int userId, string userName)
|
||
{
|
||
using (var client = RedisManager.GetClient())
|
||
{
|
||
var deposit = this.Get(p => p.id == id);
|
||
using (var datalock = client.AcquireLock("DataLock:" + deposit.resid, TimeSpan.FromSeconds(10)))//5秒内释放锁
|
||
{
|
||
LogHelper.Info("id:" + id.ToString());
|
||
using (var db = new zxdContext())
|
||
{
|
||
using (var trans = db.Database.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var orderDeposit = db.WX_SzzyOrderDeposit.FirstOrDefault(p => p.id == id);
|
||
if (orderDeposit != null)
|
||
{
|
||
OrderAndPayInfoEvent eventInfo = new OrderAndPayInfoEvent() { depositList = new List<OrderAndPayInfo_Deposit>(), orderList = new List<OrderAndPayInfo_Order>(), payList = new List<OrderAndPayInfo_Pay>() };
|
||
//已经驳回,不需要在进行处理
|
||
if (orderDeposit.auditstatus == -1)
|
||
{
|
||
return new retMsg() { result = false, retcode = 300, retmsg = "exists" };
|
||
}
|
||
if (status == 0 && orderDeposit.auditstatus != 1)
|
||
{
|
||
return new retMsg() { result = false, retcode = 500, retmsg = "notauditcantcacel" };
|
||
}
|
||
var sumLastPrice = db.WX_SzzyOrderDeposit.Where(m => m.resid == orderDeposit.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);//获取总的金额
|
||
var lastPriceLJ = sumLastPrice;//当前的总金额
|
||
//状态空是取消到账使用,状态有值是驳回
|
||
if (status.HasValue && status.Value == -1)
|
||
{
|
||
if (orderDeposit.auditstatus == 1)
|
||
{
|
||
return new retMsg() { result = false, retcode = 400, retmsg = "isaudit" };
|
||
}
|
||
|
||
orderDeposit.auditstatus = status.Value;
|
||
orderDeposit.auditor = userId;
|
||
orderDeposit.auditorname = userName;
|
||
orderDeposit.audittime = DateTime.Now;
|
||
orderDeposit.rejectremark = rejectremark;
|
||
}
|
||
|
||
//重置使用金额,剩余金额
|
||
orderDeposit.lastprice = (orderDeposit.lastprice ?? 0) + (orderDeposit.useprice ?? 0);
|
||
orderDeposit.useprice = 0;
|
||
//= orderDeposit.payprice;
|
||
|
||
//db.SaveChanges();
|
||
var orderids = new List<int>();
|
||
//驳回的时候,所有使用的金额都要删除
|
||
var orderpays = db.WX_SzzyOrderPay.Where(p => p.depositid == orderDeposit.id && p.isdelete == 0);
|
||
foreach (var item in orderpays)
|
||
{
|
||
//未审核通过无需日志
|
||
if (status == 0 && item.auditstatus == 1)
|
||
{
|
||
item.auditstatus = -1;
|
||
item.rejectremark = rejectremark;
|
||
item.auditor = userId;
|
||
item.audittime = DateTime.Now;
|
||
item.auditorname = userName;
|
||
|
||
//item.isdelete = 1;
|
||
orderids.Add(item.orderid);
|
||
//取消使用
|
||
lastPriceLJ += item.payprice;
|
||
var log = new Wx_SzzyOrderPayUseLog(orderDeposit.resid, item.orderid, item.depositid ?? 0, item.paytype, item.paytypename, item.payno, item.payprice, item.audittime ?? DateTime.Now, item.channel.Value, 3, lastPriceLJ, "到款取消使用");
|
||
//_payuselog.Add(log);
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
}
|
||
else
|
||
{
|
||
item.auditstatus = -1;
|
||
item.rejectremark = rejectremark;
|
||
item.auditor = userId;
|
||
item.audittime = DateTime.Now;
|
||
item.auditorname = userName;
|
||
//item.isdelete = 1;
|
||
orderids.Add(item.orderid);
|
||
}
|
||
}
|
||
db.SaveChanges();
|
||
//var oldorderpays = db.WX_SzzyOrderPay.Where(p => p.orderid == orderDeposit.id && p.isdelete == 0);
|
||
////var oldorderids = new List<int>();
|
||
//foreach (var item in oldorderpays)
|
||
//{
|
||
|
||
// if (!item.depositid.HasValue)
|
||
// {
|
||
// //旧数据如果订金ID为空,更新数据
|
||
// item.depositid = id;
|
||
// }
|
||
|
||
// orderids.Add(item.orderid);
|
||
// //oldorderids.Add(item.orderid);
|
||
// if (status == 0 && item.auditstatus == 1)
|
||
// {
|
||
// item.auditstatus = 0;
|
||
// item.isdelete = 1;
|
||
// lastPriceLJ += item.payprice;
|
||
// var log = new Wx_SzzyOrderPayUseLog(orderDeposit.resid, item.orderid, item.depositid ?? 0, item.paytype, item.paytypename, item.payno, item.payprice, item.audittime ?? DateTime.Now, item.channel.Value, 3, lastPriceLJ, "到款取消使用");
|
||
// //_payuselog.Add(log);
|
||
// db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
// }
|
||
// else
|
||
// {
|
||
// item.auditstatus = 0;
|
||
// item.isdelete = 1;
|
||
// }
|
||
// SendInfoFormat(eventInfo, null, null, item);//将pay信息formrt
|
||
//}
|
||
//取消到账
|
||
//if (status == 0)
|
||
//{
|
||
// var log = new Wx_SzzyOrderPayUseLog(orderDeposit.resid, orderDeposit.orderid, orderDeposit.id, orderDeposit.paytype, orderDeposit.paytypename, orderDeposit.payno, -orderDeposit.payprice, DateTime.Now, orderDeposit.channel.Value, 4);
|
||
// _payuselog.Add(log);
|
||
//}
|
||
|
||
db.SaveChanges();
|
||
|
||
var ids = orderids.Distinct().Select<int, decimal>(x => Convert.ToDecimal(x));
|
||
if (ids.Any())
|
||
{
|
||
foreach (var orderid in ids)
|
||
{
|
||
//重新获取订单使用的金额数据
|
||
//var orderpayList = db.WX_SzzyOrderPay.Where(p => p.orderid == orderid && p.isdelete == 0 && !orderids.Contains(p.depositid.Value) && !oldorderids.Contains(p.orderid));
|
||
var orderpayList = db.WX_SzzyOrderPay.Where(p => p.orderid == orderid && p.isdelete == 0 && p.auditstatus == 1);
|
||
decimal arrivalPay = 0;
|
||
if (orderpayList != null && orderpayList.Count() > 0)
|
||
arrivalPay = orderpayList.Sum(p => p.payprice);
|
||
|
||
var oid = Convert.ToDecimal(orderid);
|
||
var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == oid);
|
||
if (order != null)
|
||
{
|
||
#region 订单状态判断。
|
||
string[] openStatus = { "220", "80", "205" };
|
||
if (status == 0 && openStatus.Contains(order.ORDERSTATUS))
|
||
{
|
||
return new retMsg { result = false, retcode = 220, retmsg = "订单" + order.ORDERID + "已开通,不能取消使用!" };
|
||
}
|
||
else if (status == 0 && order.ORDERSTATUS == "90")
|
||
{
|
||
return new retMsg { result = false, retcode = 220, retmsg = "订单" + order.ORDERID + "已退款,不能取消使用!" };
|
||
}
|
||
#endregion
|
||
|
||
//如果订单是未开通状态才需要处理订单状态
|
||
if(order.ISOPEN == 0)
|
||
{
|
||
if (arrivalPay >= order.NEEDPAY)
|
||
{
|
||
order.ORDERSTATUS = "200";
|
||
order.ORDERSTATUSNAME = "已支付";
|
||
}
|
||
else if (arrivalPay > 0)
|
||
{
|
||
order.ORDERSTATUS = "190";
|
||
order.ORDERSTATUSNAME = "已提交支付";
|
||
}
|
||
else
|
||
{
|
||
order.ORDERSTATUS = "180";
|
||
order.ORDERSTATUSNAME = "新订单";
|
||
}
|
||
if (arrivalPay > 0)
|
||
{
|
||
var payDate = orderpayList.OrderByDescending(p => p.paydate).First().paydate;
|
||
order.ARRIVALPAY = arrivalPay;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
order.ARRIVALTIME = payDate;
|
||
}
|
||
else
|
||
{
|
||
order.ARRIVALPAY = null;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
order.ARRIVALTIME = null;
|
||
}
|
||
}
|
||
|
||
SendInfoFormat(eventInfo, null, order, null);//将订单信息formrt
|
||
}
|
||
}
|
||
}
|
||
|
||
db.SaveChanges();
|
||
|
||
trans.Commit();
|
||
|
||
//推送订金状态到坐席(弃用,用统一的一个推送)
|
||
//Common.EventBus.EventBus.Instance.Publish(new DepositRejectEvent(orderDeposit.id, orderDeposit.auditstatus, rejectremark, userId, userName, orderDeposit.channel.Value));
|
||
|
||
SendInfoFormat(eventInfo, deposit, null, null);//将订金信息formrt
|
||
Common.EventBus.EventBus.Instance.Publish(eventInfo);//推送订金信息
|
||
|
||
|
||
return new retMsg() { result = true, retcode = 200, retmsg = "success" };
|
||
}
|
||
return new retMsg() { result = false, retcode = 100, retmsg = "para" };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex);
|
||
trans.Rollback();
|
||
return new retMsg { result = false, retcode = 500 };
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 订单列表=》取消到账(订单改成新订单,订金取消使用)
|
||
/// </summary>
|
||
/// <param name="orderId"></param>
|
||
/// <returns></returns>
|
||
public retMsg OrderCancel(int orderId, int userId, string userName)
|
||
{
|
||
using (var client = RedisManager.GetClient())
|
||
{
|
||
var ord = new WX_SZZYORDER_BL().Get(p => p.ORDERID == orderId);
|
||
using (var datalock = client.AcquireLock("DataLock:" + ord.RESID, TimeSpan.FromSeconds(10)))
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
using (var trans = db.Database.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
OrderAndPayInfoEvent eventInfo = new OrderAndPayInfoEvent() { depositList = new List<OrderAndPayInfo_Deposit>(), orderList = new List<OrderAndPayInfo_Order>(), payList = new List<OrderAndPayInfo_Pay>() };
|
||
|
||
var order = db.WX_SZZYORDER.FirstOrDefault(m => m.ORDERID == orderId);
|
||
//如果已经开通过,提示不能操作
|
||
string[] openStatus = { "220", "80", "205" };
|
||
if (openStatus.Contains(order.ORDERSTATUS))
|
||
{
|
||
return new retMsg { result = false, retcode = 220, retmsg = "订单已开通,不能取消使用" };
|
||
}
|
||
else if (order.ORDERSTATUS == "90")
|
||
{
|
||
return new retMsg { result = false, retcode = 220, retmsg = "订单已退款,不能取消使用!" };
|
||
}
|
||
else if (!order.ARRIVALPAY.HasValue || order.ARRIVALPAY.Value == 0)
|
||
{
|
||
return new retMsg { result = false, retcode = 220, retmsg = "本订单没有到账金额,不能执行此操作!" };
|
||
}
|
||
|
||
var Alloldorderpays = db.WX_SzzyOrderPay.Where(p => p.orderid == orderId && p.isdelete == 0 && p.auditstatus == 1);
|
||
foreach (var pay in Alloldorderpays)
|
||
{
|
||
if (pay.depositid.HasValue)//pay表有订金字段,那么需要退定金操作
|
||
{
|
||
var orderDeposit = db.WX_SzzyOrderDeposit.FirstOrDefault(p => p.id == pay.depositid);
|
||
if (orderDeposit != null)
|
||
{
|
||
//已经驳回,不需要在进行处理
|
||
if (orderDeposit.auditstatus == -1)
|
||
{
|
||
return new retMsg() { result = false, retcode = 300, retmsg = "exists" };
|
||
}
|
||
orderDeposit.useprice = orderDeposit.useprice - pay.payprice;
|
||
orderDeposit.lastprice += pay.payprice;//加回之前的金额
|
||
|
||
pay.auditstatus = 0;//pay表需要将数据删除
|
||
pay.isdelete = 1;//pay表需要将数据删除
|
||
|
||
var orderids = new List<int>();
|
||
//需要将使用了的订金金额加回去
|
||
db.SaveChanges();
|
||
SendInfoFormat(eventInfo, orderDeposit, null, null);//将订金信息formrt
|
||
|
||
var sumLastPrice = db.WX_SzzyOrderDeposit.Where(m => m.resid == orderDeposit.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);//获取总的金额
|
||
var log = new Wx_SzzyOrderPayUseLog(orderDeposit.resid, orderId, orderDeposit.id, orderDeposit.paytype, orderDeposit.paytypename, orderDeposit.payno, pay.payprice, DateTime.Now, pay.channel.Value, 3, sumLastPrice, "订单取消使用回退金额");
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
|
||
//推送订金状态到坐席
|
||
//Common.EventBus.EventBus.Instance.Publish(new DepositRejectEvent(orderDeposit.id, orderDeposit.auditstatus, "", userId, userName, orderDeposit.channel.Value));
|
||
|
||
//return new retMsg() { result = true, retcode = 200, retmsg = "success" };
|
||
}
|
||
}
|
||
else
|
||
{//如果找不到订金,那么可能是老订单,需要直接改变支付的状态
|
||
pay.auditstatus = 0;
|
||
pay.isdelete = 1;
|
||
}
|
||
SendInfoFormat(eventInfo, null, null, pay);//将订金信息formrt
|
||
}
|
||
|
||
order.ORDERSTATUS = "180";
|
||
order.ORDERSTATUSNAME = "新订单";
|
||
order.ARRIVALPAY = null;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
order.ARRIVALTIME = null;
|
||
db.SaveChanges();
|
||
trans.Commit();
|
||
|
||
SendInfoFormat(eventInfo, null, order, null);//将订金信息formrt
|
||
Common.EventBus.EventBus.Instance.Publish(eventInfo);//推送订金信息
|
||
//Common.EventBus.EventBus.Instance.Publish(new ReSetOrderEvent(orderId, order.CHANNEL));//推送取消订单
|
||
return new retMsg() { result = true, retcode = 200, retmsg = "success" };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex);
|
||
trans.Rollback();
|
||
return new retMsg { result = false, retcode = 500 };
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 删除到账
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public retMsg Delete(int id)
|
||
{
|
||
using (var client = RedisManager.GetClient())
|
||
{
|
||
var deposit = this.Get(p => p.id == id);
|
||
using (var datalock = client.AcquireLock("DataLock:" + deposit.resid, TimeSpan.FromSeconds(10)))
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
using (var trans = db.Database.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
OrderAndPayInfoEvent eventInfo = new OrderAndPayInfoEvent() { depositList = new List<OrderAndPayInfo_Deposit>(), orderList = new List<OrderAndPayInfo_Order>(), payList = new List<OrderAndPayInfo_Pay>() };
|
||
|
||
var orderDeposit = db.WX_SzzyOrderDeposit.FirstOrDefault(p => p.id == id);
|
||
if (orderDeposit != null)
|
||
{
|
||
if (orderDeposit.auditstatus != 1)
|
||
{
|
||
return new retMsg() { result = false, retcode = 500, retmsg = "notauditcantcacel" };
|
||
}
|
||
|
||
var mmmm = (orderDeposit.lastprice ?? 0) + (orderDeposit.useprice ?? 0) + (orderDeposit.frozenprice ?? 0);
|
||
|
||
if (orderDeposit.payprice > mmmm)
|
||
{
|
||
return new retMsg() { result = false, retcode = 500, retmsg = "hasrefun" };
|
||
}
|
||
//if (orderDeposit.isuse == 0)
|
||
//{
|
||
var sumLastPrice = db.WX_SzzyOrderDeposit.Where(m => m.resid == orderDeposit.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);//获取总的金额
|
||
var lastpriceLJ = sumLastPrice;//当前的总金额
|
||
|
||
//var lastpriceLJ = orderDeposit.lastprice;
|
||
orderDeposit.auditstatus = 0;
|
||
orderDeposit.isdelete = 1;
|
||
orderDeposit.lastprice = orderDeposit.payprice;//将金额返回
|
||
orderDeposit.useprice = 0;
|
||
var orderids = new List<int>();
|
||
//驳回的时候,所有使用的金额都要取消使用
|
||
var orderpays = db.WX_SzzyOrderPay.Where(p => p.depositid == orderDeposit.id && p.isdelete == 0 && p.auditstatus == 1);
|
||
if (orderpays != null && orderpays.Any())
|
||
{
|
||
foreach (var item in orderpays)
|
||
{
|
||
item.auditstatus = 0;
|
||
item.isdelete = 1;
|
||
|
||
orderids.Add(item.orderid);
|
||
lastpriceLJ += item.payprice;//加回金额
|
||
var log = new Wx_SzzyOrderPayUseLog(orderDeposit.resid, item.orderid, item.depositid ?? 0, item.paytype, item.paytypename, item.payno, item.payprice, item.audittime ?? DateTime.Now, item.channel.Value, 3, lastpriceLJ, "取消到账回退金额");
|
||
//_payuselog.Add(log);
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
|
||
SendInfoFormat(eventInfo, null, null, item);//将订金信息formrt
|
||
}
|
||
}
|
||
//驳回的时候,所有使用的金额都要取消使用
|
||
var oldorderpays = db.WX_SzzyOrderPay.Where(p => p.orderid == orderDeposit.id && p.isdelete == 0 && p.auditstatus == 1);
|
||
//var oldorderids = new List<int>();
|
||
if (oldorderpays != null && oldorderpays.Any())
|
||
{
|
||
foreach (var item in oldorderpays)
|
||
{
|
||
item.auditstatus = 0;
|
||
item.isdelete = 1;
|
||
if (!item.depositid.HasValue)
|
||
{
|
||
//旧数据如果订金ID为空,更新数据
|
||
item.depositid = id;
|
||
}
|
||
|
||
orderids.Add(item.orderid);
|
||
//oldorderids.Add(item.orderid);
|
||
lastpriceLJ += item.payprice;//加回金额
|
||
var log = new Wx_SzzyOrderPayUseLog(orderDeposit.resid, item.orderid, item.depositid ?? 0, item.paytype, item.paytypename, item.payno, item.payprice, item.audittime ?? DateTime.Now, item.channel.Value, 3, lastpriceLJ, "取消到账回退金额");
|
||
//_payuselog.Add(log);
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
SendInfoFormat(eventInfo, null, null, item);//将订金信息formrt
|
||
}
|
||
}
|
||
db.SaveChanges();
|
||
|
||
|
||
var NowsumLastPrice = db.WX_SzzyOrderDeposit.Where(m => m.resid == orderDeposit.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);//获取总的金额
|
||
var log2 = new Wx_SzzyOrderPayUseLog(orderDeposit.resid, orderDeposit.orderid, orderDeposit.id, orderDeposit.paytype, orderDeposit.paytypename, orderDeposit.payno, -orderDeposit.payprice.Value, DateTime.Now, orderDeposit.channel.Value, 4, NowsumLastPrice, "取消到账删除金额");
|
||
//_payuselog.Add(log2);
|
||
db.Wx_SzzyOrderPayUseLog.Add(log2);
|
||
db.SaveChanges();
|
||
|
||
var ids = orderids.Distinct().Select<int, decimal>(x => Convert.ToDecimal(x));
|
||
if (ids.Any())
|
||
{
|
||
foreach (var orderid in ids)
|
||
{
|
||
//重新获取订单使用的金额数据
|
||
//var orderpayList = db.WX_SzzyOrderPay.Where(p => p.orderid == orderid && p.isdelete == 0 && !orderids.Contains(p.depositid.Value) && !oldorderids.Contains(p.orderid));
|
||
var orderpayList = db.WX_SzzyOrderPay.Where(p => p.orderid == orderid && p.isdelete == 0 && p.auditstatus == 1);
|
||
decimal arrivalPay = 0;
|
||
if (orderpayList != null && orderpayList.Count() > 0)
|
||
arrivalPay = orderpayList.Sum(p => p.payprice);
|
||
|
||
|
||
var oid = Convert.ToDecimal(orderid);
|
||
var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == oid);
|
||
if (order != null)
|
||
{
|
||
#region 订单状态判断。
|
||
string[] openStatus = { "220", "80", "205" };
|
||
if (openStatus.Contains(order.ORDERSTATUS))
|
||
{
|
||
return new retMsg { result = false, retcode = 220, retmsg = "订单" + order.ORDERID + "已开通,不能取消到账" };
|
||
}
|
||
else if (order.ORDERSTATUS == "90")
|
||
{
|
||
return new retMsg { result = false, retcode = 220, retmsg = "订单" + order.ORDERID + "已退款,不能取消到账!" };
|
||
}
|
||
#endregion
|
||
|
||
if (arrivalPay >= order.NEEDPAY)
|
||
{
|
||
order.ORDERSTATUS = "200";
|
||
order.ORDERSTATUSNAME = "已支付";
|
||
}
|
||
else if (arrivalPay > 0)
|
||
{
|
||
order.ORDERSTATUS = "190";
|
||
order.ORDERSTATUSNAME = "已提交支付";
|
||
}
|
||
else
|
||
{
|
||
order.ORDERSTATUS = "180";
|
||
order.ORDERSTATUSNAME = "新订单";
|
||
}
|
||
if (arrivalPay > 0)
|
||
{
|
||
var payDate = orderpayList.OrderByDescending(p => p.paydate).First().paydate;
|
||
order.ARRIVALPAY = arrivalPay;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
order.ARRIVALTIME = payDate;
|
||
}
|
||
else
|
||
{
|
||
order.ARRIVALPAY = null;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
order.ARRIVALTIME = null;
|
||
}
|
||
SendInfoFormat(eventInfo, null, order, null);//将订金信息formrt
|
||
}
|
||
}
|
||
}
|
||
db.SaveChanges();
|
||
trans.Commit();
|
||
|
||
SendInfoFormat(eventInfo, orderDeposit, null, null);//将订金信息formrt
|
||
Common.EventBus.EventBus.Instance.Publish(eventInfo);//推送订金信息
|
||
//Common.EventBus.EventBus.Instance.Publish(new DepositDeleteEvent(orderDeposit.id, 0, 1, orderDeposit.channel.Value));//废弃,采用统一的同步方式同步
|
||
return new retMsg() { result = true, retcode = 200, retmsg = "success" };
|
||
//}
|
||
//return new retMsg() { result = false, retcode = 300, retmsg = "isuse" };
|
||
}
|
||
return new retMsg() { result = false, retcode = 100, retmsg = "notexists" };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex);
|
||
trans.Rollback();
|
||
return new retMsg { result = false, retcode = 500 };
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
public class OrderDepositView {
|
||
public int? depositid { get; set; }
|
||
public string umid { get; set; }
|
||
public string deptname { get; set; }
|
||
public decimal orderid { get; set; }
|
||
public decimal? payprice { get; set; }
|
||
public decimal? needpay { get; set; }
|
||
public decimal? arrivalpay { get; set; }
|
||
public string payname { get; set; }
|
||
public string paytypename { get; set; }
|
||
public string payno { get; set; }
|
||
public string remark { get; set; }
|
||
public DateTime? paydate { get; set; }
|
||
}
|
||
}
|