You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
8.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using djy.Paas.IService;
using djy.Paas.Model;
using djy.Model;
using Ys.Core.Common;
namespace djy.Paas.Service
{
public class FinanceService : ServBase, IFinanceService
{
/// <summary>
///
/// </summary>
/// <param name="Dto"></param>
/// <param name="exptype">1验证是否可以扣款 2扣除操作 </param>
/// <returns></returns>
public ReturnResult<object> Expend(CustFee Dto,int exptype)
{
_LogsAdd("Expend消费检查和扣款","open",Dto);
var rs = new ReturnResult<object>();
try {
if (Dto == null) {
rs.Not("错误的null请求");
return rs;
}
//if (Dto.SENDUSERID.IsNull()) {
// rs.Not("发送人ID丢失");
// 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.djyolddb).Select<User>().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.djyolddb).Select<User>().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.djyolddb).Select<CustPrice>().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.djyolddb).Transaction(() =>
{ //执行事务提交
//执行扣款
var payiscredit = false;
var userbal = DbBus.Get(DbList.djyolddb).Select<CustBalance>().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.djyolddb).Update<CustBalance>().Set(a => a.Balance - Dto.PRICE)
.Where(a => a.COMNAME == Dto.COMNAME).ExecuteAffrows();
//增加流水
var addcount = DbBus.Get(DbList.djyolddb).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.djyolddb).Select<CustBalance>().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;
}
public ReturnPagedResult<CustFee> ExpendList(CustFee Dto, ApiFromDto ApiDto)
{
var rs = new ReturnPagedResult<CustFee>();
{
var bsnolist =new List<string>();
if (Dto.BSNO.IsNotNull()) {
bsnolist= Dto.BSNO.Split(',').ToList();
}
var sendtype = -1;
if (ApiDto.CarrierCode.IsNotNull()) {
try {
var car= DjyTools.GetCarrierList().Where(w => w.Code.ToLower() == ApiDto.CarrierCode.ToLower()).First();
if (car != null) {
sendtype = car.IntId;
}
}
catch { }
}
var sql = DbBus.Get(DbList.djyolddb).Select<CustFee>().WhereIf(Dto.BSNO.IsNotNull(), w => w.BSNO == Dto.BSNO)
.WhereIf(Dto.BSTYPE!=null,w=>w.BSTYPE==Dto.BSTYPE)
.WhereIf(sendtype>0,w=>w.SENDTYPE==sendtype)
.WhereIf(Dto.BSNO.IsNotNull()&&bsnolist.Count==1,w=>w.BSNO==Dto.BSNO)
.WhereIf(bsnolist.Count>1,w=>bsnolist.Contains(w.BSNO))
.WhereIf(ApiDto.StartTime>0,w=>w.CREATETIME>=ApiDto.StartTime.ToDataTime())
.WhereIf(ApiDto.EndTime > 0, w => w.CREATETIME <= ApiDto.EndTime.ToDataTime())
.WhereIf(Dto.MBLNO.IsNotNull(), w => w.MBLNO == Dto.MBLNO.Trim()).OrderByDescending(w=>w.CREATETIME);
rs.Data = sql.Count(out var getcount).Page(ApiDto.Page, ApiDto.Limit).ToList();
rs.Pageset(ApiDto.Limit,ApiDto.Page,getcount);
rs.OK("ok");
}
return rs;
}
}
}