AskDataRepository.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 AskDataRepository : BaseRepository<Res_AskData, QueryAskDataDto>
  15. {
  16. private readonly IMapper _mapper;
  17. public AskDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  18. {
  19. _mapper = mapper;
  20. }
  21. public async Task<Result> QueryAskData(QueryAskDataDto dto)
  22. {
  23. string sqlWhere = string.Empty;
  24. if (!string.IsNullOrWhiteSpace(dto.Country))
  25. {
  26. sqlWhere += string.Format(@"And Country like '%{0}%'", dto.Country);
  27. }
  28. if (!string.IsNullOrWhiteSpace(dto.Area))
  29. {
  30. sqlWhere += string.Format(@"And Area like '%{0}%'", dto.Area);
  31. }
  32. if (!string.IsNullOrWhiteSpace(dto.Field))
  33. {
  34. sqlWhere += string.Format(@"And Field like '%{0}%'", dto.Field);
  35. }
  36. sqlWhere += string.Format(@"And IsDel={0}", 0);
  37. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  38. {
  39. Regex r = new Regex("And");
  40. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  41. }
  42. string sql = string.Format(@"select * from Res_AskData {0} order by CreateTime desc", sqlWhere);
  43. List<Res_AskData> askDatd = await _sqlSugar.SqlQueryable<Res_AskData>(sql).ToListAsync();
  44. List<Grp_DelegationInfo> delegationInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().Where(a => a.IsDel == 0).ToListAsync();
  45. var msg = string.Empty;
  46. if (askDatd.Count != 0 && delegationInfo.Count != 0) msg = "查询成功!";
  47. else msg = "查询成功!";
  48. return new Result() { Code = 0, Msg = msg, Data = new { askDatd = askDatd, delegationInfo = delegationInfo } };
  49. }
  50. /// <summary>
  51. /// 请示数据库操作(Status:1.新增,2.修改)
  52. /// </summary>
  53. /// <param name="dto"></param>
  54. /// <returns></returns>
  55. /// <exception cref="NotImplementedException"></exception>
  56. public async Task<Result> OpAskData(OpAskDataDto dto)
  57. {
  58. Result result = new Result() { Code = -2, Msg = "未知错误" };
  59. try
  60. {
  61. if (dto.Status == 1)//添加
  62. {
  63. string selectSql = string.Format(@"select * from Res_AskData where Country='{0}' and Area='{1}' and UnitName='{2} and IsDel={3}'", dto.Country, dto.Area,dto.UnitName, 0);
  64. var res_AskData = await _sqlSugar.SqlQueryable<Res_AskData>(selectSql).FirstAsync();//查询是否存在
  65. if (res_AskData != null)
  66. {
  67. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  68. }
  69. else//不存在,可添加
  70. {
  71. Res_AskData _AskData = _mapper.Map<Res_AskData>(dto);
  72. int id = await _sqlSugar.Insertable(_AskData).ExecuteReturnIdentityAsync();
  73. if (id == 0)
  74. {
  75. return result = new Result() { Code = -1, Msg = "添加失败!" };
  76. }
  77. return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  78. }
  79. }
  80. else if (dto.Status == 2)//修改
  81. {
  82. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_AskData
  83. {
  84. Country=dto.Country,
  85. Area=dto.Area,
  86. UnitName =dto.UnitName,
  87. Field =dto.Field,
  88. TalkCase =dto.TalkCase,
  89. CreateUserId = dto.CreateUserId,
  90. Remark = dto.Remark,
  91. });
  92. if (!res)
  93. {
  94. return result = new Result() { Code = -1, Msg = "修改失败!" };
  95. }
  96. return result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  97. }
  98. else
  99. {
  100. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. return result = new Result() { Code = -2, Msg = "程序错误!" };
  106. }
  107. }
  108. }
  109. }