using OASystem.Domain.Entities.Resource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using OASystem.Domain.Dtos.Resource;
using EyeSoft.Extensions;
using OASystem.Domain;
using OASystem.Domain.ViewModels.Resource;
using OASystem.Domain.AesEncryption;
namespace OASystem.Infrastructure.Repositories.Resource
{
///
/// 策划部供应商资料
/// 仓储
///
public class MediaSuppliersRepository : BaseRepository
{
private readonly IMapper _mapper;
private readonly List _portIds;
public MediaSuppliersRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
{
_mapper = mapper;
_portIds = new List { 1, 2, 3 };
}
///
/// 初始化数据
///
///
public async Task Init()
{
var result = new JsonView() { Code = 400, Msg = "操作失败" };
var typeData = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && x.STid == 21)
.Select(x => new { x.Id, Text = x.Name })
.ToListAsync();
result.Code = 200;
result.Data = typeData;
return result;
}
///
/// 详情
///
///
///
public async Task Info(MediaSupplierInfoDto dto)
{
var result = new JsonView() { Code = 400, Msg = "操作失败" };
if (!_portIds.Contains(dto.PortType)) { result.Msg = MsgTips.Port; return result; }
if (dto.Id < 1) { result.Msg = MsgTips.Id; return result; }
var info = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && x.Id == dto.Id)
.Select(x => new MediaSupplierInfoView()
{
Id = x.Id,
TypeId = x.TypeId,
Privince = x.Privince,
City = x.City,
UnitName = x.UnitName,
UnitAbbreviation = x.UnitAbbreviation,
UnitAddress = x.UnitAddress,
Contact = x.Contact,
Sex = x.Sex,
Post = x.Post,
Fax = x.Fax,
Tel = x.Tel,
Email = x.Email,
WeChat = x.WeChat,
Remark = x.Remark
})
.FirstAsync();
EncryptionProcessor.DecryptProperties(info);
result.Code = 200;
result.Data = info;
result.Msg = MsgTips.Succeed;
return result;
}
///
/// 分页查询
///
///
///
public async Task PageItem(MediaSupplierPageItemDto dto)
{
var result = new JsonView() { Code = 400, Msg = "操作失败" };
if (!_portIds.Contains(dto.PortType)) { result.Msg = MsgTips.Port; return result; }
var typeLabel = new List();
if (!string.IsNullOrEmpty(dto.TypeLabel))
{
typeLabel = dto.TypeLabel.Split(',')
.Select(s =>
{
int value = 0;
if (int.TryParse(s, out value)) return value;
return value;
})
.Where(i => i != 0)
.ToList();
}
var search = AesEncryptionHelper.Encrypt(dto.Search);
RefAsync total = 0;
var infos = await _sqlSugar.Queryable()
.LeftJoin((ms, u) => ms.CreateUserId == u.Id)
.Where((ms, u) => ms.IsDel == 0)
.WhereIF(typeLabel.Count > 0 , (ms, u) => typeLabel.Contains(ms.TypeId))
.WhereIF(!string.IsNullOrEmpty(search), (ms, u) => ms.UnitName.Contains(search) || ms.Contact.Contains(search) || ms.Tel.Contains(search))
.OrderByDescending((ms, u) => ms.CreateTime)
.Select((ms, u) => new PageItemView()
{
Id = ms.Id,
Privince = ms.Privince,
City = ms.City,
UnitName = ms.UnitName,
Contact = ms.Contact,
Sex = ms.Sex,
Post = ms.Post,
Tel = ms.Tel,
CreateUserName = u.CnName,
CreateTime = ms.CreateTime
})
.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
foreach (var info in infos) EncryptionProcessor.DecryptProperties(info);
result.Msg = MsgTips.Succeed;
result.Code = 200;
result.Data = infos;
result.Count = total;
return result;
}
///
/// 操作(添加 Or 编辑)
///
///
///
public async Task AddOrEdit(MediaSupplierAddOrEditDto dto)
{
var result = new JsonView() { Code = 400, Msg = "操作失败" };
if (!_portIds.Contains(dto.PortType)) { result.Msg = MsgTips.Port; return result; }
var userId = dto.CurrUserId;
if (userId < 1) { result.Msg = MsgTips.UserId; return result; }
var info = _mapper.Map(dto);
info.CreateUserId = userId;
EncryptionProcessor.DecryptProperties(info);
if (info.Id < 1) //添加
{
var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
if (add < 1) return result;
}
else //编辑
{
var upd = await _sqlSugar.Updateable(info).IgnoreColumns(x => new { x.CreateUserId, x.CreateTime }).ExecuteCommandAsync();
if (upd < 1) return result;
}
result.Code = 200;
result.Msg = $"操作成功!";
return result;
}
///
/// 软删除
///
///
///
public async Task SoftDel(MediaSupplierSoftDelDto dto)
{
var result = new JsonView() { Code = 400, Msg = "操作失败" };
int userId = dto.CurrUserId, id = dto.Id;
if (!_portIds.Contains(dto.PortType)) { result.Msg = MsgTips.Port; return result; }
if (userId < 1) { result.Msg = MsgTips.UserId; return result; }
if (id < 1) { result.Msg = MsgTips.Id; return result; }
var status = await _sqlSugar.Updateable()
.SetColumns(x => new Res_MediaSuppliers() { IsDel = 1, DeleteUserId = userId, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") })
.Where(x => x.Id == id)
.ExecuteCommandAsync();
if (status > 0)
{
result.Code = 200;
result.Msg = $"操作成功!";
}
return result;
}
}
}