using AutoMapper;
using NPOI.SS.Formula.Functions;
using OASystem.Domain;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Dtos.System;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.Groups;
using OASystem.Infrastructure.Repositories.System;
using OASystem.Infrastructure.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Groups
{
///
/// 出入境费用 仓库
///
public class EnterExitCostRepository : BaseRepository
{
private readonly IMapper _mapper;
private readonly SetDataRepository _setDataRep;
public EnterExitCostRepository(SqlSugarClient sqlSugar, IMapper mapper, SetDataRepository setDataRep)
: base(sqlSugar)
{
_setDataRep = setDataRep;
_mapper = mapper;
}
///
/// 权限用户基础数据
///
///
public async Task> PermissionViewUsersAsync()
{
var gj_jobPostIds = new List() { 22, 32 };
var viewPermissionData = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && ((x.DepId == 7 && gj_jobPostIds.Contains(x.JobPostId)) || x.DepId == 6))
.Select(x => new PermissionViewUserview { Id = x.Id, Name = x.CnName })
.ToListAsync();
return viewPermissionData;
}
///
/// 出入境费用正式 --> 权限验证
///
/// 团组Id
/// 操作权限 1:查看;2:新增;3:编辑;4:删除;5:下载;
///
public async Task PermissionValidationAsync(int groupId, int userId, int permission = 1)
{
//参数验证
if (groupId < 1) return false;
if (userId < 1) return false;
var permissionIds = new List() { 1, 2, 3, 4, 5 };
if (!permissionIds.Contains(permission)) return false;
var info = await _sqlSugar.Queryable()
.Where(it => it.GroupId == groupId && it.UserId == userId && it.Permission == permission)
.FirstAsync();
if (info != null) return true;
return false;
}
///
/// 更新出入境费用汇率信息
///
///
public async Task SetEnterExitCostCurrencyChange()
{
Result result = new Result { Code = -1, Msg = "未知错误" };
var enterExitList = await _sqlSugar.Queryable().ToListAsync();
foreach (var item in enterExitList)
{
string rateStr = string.Format(@"美元(USD):{0}|日元(JPY):{1}|欧元(EUR):{2}|英镑(GBP):{3}|港币(HKD):{4}", item.RateUSD, item.RateJPY, item.RateEUR, item.RateGBP, item.RateHKD);
item.CurrencyRemark = rateStr;
}
var res = await _sqlSugar.Updateable(enterExitList)
.WhereColumns(it => new { it.Id })
.UpdateColumns(it => new { it.CurrencyRemark })
.ExecuteCommandAsync();
if (res > 0)
{
result.Code = 0;
result.Msg = res.ToString();
return result;
}
return result;
}
///
/// 移动端操作权限验证
///
///
///
/// 1 查看 2 编辑
///
public async Task<(bool, string)> MobilePermissionsValidationAsync(int groupId,int currUserId,int permissionType = 1)
{
bool status = false;
string msg = string.Empty;
var enterExitCostData = await _sqlSugar.Queryable().OrderByDescending(x => x.CreateTime).FirstAsync(it => it.DiId == groupId && it.IsDel == 0);
if (permissionType == 1)
{
status = await PermissionValidationAsync(groupId, currUserId);//查看权限
if (enterExitCostData.CreateUserId == currUserId) status = true; //查看权限
if (!status)
{
var setUserName = await _sqlSugar.Queryable().Where(x => x.Id == enterExitCostData.CreateUserId).Select(x => x.CnName).FirstAsync();
msg = $"{setUserName}设置了你不能查看当前团组的出入境费用明细数据,如要查看,请联系{setUserName}!";
}
return (status, msg);
}
else if (permissionType == 2)
{
if (enterExitCostData.CreateUserId == currUserId) status = true; //编辑权限
if (!status)
{
var setUserName = await _sqlSugar.Queryable().Where(x => x.Id == enterExitCostData.CreateUserId).Select(x => x.CnName).FirstAsync();
msg = $"您没有编辑权限,“{setUserName}”才可编辑!";
}
return (status, msg);
}
else msg = $"请传入正确的权限验证类型!";
return (status, msg);
}
///
/// 查询 根据 Diid 查询
///
///
public async Task GetEnterExitCostInfoByDiId(EnterExitCostInfobyDiIdDto dto)
{
var result = new JsonView() { Code = 400 , Msg = "暂无数据" };
var enterExitCostData = await _sqlSugar.Queryable().OrderByDescending(x => x.CreateTime).FirstAsync(it => it.DiId == dto.DiId && it.IsDel == 0);
//Sys_PageFunctionPermission Function = _mapper.Map(dto);
var enterExitCostInfoView = new EnterExitCostInfoView();
if (dto.PortType == 1) // Web
{
if (enterExitCostData != null)
{
enterExitCostInfoView = _mapper.Map(enterExitCostData);
var isView = await PermissionValidationAsync(dto.DiId, dto.CurrUserId);
if (isView) enterExitCostInfoView.IsView = true; //查看权限
if (enterExitCostData.CreateUserId == dto.CurrUserId) {
enterExitCostInfoView.IsSave = true; //操作权限
enterExitCostInfoView.IsView = true; //查看权限
}
if (!enterExitCostInfoView.IsView)
{
var setUserName = await _sqlSugar.Queryable().Where(x => x.Id == enterExitCostData.CreateUserId).Select(x => x.CnName).FirstAsync();
result.Msg = $"{setUserName}设置了你不能查看当前团组的出入境费用明细数据,如要查看,请联系{setUserName}!";
return result;
}
enterExitCostInfoView.ViewUsers = await _sqlSugar
.Queryable()
.Where(x => x.IsDel == 0 && x.GroupId == dto.DiId)
.Select(x => x.UserId)
.ToArrayAsync();
//enterExitCostInfoView.FirstItemRemark = enterExitCostData.Remark;
var dayAndCostSql = string.Format(@"Select ntf.Country,ntf.City,ssd.Name CurremcyCode,ssd.Remark CurrencyName,gdac.* From Grp_DayAndCost gdac
Left Join Grp_NationalTravelFee ntf On ntf.Id = gdac.NationalTravelFeeId
Left Join Sys_SetData ssd On ssd.STid = 66 And gdac.Currency = ssd.Id
Where gdac.Isdel = 0 And ntf.IsDel = 0 And gdac.DiId = {0} Order By [Type] ASC,[Days] ASC", dto.DiId);
var dayAndCostData = await _sqlSugar.SqlQueryable(dayAndCostSql).ToListAsync();
enterExitCostInfoView.QuarterageData = dayAndCostData.Where(it => it.Type == 1).ToList(); //住宿费 1
enterExitCostInfoView.BoardWagesData = dayAndCostData.Where(it => it.Type == 2).ToList(); //伙食费 2
enterExitCostInfoView.MiscellaneousFeeData = dayAndCostData.Where(it => it.Type == 3).ToList(); //公杂费 3
enterExitCostInfoView.TrainingExpenseData = dayAndCostData.Where(it => it.Type == 4).ToList(); //培训费 4
enterExitCostInfoView.DayOtherPriceData = _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && x.Diid == dto.DiId)
.OrderBy(x => x.Index)
.Select(x => new DayOtherPriceView
{
DiId = x.Diid,
Id = x.Id,
Cost = x.Cost,
Currency = x.Currency,
Index = x.Index,
SetDataId = x.SetDataId,
SubTotal = x.SubTotal,
Remark = x.Remark,
})
.ToList();
enterExitCostInfoView.Currencys = (List?)CommonFun.GetCurrencyChinaToList(enterExitCostData.CurrencyRemark);
result.Msg = "操作成功";
result.Code = 200;
}
else
{
// List views = new List()
// {
// new DayAndCostInfoView {Days=1},
// new DayAndCostInfoView {Days=2},
// new DayAndCostInfoView {Days=3},
// new DayAndCostInfoView {Days=4},
// new DayAndCostInfoView {Days=5},
// new DayAndCostInfoView {Days=6},
// new DayAndCostInfoView {Days=7},
// new DayAndCostInfoView {Days=8},
// };
// enterExitCostInfoView.QuarterageData = views;
// enterExitCostInfoView.BoardWagesData = views;
// enterExitCostInfoView.MiscellaneousFeeData = views;
// enterExitCostInfoView.TrainingExpenseData = views;
// result.Code = 0;
enterExitCostInfoView.IsView = true;
enterExitCostInfoView.IsSave = true;
result.Msg = "未查询出数据!可操作该条数据!";
}
result.Data = enterExitCostInfoView;
}
else if (dto.PortType == 2 || dto.PortType == 3)
{
var mobileInfo = new EnterExitCostMobileInfoView();
if (enterExitCostData != null)
{
mobileInfo = _mapper.Map(enterExitCostData);
var dayAndCostData = _sqlSugar.Queryable()
.LeftJoin((dac,ntf) => dac.NationalTravelFeeId == ntf.Id)
.LeftJoin((dac, ntf, sd) => dac.Currency == sd.Id)
.Where((dac, ntf, sd) => dac.IsDel == 0 && dac.DiId == dto.DiId)
.Select((dac, ntf, sd) => new DayAndCostMobileInfoView
{
SubId = dac.Id,
DiId = dac.DiId,
Type = dac.Type,
Days = dac.Days,
NationalTravelFeeId = dac.NationalTravelFeeId,
Arae = $"{ntf.Country}-{ntf.City}",
Cost = dac.Cost,
Currency = dac.Currency,
CurrencyName = sd.Name,
SubTotal = dac.SubTotal,
})
.ToList();
mobileInfo.ChoiceOneTotalCost = enterExitCostData.InsidePay;
mobileInfo.ChoiceOneDetails = new ChoiceOneMobileInfoView() {
Visa = enterExitCostData.Visa,
VisaRemark = enterExitCostData.VisaRemark,
YiMiao = enterExitCostData.YiMiao,
HeSuan = enterExitCostData.HeSuan,
Service = enterExitCostData.Service,
Ticket = enterExitCostData.Ticket,
Safe = enterExitCostData.Safe,
};
mobileInfo.ChoiceTwoDetails = new ChoiceTwoMobileInfoView()
{
AirJJ = enterExitCostData.AirJJ,
AirGW = enterExitCostData.AirGW,
AirTD = enterExitCostData.AirTD,
CityTranffic = enterExitCostData.CityTranffic
};
mobileInfo.ChoiceThreeDetails = dayAndCostData.Where(it => it.Type == 1).OrderBy(it => it.Days).ToArray(); //住宿费 1
mobileInfo.ChoiceFourDetails = dayAndCostData.Where(it => it.Type == 2).OrderBy(it => it.Days).ToArray(); //伙食费 2
mobileInfo.ChoiceFiveDetails = dayAndCostData.Where(it => it.Type == 3).OrderBy(it => it.Days).ToArray(); //公杂费 3
mobileInfo.ChoiceSixDetails = dayAndCostData.Where(it => it.Type == 4).OrderBy(it => it.Days).ToArray(); //培训费 4
//其他款项
mobileInfo.OtherExpensesDetails = _sqlSugar.Queryable()
.LeftJoin((dop,sd1) => dop.SetDataId == sd1.Id)
.LeftJoin((dop, sd1,sd2) => dop.Currency == sd2.Id)
.Where((dop, sd1, sd2) => dop.IsDel == 0 && dop.Diid == dto.DiId)
.OrderBy((dop, sd1, sd2) => dop.Index)
.Select((dop, sd1, sd2) => new DayOtherPriceMobileInfoView
{
SubId = dop.Id,
DiId = dop.Diid,
Index = dop.Index,
SetDataId = dop.SetDataId,
SetDataName = sd1.Name,
Cost = dop.Cost,
Currency = dop.Currency,
CurrencyName = sd2.Remark,
SubTotal = dop.SubTotal,
Remark = dop.Remark,
})
.ToArray();
mobileInfo.Currencys = (List?)CommonFun.GetCurrencyChinaToList(enterExitCostData.CurrencyRemark);
result.Msg = "查询成功";
result.Code = 200;
}
result.Data = mobileInfo;
}
else result.Msg = MsgTips.Port;
return result;
}
///
/// Add Or Update
///
///
///
public async Task PostEnterExitCostOperate(EnterExitCostOperateDto dto)
{
Result result = new Result() { Code = -1, Msg = "操作失败!" };
if (dto.SumJJC == 0) dto.OutsideJJPay = 0;
if (dto.SumGWC == 0) dto.OutsideGWPay = 0;
if (dto.SumTDC == 0) dto.OutsideTDPay = 0;
var enterExitCost = _mapper.Map(dto);
//enterExitCost.Remark = dto.FirstItemRemark;
//enterExitCost.InsidePay = enterExitCost.Visa + enterExitCost.YiMiao + enterExitCost.HeSuan + enterExitCost.Service + enterExitCost.Safe + enterExitCost.Ticket;
List subData = new List();
var quarterageData = _mapper.Map>(dto.QuarterageData); //住宿费 1
quarterageData = quarterageData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
if (quarterageData.Count > 0) subData.AddRange(quarterageData);
var boardWagesData = _mapper.Map>(dto.BoardWagesData); //伙食费 2
boardWagesData = boardWagesData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
if (boardWagesData.Count > 0) subData.AddRange(boardWagesData);
var miscellaneousFeeData = _mapper.Map>(dto.MiscellaneousFeeData); //公杂费 3
miscellaneousFeeData = miscellaneousFeeData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
if (miscellaneousFeeData.Count > 0) subData.AddRange(miscellaneousFeeData);
var trainingExpenseData = _mapper.Map>(dto.TrainingExpenseData); //培训费 4
trainingExpenseData = trainingExpenseData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
if (trainingExpenseData.Count > 0) subData.AddRange(trainingExpenseData);
//处理币种string
enterExitCost.CurrencyRemark = CommonFun.GetCurrencyChinaToString(dto.Currencys);
enterExitCost.CreateUserId = dto.UserId;
_sqlSugar.BeginTran();
if (dto.PortType == 1)
{
try
{
int enterExitId = 0;
if (enterExitCost.Id > 0) //修改
{
//操作权限验证 只有创建人可修改
var info = await _sqlSugar.Queryable((eec,u) => new JoinQueryInfos(JoinType.Left,eec.CreateUserId == u.Id))
.Where((eec, u) => eec.IsDel == 0 && eec.Id == enterExitCost.Id)
.Select((eec, u) => new { eec.CreateUserId,u.CnName })
.FirstAsync();
if (info.CreateUserId != dto.UserId)
{
_sqlSugar.RollbackTran();
result.Msg = $"该条信息只有操作人“{info.CnName}”可修改!";
return result;
}
//修改验证
//IsConfirm == 2 不可修改
//var info = _sqlSugar.Queryable().Where(it => it.IsDel == 0 && it.Id == enterExitCost.Id).First();
//if (info.IsConfirm == 2)
//{
// result.Msg = "该团出入境费用已确认!不可更改!!";
// return result;
//}
var updateId = _sqlSugar.Updateable(enterExitCost)
.IgnoreColumns(it => new { it.DeleteUserId, it.DeleteTime, it.CreateUserId, it.CreateTime, it.IsDel })
.ExecuteCommand();
if (updateId > 0)
{
enterExitId = enterExitCost.Id;
}
else
{
_sqlSugar.RollbackTran();
result.Msg = "修改操作失败!";
return result;
}
}
else
{
//效重 根据团组Id
var info = _sqlSugar.Queryable().Where(it => it.IsDel == 0 && it.DiId == dto.DiId).First();
if (info != null )
{
_sqlSugar.RollbackTran();
result.Msg = "该团组相关数据已存在,请勿重新添加!";
return result;
}
//添加
enterExitId = _sqlSugar.Insertable(enterExitCost).ExecuteReturnIdentity();
if (enterExitId < 1)
{
_sqlSugar.RollbackTran();
result.Msg = "添加操作失败!";
return result;
}
//默认添加查看权限
var users = await PermissionViewUsersAsync();
var userIds = users.Select(x => x.Id).ToList();
if (userIds.Any())
{
var permissions = new List();
foreach (var userId in userIds)
{
permissions.Add(new Grp_EnterExitCostPermission()
{
GroupId = enterExitId,
UserId = userId,
Permission = 1,
CreateUserId = dto.UserId,
});
}
if (permissions.Any())
{
_sqlSugar.Insertable(permissions).ExecuteReturnIdentity();
}
}
}
if (enterExitId > 0)
{
string sqlDelete = string.Format(@" Delete From Grp_DayAndCost Where Diid = {0} ", dto.DiId);
await ExecuteCommandAsync(sqlDelete);
foreach (var item in subData)
{
var updateId = _sqlSugar.Insertable(item).ExecuteCommand();
if (updateId < 1)
{
_sqlSugar.RollbackTran();
result.Msg = "添加操作失败!";
return result;
}
}
}
_sqlSugar.CommitTran();
result.Code = 0;
result.Msg = "操作成功!";
result.Data = new { sign = enterExitId };
}
catch (Exception ex)
{
_sqlSugar.RollbackTran();
result.Msg = ex.Message;
}
}
else result.Msg = MsgTips.Port;
return result;
}
///
/// 确认费用
///
///
///
public async Task ConfirmCost(ConfirmCostDto dto)
{
var _view = new JsonView() { Code = 20001, Msg = "操作失败!" };
if (dto.Id < 1)
{
_view.Msg = MsgTips.Id;
return _view;
}
int confirm = 1;
if (dto.Type == 1) confirm = 2; // 确认
else if (dto.Type == 2) { confirm = 1; } // 撤回
else
{
_view.Msg = "请传入正确的type;1:确认 2:撤回";
return _view;
}
var update = await _sqlSugar.Updateable()
.SetColumns(x => new Grp_EnterExitCost() { IsConfirm = confirm })
.Where(x => x.Id == dto.Id)
.ExecuteCommandAsync();
if (update>0)
{
_view.Code = 200;
_view.Msg = "操作成功!";
return _view;
}
return _view;
}
///
/// Del
///
///
///
public Task PostEnterExitCostSubItemDel(EnterExitCostSubItemDelDto dto)
{
Result result = new Result();
var data = new Grp_DayAndCost()
{
Id = dto.Id,
IsDel = 1,
DeleteUserId = dto.DeleteUserId,
DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
};
var del = _sqlSugar.Updateable(data).UpdateColumns(it => new { it.IsDel, it.DeleteUserId, it.DeleteTime }).ExecuteCommand();
if (del > 0) result.Code = 0;
else result.Msg = string.Format("删除失败!");
return Task.FromResult(result);
}
///
/// 团组模块 - 出入境国家费用标准 - Add Or Update
///
///
///
public async Task PostNationalTravelFeeOperate(NationalTravelFeeOperateDto dto)
{
Result result = new Result();
Grp_NationalTravelFee nationalTravelFee = _mapper.Map(dto);
nationalTravelFee.LastUpdateUserId = dto.UserId;
nationalTravelFee.CreateUserId = dto.UserId;
if (dto.PortType == 1) //web
{
var _nationalTravelFee = _sqlSugar.Storageable(nationalTravelFee).ToStorage();
if (dto.Id == 0) //新增
{
string selectSql = string.Format(@"Select * From Grp_NationalTravelFee
Where IsDel = 0
And Continent = '{0}'
And Country = '{1}'
And City = '{2}'", dto.Continent, dto.Country, dto.City);
Grp_NationalTravelFee nationalTravelFee1 = new Grp_NationalTravelFee();
nationalTravelFee1 = await _sqlSugar.SqlQueryable(selectSql).FirstAsync();
if (nationalTravelFee1 != null)
{
result.Msg = "该国家 城市已存在,请勿重复添加!";
return result;
}
_nationalTravelFee.AsInsertable.ExecuteCommand(); //不存在插入
}
else
{
_nationalTravelFee.AsUpdateable
.IgnoreColumns(it => new { it.CreateUserId, it.CreateTime, it.IsDel })
.WhereColumns(it => it.Id)
.ExecuteCommand(); //存在更新
}
result.Code = 0;
}
else if (dto.PortType == 2) //Android
{
}
else if (dto.PortType == 3) //Ios
{
}
else result.Msg = MsgTips.Port;
return result;
}
///
/// 一键清空
/// 删除主表数据 Update
/// 删除子表数据 delete
///
///
///
///
public async Task _OneClickClear(int id, int diId, int userId)
{
Result result = new Result() { Code = -1, Msg = "操作成功!" };
if (id < 1) return result = new Result() { Code = -1, Msg = "请传入有效的Id参数!" };
if (diId < 1) return result = new Result() { Code = -1, Msg = "请传入有效的Id参数!" };
if (userId < 1) return result = new Result() { Code = -1, Msg = "请传入有效的UserId参数!" };
BeginTran();
Grp_EnterExitCost _EnterExitCost = new Grp_EnterExitCost() {
Id = id,
IsDel = 1,
DeleteUserId = userId,
DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
//int update = await _sqlSugar.Updateable(_EnterExitCost).Where(it => it.Id == id).ExecuteCommandAsync();
int update = await _sqlSugar.Updateable(_EnterExitCost)
.UpdateColumns(it => new {
it.IsDel,
it.DeleteUserId ,
it.DeleteTime
})
.Where(it => it.Id == id).ExecuteCommandAsync();
if (update > 0) {
string sqlDelete = string.Format(@"Delete From Grp_DayAndCost Where Diid = {0} ", diId);
await ExecuteCommandAsync(sqlDelete);
result.Code = 0;
result.Msg = "操作成功!";
CommitTran();
}
else
{
result.Msg = "操作失败";
RollbackTran();
}
return result;
}
}
}