using NPOI.SS.Formula.Functions;
using OASystem.Domain;
using OASystem.Domain.Entities.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Groups
{
///
/// 费用审核仓储
///
public class FeeAuditRepository:BaseRepository
{
public FeeAuditRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
{
}
///
/// 费用自动审核
///
///
/// 1.酒店 76
/// 2.op 79
///
/// 团组Id
/// 数据Id(模块类型主表Id)
///
public async Task FeeAutomaticAudit(int feeType, int diId, int dataId)
{
var _view = new JsonView() { Code = 201, Msg = "自动审核操作失败" };
if (diId < 1) { _view.Msg = MsgTips.DiId; return _view; }
if (dataId < 1) { _view.Msg = MsgTips.Id; return _view; }
List stids = new List() { 17, 66 };
var setData = _sqlSugar.Queryable().Where(x => x.IsDel == 0 && stids.Contains(x.STid)).ToList();
string _teamCurrency = string.Empty;
decimal _rate = 0.00M;
var groupInfo = _sqlSugar.Queryable< Grp_GroupCostParameter >().Where(x => x.IsDel ==0 && x.DiId == diId).First();
if (groupInfo == null) { _view.Msg = $"团组成本信息未填写!"; return _view; }
_teamCurrency = groupInfo.Currency;
//币种验证 统一为currencycode三字码
if (int.TryParse(_teamCurrency,out int currency))
{
_teamCurrency = setData.Find(x => x.Id == currency)?.Name ?? "";
}
_rate = groupInfo.Rate;
var costContents = _sqlSugar.Queryable().Where(x => x.IsDel == 0 && x.Diid == diId).ToList();
if (costContents.Count < 1) { _view.Msg = $"团组成本信息未填写!"; return _view; }
//处理 成本详细信息 日期为空
for (int i = 0; i < costContents.Count; i++)
{
if (string.IsNullOrEmpty( costContents[i].Date))
{
int index = i - 1;
if (index >= 0)
{
costContents[i].Date = costContents[index].Date;
}
}
}
if (feeType == 1)
{
var hotelCostInfo = _sqlSugar.Queryable().Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId).First();
var hotelCostDetails = _sqlSugar.Queryable().Where(x => x.IsDel == 0 && x.DiId == diId && x.HrId == dataId).ToList();
if (hotelCostInfo == null)
{
_view.Msg = $"酒店费用数据未填写";
return _view;
}
//验证币种是否相等
string hotelCurrency = setData.Find(x => x.Id == hotelCostInfo.CardPriceCurrency)?.Name ?? "";
if (hotelCurrency != _teamCurrency)
{
if (!hotelCurrency.Equals("CNY"))
{
_view.Msg = $"币种不一致";
return _view;
}
}
else _rate = 1.0000M;
bool isAutoAudit = true; //是否自动审核
DateTime checkIn = Convert.ToDateTime(hotelCostInfo.CheckInDate),
checkOut = Convert.ToDateTime(hotelCostInfo.CheckOutDate);
var hotelCostInfos = costContents.Where(x => Convert.ToDateTime(x.Date) >= checkIn && Convert.ToDateTime(x.Date) <= checkOut).ToList();
if (hotelCostInfos.Count < 1) isAutoAudit = false;
decimal otherFee = hotelCostDetails.Where(x => x.PriceType != 1).Sum(x => x.Price);
if (otherFee > 0) { otherFee /= 3; }
foreach (var item in hotelCostInfos)
{
//1.判断费用是否 <= 成本费用
//1.1 判断单间费用
if (item.SGR > 0)
{
decimal singleRoomPrice = hotelCostInfo.SingleRoomPrice + otherFee;
if (singleRoomPrice > item.SGR * _rate) isAutoAudit = false;
}
//1.2 判断双人间费用
if (item.TBR > 0)
{
decimal doubleRoomPrice = hotelCostInfo.DoubleRoomPrice + otherFee;
if (doubleRoomPrice > item.TBR * _rate) isAutoAudit = false;
}
//1.3 判断套房费用
if (item.Suite > 0)
{
decimal suiteRoomPrice = hotelCostInfo.SuiteRoomPrice + otherFee;
if (suiteRoomPrice > item.Suite * _rate) isAutoAudit = false;
}
//1.4 判断其他房型费用
if (item.JS_ES > 0)
{
decimal otherRoomPrice = hotelCostInfo.OtherRoomPrice + otherFee;
if (otherRoomPrice > item.JS_ES * _rate) isAutoAudit = false;
}
}
//2.判断是否自动审核
if (isAutoAudit)
{
var ccpUpdate = _sqlSugar.Updateable()
.SetColumns(it => it.IsAuditGM == 3)
.SetColumns(it => it.AuditGMOperate == 4)
.SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
.Where(s => s.DIId == diId && s.CTable == 76 && s.CId == dataId)
.ExecuteCommand();
if (ccpUpdate > 0)
{
_view.Code = 200;
_view.Msg = "审核成功";
return _view;
}
}
else {
//撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
var ccpInfo = _sqlSugar.Queryable()
.Where(s => s.DIId == diId && s.CTable == 76 && s.CId == dataId && s.IsAuditGM == 3)
.First();
if (ccpInfo != null)
{
var ccpUpdate = _sqlSugar.Updateable()
.SetColumns(it => it.IsAuditGM == 0)
.SetColumns(it => it.AuditGMOperate == 0)
.SetColumns(it => it.AuditGMDate == string.Empty)
.Where(s => s.Id == ccpInfo.Id)
.ExecuteCommand();
}
}
}
else if (feeType == 2)
{
//1.含超时费用 手动审核
}
else _view.Msg = $"请传入有效的feeType参数";
return _view;
}
}
}