EnterExitCostDraftRepository.cs 21 KB

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