RestaurantRepository.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using AutoMapper;
  2. using NPOI.SS.Formula.Functions;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.Groups;
  5. using OASystem.Domain.Dtos.System;
  6. using OASystem.Domain.Entities.Groups;
  7. using OASystem.Domain.ViewModels.Groups;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace OASystem.Infrastructure.Repositories.Groups
  14. {
  15. /// <summary>
  16. /// 餐厅信息 仓储
  17. /// </summary>
  18. public class RestaurantRepository : BaseRepository<Grp_RestaurantInfo, Grp_RestaurantInfo>
  19. {
  20. private readonly IMapper _mapper;
  21. private readonly JsonView _jsonView;
  22. public RestaurantRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  23. {
  24. _mapper = mapper;
  25. _jsonView = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败" };
  26. }
  27. /// <summary>
  28. /// 基础数据源
  29. /// </summary>
  30. /// <returns></returns>
  31. public async Task<JsonView> InitAsync()
  32. {
  33. var groupData = await _sqlSugar
  34. .Queryable<Grp_DelegationInfo>()
  35. .Where(x => x.IsDel == 0)
  36. .OrderByDescending(x => x.Id)
  37. .Select(x => new { Id = x.Id, GroupName = x.TeamName })
  38. .ToListAsync();
  39. var mealTypeData = new List<dynamic>() {
  40. new { Id = 1, Name = "早餐", DefaultStartTime = "08:00" },
  41. new { Id = 2, Name = "午餐", DefaultStartTime = "12:00" },
  42. new { Id = 3, Name = "晚餐", DefaultStartTime = "18:00" }
  43. };
  44. _jsonView.Code = StatusCodes.Status200OK;
  45. _jsonView.Msg = "操作成功!";
  46. _jsonView.Data = new { groupData = groupData ,mealType = mealTypeData };
  47. return _jsonView;
  48. }
  49. /// <summary>
  50. /// 详情
  51. /// </summary>
  52. /// <returns></returns>
  53. public async Task<JsonView> InfoAsync(int portType, int id)
  54. {
  55. if (!SharingStaticData.PortTypes.Contains(portType)) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "端口类型错误!" };
  56. if (id < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.Id };
  57. var info = await _sqlSugar.Queryable<Grp_RestaurantInfo>()
  58. .Where(x => x.IsDel == 0 && x.Id == id)
  59. .FirstAsync();
  60. if (info == null) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "数据未填写!", Data = new { } };
  61. var resData = _mapper.Map<RestaurantInfoView>(info);
  62. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = resData };
  63. }
  64. /// <summary>
  65. /// Item
  66. /// </summary>
  67. /// <returns></returns>
  68. public async Task<JsonView> ItemAsync(int portType, int groupId)
  69. {
  70. if (!SharingStaticData.PortTypes.Contains(portType)) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "端口类型错误!" };
  71. if (groupId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.DiId };
  72. var sql = string.Format(@" SELECT
  73. ri.Id,
  74. ri.GroupId,
  75. ri.Date,
  76. ri.StartTime,
  77. ri.EndTime,
  78. CASE WHEN ri.Type = 1 THEN '早餐'
  79. WHEN ri.Type = 2 THEN '午餐'
  80. WHEN ri.Type = 3 THEN '晚餐'
  81. ELSE '其他'
  82. END AS 'Type',
  83. ri.Name,
  84. ri.Address,
  85. ri.Tel,
  86. ri.Remark,
  87. ri.CreateTime,
  88. u.CnName AS CreateUserName
  89. FROM
  90. Grp_RestaurantInfo ri
  91. LEFT JOIN Sys_Users u ON ri.CreateUserId = u.Id
  92. WHERE
  93. ri.IsDel = 0
  94. AND ri.GroupId = {0}
  95. ORDER BY
  96. ri.Date ASC
  97. ", groupId);
  98. var infos = await _sqlSugar.SqlQueryable<RestaurantItemView>(sql).ToListAsync();
  99. if (!infos.Any()) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "数据未填写!",Data = Array.Empty<dynamic>() };
  100. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = infos };
  101. }
  102. /// <summary>
  103. /// 操作
  104. /// </summary>
  105. /// <returns></returns>
  106. public async Task<JsonView> OpAsync(RestaurantOpDto dto)
  107. {
  108. int status = dto.Status;
  109. var info = _mapper.Map<Grp_RestaurantInfo>(dto);
  110. info.CreateUserId = dto.CurrUserId;
  111. if (status == 1)
  112. {
  113. //验重
  114. var isNull = await _sqlSugar.Queryable<Grp_RestaurantInfo>().Where(x => x.IsDel == 0 && x.GroupId == info.GroupId && x.Name.Equals(info.Name)).FirstAsync();
  115. if (isNull != null) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "该团组下该餐厅名称已存在,不可重复添加!", Data = new { } };
  116. var infoId = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  117. if (infoId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "添加失败!", Data = new { } };
  118. }
  119. else if (status == 2)
  120. {
  121. if (dto.Id < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.Id, Data = new { } };
  122. var infoId = await _sqlSugar.Updateable(info).IgnoreColumns(x => new { x.DeleteTime, x.DeleteUserId, x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();
  123. if (infoId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "修改失败!", Data = new { } };
  124. }
  125. else new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "请传入有效的Status!", Data = new { } };
  126. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = new { } };
  127. }
  128. /// <summary>
  129. /// 操作
  130. /// </summary>
  131. /// <returns></returns>
  132. public async Task<JsonView> DelAsync(RestaurantDelDto dto)
  133. {
  134. int userId = dto.DeleteUserId;
  135. var id = dto.Id;
  136. if (userId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.UserId, Data = new { } };
  137. if (id < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.Id, Data = new { } };
  138. var del = await _sqlSugar.Updateable<Grp_RestaurantInfo>()
  139. .SetColumns(x => new Grp_RestaurantInfo() { DeleteUserId = userId, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), IsDel = 1 })
  140. .Where(x => x.Id == id)
  141. .ExecuteCommandAsync();
  142. if(del < 1) return new JsonView() { Code = StatusCodes.Status200OK, Msg = "删除失败!", Data = new { } };
  143. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = new { } };
  144. }
  145. }
  146. }