ThreeCodeRepository.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 SqlSugar;
  7. namespace OASystem.Infrastructure.Repositories.Resource
  8. {
  9. public class ThreeCodeRepository:BaseRepository<Res_ThreeCode,ThreeCodeView>
  10. {
  11. private readonly IMapper _mapper;
  12. public ThreeCodeRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  13. {
  14. _mapper = mapper;
  15. }
  16. /// <summary>
  17. /// sqlSugar分页查询
  18. /// </summary>
  19. /// <param name="PageIndex"></param>
  20. /// <param name="PageSize"></param>
  21. /// <returns></returns>
  22. public async Task<JsonView> QuerThreeCode(int PageIndex, int PageSize)
  23. {
  24. JsonView result = new JsonView();
  25. try
  26. {
  27. int totalCount = 0;
  28. List<Res_ThreeCode> page = _sqlSugar.Queryable<Res_ThreeCode>().ToPageList(PageIndex, PageSize, ref totalCount);
  29. if (page.Count != 0)
  30. {
  31. result=new JsonView(){Code=200,Msg = "查询成功!",Data = page,Count = totalCount };
  32. }
  33. else
  34. {
  35. result = new JsonView(){ Code = 400, Msg = "查询失败!", Data = page, Count = totalCount };
  36. }
  37. }
  38. catch (Exception)
  39. {
  40. result = new JsonView() { Code = 400, Msg = "程序错误!", Data = null, Count = 0 };
  41. throw;
  42. }
  43. return result;
  44. }
  45. /// <summary>
  46. /// 添加、编辑操作
  47. /// </summary>
  48. /// <param name="dto"></param>
  49. /// <returns></returns>
  50. public async Task<Result> ThreeCodeOperation(ThreeCodeOperationDto dto)
  51. {
  52. Result result = new Result() { Code = -2, Msg = "未知错误" };
  53. try
  54. {
  55. if (dto.Status == 1)//添加
  56. {
  57. string selectSql = string.Format(@"select * from Res_ThreeCode where Three='{0}' and Country='{1}' and City='{2}' and AirPort='{3}'"
  58. , dto.Three, dto.Country, dto.City, dto.AirPort);
  59. var ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(selectSql).FirstAsync();//查询是否存在
  60. if (ThreeCode != null)
  61. {
  62. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  63. }
  64. else//不存在,可添加
  65. {
  66. Res_ThreeCode _ThreeCodeDto = _mapper.Map<Res_ThreeCode>(dto);
  67. int id = await AddAsyncReturnId(_ThreeCodeDto);
  68. if (id == 0)
  69. {
  70. return result = new Result() { Code = -1, Msg = "添加失败!" };
  71. }
  72. result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  73. }
  74. }
  75. else if (dto.Status == 2)//修改
  76. {
  77. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_ThreeCode
  78. {
  79. Three=dto.Three,
  80. Four=dto.Four,
  81. Country=dto.Country,
  82. City = dto.City,
  83. AirPort = dto.AirPort,
  84. AirPort_En = dto.AirPort_En,
  85. Remark = dto.Remark,
  86. });
  87. if (!res)
  88. {
  89. return result = new Result() { Code = -1, Msg = "修改失败!" };
  90. }
  91. result = new Result() { Code = 0, Msg = "修改成功!" };
  92. }
  93. else
  94. {
  95. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. return result = new Result() { Code = -2, Msg = "程序错误!" };
  101. }
  102. return result;
  103. }
  104. }
  105. }