VisaCommissionRepository.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using AutoMapper;
  2. using OASystem.Domain.Dtos.Groups;
  3. using OASystem.Domain.Entities.Groups;
  4. using OASystem.Domain.ViewModels.Groups;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace OASystem.Infrastructure.Repositories.Groups
  12. {
  13. /// <summary>
  14. /// 签证提成录入
  15. /// 仓储
  16. /// </summary>
  17. public class VisaCommissionRepository : BaseRepository<Grp_VisaCommission, VisaCommissionViewModel>
  18. {
  19. private readonly IMapper _mapper;
  20. private readonly DelegationInfoRepository _groupRep;
  21. public VisaCommissionRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep)
  22. : base(sqlSugar)
  23. {
  24. _mapper = mapper;
  25. _groupRep = groupRep;
  26. }
  27. /// <summary>
  28. /// Init
  29. /// </summary>
  30. /// <returns></returns>
  31. public async Task<JsonView> Init()
  32. {
  33. var groupInfo = await _groupRep.GetGroupNameList(new Domain.Dtos.Groups.GroupNameDto() { PortType = 1 });
  34. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = new { GroupData = groupInfo.Data } };
  35. }
  36. /// <summary>
  37. /// Item
  38. /// </summary>
  39. /// <returns></returns>
  40. public async Task<JsonView> Item(VisaCommissionItemDto _dto)
  41. {
  42. var data = await _sqlSugar.Queryable<Grp_VisaCommission>()
  43. .Where(x => x.IsDel == 0 && x.DiId == _dto.DiId && x.UId == _dto.CurrUserId)
  44. .Select(x => new
  45. {
  46. x.Id,
  47. x.Country,
  48. x.Quantity,
  49. x.Remark
  50. })
  51. .ToListAsync();
  52. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().Where(x => x.IsDel == 0 && x.Id == _dto.DiId).FirstAsync();
  53. if (groupInfo != null)
  54. {
  55. string[] countryArray = _groupRep.GroupSplitCountry(groupInfo.VisitCountry).ToArray();
  56. foreach (string country in countryArray)
  57. {
  58. if (data.Find(x => country.Equals(x.Country)) == null)
  59. {
  60. data.Add(new
  61. {
  62. Id = 0,
  63. Country = country,
  64. Quantity = 0,
  65. Remark = ""
  66. });
  67. }
  68. }
  69. }
  70. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = data };
  71. }
  72. /// <summary>
  73. /// Save
  74. /// </summary>
  75. /// <returns></returns>
  76. public async Task<JsonView> Save(VisaCommissionSaveDto _dto)
  77. {
  78. var data = _mapper.Map<List<Grp_VisaCommission>>(_dto.Items);
  79. for (int i = 0; i < data.Count; i++)
  80. {
  81. data[i].CreateUserId = _dto.Items.FirstOrDefault()?.CurrUserId ?? 0;
  82. data[i].UId = _dto.Items.FirstOrDefault()?.CurrUserId ?? 0;
  83. }
  84. //添加数据
  85. var addData = data.Where(x => x.Id < 1).ToList();
  86. //修改数据
  87. var updData = data.Where(x => x.Id > 0).ToList();
  88. _sqlSugar.BeginTran();
  89. bool status = false;
  90. if (addData.Count > 0) {
  91. var addCount = await _sqlSugar.Insertable<Grp_VisaCommission>(addData).ExecuteCommandAsync();
  92. if (addCount > 0) status=true;
  93. }
  94. if (updData.Count > 0)
  95. {
  96. var updCount = await _sqlSugar.Updateable<Grp_VisaCommission>(updData)
  97. .UpdateColumns(x => new
  98. {
  99. x.Country,
  100. x.Quantity,
  101. x.Remark
  102. })
  103. .WhereColumns(x => x.Id)
  104. .ExecuteCommandAsync();
  105. if (updCount > 0) status = true;
  106. }
  107. var jv = new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!" };
  108. if (status) _sqlSugar.CommitTran();
  109. else
  110. {
  111. _sqlSugar.RollbackTran();
  112. jv.Code = StatusCodes.Status400BadRequest;
  113. jv.Msg = "操作失败";
  114. }
  115. return jv;
  116. }
  117. /// <summary>
  118. /// Create
  119. /// </summary>
  120. /// <returns></returns>
  121. public async Task<JsonView> Create(VisaCommissionCreateDto _dto)
  122. {
  123. var _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  124. //添加查重
  125. var info = await _sqlSugar.Queryable<Grp_VisaCommission>()
  126. .Where(x => x.IsDel == 0 && x.DiId == _dto.DiId && x.Country.Equals(_dto.Country))
  127. .FirstAsync();
  128. //if (info != null)
  129. //{
  130. // _view.Code = StatusCodes.Status400BadRequest;
  131. // _view.Msg = "该数据已存在请勿重新添加!";
  132. // return _view;
  133. //}
  134. var createInfo = _mapper.Map<Grp_VisaCommission>(_dto);
  135. createInfo.CreateUserId = _dto.CurrUserId;
  136. createInfo.UId = _dto.CurrUserId;
  137. var createStatus = await _sqlSugar.Insertable<Grp_VisaCommission>(createInfo)
  138. .ExecuteCommandAsync();
  139. if (createStatus > 0)
  140. {
  141. _view.Code = StatusCodes.Status200OK;
  142. _view.Msg = "操作成功!";
  143. return _view;
  144. }
  145. return _view;
  146. }
  147. /// <summary>
  148. /// Edit
  149. /// </summary>
  150. /// <param name="id"></param>
  151. /// <param name="_dto"></param>
  152. /// <returns></returns>
  153. public async Task<JsonView> Edit(int id,VisaCommissionCreateDto _dto)
  154. {
  155. var _view = new JsonView() { Code = StatusCodes.Status204NoContent, Msg = "操作失败!" };
  156. var editInfo = _mapper.Map<Grp_VisaCommission>(_dto);
  157. editInfo.CreateUserId = _dto.CurrUserId;
  158. editInfo.UId = _dto.CurrUserId;
  159. var editStatus = await _sqlSugar.Updateable<Grp_VisaCommission>(editInfo)
  160. .UpdateColumns(x => new
  161. {
  162. x.Country,
  163. x.Quantity,
  164. x.Remark
  165. })
  166. .Where(x => x.Id == id)
  167. .ExecuteCommandAsync();
  168. if (editStatus > 0)
  169. {
  170. _view.Code = StatusCodes.Status200OK;
  171. _view.Msg = "操作成功!";
  172. return _view;
  173. }
  174. return _view;
  175. }
  176. /// <summary>
  177. /// Del
  178. /// </summary>
  179. /// <param name="id"></param>
  180. /// <param name="currUserId"></param>
  181. /// <returns></returns>
  182. public async Task<JsonView> Del(int id,int currUserId)
  183. {
  184. var _view = new JsonView() { Code = StatusCodes.Status204NoContent, Msg = "操作失败!" };
  185. var editStatus = await _sqlSugar.Updateable<Grp_VisaCommission>()
  186. .SetColumns(x => new Grp_VisaCommission
  187. {
  188. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  189. DeleteUserId = currUserId,
  190. })
  191. .Where(x => x.Id == id)
  192. .ExecuteCommandAsync();
  193. if (editStatus > 0)
  194. {
  195. _view.Code = StatusCodes.Status200OK;
  196. _view.Msg = "操作成功!";
  197. return _view;
  198. }
  199. return _view;
  200. }
  201. }
  202. }