LocalGuideDataRepository.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using AutoMapper;
  2. using OASystem.Domain;
  3. using OASystem.Domain.Dtos.Resource;
  4. using OASystem.Domain.Entities.Groups;
  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 LocalGuideDataRepository : BaseRepository<Res_LocalGuideData, LocalGuideDataView>
  15. {
  16. private readonly IMapper _mapper;
  17. public LocalGuideDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  18. {
  19. _mapper=mapper;
  20. }
  21. public async Task<Result> LocalGuideOperation(LocalGuideOperationDto 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_LocalGuideData where UnitArea='{0}' and UnitName='{1}' and Contact='{2}' and ContactTel='{3}'"
  29. , dto.UnitArea, dto.UnitName, dto.Contact, dto.ContactTel);
  30. var LocalGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(selectSql).FirstAsync();//查询是否存在
  31. if (LocalGuideData != null)
  32. {
  33. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  34. }
  35. else//不存在,可添加
  36. {
  37. Res_LocalGuideData _LocalGuideDat = _mapper.Map<Res_LocalGuideData>(dto);
  38. int id = await AddAsyncReturnId(_LocalGuideDat);
  39. if (id == 0)
  40. {
  41. return result = new Result() { Code = -1, Msg = "添加失败!" };
  42. }
  43. 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_LocalGuideData
  49. {
  50. UnitArea = dto.UnitArea,
  51. UnitName = dto.UnitName,
  52. Address = dto.Address,
  53. Contact = dto.Contact,
  54. ContactTel = dto.ContactTel,
  55. ContactEmail = dto.ContactEmail,
  56. ContactFax = dto.ContactFax,
  57. OtherInfo = dto.OtherInfo,
  58. Score = dto.Score,
  59. SuitScore = dto.SuitScore,
  60. ServeScore = dto.ServeScore,
  61. TalkProScore = dto.TalkProScore,
  62. TimeScore = dto.TimeScore,
  63. FitScore = dto.FitScore,
  64. StrainScore = dto.StrainScore,
  65. LocalAndChineseScore = dto.LocalAndChineseScore,
  66. Remark = dto.Remark,
  67. });
  68. if (!res)
  69. {
  70. return result = new Result() { Code = -1, Msg = "修改失败!" };
  71. }
  72. result = new Result() { Code = 0, Msg = "修改成功!" };
  73. }
  74. else
  75. {
  76. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. return result = new Result() { Code = -2, Msg = "程序错误!" };
  82. }
  83. return result;
  84. }
  85. public async Task<Result> QueryLocalGuide(QueryLocalGuide dto)
  86. {
  87. Result result = new Result() { Code = -2, Msg = "未知错误" };
  88. try
  89. {
  90. string sqlWhere = string.Empty;
  91. if (!string.IsNullOrWhiteSpace(dto.UnitName))
  92. {
  93. sqlWhere += string.Format(@" And UnitName like '%{0}%'", dto.UnitName);
  94. }
  95. if (!string.IsNullOrWhiteSpace(dto.UnitArea) && dto.UnitArea != "全部")
  96. {
  97. sqlWhere += string.Format(@" And UnitArea like '%{0}%'", dto.UnitArea);
  98. }
  99. if (!string.IsNullOrWhiteSpace(dto.Contact))
  100. {
  101. sqlWhere += string.Format(@" And Contact like '%{0}%'", dto.Contact);
  102. }
  103. if (!string.IsNullOrWhiteSpace(dto.ContactTel))
  104. {
  105. sqlWhere += string.Format(@" And ContactTel like '%{0}%'", dto.ContactTel);
  106. }
  107. sqlWhere += string.Format(@" And IsDel={0}", 0);
  108. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  109. {
  110. Regex r = new Regex("And");
  111. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  112. }
  113. if (dto.PortType == 1)
  114. {
  115. string sql = string.Format(@"select * from Res_LocalGuideData {0}", sqlWhere);
  116. List<Res_LocalGuideData> LocalGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(sql).ToListAsync();
  117. if (LocalGuideData.Count == 0)
  118. {
  119. return result = new Result() { Code = -1, Msg = "暂无数据" };
  120. }
  121. LocalGuideData = LocalGuideData.OrderByDescending(x => x.CreateTime).ToList();
  122. if (dto.PageSize == 0 || dto.PageIndex == 0)
  123. {
  124. return result = new Result()
  125. {
  126. Code = 0,
  127. Msg = "查询成功",
  128. Data = LocalGuideData,
  129. };
  130. }
  131. else
  132. {
  133. int count = LocalGuideData.Count;
  134. float totalPage = (float)count / dto.PageSize;//总页数
  135. if (totalPage == 0) totalPage = 1;
  136. else totalPage = (int)Math.Ceiling((double)totalPage);
  137. List<Res_LocalGuideData> ListData = new List<Res_LocalGuideData>();
  138. for (int i = 0; i < dto.PageSize; i++)
  139. {
  140. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  141. if (RowIndex < LocalGuideData.Count)
  142. {
  143. ListData.Add(LocalGuideData[RowIndex]);
  144. }
  145. else
  146. {
  147. break;
  148. }
  149. }
  150. return result = new Result()
  151. {
  152. Code = 0,
  153. Msg = "查询成功",
  154. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
  155. };
  156. }
  157. }
  158. else if (dto.PortType == 2)
  159. {
  160. string sql = string.Format(@"select * from Res_LocalGuideData {0}", sqlWhere);
  161. List<Res_LocalGuideData> LocalGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(sql).ToListAsync();
  162. if (LocalGuideData.Count == 0)
  163. {
  164. return result = new Result() { Code = -1, Msg = "暂无数据" };
  165. }
  166. LocalGuideData = LocalGuideData.OrderByDescending(x => x.CreateTime).ToList();
  167. if (dto.PageSize == 0 && dto.PageIndex == 0)
  168. {
  169. return result = new Result()
  170. {
  171. Code = 0,
  172. Msg = "查询成功",
  173. Data = LocalGuideData,
  174. };
  175. }
  176. else
  177. {
  178. int count = LocalGuideData.Count;
  179. float totalPage = (float)count / dto.PageSize;//总页数
  180. if (totalPage == 0) totalPage = 1;
  181. else totalPage = (int)Math.Ceiling((double)totalPage);
  182. List<Res_LocalGuideData> ListData = new List<Res_LocalGuideData>();
  183. for (int i = 0; i < dto.PageSize; i++)
  184. {
  185. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  186. if (RowIndex < LocalGuideData.Count)
  187. {
  188. ListData.Add(LocalGuideData[RowIndex]);
  189. }
  190. else
  191. {
  192. break;
  193. }
  194. }
  195. return result = new Result()
  196. {
  197. Code = 0,
  198. Msg = "查询成功",
  199. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
  200. };
  201. }
  202. }
  203. else
  204. {
  205. return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
  206. }
  207. }
  208. catch (Exception)
  209. {
  210. return result;
  211. throw;
  212. }
  213. }
  214. }
  215. }