HotelDataRepository.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = "程序错误!" };
  75. }
  76. return result;
  77. }
  78. public async Task<Result> QueryHotelData(QueryHotelDataDto dto)
  79. {
  80. Result result = new Result() { Code = -2, Msg = "未知错误" };
  81. try
  82. {
  83. string sqlWhere = string.Empty;
  84. if (!string.IsNullOrWhiteSpace(dto.Name))
  85. {
  86. sqlWhere += string.Format(@" And Name like '%{0}%'", dto.Name);
  87. }
  88. if (!string.IsNullOrWhiteSpace(dto.City) && dto.City != "全部")
  89. {
  90. sqlWhere += string.Format(@" And City like '%{0}%'", dto.City);
  91. }
  92. if (!string.IsNullOrWhiteSpace(dto.Contact))
  93. {
  94. sqlWhere += string.Format(@" And Contact like '%{0}%'", dto.Contact);
  95. }
  96. if (!string.IsNullOrWhiteSpace(dto.ContactPhone))
  97. {
  98. sqlWhere += string.Format(@" And ContactPhone like '%{0}%'", dto.ContactPhone);
  99. }
  100. sqlWhere += string.Format(@" And IsDel={0}", 0);
  101. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  102. {
  103. Regex r = new Regex("And");
  104. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  105. }
  106. if (dto.PortType == 1)
  107. {
  108. string sql = string.Format(@"select * from Res_HotelData {0}", sqlWhere);
  109. List<Res_HotelData> HotelDataData = await _sqlSugar.SqlQueryable<Res_HotelData>(sql).ToListAsync();
  110. if (HotelDataData.Count == 0)
  111. {
  112. return result = new Result() { Code = -1, Msg = "暂无数据" };
  113. }
  114. HotelDataData = HotelDataData.OrderByDescending(x => x.CreateTime).ToList();
  115. if (dto.PageSize == 0 && dto.PageIndex == 0)
  116. {
  117. return result = new Result()
  118. {
  119. Code = 0,
  120. Msg = "查询成功",
  121. Data = HotelDataData,
  122. };
  123. }
  124. else
  125. {
  126. int count = HotelDataData.Count;
  127. float totalPage = (float)count / dto.PageSize;//总页数
  128. if (totalPage == 0) totalPage = 1;
  129. else totalPage = (int)Math.Ceiling((double)totalPage);
  130. List<Res_HotelData> _HotelData = new List<Res_HotelData>();
  131. for (int i = 0; i < dto.PageSize; i++)
  132. {
  133. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  134. if (RowIndex < HotelDataData.Count)
  135. {
  136. _HotelData.Add(HotelDataData[RowIndex]);
  137. }
  138. else
  139. {
  140. break;
  141. }
  142. }
  143. return result = new Result()
  144. {
  145. Code = 0,
  146. Msg = "查询成功",
  147. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _HotelData },
  148. };
  149. }
  150. }
  151. else if (dto.PortType == 2)
  152. {
  153. string sql = string.Format(@"select * from Res_HotelData {0}", sqlWhere);
  154. List<Res_HotelData> HotelDataData = await _sqlSugar.SqlQueryable<Res_HotelData>(sql).ToListAsync();
  155. if (HotelDataData.Count == 0)
  156. {
  157. return result = new Result() { Code = -1, Msg = "暂无数据" };
  158. }
  159. HotelDataData = HotelDataData.OrderByDescending(x => x.CreateTime).ToList();
  160. if (dto.PageSize == 0 && dto.PageIndex == 0)
  161. {
  162. return result = new Result()
  163. {
  164. Code = 0,
  165. Msg = "查询成功",
  166. Data = HotelDataData,
  167. };
  168. }
  169. else
  170. {
  171. int count = HotelDataData.Count;
  172. float totalPage = (float)count / dto.PageSize;//总页数
  173. if (totalPage == 0) totalPage = 1;
  174. else totalPage = (int)Math.Ceiling((double)totalPage);
  175. List<Res_HotelData> _HotelData = new List<Res_HotelData>();
  176. for (int i = 0; i < dto.PageSize; i++)
  177. {
  178. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  179. if (RowIndex < HotelDataData.Count)
  180. {
  181. _HotelData.Add(HotelDataData[RowIndex]);
  182. }
  183. else
  184. {
  185. break;
  186. }
  187. }
  188. return result = new Result()
  189. {
  190. Code = 0,
  191. Msg = "查询成功",
  192. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _HotelData },
  193. };
  194. }
  195. }
  196. else
  197. {
  198. return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
  199. }
  200. }
  201. catch (Exception)
  202. {
  203. return result;
  204. throw;
  205. }
  206. }
  207. }
  208. }