EnterExitCostDraftRepository.cs 19 KB

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