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; 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 { x.Id, x.TypeId, x.Privince, x.City, x.UnitName, x.UnitAbbreviation, x.UnitAddress, x.Contact, x.Sex, x.Post, x.Fax, x.Tel, x.Email, x.WeChat, x.Remark }) .FirstAsync(); 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 = 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 { ms.Id, ms.Privince, ms.City, ms.UnitName, ms.Contact, ms.Sex, ms.Post, ms.Tel, CreateUserName = u.CnName, ms.CreateTime }) .ToPageListAsync(dto.PageIndex, dto.PageSize, total); 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; 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; } } }