EnterExitCostDraftRepository.cs 23 KB

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