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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Groups
{
///
/// 餐厅信息 仓储
///
public class RestaurantRepository : BaseRepository
{
private readonly IMapper _mapper;
private readonly JsonView _jsonView;
public RestaurantRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
{
_mapper = mapper;
_jsonView = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败" };
}
///
/// 基础数据源
///
///
public async Task InitAsync()
{
var groupData = await _sqlSugar
.Queryable()
.Where(x => x.IsDel == 0)
.OrderByDescending(x => x.Id)
.Select(x => new { Id = x.Id, GroupName = x.TeamName })
.ToListAsync();
var mealTypeData = new List() {
new { Id = 1, Name = "早餐", DefaultStartTime = "08:00" },
new { Id = 2, Name = "午餐", DefaultStartTime = "12:00" },
new { Id = 3, Name = "晚餐", DefaultStartTime = "18:00" }
};
_jsonView.Code = StatusCodes.Status200OK;
_jsonView.Msg = "操作成功!";
_jsonView.Data = new { groupData = groupData ,mealType = mealTypeData };
return _jsonView;
}
///
/// 详情
///
///
public async Task InfoAsync(int portType, int id)
{
if (!SharingStaticData.PortTypes.Contains(portType)) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "端口类型错误!" };
if (id < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.Id };
var info = await _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && x.Id == id)
.FirstAsync();
if (info == null) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "数据未填写!", Data = new { } };
var resData = _mapper.Map(info);
return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = resData };
}
///
/// Item
///
///
public async Task ItemAsync(int portType, int groupId)
{
if (!SharingStaticData.PortTypes.Contains(portType)) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "端口类型错误!" };
if (groupId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.DiId };
var sql = string.Format(@" SELECT
ri.Id,
ri.GroupId,
ri.Date,
ri.StartTime,
ri.EndTime,
CASE WHEN ri.Type = 1 THEN '早餐'
WHEN ri.Type = 2 THEN '午餐'
WHEN ri.Type = 3 THEN '晚餐'
ELSE '其他'
END AS 'Type',
ri.Name,
ri.Address,
ri.Tel,
ri.Remark,
ri.CreateTime,
u.CnName AS CreateUserName
FROM
Grp_RestaurantInfo ri
LEFT JOIN Sys_Users u ON ri.CreateUserId = u.Id
WHERE
ri.IsDel = 0
AND ri.GroupId = {0}
ORDER BY
ri.Date ASC
", groupId);
var infos = await _sqlSugar.SqlQueryable(sql).ToListAsync();
if (!infos.Any()) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "数据未填写!",Data = Array.Empty() };
return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = infos };
}
///
/// 操作
///
///
public async Task OpAsync(RestaurantOpDto dto)
{
int status = dto.Status;
var info = _mapper.Map(dto);
info.CreateUserId = dto.CurrUserId;
if (status == 1)
{
//验重
var isNull = await _sqlSugar.Queryable().Where(x => x.IsDel == 0 && x.GroupId == info.GroupId && x.Name.Equals(info.Name)).FirstAsync();
if (isNull != null) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "该团组下该餐厅名称已存在,不可重复添加!", Data = new { } };
var infoId = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
if (infoId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "添加失败!", Data = new { } };
}
else if (status == 2)
{
if (dto.Id < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.Id, Data = new { } };
var infoId = await _sqlSugar.Updateable(info).IgnoreColumns(x => new { x.DeleteTime, x.DeleteUserId, x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();
if (infoId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "修改失败!", Data = new { } };
}
else new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "请传入有效的Status!", Data = new { } };
return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = new { } };
}
///
/// 操作
///
///
public async Task DelAsync(RestaurantDelDto dto)
{
int userId = dto.DeleteUserId;
var id = dto.Id;
if (userId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.UserId, Data = new { } };
if (id < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.Id, Data = new { } };
var del = await _sqlSugar.Updateable()
.SetColumns(x => new Grp_RestaurantInfo() { DeleteUserId = userId, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), IsDel = 1 })
.Where(x => x.Id == id)
.ExecuteCommandAsync();
if(del < 1) return new JsonView() { Code = StatusCodes.Status200OK, Msg = "删除失败!", Data = new { } };
return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = new { } };
}
}
}