HotelDataRepository.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(OperationHotelData 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}'"
  28. , dto.Name);
  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. }
  78. }