using AutoMapper;
using OASystem.Domain;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.Entities.Resource;
using OASystem.Domain.ViewModels.Groups;
using OASystem.Infrastructure.Repositories.Resource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Groups
{
///
/// 团组签证费用详情 info
/// 仓储
///
public class VisaFeeInfoRepository:BaseRepository
{
private readonly IMapper _mapper;
private Result _result;
private readonly CountryFeeRepository _countryFeeRep;
public VisaFeeInfoRepository(SqlSugarClient sqlSugar, IMapper mapper, CountryFeeRepository countryFeeRep)
: base(sqlSugar)
{
_mapper = mapper;
_result = new Result() { Code = -2,Msg = "操作失败!"};
_countryFeeRep = countryFeeRep;
}
///
/// Init
///
///
///
///
public async Task _Init()
{
var data = await _sqlSugar.Queryable().Where(it => it.IsDel == 0).ToListAsync();
_result.Code = 0;
_result.Data = data;
_result.Msg = "操作成功!";
return _result;
}
///
/// List
///
///
///
///
public async Task _List(int portType, int diId)
{
if (diId < 0) return _result = new Result() { Code = -1, Msg = "请传入有效的DiId参数" };
if (portType < 1 || portType > 3) return _result = new Result() { Code = -1, Msg = "请传入有效的portType参数" };
string sql = string.Format($@"Select vfi.Id,vfi.IsChecked,cfc.VisaCountry AS Country,cfc.VisaPrice As VisaFee,
vfi.AgencyFee,vfi.OtherFee,vfi.Remark
From Grp_VisaFeeInfo vfi
Left Join Res_CountryFeeCost cfc On vfi.CountryVisaFeeId = cfc.Id
Where vfi.Isdel = 0 And vfi.Diid = {diId}");
var data = await _sqlSugar.SqlQueryable(sql).ToListAsync();
_result.Code = 0;
_result.Data = data;
_result.Msg = "操作成功!";
return _result;
}
///
/// List
///
///
///
///
public async Task _AddAndUpdate(VisaFeeAddAndUpdateDto dto)
{
if (dto.Status < 1 || dto.Status > 2) return _result = new Result() { Code = -1, Msg = "请传入有效的Status参数" };
if (dto.VisaFeeInfos.Count < 1) return _result = new Result() { Code = -1, Msg = "请传入有效的签证费用集合参数" };
if (dto.PortType < 1 || dto.PortType > 3) return _result = new Result() { Code = -1, Msg = "请传入有效的portType参数" };
List visaInfos = new List();
BeginTran();
bool visaFeeUpdate = false;
foreach (var item in dto.VisaFeeInfos)
{
int countryVisaFeeId = 0;
var info =await _countryFeeRep._InfoByCountryName(item.Country);
if (info == null) //添加
{
int addId = _sqlSugar.Insertable(
new Res_CountryFeeCost()
{
VisaCountry = item.Country,
VisaPrice = item.VisaFee,
LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
CreateUserId = dto.OpUserId,
}).ExecuteReturnIdentity();
if (addId > 0) countryVisaFeeId = addId;
}
else //修改
{
countryVisaFeeId = info.Id;
if (item.VisaFee != info.VisaPrice) //价格不同的时候执行修改
{
Res_CountryFeeCost _CountryFeeCost = new Res_CountryFeeCost()
{
Id = info.Id,
VisaPrice = item.VisaFee,
LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
};
int update = _sqlSugar.Updateable(_CountryFeeCost).UpdateColumns(it => new { it.VisaPrice, it.LastUpdateTime }).WhereColumns(it => it.Id).ExecuteCommand();
if (update > 0)
{
visaFeeUpdate = true;
}
}
}
visaInfos.Add(new Grp_VisaFeeInfo()
{
Id = item.Id,
DiId = dto.DiId,
IsChecked = item.IsChecked,
CountryVisaFeeId = countryVisaFeeId,
AgencyFee = item.AgencyFee,
OtherFee = item.OtherFee
});
}
if (dto.Status == 1) //添加
{
var add = _sqlSugar.Insertable(visaInfos).ExecuteCommand();
if (add > 0)
{
CommitTran();
return new Result() {Code = 0 ,Msg = "操作成功!" };
}
}
else if (dto.Status == 2) //修改
{
var update = _sqlSugar.Updateable(visaInfos).IgnoreColumns(it => new { it.CreateUserId,it.CreateTime,it.IsDel }).WhereColumns(it => it.Id).ExecuteCommand();
if (update > 0 || visaFeeUpdate)
{
CommitTran();
return new Result() { Code = 0, Msg = "操作成功!" };
}
}
RollbackTran();
return _result;
}
///
/// 三公费用签证费用提示
///
///
///
///
public async Task EntryAndExitTips(int diId)
{
if (diId < 1) return _result = new Result() { Code = -1, Msg = "请传入有效的DiId参数" };
var visaData = await _sqlSugar.Queryable().Where(it => it.IsDel == 0).ToListAsync();
List ids = visaData.Select (it => it.CountryVisaFeeId).ToList();
var visaCountryDatas = await _sqlSugar.Queryable< Res_CountryFeeCost >().Where(it => ids.Contains(it.Id)).ToListAsync();
List datas = new List();
string remark = "";
decimal feeTotal = 0.00M;
foreach (var item in visaData)
{
if (item.IsChecked == 1)
{
var countryData = visaCountryDatas.Find(it => it.Id == item.CountryVisaFeeId);
decimal visaFeeTotal = countryData?.VisaPrice ?? 0.00M + item.AgencyFee + item.OtherFee;
remark += $@"{countryData?.VisaCountry ?? ""}:签证总费用:{visaFeeTotal}元/人 其中(";
if (countryData?.VisaPrice > 0) remark += $@"签证费用:{countryData?.VisaPrice.ToString("#0.00")}元、";
if (item.AgencyFee > 0) remark += $@"代办费:{item.AgencyFee.ToString("#0.00")}元、";
if (item.OtherFee > 0) remark += $@"其他费用:{item.OtherFee.ToString("#0.00")}元";
if (visaFeeTotal > 0) remark = remark.Substring(0,remark.Length -1);
remark += ");";
feeTotal += visaFeeTotal;
datas.Add(new
{
Country = countryData?.VisaCountry ?? "",
visaFeeTotal = visaFeeTotal,
VisaFee = countryData?.VisaPrice ?? 0.00M,
item.AgencyFee,
item.OtherFee
});
}
}
_result.Code = 0;
_result.Data = new {
feeTotal = feeTotal,
data = datas,
remark = remark
};
return _result;
}
}
}