HotelDataRepository.cs 9.1 KB

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