using AutoMapper;
using OASystem.Domain.AesEncryption;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.Groups;
using OASystem.Infrastructure.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Groups
{
///
/// 签证提成录入
/// 仓储
///
public class VisaCommissionRepository : BaseRepository
{
private readonly IMapper _mapper;
private readonly DelegationInfoRepository _groupRep;
public VisaCommissionRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep)
: base(sqlSugar)
{
_mapper = mapper;
_groupRep = groupRep;
}
///
/// Init
///
///
public async Task Init()
{
var groupInfo = await _groupRep.GetGroupNameList(new Domain.Dtos.Groups.GroupNameDto() { PortType = 1 });
return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = new { GroupData = groupInfo.Data } };
}
///
/// Item
///
///
public async Task Item(VisaCommissionItemDto _dto)
{
var data = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && x.DiId == _dto.DiId && x.UId == _dto.CurrUserId)
.Select(x => new
{
x.Id,
x.Country,
x.Quantity,
x.Remark
})
.ToListAsync();
var groupInfo = await _sqlSugar.Queryable().Where(x => x.IsDel == 0 && x.Id == _dto.DiId).FirstAsync();
if (groupInfo != null)
{
string[] countryArray = _groupRep.GroupSplitCountry(groupInfo.VisitCountry).ToArray();
foreach (string country in countryArray)
{
if (data.Find(x => country.Equals(x.Country)) == null)
{
data.Add(new
{
Id = 0,
Country = country,
Quantity = 0,
Remark = ""
});
}
}
}
return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = data };
}
///
/// Save
///
///
public async Task Save(VisaCommissionSaveDto _dto)
{
var data = _mapper.Map>(_dto.Items);
for (int i = 0; i < data.Count; i++)
{
data[i].CreateUserId = _dto.Items.FirstOrDefault()?.CurrUserId ?? 0;
data[i].UId = _dto.Items.FirstOrDefault()?.CurrUserId ?? 0;
}
//添加数据
var addData = data.Where(x => x.Id < 1).ToList();
//修改数据
var updData = data.Where(x => x.Id > 0).ToList();
_sqlSugar.BeginTran();
bool status = false;
if (addData.Count > 0) {
var addCount = await _sqlSugar.Insertable(addData).ExecuteCommandAsync();
if (addCount > 0) status=true;
}
if (updData.Count > 0)
{
var updCount = await _sqlSugar.Updateable(updData)
.UpdateColumns(x => new
{
x.Country,
x.Quantity,
x.Remark
})
.WhereColumns(x => x.Id)
.ExecuteCommandAsync();
if (updCount > 0) status = true;
}
var jv = new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!" };
if (status) _sqlSugar.CommitTran();
else
{
_sqlSugar.RollbackTran();
jv.Code = StatusCodes.Status400BadRequest;
jv.Msg = "操作失败";
}
return jv;
}
///
/// Create
///
///
public async Task Create(VisaCommissionCreateDto _dto)
{
var _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
//添加查重
var info = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && x.DiId == _dto.DiId && x.Country.Equals(_dto.Country))
.FirstAsync();
//if (info != null)
//{
// _view.Code = StatusCodes.Status400BadRequest;
// _view.Msg = "该数据已存在请勿重新添加!";
// return _view;
//}
var createInfo = _mapper.Map(_dto);
createInfo.CreateUserId = _dto.CurrUserId;
createInfo.UId = _dto.CurrUserId;
var createStatus = await _sqlSugar.Insertable(createInfo)
.ExecuteCommandAsync();
if (createStatus > 0)
{
_view.Code = StatusCodes.Status200OK;
_view.Msg = "操作成功!";
return _view;
}
return _view;
}
///
/// Edit
///
///
///
///
public async Task Edit(int id,VisaCommissionCreateDto _dto)
{
var _view = new JsonView() { Code = StatusCodes.Status204NoContent, Msg = "操作失败!" };
var editInfo = _mapper.Map(_dto);
editInfo.CreateUserId = _dto.CurrUserId;
editInfo.UId = _dto.CurrUserId;
var editStatus = await _sqlSugar.Updateable(editInfo)
.UpdateColumns(x => new
{
x.Country,
x.Quantity,
x.Remark
})
.Where(x => x.Id == id)
.ExecuteCommandAsync();
if (editStatus > 0)
{
_view.Code = StatusCodes.Status200OK;
_view.Msg = "操作成功!";
return _view;
}
return _view;
}
///
/// Del
///
///
///
///
public async Task Del(int id,int currUserId)
{
var _view = new JsonView() { Code = StatusCodes.Status204NoContent, Msg = "操作失败!" };
var editStatus = await _sqlSugar.Updateable()
.SetColumns(x => new Grp_VisaCommission
{
DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
DeleteUserId = currUserId,
})
.Where(x => x.Id == id)
.ExecuteCommandAsync();
if (editStatus > 0)
{
_view.Code = StatusCodes.Status200OK;
_view.Msg = "操作成功!";
return _view;
}
return _view;
}
}
}