LocalGuideDataRepository.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  108. {
  109. Regex r = new Regex("And");
  110. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  111. }
  112. if (dto.PortType == 1)
  113. {
  114. string sql = string.Format(@"select * from Res_LocalGuideData {0}", sqlWhere);
  115. List<Res_LocalGuideData> LocalGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(sql).ToListAsync();
  116. if (LocalGuideData.Count == 0)
  117. {
  118. return result = new Result() { Code = -1, Msg = "暂无数据" };
  119. }
  120. LocalGuideData = LocalGuideData.OrderByDescending(x => x.CreateTime).ToList();
  121. if (dto.PageSize == 0 || dto.PageIndex == 0)
  122. {
  123. return result = new Result()
  124. {
  125. Code = 0,
  126. Msg = "查询成功",
  127. Data = LocalGuideData,
  128. };
  129. }
  130. else
  131. {
  132. int count = LocalGuideData.Count;
  133. float totalPage = (float)count / dto.PageSize;//总页数
  134. if (totalPage == 0) totalPage = 1;
  135. else totalPage = (int)Math.Ceiling((double)totalPage);
  136. List<Res_LocalGuideData> ListData = new List<Res_LocalGuideData>();
  137. for (int i = 0; i < dto.PageSize; i++)
  138. {
  139. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  140. if (RowIndex < LocalGuideData.Count)
  141. {
  142. ListData.Add(LocalGuideData[RowIndex]);
  143. }
  144. else
  145. {
  146. break;
  147. }
  148. }
  149. return result = new Result()
  150. {
  151. Code = 0,
  152. Msg = "查询成功",
  153. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
  154. };
  155. }
  156. }
  157. else if (dto.PortType == 2)
  158. {
  159. string sql = string.Format(@"select * from Res_LocalGuideData {0}", sqlWhere);
  160. List<Res_LocalGuideData> LocalGuideData = await _sqlSugar.SqlQueryable<Res_LocalGuideData>(sql).ToListAsync();
  161. if (LocalGuideData.Count == 0)
  162. {
  163. return result = new Result() { Code = -1, Msg = "暂无数据" };
  164. }
  165. LocalGuideData = LocalGuideData.OrderByDescending(x => x.CreateTime).ToList();
  166. if (dto.PageSize == 0 && dto.PageIndex == 0)
  167. {
  168. return result = new Result()
  169. {
  170. Code = 0,
  171. Msg = "查询成功",
  172. Data = LocalGuideData,
  173. };
  174. }
  175. else
  176. {
  177. int count = LocalGuideData.Count;
  178. float totalPage = (float)count / dto.PageSize;//总页数
  179. if (totalPage == 0) totalPage = 1;
  180. else totalPage = (int)Math.Ceiling((double)totalPage);
  181. List<Res_LocalGuideData> ListData = new List<Res_LocalGuideData>();
  182. for (int i = 0; i < dto.PageSize; i++)
  183. {
  184. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  185. if (RowIndex < LocalGuideData.Count)
  186. {
  187. ListData.Add(LocalGuideData[RowIndex]);
  188. }
  189. else
  190. {
  191. break;
  192. }
  193. }
  194. return result = new Result()
  195. {
  196. Code = 0,
  197. Msg = "查询成功",
  198. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = ListData },
  199. };
  200. }
  201. }
  202. else
  203. {
  204. return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
  205. }
  206. }
  207. catch (Exception)
  208. {
  209. return result;
  210. throw;
  211. }
  212. }
  213. }
  214. }