EnterExitCostDraftRepository.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using OASystem.Domain.Dtos.Groups;
  2. using OASystem.Domain.Entities.Groups;
  3. using OASystem.Domain.ViewModels.Groups;
  4. using OASystem.Domain;
  5. using OASystem.Infrastructure.Repositories.System;
  6. using OASystem.Infrastructure.Tools;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using AutoMapper;
  13. namespace OASystem.Infrastructure.Repositories.Groups
  14. {
  15. /// <summary>
  16. /// 出入境费用-草稿 仓库
  17. /// </summary>
  18. public class EnterExitCostDraftRepository : BaseRepository<Grp_EnterExitCostDraft, EnterExitCostDraftView>
  19. {
  20. private readonly IMapper _mapper;
  21. private readonly SetDataRepository _setDataRep;
  22. public EnterExitCostDraftRepository(SqlSugarClient sqlSugar, IMapper mapper, SetDataRepository setDataRep)
  23. : base(sqlSugar)
  24. {
  25. _setDataRep = setDataRep;
  26. _mapper = mapper;
  27. }
  28. /// <summary>
  29. /// 出入境费用草稿 --> 权限验证
  30. /// </summary>
  31. /// <param name="groupId">草稿Id</param>
  32. /// <param name="permission">操作权限 1:查看;2:新增;3:编辑;4:删除;5:下载; </param>
  33. /// <returns></returns>
  34. public async Task<bool> PermissionValidationAsync(int groupId, int permission)
  35. {
  36. //参数验证
  37. if (groupId < 1) return false;
  38. var permissionIds = new List<int>() { 1, 2, 3, 4, 5 };
  39. if (!permissionIds.Contains(permission)) return false;
  40. var info = await _sqlSugar.Queryable<Grp_EnterExitCostDraftPermission>()
  41. .Where(it => it.DraftId == groupId && it.Permission == permission)
  42. .FirstAsync();
  43. if (info != null) return true;
  44. return false;
  45. }
  46. /// <summary>
  47. /// 查询 根据 Id 查询
  48. /// </summary>
  49. /// <returns></returns>
  50. public async Task<JsonView> EnterExitCostDraftInfoById(EnterExitCostDraftInfoByIdDto dto)
  51. {
  52. var result = new JsonView() { Code = 400, Msg = "暂无数据" };
  53. var id = dto.Id;
  54. var enterExitCostDraftData = await _sqlSugar.Queryable<Grp_EnterExitCostDraft>().OrderByDescending(x => x.CreateTime).FirstAsync(it => it.Id == id && it.IsDel == 0);
  55. var enterExitCostInfoView = new EnterExitCostDraftInfoView();
  56. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  57. {
  58. if (enterExitCostDraftData != null)
  59. {
  60. enterExitCostInfoView = _mapper.Map<EnterExitCostDraftInfoView>(enterExitCostDraftData);
  61. var isView = await PermissionValidationAsync(enterExitCostDraftData.Id, dto.CurrUserId);
  62. if (isView) enterExitCostInfoView.IsView = true; //查看权限
  63. if (enterExitCostDraftData.CreateUserId == dto.CurrUserId)
  64. {
  65. enterExitCostInfoView.IsSave = true; //操作权限
  66. enterExitCostInfoView.IsView = true; //查看权限
  67. }
  68. if (!enterExitCostInfoView.IsView)
  69. {
  70. var setUserName = await _sqlSugar.Queryable<Sys_Users>().Where(x => x.Id == enterExitCostDraftData.CreateUserId).Select(x => x.CnName).FirstAsync();
  71. result.Msg = $"{setUserName}设置了你不能查看当前团组的出入境费用明细数据-草稿,如要查看,请联系{setUserName}!";
  72. return result;
  73. }
  74. var dayAndCostDraftSql = string.Format(@"Select ntf.Country,ntf.City,ssd.Name CurremcyCode,ssd.Remark CurrencyName,gdac.* From Grp_DayAndCostDraft gdac
  75. Left Join Grp_NationalTravelFee ntf On ntf.Id = gdac.NationalTravelFeeId
  76. Left Join Sys_SetData ssd On ssd.STid = 66 And gdac.Currency = ssd.Id
  77. Where gdac.Isdel = 0 And ntf.IsDel = 0 And gdac.ParentId = {0} Order By [Type] ASC,[Days] ASC", id);
  78. var dayAndCostDraftData = await _sqlSugar.SqlQueryable<DayAndCostDraftInfoView>(dayAndCostDraftSql).ToListAsync();
  79. enterExitCostInfoView.QuarterageData = dayAndCostDraftData.Where(it => it.Type == 1).ToList(); //住宿费 1
  80. enterExitCostInfoView.BoardWagesData = dayAndCostDraftData.Where(it => it.Type == 2).ToList(); //伙食费 2
  81. enterExitCostInfoView.MiscellaneousFeeData = dayAndCostDraftData.Where(it => it.Type == 3).ToList(); //公杂费 3
  82. enterExitCostInfoView.TrainingExpenseData = dayAndCostDraftData.Where(it => it.Type == 4).ToList(); //培训费 4
  83. enterExitCostInfoView.DayOtherPriceData = _sqlSugar.Queryable<Grp_DayOtherPriceDraft>()
  84. .Where(x => x.IsDel == 0 && x.ParentId == id)
  85. .Select(x => new DayOtherPriceDraftView
  86. {
  87. ParentId = x.ParentId,
  88. Id = x.Id,
  89. Cost = x.Cost,
  90. Currency = x.Currency,
  91. Index = x.Index,
  92. SetDataId = x.SetDataId,
  93. SubTotal = x.SubTotal,
  94. Remark = x.Remark,
  95. })
  96. .OrderBy(x => x.Index)
  97. .ToList();
  98. enterExitCostInfoView.Currencys = (List<CurrencyInfo>?)CommonFun.GetCurrencyChinaToList(enterExitCostDraftData.CurrencyRemark);
  99. result.Msg = "操作成功";
  100. result.Code = 200;
  101. }
  102. else
  103. {
  104. // List<DayAndCostInfoView> views = new List<DayAndCostInfoView>()
  105. // {
  106. // new DayAndCostInfoView {Days=1},
  107. // new DayAndCostInfoView {Days=2},
  108. // new DayAndCostInfoView {Days=3},
  109. // new DayAndCostInfoView {Days=4},
  110. // new DayAndCostInfoView {Days=5},
  111. // new DayAndCostInfoView {Days=6},
  112. // new DayAndCostInfoView {Days=7},
  113. // new DayAndCostInfoView {Days=8},
  114. // };
  115. // enterExitCostInfoView.QuarterageData = views;
  116. // enterExitCostInfoView.BoardWagesData = views;
  117. // enterExitCostInfoView.MiscellaneousFeeData = views;
  118. // enterExitCostInfoView.TrainingExpenseData = views;
  119. // result.Code = 0;
  120. result.Msg = "未查询出数据!!!";
  121. }
  122. result.Data = enterExitCostInfoView;
  123. }
  124. else result.Msg = MsgTips.Port;
  125. return result;
  126. }
  127. /// <summary>
  128. /// Add Or Update
  129. /// </summary>
  130. /// <param name="dto"></param>
  131. /// <returns></returns>
  132. public async Task<Result> EnterExitCostDraftOp(EnterExitCostDraftOpDto dto)
  133. {
  134. Result result = new Result() { Code = -1, Msg = "操作失败!" };
  135. if (dto.SumJJC == 0) dto.OutsideJJPay = 0;
  136. if (dto.SumGWC == 0) dto.OutsideGWPay = 0;
  137. if (dto.SumTDC == 0) dto.OutsideTDPay = 0;
  138. var enterExitCost = _mapper.Map<Grp_EnterExitCostDraft>(dto);
  139. enterExitCost.Remark = dto.FirstItemRemark;
  140. //enterExitCost.InsidePay = enterExitCost.Visa + enterExitCost.YiMiao + enterExitCost.HeSuan + enterExitCost.Service + enterExitCost.Safe + enterExitCost.Ticket;
  141. var subData = new List<Grp_DayAndCostDraft>();
  142. var quarterageData = _mapper.Map<List<Grp_DayAndCostDraft>>(dto.QuarterageData); //住宿费 1
  143. quarterageData = quarterageData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
  144. if (quarterageData.Any()) subData.AddRange(quarterageData);
  145. var boardWagesData = _mapper.Map<List<Grp_DayAndCostDraft>>(dto.BoardWagesData); //伙食费 2
  146. boardWagesData = boardWagesData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
  147. if (boardWagesData.Any()) subData.AddRange(boardWagesData);
  148. var miscellaneousFeeData = _mapper.Map<List<Grp_DayAndCostDraft>>(dto.MiscellaneousFeeData); //公杂费 3
  149. miscellaneousFeeData = miscellaneousFeeData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
  150. if (miscellaneousFeeData.Any()) subData.AddRange(miscellaneousFeeData);
  151. var trainingExpenseData = _mapper.Map<List<Grp_DayAndCostDraft>>(dto.TrainingExpenseData); //培训费 4
  152. trainingExpenseData = trainingExpenseData.Select(it => { it.CreateUserId = dto.UserId; return it; }).ToList();
  153. if (trainingExpenseData.Any()) subData.AddRange(trainingExpenseData);
  154. //处理币种string
  155. enterExitCost.CurrencyRemark = CommonFun.GetCurrencyChinaToString(dto.Currencys);
  156. enterExitCost.CreateUserId = dto.UserId;
  157. _sqlSugar.BeginTran();
  158. if (dto.PortType == 1)
  159. {
  160. try
  161. {
  162. int enterExitId = 0;
  163. var isNull = _sqlSugar.Queryable<Grp_EnterExitCostDraft>().Where(it => it.IsDel == 0 && it.DraftName.Equals(dto.DraftName)).First();
  164. if (isNull != null) //修改
  165. {
  166. //编辑验证
  167. if (isNull.CreateUserId != dto.UserId)
  168. {
  169. var userName = _sqlSugar.Queryable<Sys_Users>().Where(x => isNull.CreateUserId == isNull.Id).Select(x => x.CnName).First();
  170. _sqlSugar.RollbackTran();
  171. result.Msg = $"该条信息只有操作人“{userName}”可修改!";
  172. return result;
  173. }
  174. var updateId = _sqlSugar.Updateable(enterExitCost)
  175. .IgnoreColumns(it => new { it.DeleteUserId, it.DeleteTime, it.CreateUserId, it.CreateTime, it.IsDel })
  176. .WhereColumns(it => it.DraftName)
  177. .ExecuteCommand();
  178. if (updateId > 0)
  179. {
  180. enterExitId = isNull.Id;
  181. }
  182. else
  183. {
  184. _sqlSugar.RollbackTran();
  185. result.Msg = "修改操作失败!";
  186. return result;
  187. }
  188. }
  189. else //添加
  190. {
  191. //添加
  192. enterExitId = _sqlSugar.Insertable(enterExitCost).ExecuteReturnIdentity();
  193. if (enterExitId < 1)
  194. {
  195. _sqlSugar.RollbackTran();
  196. result.Msg = "添加操作失败!";
  197. return result;
  198. }
  199. subData.Select(x => { x.Id = 0; return x; }).ToList();
  200. dto.DayOtherPriceData.Select(x => { x.Id = 0; return x; }).ToList();
  201. }
  202. if (enterExitId > 0)
  203. {
  204. subData = subData.Select(x => { x.ParentId = enterExitId; return x; }).ToList();
  205. var addList = subData.Where(x => x.Id == 0).ToList();
  206. var updateList = subData.Where(x => x.Id > 0).ToList();
  207. if (addList.Any())
  208. {
  209. var rowCount = _sqlSugar.Insertable(addList).ExecuteCommand();
  210. }
  211. if (updateList.Any())
  212. {
  213. var rowCount = _sqlSugar.Updateable(updateList).ExecuteCommand();
  214. }
  215. if (dto.DayOtherPriceData.Any())
  216. {
  217. var inserList = dto.DayOtherPriceData
  218. .Where(x => x.Id == 0)
  219. .Select(x => new Grp_DayOtherPriceDraft
  220. {
  221. Cost = x.Cost,
  222. Id = x.Id,
  223. CreateTime = DateTime.Now,
  224. CreateUserId = dto.UserId,
  225. Currency = x.Currency,
  226. ParentId = enterExitId,
  227. Index = x.Index,
  228. IsDel = 0,
  229. SetDataId = x.SetDataId,
  230. SubTotal = x.SubTotal,
  231. Remark = x.Remark,
  232. })
  233. .ToList();
  234. var updateList1 = dto.DayOtherPriceData
  235. .Where(x => x.Id != 0)
  236. .Select(x => new Grp_DayOtherPriceDraft
  237. {
  238. Cost = x.Cost,
  239. Id = x.Id,
  240. CreateTime = DateTime.Now,
  241. CreateUserId = dto.UserId,
  242. Currency = x.Currency,
  243. ParentId = enterExitId,
  244. Index = x.Index,
  245. IsDel = 0,
  246. SetDataId = x.SetDataId,
  247. SubTotal = x.SubTotal,
  248. Remark = x.Remark
  249. })
  250. .ToList();
  251. if (inserList.Any())
  252. {
  253. var rowCount = _sqlSugar.Insertable(inserList).ExecuteCommand();
  254. }
  255. if (updateList1.Any())
  256. {
  257. var rowCount = _sqlSugar.Updateable(updateList1).ExecuteCommand();
  258. }
  259. }
  260. }
  261. _sqlSugar.CommitTran();
  262. result.Code = 0;
  263. result.Msg = "操作成功!";
  264. result.Data = new { sign = enterExitId, draftName = enterExitCost.DraftName };
  265. }
  266. catch (Exception ex)
  267. {
  268. _sqlSugar.RollbackTran();
  269. result.Msg = ex.Message;
  270. }
  271. }
  272. else result.Msg = MsgTips.Port;
  273. return result;
  274. }
  275. /// <summary>
  276. /// Del
  277. /// </summary>
  278. /// <param name="dto"></param>
  279. /// <returns></returns>
  280. public async Task<Result> EnterExitCostDraftSubItemDel(EnterExitCostDraftSubItemDelDto dto)
  281. {
  282. Result result = new Result();
  283. var data = new Grp_DayAndCostDraft()
  284. {
  285. Id = dto.Id,
  286. IsDel = 1,
  287. DeleteUserId = dto.DeleteUserId,
  288. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
  289. };
  290. var del = _sqlSugar.Updateable(data).UpdateColumns(it => new { it.IsDel, it.DeleteUserId, it.DeleteTime }).ExecuteCommand();
  291. if (del > 0) result.Code = 0;
  292. else result.Msg = string.Format("删除失败!");
  293. return result;
  294. }
  295. /// <summary>
  296. /// 一键清空
  297. /// 删除主表数据 Update
  298. /// 删除子表数据 delete
  299. /// </summary>
  300. /// <param name="id"></param>
  301. /// <param name="userId"></param>
  302. /// <returns></returns>
  303. public async Task<Result> OneClickEmpty(int id, int userId)
  304. {
  305. Result result = new Result() { Code = -1, Msg = "操作成功!" };
  306. if (id < 1) return result = new Result() { Code = -1, Msg = "请传入有效的Id参数!" };
  307. if (userId < 1) return result = new Result() { Code = -1, Msg = "请传入有效的UserId参数!" };
  308. BeginTran();
  309. int update = await _sqlSugar.Updateable<Grp_EnterExitCostDraft>()
  310. .SetColumns(it => new Grp_EnterExitCostDraft()
  311. {
  312. IsDel = 1,
  313. DeleteUserId = userId,
  314. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  315. })
  316. .Where(it => it.Id == id)
  317. .ExecuteCommandAsync();
  318. if (update > 0)
  319. {
  320. await _sqlSugar.Updateable<Grp_DayAndCostDraft>()
  321. .SetColumns(it => new Grp_DayAndCostDraft()
  322. {
  323. IsDel = 1,
  324. DeleteUserId = userId,
  325. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  326. })
  327. .Where(it => it.ParentId == id)
  328. .ExecuteCommandAsync();
  329. await _sqlSugar.Updateable<Grp_DayOtherPriceDraft>()
  330. .SetColumns(it => new Grp_DayOtherPriceDraft()
  331. {
  332. IsDel = 1,
  333. DeleteUserId = userId,
  334. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  335. })
  336. .Where(it => it.ParentId == id)
  337. .ExecuteCommandAsync();
  338. result.Code = 0;
  339. result.Msg = "操作成功!";
  340. CommitTran();
  341. }
  342. else
  343. {
  344. result.Msg = "操作失败";
  345. RollbackTran();
  346. }
  347. return result;
  348. }
  349. }
  350. }