913 lines
53 KiB
C#
913 lines
53 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.BLL.Util;
|
||
using CRM.Core.BLL.Wx;
|
||
using CRM.Core.DAL;
|
||
using CRM.Core.DTO;
|
||
using CRM.Core.DTO.Ord;
|
||
using CRM.Core.Model.Entity;
|
||
using CRM.Core.Model.Enum;
|
||
using CRM.Core.Model.Map;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
using System.Threading.Tasks;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.Common.WxPay;
|
||
|
||
namespace CRM.Core.BLL.Application.Pay
|
||
{
|
||
public class PayServices
|
||
{
|
||
private readonly WX_SzzyOrderDeposit_BL _orderDeposit = new WX_SzzyOrderDeposit_BL();
|
||
private readonly WX_SzzyOrderPay_BL _orderPay = new WX_SzzyOrderPay_BL();
|
||
private readonly CACHE_BL cache_BL = new CACHE_BL();
|
||
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();
|
||
public retMsg SetPayName(int id, string payName)
|
||
{
|
||
try
|
||
{
|
||
//var payInfo = _orderPay.Get(p => p.id == id);
|
||
|
||
//if (payInfo == null)
|
||
//{
|
||
// return new retMsg { result = false, retcode = -1 };
|
||
//}
|
||
|
||
//payInfo.payname = payName;
|
||
//_orderPay.Update(payInfo);
|
||
|
||
var orderDeposit = _orderDeposit.Get(p => p.id == id);
|
||
|
||
if (orderDeposit == null)
|
||
{
|
||
return new retMsg { result = false, retcode = -1 };
|
||
}
|
||
|
||
orderDeposit.payname = payName;
|
||
_orderDeposit.Update(orderDeposit);
|
||
|
||
Common.EventBus.EventBus.Instance.Publish(new SetPayNameEvent(orderDeposit.id, orderDeposit.payno, payName, orderDeposit.channel.Value));
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(e);
|
||
return new retMsg { result = false };
|
||
}
|
||
return new retMsg { result = true };
|
||
}
|
||
|
||
//public retMsg Add(CRM.Core.DTO.Ord.PayDto dto)
|
||
//{
|
||
// var payTypeName = cache_BL.GetList_Parameter(ParameterGroup.OrderPayType).FirstOrDefault(p => p.PARAVALUE == dto.paytype.ToString()).PARANAME;
|
||
|
||
// int? channel = 0;
|
||
// decimal usepriceall = 0;
|
||
// decimal lastpriceall = 0;
|
||
// var msg1 = new retMsg();
|
||
// //retMsg msg2 = null;
|
||
|
||
// if (dto.depprice.HasValue && dto.depprice.Value > 0)
|
||
// {
|
||
// //如果有使用已经确认的金额,直接使用
|
||
// msg1 = Use(new DTO.Ord.PayUseDto() { resid = dto.resid, depprice = dto.depprice.Value, orderuselist = dto.orderuselist }, 0);
|
||
// }
|
||
// if (!string.IsNullOrEmpty(dto.payno))
|
||
// {
|
||
// //使用了流水号的场景
|
||
// using (var db = new zxdContext())
|
||
// {
|
||
// using (var trans = db.Database.BeginTransaction())
|
||
// {
|
||
// try
|
||
// {
|
||
// //查看是否已经有确认到账记录
|
||
// var depost = db.WX_SzzyOrderDeposit.FirstOrDefault(p => p.payno == dto.payno && p.auditstatus == 1);
|
||
// var depositId = 0;
|
||
// if (depost == null)
|
||
// {
|
||
// depositId = new SEQUENCES_BL().Seq_OrderDeposit();
|
||
|
||
// lastpriceall = dto.payprice;
|
||
// }
|
||
// else
|
||
// {
|
||
// depositId = depost.id;
|
||
// //如果已经存在说明已经使用过了,那要重新计算使用金额跟剩余金额
|
||
// usepriceall = depost.useprice ?? 0;
|
||
// lastpriceall = depost.lastprice ?? 0;
|
||
// }
|
||
|
||
// foreach (var item in dto.orderuselist)
|
||
// {
|
||
// if (lastpriceall <= 0)
|
||
// {
|
||
// //如果剩余金额小于等于0,说明已经没钱支付了,不执行下面的操作
|
||
// continue;
|
||
// }
|
||
|
||
// var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == item.orderid);
|
||
// if (order == null)
|
||
// throw new Exception("参数错误,未能找到订单!");
|
||
// else
|
||
// {
|
||
// //如果已经支付过,赋值到当前临时记录的到账金额
|
||
// if (order.ARRIVALPAY.HasValue)
|
||
// item.arrivalpay = order.ARRIVALPAY.Value;
|
||
// if (order.ORDERSTATUS == "180" || order.ORDERSTATUS == "190" || order.ORDERSTATUS == "195")
|
||
// {
|
||
// order.ORDERSTATUS = "190";
|
||
// order.ORDERSTATUSNAME = "已提交支付";
|
||
|
||
// channel = order.CHANNEL;
|
||
// }
|
||
// else
|
||
// {
|
||
// //已经是支付状态,说明已经给够钱了,不需要再支付了
|
||
// LogHelper.Info(order.ORDERID.ToString() + "支付金额已经够了,不需要再支付了!");
|
||
// continue;
|
||
// //return new retMsg { result = false, retcode = 100, retmsg = order.ORDERID.ToString() + "支付金额已经够了,不需要再支付了!" };
|
||
// }
|
||
// }
|
||
|
||
// //如果订单金额跟到账金额相等,说明这个订单已经付够钱了
|
||
// if (item.arrivalpay == item.needpay)
|
||
// {
|
||
// continue;
|
||
// }
|
||
|
||
// //订单剩余使用金额=订单金额-已支付金额
|
||
// //如果第一笔支付,那已支付金额为0
|
||
// //当前需要付款的金额
|
||
// var curPay = order.NEEDPAY.Value - (order.ARRIVALPAY ?? 0);
|
||
|
||
// decimal usePrice = 0;
|
||
|
||
// if (lastpriceall >= curPay)
|
||
// {
|
||
// //剩余金额大于等于当前需要付款的金额,剩余金额足够支付
|
||
// usePrice = curPay;
|
||
// }
|
||
// else
|
||
// {
|
||
// //剩余金额不够支付,剩余金额全部支付
|
||
// usePrice = lastpriceall;
|
||
// }
|
||
|
||
// //使用金额加上当前支付的金额
|
||
// usepriceall += usePrice;
|
||
// //剩余金额降掉当前支付的金额
|
||
// lastpriceall -= usePrice;
|
||
|
||
// var pay = new WX_SzzyOrderPay(item.orderid, item.needpay, dto.paytype, payTypeName, dto.paydate, usePrice, 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 = depositId;
|
||
|
||
// db.WX_SzzyOrderPay.Add(pay);
|
||
|
||
// item.arrivalpay += usePrice;
|
||
// }
|
||
// if (depost == null)
|
||
// {
|
||
// depost = new WX_SzzyOrderDeposit(dto.resid, dto.paytype, payTypeName, dto.paydate, dto.payprice, dto.payname, dto.remark, dto.payno, dto.isuse, dto.companycode, channel, dto.tradeno)
|
||
// {
|
||
// id = depositId
|
||
// };
|
||
// if (dto.creator.HasValue)
|
||
// {
|
||
// depost.creator = dto.creator.Value;
|
||
// }
|
||
// if (!string.IsNullOrEmpty(dto.creatorname))
|
||
// {
|
||
// depost.creatorname = dto.creatorname;
|
||
// }
|
||
|
||
// depost.useprice = usepriceall;
|
||
// depost.lastprice = lastpriceall;
|
||
|
||
// db.WX_SzzyOrderDeposit.Add(depost);
|
||
// }
|
||
// else
|
||
// {
|
||
// depost.useprice = usepriceall;
|
||
// depost.lastprice = lastpriceall;
|
||
// }
|
||
|
||
// db.SaveChanges();
|
||
|
||
// trans.Commit();
|
||
|
||
// #region 发送通知
|
||
// try
|
||
// {
|
||
// var comcode = _company.GetInfoByChannel(channel.Value);
|
||
// _msg.PushNewsMsg("DJTZ", $"到款【{depositId}】已经【提交】,请您审核!", comcode == null ? "事业部" : comcode.CompanyName, depositId.ToString(), comcode == null ? depost.companycode : comcode.CompanyCode);
|
||
// }
|
||
// catch (Exception e)
|
||
// {
|
||
// LogHelper.Error("发送通知出错:" + e.ToString());
|
||
// }
|
||
// #endregion
|
||
// if (dto.depprice.HasValue && dto.depprice.Value > 0)
|
||
// {
|
||
// if (msg1.result)
|
||
// {
|
||
// return new retMsg { result = true, retcode = 200, retmsg = depositId.ToString() };
|
||
// }
|
||
// else if (!msg1.result)
|
||
// {
|
||
// return new retMsg { result = false, retcode = 300, retmsg = depositId.ToString() };
|
||
// }
|
||
// }
|
||
// return new retMsg { result = true, retcode = 200, retmsg = depositId.ToString() };
|
||
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// LogHelper.Error(ex);
|
||
// trans.Rollback();
|
||
// if (dto.depprice.HasValue && dto.depprice.Value > 0 && msg1.result)
|
||
// {
|
||
// return new retMsg { result = false, retcode = 400 };
|
||
// }
|
||
// return new retMsg { result = false, retcode = 500 };
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// return msg1;
|
||
// }
|
||
//}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <param name="type">1:普通订金 2:自动确认的订金</param>
|
||
/// <returns></returns>
|
||
public retMsg<List<DepositInfoDto>> Add(CRM.Core.DTO.Ord.PayDto2 dto, int type = 1)
|
||
{
|
||
List<WX_SzzyOrderDeposit> needCheckDeposit = new List<WX_SzzyOrderDeposit>();//需要进行自动对账的程序
|
||
var depositInfoList = new List<DepositInfoDto>();
|
||
using (var client = RedisManager.GetClient())
|
||
{
|
||
using (var datalock = client.AcquireLock("DataLock:" + dto.resid, TimeSpan.FromSeconds(10)))
|
||
{
|
||
int? channel = 0;
|
||
if (dto.channel.HasValue)
|
||
{
|
||
channel = dto.channel;//渠道
|
||
}
|
||
//var msg1 = new retMsg<List<DepositInfoDto>>();
|
||
//if (dto.depprice.HasValue && dto.depprice.Value > 0)
|
||
//{
|
||
// //如果有使用已经确认的金额,直接使用
|
||
// msg1 = Use(new DTO.Ord.PayUseDto() { resid = dto.resid, depprice = dto.depprice.Value, orderuselist = dto.orderuselist }, 0);
|
||
//}
|
||
LogHelper.Info("PayService.Add:" + dto.ToJson());
|
||
if (dto.paylist.Any())
|
||
{
|
||
|
||
using (var db = new zxdContext())
|
||
{
|
||
|
||
using (var trans = db.Database.BeginTransaction())
|
||
{
|
||
OrderAndPayInfoEvent eventInfo = new OrderAndPayInfoEvent();
|
||
try
|
||
{
|
||
|
||
|
||
foreach (var payinfo in dto.paylist)//循环提交上来的支付流水
|
||
{
|
||
if (!string.IsNullOrEmpty(payinfo.payno))
|
||
{
|
||
payinfo.payno = payinfo.payno.Trim();//数据payno
|
||
}
|
||
var payTypeName = cache_BL.GetList_Parameter(ParameterGroup.OrderPayType).FirstOrDefault(p => p.PARAVALUE == payinfo.paytype.ToString()).PARANAME;
|
||
decimal usepriceall = 0;
|
||
decimal lastpriceall = 0;
|
||
//查看是否已经有确认到账记录
|
||
//var depost = db.WX_SzzyOrderDeposit.FirstOrDefault(p => p.payno == payinfo.payno && p.auditstatus == 1);
|
||
//var depositId = 0;
|
||
//if (depost == null)
|
||
//{
|
||
var depositId = new SEQUENCES_BL().Seq_OrderDeposit();//订金ID
|
||
lastpriceall = payinfo.payprice;
|
||
//}
|
||
//else
|
||
//{
|
||
//depositId = depost.id;
|
||
////如果已经存在说明已经使用过了,那要重新计算使用金额跟剩余金额
|
||
//usepriceall = depost.useprice ?? 0;
|
||
//lastpriceall = depost.lastprice ?? 0;
|
||
|
||
//var model = new WX_SzzyOrderDeposit(dto.resid, payinfo.paytype, payTypeName, payinfo.paydate, payinfo.payprice, payinfo.payname, payinfo.remark, payinfo.payno, 0, payinfo.companycode, channel, payinfo.tradeno)
|
||
//{
|
||
// id = new SEQUENCES_BL().Seq_OrderDeposit(),
|
||
// checkreslut = "该笔流水号已被确认过,不可重复提交",
|
||
// checkpaytime = payinfo.paydate,
|
||
// auditstatus = -1,
|
||
// auditor = -1,
|
||
// auditorname = "系统",
|
||
// audittime = DateTime.Now,
|
||
// rejectremark = "该笔流水号已被确认过,不可重复提交"
|
||
//};
|
||
//db.WX_SzzyOrderDeposit.Add(model);//订金添加数据
|
||
//WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, model, null, null);//将pay信息formrt
|
||
//continue;
|
||
//}
|
||
|
||
foreach (var item in dto.orderuselist)
|
||
{
|
||
if (lastpriceall <= 0)
|
||
{
|
||
//如果剩余金额小于等于0,说明已经没钱支付了,不执行下面的操作
|
||
continue;
|
||
}
|
||
|
||
var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == item.orderid);
|
||
if (order == null)
|
||
throw new Exception("参数错误,未能找到订单!");
|
||
else
|
||
{
|
||
//如果已经支付过,赋值到当前临时记录的到账金额
|
||
if (order.ARRIVALPAY.HasValue)
|
||
item.arrivalpay = order.ARRIVALPAY.Value;
|
||
if (order.ORDERSTATUS == "180" || order.ORDERSTATUS == "190" || order.ORDERSTATUS == "195")
|
||
{
|
||
order.ORDERSTATUS = "190";
|
||
order.ORDERSTATUSNAME = "已提交支付";
|
||
|
||
channel = order.CHANNEL;
|
||
}
|
||
else
|
||
{
|
||
//已经是支付状态,说明已经给够钱了,不需要再支付了
|
||
LogHelper.Info(order.ORDERID.ToString() + "支付金额已经够了,不需要再支付了!");
|
||
continue;
|
||
//return new retMsg { result = false, retcode = 100, retmsg = order.ORDERID.ToString() + "支付金额已经够了,不需要再支付了!" };
|
||
}
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, null, order, null);//将pay信息formrt
|
||
}
|
||
|
||
//如果订单金额跟到账金额相等,说明这个订单已经付够钱了
|
||
if (item.arrivalpay == item.needpay)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
//订单剩余使用金额=订单金额-已支付金额
|
||
//如果第一笔支付,那已支付金额为0
|
||
//当前需要付款的金额
|
||
var curPay = order.NEEDPAY.Value - (order.ARRIVALPAY ?? 0);
|
||
|
||
decimal usePrice = 0;
|
||
|
||
if (lastpriceall >= curPay)
|
||
{
|
||
//剩余金额大于等于当前需要付款的金额,剩余金额足够支付
|
||
usePrice = curPay;
|
||
}
|
||
else
|
||
{
|
||
//剩余金额不够支付,剩余金额全部支付
|
||
usePrice = lastpriceall;
|
||
}
|
||
|
||
//if (depost != null)
|
||
//{
|
||
// //使用金额加上当前支付的金额
|
||
// usepriceall += usePrice;
|
||
// //剩余金额降掉当前支付的金额
|
||
// lastpriceall -= usePrice;
|
||
//}
|
||
|
||
var pay = new WX_SzzyOrderPay(item.orderid, item.needpay, payinfo.paytype, payTypeName, payinfo.paydate, usePrice, payinfo.payname, payinfo.remark, payinfo.payno, payinfo.companycode, channel, payinfo.tradeno);
|
||
if (payinfo.creator.HasValue)
|
||
{
|
||
pay.creator = payinfo.creator;
|
||
}
|
||
if (!string.IsNullOrEmpty(payinfo.creatorname))
|
||
{
|
||
pay.creatorname = payinfo.creatorname;
|
||
}
|
||
pay.deptcode = dto.deptcode;
|
||
|
||
pay.depositid = depositId;
|
||
|
||
db.WX_SzzyOrderPay.Add(pay);
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, null, null, pay);//将pay信息formrt
|
||
item.arrivalpay += usePrice;
|
||
}
|
||
|
||
//if (depost == null)
|
||
//{
|
||
var depost = new WX_SzzyOrderDeposit(payinfo.resid, payinfo.paytype, payTypeName, payinfo.paydate, payinfo.payprice, payinfo.payname, payinfo.remark, payinfo.payno, payinfo.isuse, payinfo.companycode, channel, payinfo.tradeno)
|
||
{
|
||
id = depositId
|
||
};
|
||
if (payinfo.creator.HasValue)
|
||
{
|
||
depost.creator = payinfo.creator;
|
||
}
|
||
if (!string.IsNullOrEmpty(payinfo.creatorname))
|
||
{
|
||
depost.creatorname = payinfo.creatorname;
|
||
}
|
||
depost.deptcode = dto.deptcode;
|
||
|
||
depost.useprice = usepriceall;
|
||
depost.lastprice = lastpriceall;
|
||
depost.isdelete = 0;
|
||
if (type == 1)
|
||
{
|
||
depost.checkpaytime = depost.paydate;//默认时间一样
|
||
}
|
||
else if (type == 2)
|
||
{
|
||
depost.checkpaytime = payinfo.checkpaytime;
|
||
}
|
||
db.WX_SzzyOrderDeposit.Add(depost);
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, depost, null, null);//将pay信息formrt
|
||
needCheckDeposit.Add(depost);//新增的流水需要进行自动审核这一关
|
||
//}
|
||
//else
|
||
//{
|
||
// depost.useprice = usepriceall;
|
||
// depost.lastprice = lastpriceall;
|
||
//}
|
||
depositInfoList.Add(new DepositInfoDto() { id = depost.id, payno = depost.payno, companycode = depost.companycode });
|
||
}
|
||
db.SaveChanges();
|
||
trans.Commit();
|
||
|
||
if (eventInfo.channel == 0)
|
||
{
|
||
eventInfo.channel = channel.Value;
|
||
}
|
||
|
||
Common.EventBus.EventBus.Instance.Publish(eventInfo);
|
||
#region 发送通知
|
||
try
|
||
{
|
||
var comcode = _company.GetInfoByChannel(channel.Value);
|
||
foreach (var item in depositInfoList)
|
||
{
|
||
_msg.PushNewsMsg("DJTZ", $"到款【{item.id.ToString()}】已经【提交】,请您审核!", comcode == null ? "事业部" : comcode.CompanyName, item.id.ToString(), comcode == null ? item.companycode : comcode.CompanyCode);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error("发送通知出错:" + e.ToString());
|
||
}
|
||
#endregion
|
||
//if (dto.depprice.HasValue && dto.depprice.Value > 0)
|
||
//{
|
||
// if (msg1.result)
|
||
// {
|
||
// return new retMsg<List<DepositInfoDto>> { result = true, retcode = 200, retmsg = depositInfoList };
|
||
// }
|
||
// else if (!msg1.result)
|
||
// {
|
||
// return new retMsg<List<DepositInfoDto>> { result = false, retcode = 300, retmsg = depositInfoList };
|
||
// }
|
||
//}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex);
|
||
trans.Rollback();
|
||
//if (dto.depprice.HasValue && dto.depprice.Value > 0 && msg1.result)
|
||
//{
|
||
// return new retMsg<List<DepositInfoDto>> { result = false, retcode = 400, retmsg = null };
|
||
//}
|
||
return new retMsg<List<DepositInfoDto>> { result = false, retcode = 500, retmsg = null };
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
return new retMsg<List<DepositInfoDto>> { result = false, retcode = 100, retmsg = null };
|
||
}
|
||
}
|
||
}
|
||
if (type == 1)//手工提交的订金,需要去自动审核
|
||
{
|
||
foreach (var item in needCheckDeposit)
|
||
{
|
||
|
||
if (!(item.paytype == 1 || item.paytype == 5) || string.IsNullOrEmpty(item.payno))//非微信和支付和空的流水不需要去自动验证到账
|
||
{
|
||
continue;
|
||
}
|
||
DateTime? PayTime = item.paydate;
|
||
bool CheckStatus = false;//对账状态
|
||
string CheckMsg = string.Empty;//对账结果描述
|
||
bool isReject = false;//是否需要驳回
|
||
var orderPayNo = _orderPay.Get(p => p.payno == item.payno && p.auditstatus == 1);
|
||
var orderd = _orderDeposit.Get(p => p.payno == item.payno && p.auditstatus == 1);
|
||
|
||
if (orderPayNo != null || orderd != null)//只要从一个地方找到了流水,就直接驳回了
|
||
{
|
||
CheckStatus = false;
|
||
CheckMsg = "该笔流水号已被确认过,不可重复提交";
|
||
}
|
||
else
|
||
{
|
||
CheckPayApi api = new CheckPayApi(
|
||
Utility.GetSettingByKey("checkPayApiKey"),
|
||
Utility.GetSettingByKey("checkPayApiSecret"),
|
||
Utility.GetSettingByKey("checkPayApiIdentityKey"));
|
||
Dictionary<string, object> param = new Dictionary<string, object>();
|
||
param.Add("channel", item.channel);
|
||
//param.Add("outTradeNo", );
|
||
//请求
|
||
ApiResult<PayStatus> resapi = new ApiResult<PayStatus>();
|
||
if (item.paytype == 1)
|
||
{
|
||
param.Add("tradeNo", item.payno);
|
||
resapi = api.AliPayStatus(param);
|
||
}
|
||
else
|
||
{
|
||
|
||
param.Add("transactionId", item.payno);
|
||
resapi = api.WechatPayStatus(param);
|
||
}
|
||
|
||
if (resapi.code == "200")
|
||
{
|
||
if (resapi.data.totalAmount != (item.payprice * 100))
|
||
{
|
||
CheckStatus = false;
|
||
CheckMsg = "提交的支付金额错误";
|
||
isReject = true;
|
||
}
|
||
else if (resapi.data.tradeStatus == 1)//验证通过
|
||
{
|
||
CheckStatus = true;
|
||
CheckMsg = "通过";
|
||
item.paydate = resapi.data.payTime.Value.Date;//更新支付时间
|
||
item.checkpaytime = resapi.data.payTime.Value;//更新支付时间
|
||
}
|
||
else
|
||
{
|
||
CheckStatus = false;
|
||
CheckMsg = "支付流水号正确,但状态异常";
|
||
isReject = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//验证失败
|
||
CheckStatus = false;
|
||
CheckMsg = "支付流水未查到或错误";
|
||
}
|
||
}
|
||
|
||
item.checkreslut = CheckMsg;
|
||
_orderDeposit.Update(item);
|
||
if (CheckStatus)//如果是对账成功,自动进行审核
|
||
{
|
||
//自动审核
|
||
var res = _orderDeposit.Audit(item, 0, "系统");
|
||
}
|
||
if (isReject)
|
||
{
|
||
//订金驳回审核
|
||
_orderDeposit.Reject(item.id, -1, CheckMsg, 0, "系统");
|
||
}
|
||
}
|
||
}
|
||
return new retMsg<List<DepositInfoDto>> { result = true, retcode = 200, retmsg = depositInfoList };
|
||
}
|
||
|
||
public retMsg<List<DepositInfoDto>> Use(CRM.Core.DTO.Ord.PayUseDto dto, int usetype = 2)
|
||
{
|
||
int? channel = 0;
|
||
//decimal usepriceall = 0;
|
||
//decimal lastpriceall = 0;
|
||
using (var client = RedisManager.GetClient())
|
||
{
|
||
using (var datalock = client.AcquireLock("DataLock:" + dto.resid, TimeSpan.FromSeconds(10)))
|
||
{
|
||
|
||
//CompanyVirlual channelInfo = _company.GetChannel(dto.channel);
|
||
List<CompanyVirlual> channelInfoList = _company.GetChannelList(dto.channel);
|
||
using (var db = new zxdContext())
|
||
{
|
||
using (var trans = db.Database.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
OrderAndPayInfoEvent eventInfo = new OrderAndPayInfoEvent();
|
||
//找到客户可用的流水列表
|
||
var predicates = new List<Expression<Func<WX_SzzyOrderDeposit, bool>>>();
|
||
foreach (var channelInfo in channelInfoList)//获取数据列表
|
||
{
|
||
foreach (var item in channelInfo.channels)
|
||
{
|
||
predicates.Add(m => m.channel >= item.minChannel && m.channel <= item.maxChannel);
|
||
}
|
||
}
|
||
//paylist = paylist.WhereOR(predicates.ToArray());
|
||
var deposits = db.WX_SzzyOrderDeposit.Where(p => p.auditstatus == 1 && p.resid == dto.resid && p.lastprice > 0).WhereOR(predicates.ToArray()).ToList();//加了渠道过滤
|
||
|
||
if (!deposits.Any())
|
||
throw new Exception("找不到可用的流水!");
|
||
var ddeppriceLJ = dto.depprice;//需要使用的金额
|
||
foreach (var item in dto.orderuselist)
|
||
{
|
||
var order = db.WX_SZZYORDER.FirstOrDefault(p => p.ORDERID == item.orderid);
|
||
if (order == null)
|
||
throw new Exception("参数错误,未能找到订单!");
|
||
else
|
||
{
|
||
if (order.ORDERSTATUS == "180" || order.ORDERSTATUS == "190" || order.ORDERSTATUS == "195")
|
||
{
|
||
//订单剩余使用金额=订单金额-已支付金额
|
||
//如果第一笔支付,那已支付金额为0
|
||
//当前需要付款的金额
|
||
var curPay = order.NEEDPAY.Value - (order.ARRIVALPAY ?? 0);
|
||
|
||
if (ddeppriceLJ >= curPay)
|
||
{
|
||
order.ORDERSTATUS = "200";
|
||
order.ORDERSTATUSNAME = "已支付";
|
||
}
|
||
else
|
||
{
|
||
curPay = ddeppriceLJ.Value;//剩余的使用金额还没有需要支付的多,那么需要取使用的钱来计算
|
||
order.ORDERSTATUS = "190";
|
||
order.ORDERSTATUSNAME = "已提交支付";
|
||
}
|
||
|
||
channel = order.CHANNEL;
|
||
|
||
|
||
|
||
var xdDeposits = deposits.Where(p => p.lastprice == curPay);
|
||
if (xdDeposits.Any())
|
||
{
|
||
//找到刚好支付的金额,就使用该流水号,多个取第一个
|
||
var xx = xdDeposits.OrderBy(p => p.ctime).First();
|
||
var xdDeposit = db.WX_SzzyOrderDeposit.FirstOrDefault(m => m.id == xx.id);
|
||
|
||
var pay = new WX_SzzyOrderPay(item.orderid, item.needpay, xdDeposit.paytype, xdDeposit.paytypename, xdDeposit.paydate, curPay, xdDeposit.payname, xdDeposit.remark, xdDeposit.payno, xdDeposit.companycode, channel, xdDeposit.tradeno)
|
||
{
|
||
creator = xdDeposit.creator,
|
||
creatorname = xdDeposit.creatorname,
|
||
depositid = xdDeposit.id
|
||
};
|
||
//已经是确认过的到账,使用直接就是通过状态
|
||
pay.auditstatus = 1;
|
||
pay.auditor = xdDeposit.auditor;
|
||
pay.auditorname = xdDeposit.auditorname;
|
||
pay.audittime = DateTime.Now;
|
||
pay.deptcode = xdDeposit.deptcode;
|
||
|
||
//_orderPay.Add(pay);
|
||
db.WX_SzzyOrderPay.Add(pay);
|
||
if (xdDeposit.useprice.HasValue)
|
||
xdDeposit.useprice += curPay;
|
||
else
|
||
xdDeposit.useprice = curPay;
|
||
xdDeposit.lastprice -= curPay;
|
||
|
||
if (order.ARRIVALPAY.HasValue)
|
||
{
|
||
order.ARRIVALPAY += curPay;
|
||
}
|
||
else
|
||
{
|
||
order.ARRIVALPAY = curPay;
|
||
}
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
order.ARRIVALTIME = xdDeposit.paydate;
|
||
|
||
db.SaveChanges();
|
||
var sumLastPrice = db.WX_SzzyOrderDeposit.Where(m => m.resid == dto.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);
|
||
var log = new Wx_SzzyOrderPayUseLog(xdDeposit.resid, item.orderid, pay.depositid ?? 0, pay.paytype, pay.paytypename, pay.payno, -pay.payprice, pay.audittime ?? DateTime.Now, pay.channel.Value, usetype, sumLastPrice, "金额使用");
|
||
//_payuselog.Add(log);
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
db.SaveChanges();
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, null, null, pay);//将pay信息formrt
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, xdDeposit, null, null);//将pay信息formrt
|
||
ddeppriceLJ -= curPay;
|
||
}
|
||
else
|
||
{
|
||
//没找到刚好够支付的金额,并且剩余金额大于需要付款金额,使用第一个能用的流水号
|
||
var dyDeposits = deposits.Where(p => p.lastprice > curPay);
|
||
|
||
if (dyDeposits.Any())
|
||
{
|
||
var xx = dyDeposits.OrderBy(p => p.ctime).First();
|
||
var dyDeposit = db.WX_SzzyOrderDeposit.FirstOrDefault(m => m.id == xx.id);
|
||
|
||
var pay = new WX_SzzyOrderPay(item.orderid, item.needpay, dyDeposit.paytype, dyDeposit.paytypename, dyDeposit.paydate, curPay, dyDeposit.payname, dyDeposit.remark, dyDeposit.payno, dyDeposit.companycode, channel, dyDeposit.tradeno)
|
||
{
|
||
creator = dyDeposit.creator,
|
||
creatorname = dyDeposit.creatorname,
|
||
depositid = dyDeposit.id
|
||
};
|
||
//已经是确认过的到账,使用直接就是通过状态
|
||
pay.auditstatus = 1;
|
||
pay.auditor = dyDeposit.auditor;
|
||
pay.auditorname = dyDeposit.auditorname;
|
||
pay.audittime = DateTime.Now;
|
||
pay.deptcode = dyDeposit.deptcode;
|
||
|
||
//_orderPay.Add(pay);
|
||
db.WX_SzzyOrderPay.Add(pay);
|
||
|
||
if (dyDeposit.useprice.HasValue)
|
||
dyDeposit.useprice += curPay;
|
||
else
|
||
dyDeposit.useprice = curPay;
|
||
dyDeposit.lastprice -= curPay;
|
||
|
||
if (order.ARRIVALPAY.HasValue)
|
||
{
|
||
order.ARRIVALPAY += curPay;
|
||
}
|
||
else
|
||
{
|
||
order.ARRIVALPAY = curPay;
|
||
}
|
||
order.ARRIVALTIME = dyDeposit.paydate;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
db.SaveChanges();
|
||
var sumLastPrice = db.WX_SzzyOrderDeposit.Where(m => m.resid == dto.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);
|
||
var log = new Wx_SzzyOrderPayUseLog(dyDeposit.resid, item.orderid, pay.depositid ?? 0, pay.paytype, pay.paytypename, pay.payno, -curPay, pay.audittime ?? DateTime.Now, pay.channel.Value, usetype, sumLastPrice, "金额使用");
|
||
//_payuselog.Add(log);
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
|
||
db.SaveChanges();
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, null, null, pay);//将pay信息formrt
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, dyDeposit, null, null);//将pay信息formrt
|
||
ddeppriceLJ -= curPay;
|
||
}
|
||
else
|
||
{
|
||
//var sPirce = price;
|
||
//没收到能一笔支付的,那就按提交时间使用流水号
|
||
foreach (var xx in deposits.OrderBy(p => p.ctime))
|
||
{
|
||
curPay = order.NEEDPAY.Value - (order.ARRIVALPAY ?? 0);
|
||
var dep = db.WX_SzzyOrderDeposit.FirstOrDefault(m => m.id == xx.id);
|
||
if (dep.lastprice <= 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
if (order.ARRIVALPAY == order.NEEDPAY)
|
||
{
|
||
break;
|
||
}
|
||
|
||
decimal usePrice = 0;
|
||
|
||
if (curPay >= dep.lastprice.Value)
|
||
{
|
||
//使用金额大于等于当前需要付款的金额,使用金额足够支付
|
||
if (ddeppriceLJ < dep.lastprice.Value)
|
||
{
|
||
usePrice = ddeppriceLJ.Value;
|
||
}
|
||
else
|
||
{
|
||
usePrice = dep.lastprice.Value;
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
//使用金额不够支付,使用金额全部支付
|
||
usePrice = curPay;
|
||
}
|
||
if (usePrice <= 0)
|
||
{
|
||
continue;//没有可用金额,不需要进行下一步
|
||
}
|
||
var pay = new WX_SzzyOrderPay(item.orderid, item.needpay, dep.paytype, dep.paytypename, dep.paydate, usePrice, dep.payname, dep.remark, dep.payno, dep.companycode, channel, dep.tradeno)
|
||
{
|
||
creator = dep.creator,
|
||
creatorname = dep.creatorname,
|
||
depositid = dep.id
|
||
};
|
||
//已经是确认过的到账,使用直接就是通过状态
|
||
pay.auditstatus = 1;
|
||
pay.auditor = dep.auditor;
|
||
pay.auditorname = dep.auditorname;
|
||
pay.audittime = DateTime.Now;
|
||
pay.deptcode = dep.deptcode;
|
||
|
||
//_orderPay.Add(pay);
|
||
db.WX_SzzyOrderPay.Add(pay);
|
||
|
||
if (dep.useprice.HasValue)
|
||
dep.useprice += usePrice;
|
||
else
|
||
dep.useprice = usePrice;
|
||
dep.lastprice -= usePrice;
|
||
|
||
//dto.depprice -= usePrice;//要使用掉的金额-已使用金额
|
||
|
||
if (order.ARRIVALPAY.HasValue)
|
||
{
|
||
order.ARRIVALPAY += usePrice;
|
||
}
|
||
else
|
||
{
|
||
order.ARRIVALPAY = usePrice;
|
||
}
|
||
order.ARRIVALTIME = dep.paydate;
|
||
order.FINALPAY = order.ARRIVALPAY;
|
||
db.SaveChanges();
|
||
var sumLastPrice = db.WX_SzzyOrderDeposit.Where(m => m.resid == dto.resid && m.isdelete == 0 && m.auditstatus == 1).Sum(m => m.lastprice);
|
||
var log = new Wx_SzzyOrderPayUseLog(dep.resid, item.orderid, pay.depositid ?? 0, pay.paytype, pay.paytypename, pay.payno, -pay.payprice, pay.audittime ?? DateTime.Now, pay.channel.Value, usetype, sumLastPrice, "金额使用");
|
||
db.Wx_SzzyOrderPayUseLog.Add(log);
|
||
db.SaveChanges();
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, null, null, pay);//将pay信息formrt
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, dep, null, null);//将pay信息formrt
|
||
ddeppriceLJ -= usePrice;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
//已经是支付状态,说明已经给够钱了,不需要再支付了
|
||
LogHelper.Info(order.ORDERID.ToString() + "支付金额已经够了,不需要再支付了!");
|
||
continue;
|
||
//return new retMsg { result = false, retcode = 100, retmsg = order.ORDERID.ToString() + "支付金额已经够了,不需要再支付了!" };
|
||
}
|
||
}
|
||
WX_SzzyOrderDeposit_BL.SendInfoFormat(eventInfo, null, order, null);//将pay信息formrt
|
||
db.SaveChanges();
|
||
}
|
||
db.SaveChanges();
|
||
trans.Commit();
|
||
Common.EventBus.EventBus.Instance.Publish(eventInfo);
|
||
#region 调用自动判断是否开通程序,异步操作
|
||
Task.Run(() =>
|
||
{
|
||
new OrderService().CanIOpenByDepositAuditOrUse(deposits.Select(m => m.id).ToArray(), 0, "系统");
|
||
});
|
||
#endregion
|
||
return new retMsg<List<DepositInfoDto>> { result = true, retcode = 200, retmsg = null };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex);
|
||
trans.Rollback();
|
||
return new retMsg<List<DepositInfoDto>> { result = false, retcode = 500, retmsg = null };
|
||
}
|
||
}
|
||
}
|
||
//return new retMsg<List<DepositInfoDto>> { result = false, retcode = 500, retmsg = null };
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 验证金额必须正确
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool CheckPriceIsRight(zxdContext db)
|
||
{
|
||
bool result = true;
|
||
|
||
|
||
|
||
|
||
return result;
|
||
}
|
||
|
||
}
|
||
}
|