using Common;
using Common.DJYModel;
using Common.Entity;
using Common.Extensions;
using djy.IService.Djy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace djy.Service.DjyService
{
public class FinanceService : ServBase, IFinanceService
{
///
///
///
///
/// 1验证是否可以扣款 2扣除操作
///
public ReturnResult Expend(CustFee Dto,int exptype)
{
_LogsAdd("Expend消费检查和扣款","open",Dto);
var rs = new ReturnResult();
try {
if (Dto == null) {
rs.Not("错误的null请求");
return rs;
}
if (Dto.BSNO.IsNull()) {
rs.Not("业务编号不能BSNO为空!");
return rs;
}
if (Dto.MBLNO.IsNull()) {
rs.Not("主提单号不能为空!");
return rs;
}
if (Dto.BSTYPE == null || Dto.SENDTYPE == null) {
rs.Not("请求的业务类型和发送类型不能为空!");
return rs;
}
if (Dto.SENDUSERID.IsNotNull())
{
var userinfo = DbBus.Get(DbList.djydb).Select().Where(w => w.GID == Dto.SENDUSERID).ToOne();
if (userinfo != null) {
Dto.SENDUSER = Dto.SENDUSER.IsNull()?userinfo.SHOWNAME : Dto.SENDUSER;
Dto.SENDUSERID = userinfo.GID;
Dto.SENDCOM = Dto.SENDCOM .IsNull() ?userinfo.COMNAME: Dto.SENDCOM;
}
}
if (Dto.LURURENID.IsNotNull()) {
var luser = DbBus.Get(DbList.djydb).Select().Where(w => w.GID == Dto.LURURENID).ToOne();
if (luser != null) {
Dto.LURUREN = luser.SHOWNAME;
Dto.LURURENID = luser.GID;
Dto.COMNAME = luser.COMNAME;
Dto.COMID = luser.CompId;
}
}
Dto.GID = Guid.NewGuid().ToString();
Dto.SENDTIME = DateTime.Now;
Dto.CREATETIME = DateTime.Now;
if (Dto.CtnrCount == 0)
{//如果没有指定数量则根据 箱型箱量计算数量
Dto.CtnrCount = Dto.GetCtnrcount();
}
if (Dto.CtnrCount <= 0) {
rs.Not("数量不能小于1!");
return rs;
}
var sql = DbBus.Get(DbList.djydb).Select().Where(w => w.BSTYPE == Dto.BSTYPE && w.SENDTYPE == Dto.SENDTYPE && w.COMID == Dto.COMID);
var price = sql.ToOne();
if (price == null) {
rs.Not("没有找到此业务的计费规则!");
return rs;
}
decimal _price = 0;
_price = (decimal)price.PRICE;
//合计支付
Dto.PRICE = _price* Dto.CtnrCount;
var PayInfo = new { Total = Dto.PRICE, Price = _price, Count = Dto.CtnrCount };
if (exptype == 1)
{
DbBus.Get(DbList.djydb).Transaction(() =>
{ //执行事务提交
//执行扣款
var payiscredit = false;
var userbal = DbBus.Get(DbList.djydb).Select().Where(w => w.COMNAME == Dto.COMNAME).ToOne();
if (userbal == null)
{
throw new Exception("没有账户钱包!");
}
if (userbal.Balance < Dto.PRICE)
{
if (Dto.IsCredit == 1)
{
_LogsAdd("账户授信支付" + Dto.PRICE.ToString() + "_财务消费expend", Dto.BSNO, Dto);
payiscredit=true;
}
else
{
_LogsAdd("账户余额不足" + Dto.PRICE.ToString() + "_财务消费expend", Dto.BSNO, Dto);
throw new Exception("账户余额不足!" + Dto.PRICE + " " + Dto.COMNAME);
}
}
var upcount = DbBus.Get(DbList.djydb).Update().Set(a => a.Balance - Dto.PRICE)
.Where(a => a.COMNAME == Dto.COMNAME).ExecuteAffrows();
//增加流水
var addcount = DbBus.Get(DbList.djydb).Insert(Dto).ExecuteAffrows();
if (addcount < 1)
{
_LogsAdd("消费记录写入失败_财务消费expend", Dto.BSNO, Dto);
throw new Exception("消费记录写入失败!");
}
rs.Data = new{RunCount=addcount ,PayInfo};
rs.OK("扣费成功!");
if (payiscredit)
{
rs.Code = 450;
rs.Message = "钱包金额不足,授信支付成功!钱包余额"+(userbal.Balance-Dto.PRICE);
}
});
}
else {
if (DbBus.Get(DbList.djydb).Select().Where(a => a.Balance >= Dto.PRICE && a.COMNAME == Dto.COMNAME).Count() > 0)
{
rs.Data = new {PayInfo };
rs.OK("钱包金额充足");
}
else {
rs.Data = new { PayInfo };
rs.Not("钱包金额不足");
}
}
}
catch(Exception ex)
{
rs.Not(ex.Message);
}
//rs.MemoData = Dto;
return rs;
}
}
}