using Aspose.Words.Lists;
using AutoMapper;
using OASystem.Domain;
using OASystem.Domain.AesEncryption;
using OASystem.Domain.Dtos.Resource;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.Entities.Resource;
using OASystem.Domain.ViewModels.Resource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Resource
{
///
/// 境外用车信息 仓储
///
public class OverseaVehicleRepository : BaseRepository
{
private readonly IMapper _mapper;
public OverseaVehicleRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
{
_mapper = mapper;
}
///
/// Init
///
///
///
public async Task InitAsync()
{
var currencyDatas = await _sqlSugar.Queryable()
.Where(x => x.STid == 66 && x.IsDel == 0)
.Select(x =>
new
{
x.Id,
x.Name
})
.ToListAsync();
var serviceTypeDatas = EnumHelper.ToSelectList(false);
var init = new {
currencyDatas,
serviceTypeDatas
};
return new JsonView(init, 1, "操作成功!");
}
///
/// List
///
///
///
public async Task ListAsync(OverseaVehicleListDto dto)
{
var search = dto.Search.Trim();
RefAsync total = 0;
var infos = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0)
.WhereIF(!string.IsNullOrEmpty(search), x => x.ContinentName.Contains(search) || x.CountryName.Contains(search) || x.CityName.Contains(search))
.OrderByDescending(x => x.CreateTime)
.Select(x => new OverseaVehicleListView() {
Id = x.Id,
ContinentName = x.ContinentName,
CountryName = x.CountryName,
CityName = x.CityName,
GuidePrice = x.GuidePrice,
OtherPrice1 = x.OtherPrice1,
OtherPrice2 = x.OtherPrice2,
Currency = x.Currency.ToString(),
LastUpdateUserName = x.LastUpdateUserId.ToString(),
LastUpdateTime = x.LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss"),
Remark = x.Remark
})
.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
if (total == 0)
{
return new JsonView(infos, total, "暂无数据!");
}
foreach (var item in infos)
{
if (int.TryParse(item.Currency,out int currencyId))
{
item.Currency = _sqlSugar.Queryable().Where(y => y.Id == currencyId).First()?.Name ?? "-";
}
if (int.TryParse(item.LastUpdateUserName, out int lastUpdateUserId))
{
item.LastUpdateUserName = _sqlSugar.Queryable().Where(y => y.Id == lastUpdateUserId).First()?.CnName ?? "-";
}
item.CarInfos = _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && x.OvId == item.Id)
.Select(x => new OverseaVehicleTypeListView() {
Id = x.Id,
CarTypeName = x.CarTypeName,
ServiceType = x.ServiceType,
Price = x.Price,
Remark = x.Remark
})
.ToList();
}
return new JsonView(infos, total, "操作成功!");
}
///
/// 详细信息
///
///
///
public async Task InfoAsync(int id)
{
if (id < 1) return new JsonView(false, "请传入有效的ID!");
var info = await _sqlSugar.Queryable()
.Where(it => it.Id == id && it.IsDel == 0)
.FirstAsync();
if (info == null) return new JsonView(false, "暂无数据!");
var subInfos = await _sqlSugar.Queryable()
.Where(it => it.OvId == id && it.IsDel == 0)
.ToListAsync();
var view = _mapper.Map(info);
view.CarInfos = _mapper.Map>(subInfos);
return new JsonView(view, 1, "操作成功!");
}
///
/// 添加
///
///
///
public async Task AddAsync(OverseaVehicleAddOrEditDto dto)
{
//参数处理
var now = DateTime.Now;
var info = _mapper.Map(dto);
info.CreateUserId = dto.CurrUserId;
info.LastUpdateUserId = dto.CurrUserId;
info.LastUpdateTime = now;
var subInfos = _mapper.Map>(dto.CarInfos);
if (subInfos == null && subInfos.Count < 1)
{
return new JsonView(false, $"请填写车型信息!");
}
_sqlSugar.BeginTran();
try
{
var infoIsNul = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0)
.Where(x => x.ContinentName.Equals(dto.ContinentName.Trim()))
.Where(x => x.CountryName.Equals(dto.CountryName.Trim()))
.Where(x => x.CityName.Equals(dto.CityName.Trim()))
.AnyAsync();
if (infoIsNul)
{
return new JsonView(false, $"该城市已存在,不可重复添加!");
}
var infoId = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
if (infoId < 1)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"车信息,操作失败!");
}
foreach (var subInfo in subInfos) {
subInfo.OvId = infoId;
subInfo.CreateUserId = dto.CurrUserId;
subInfo.LastUpdateUserId = dto.CurrUserId;
subInfo.LastUpdateTime = DateTime.Now;
}
var subCount = await _sqlSugar.Insertable(subInfos).ExecuteCommandAsync();
if (subCount < 1)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"车型信息,操作失败!");
}
}
catch (Exception ex)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"操作失败!ex:{ex.Message}");
}
_sqlSugar.CommitTran();
return new JsonView(true, "操作成功!");
}
///
/// 编辑
///
///
///
public async Task EditAsync(OverseaVehicleAddOrEditDto dto)
{
//参数处理
var now = DateTime.Now;
var info = _mapper.Map(dto);
info.LastUpdateUserId = dto.CurrUserId;
info.LastUpdateTime = now;
var subInfos = _mapper.Map>(dto.CarInfos);
if (subInfos == null && subInfos.Count < 1)
{
return new JsonView(false, $"请填写车型信息!");
}
_sqlSugar.BeginTran();
try
{
var infoId = await _sqlSugar.Updateable(info)
.UpdateColumns(it => new
{
it.ContinentName,
it.CountryName,
it.CityName,
it.GuidePrice,
it.OtherPrice1,
it.OtherPrice2,
it.Currency,
it.LastUpdateUserId,
it.LastUpdateTime,
it.Remark
})
.Where(it => it.Id == info.Id)
.ExecuteCommandAsync();
if (infoId < 1)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"车信息,操作失败!");
}
foreach (var subInfo in subInfos)
{
subInfo.LastUpdateUserId = dto.CurrUserId;
subInfo.LastUpdateTime = DateTime.Now;
}
var subCount = await _sqlSugar.Updateable(subInfos)
.UpdateColumns(it => new
{
it.CarTypeName,
it.ServiceType,
it.Price,
it.LastUpdateUserId,
it.LastUpdateTime,
it.Remark
})
.WhereColumns (it => new { it.Id })
.ExecuteCommandAsync();
if (subCount < 1)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"车型信息,操作失败!");
}
}
catch (Exception ex)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"操作失败!ex:{ex.Message}");
}
_sqlSugar.CommitTran();
return new JsonView(true, "操作成功!");
}
///
/// 删除
///
///
///
public async Task DelAsync(OverseaVehicleDelDto dto)
{
//参数处理
var now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
_sqlSugar.BeginTran();
try
{
var infoId = await _sqlSugar.Updateable()
.SetColumns(x => x.IsDel == 1)
.SetColumns(x => x.DeleteUserId == dto.CurrUserId)
.SetColumns(x => x.DeleteTime == now)
.Where(it => it.Id == dto.Id)
.ExecuteCommandAsync();
if (infoId < 1)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"车信息,操作失败!");
}
var subCount = await _sqlSugar.Updateable()
.SetColumns(x => x.IsDel == 1)
.SetColumns(x => x.DeleteUserId == dto.CurrUserId)
.SetColumns(x => x.DeleteTime == now)
.Where(it => it.OvId == dto.Id)
.ExecuteCommandAsync();
if (subCount < 1)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"车型信息,操作失败!");
}
}
catch (Exception ex)
{
_sqlSugar.RollbackTran();
return new JsonView(false, $"操作失败!ex:{ex.Message}");
}
_sqlSugar.CommitTran();
return new JsonView(true, "操作成功!");
}
}
}