HotelDataRepository.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using AutoMapper;
  2. using OASystem.Domain;
  3. using OASystem.Domain.Dtos.Groups;
  4. using OASystem.Domain.Dtos.Resource;
  5. using OASystem.Domain.Entities.Resource;
  6. using OASystem.Domain.ViewModels.Resource;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace OASystem.Infrastructure.Repositories.Resource
  13. {
  14. public class HotelDataRepository:BaseRepository<Res_HotelData,HotelDataView>
  15. {
  16. private readonly IMapper _mapper;
  17. public HotelDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  18. {
  19. _mapper= mapper;
  20. }
  21. public async Task<Result> OperationHotelData(OperationHotelDto dto)
  22. {
  23. Result result = new Result() { Code = -2, Msg = "未知错误" };
  24. try
  25. {
  26. if (dto.Status == 1)//添加
  27. {
  28. string selectSql = string.Format(@"select * from Res_HotelData where Name='{0}' and IsDel='{1}'"
  29. , dto.Name,0);
  30. var HotelData = await _sqlSugar.SqlQueryable<Res_HotelData>(selectSql).FirstAsync();//查询是否存在
  31. if (HotelData != null)
  32. {
  33. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  34. }
  35. else//不存在,可添加
  36. {
  37. Res_HotelData _HotelDataDto = _mapper.Map<Res_HotelData>(dto);
  38. int id = await AddAsyncReturnId(_HotelDataDto);
  39. if (id == 0)
  40. {
  41. return result = new Result() { Code = -1, Msg = "添加失败!" };
  42. }
  43. return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  44. }
  45. }
  46. else if (dto.Status == 2)//修改
  47. {
  48. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_HotelData
  49. {
  50. City = dto.City,
  51. Name = dto.Name,
  52. Level = dto.Level,
  53. Address = dto.Address,
  54. Tel = dto.Tel,
  55. Fax = dto.Fax,
  56. Contact = dto.Contact,
  57. ContactPhone = dto.ContactPhone,
  58. OtherInformation = dto.OtherInformation,
  59. Remark = dto.Remark,
  60. });
  61. if (!res)
  62. {
  63. return result = new Result() { Code = -1, Msg = "修改失败!" };
  64. }
  65. return result = new Result() { Code = 0, Msg = "修改成功!" };
  66. }
  67. else
  68. {
  69. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. return result = new Result() { Code = -2, Msg = ex.Message };
  75. }
  76. }
  77. public async Task<Result> QueryHotelData(QueryHotelDataDto dto)
  78. {
  79. Result result = new Result() { Code = -2, Msg = "未知错误" };
  80. try
  81. {
  82. string sqlWhere = string.Empty;
  83. if (!string.IsNullOrWhiteSpace(dto.Name))
  84. {
  85. sqlWhere += string.Format(@" And Name like '%{0}%'", dto.Name);
  86. }
  87. if (!string.IsNullOrWhiteSpace(dto.City) && dto.City != "全部")
  88. {
  89. sqlWhere += string.Format(@" And City like '%{0}%'", dto.City);
  90. }
  91. if (!string.IsNullOrWhiteSpace(dto.Contact))
  92. {
  93. sqlWhere += string.Format(@" And Contact like '%{0}%'", dto.Contact);
  94. }
  95. if (!string.IsNullOrWhiteSpace(dto.ContactPhone))
  96. {
  97. sqlWhere += string.Format(@" And ContactPhone like '%{0}%'", dto.ContactPhone);
  98. }
  99. sqlWhere += string.Format(@" And IsDel={0}", 0);
  100. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  101. {
  102. Regex r = new Regex("And");
  103. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  104. }
  105. if (dto.PortType == 1)
  106. {
  107. string sql = string.Format(@"select * from Res_HotelData {0}", sqlWhere);
  108. List<Res_HotelData> HotelDataData = await _sqlSugar.SqlQueryable<Res_HotelData>(sql).ToListAsync();
  109. if (HotelDataData.Count == 0)
  110. {
  111. return result = new Result() { Code = -1, Msg = "暂无数据" };
  112. }
  113. HotelDataData = HotelDataData.OrderByDescending(x => x.CreateTime).ToList();
  114. if (dto.PageSize == 0 && dto.PageIndex == 0)
  115. {
  116. return result = new Result()
  117. {
  118. Code = 0,
  119. Msg = "查询成功",
  120. Data = HotelDataData,
  121. };
  122. }
  123. else
  124. {
  125. int count = HotelDataData.Count;
  126. float totalPage = (float)count / dto.PageSize;//总页数
  127. if (totalPage == 0) totalPage = 1;
  128. else totalPage = (int)Math.Ceiling((double)totalPage);
  129. List<Res_HotelData> _HotelData = new List<Res_HotelData>();
  130. for (int i = 0; i < dto.PageSize; i++)
  131. {
  132. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  133. if (RowIndex < HotelDataData.Count)
  134. {
  135. _HotelData.Add(HotelDataData[RowIndex]);
  136. }
  137. else
  138. {
  139. break;
  140. }
  141. }
  142. return result = new Result()
  143. {
  144. Code = 0,
  145. Msg = "查询成功",
  146. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _HotelData },
  147. };
  148. }
  149. }
  150. else if (dto.PortType == 2)
  151. {
  152. string sql = string.Format(@"select * from Res_HotelData {0}", sqlWhere);
  153. List<Res_HotelData> HotelDataData = await _sqlSugar.SqlQueryable<Res_HotelData>(sql).ToListAsync();
  154. if (HotelDataData.Count == 0)
  155. {
  156. return result = new Result() { Code = -1, Msg = "暂无数据" };
  157. }
  158. HotelDataData = HotelDataData.OrderByDescending(x => x.CreateTime).ToList();
  159. if (dto.PageSize == 0 && dto.PageIndex == 0)
  160. {
  161. return result = new Result()
  162. {
  163. Code = 0,
  164. Msg = "查询成功",
  165. Data = HotelDataData,
  166. };
  167. }
  168. else
  169. {
  170. int count = HotelDataData.Count;
  171. float totalPage = (float)count / dto.PageSize;//总页数
  172. if (totalPage == 0) totalPage = 1;
  173. else totalPage = (int)Math.Ceiling((double)totalPage);
  174. List<Res_HotelData> _HotelData = new List<Res_HotelData>();
  175. for (int i = 0; i < dto.PageSize; i++)
  176. {
  177. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  178. if (RowIndex < HotelDataData.Count)
  179. {
  180. _HotelData.Add(HotelDataData[RowIndex]);
  181. }
  182. else
  183. {
  184. break;
  185. }
  186. }
  187. return result = new Result()
  188. {
  189. Code = 0,
  190. Msg = "查询成功",
  191. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _HotelData },
  192. };
  193. }
  194. }
  195. else
  196. {
  197. return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
  198. }
  199. }
  200. catch (Exception)
  201. {
  202. return result;
  203. throw;
  204. }
  205. }
  206. }
  207. }