AskDataRepository.cs 5.4 KB

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