FeeAuditRepository.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using NPOI.SS.Formula.Functions;
  2. using OASystem.Domain;
  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.Text;
  9. using System.Threading.Tasks;
  10. namespace OASystem.Infrastructure.Repositories.Groups
  11. {
  12. /// <summary>
  13. /// 费用审核仓储
  14. /// </summary>
  15. public class FeeAuditRepository:BaseRepository<EntityBase,ViewBase>
  16. {
  17. public FeeAuditRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
  18. {
  19. }
  20. /// <summary>
  21. /// 费用自动审核
  22. /// </summary>
  23. /// <param name="feeType">
  24. /// 1.酒店 76
  25. /// 2.op 79
  26. /// </param>
  27. /// <param name="diId">团组Id</param>
  28. /// <param name="dataId">数据Id(模块类型主表Id)</param>
  29. /// <returns></returns>
  30. public async Task<JsonView> FeeAutomaticAudit(int feeType, int diId, int dataId)
  31. {
  32. var _view = new JsonView() { Code = 201, Msg = "自动审核操作失败" };
  33. if (diId < 1) { _view.Msg = MsgTips.DiId; return _view; }
  34. if (dataId < 1) { _view.Msg = MsgTips.Id; return _view; }
  35. List<int> stids = new List<int>() { 17, 66 };
  36. var setData = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0 && stids.Contains(x.STid)).ToList();
  37. string _teamCurrency = string.Empty;
  38. decimal _rate = 0.00M;
  39. var groupInfo = _sqlSugar.Queryable< Grp_GroupCostParameter >().Where(x => x.IsDel ==0 && x.DiId == diId).First();
  40. if (groupInfo == null) { _view.Msg = $"团组成本信息未填写!"; return _view; }
  41. _teamCurrency = groupInfo.Currency;
  42. //币种验证 统一为currencycode三字码
  43. if (int.TryParse(_teamCurrency,out int currency))
  44. {
  45. _teamCurrency = setData.Find(x => x.Id == currency)?.Name ?? "";
  46. }
  47. _rate = groupInfo.Rate;
  48. string costContentSql = $"Select * From Grp_GroupCost";
  49. var costContents = _sqlSugar.SqlQueryable<GroupCostAuditView>(costContentSql).Where(x => x.IsDel == 0 && x.Diid == diId).ToList();
  50. if (costContents.Count < 1) { _view.Msg = $"团组成本信息未填写!"; return _view; }
  51. //处理 成本详细信息 日期为空
  52. for (int i = 0; i < costContents.Count; i++)
  53. {
  54. if (string.IsNullOrEmpty( costContents[i].Date))
  55. {
  56. int index = i - 1;
  57. if (index >= 0)
  58. {
  59. costContents[i].Date = costContents[index].Date;
  60. }
  61. }
  62. }
  63. if (feeType == 1)
  64. {
  65. var hotelCostInfo = _sqlSugar.Queryable<Grp_HotelReservations>().Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId).First();
  66. var hotelCostDetails = _sqlSugar.Queryable<Grp_HotelReservationsContent>().Where(x => x.IsDel == 0 && x.DiId == diId && x.HrId == dataId).ToList();
  67. if (hotelCostInfo == null)
  68. {
  69. _view.Msg = $"酒店费用数据未填写";
  70. return _view;
  71. }
  72. //验证币种是否相等
  73. string hotelCurrency = setData.Find(x => x.Id == hotelCostInfo.CardPriceCurrency)?.Name ?? "";
  74. if (hotelCurrency != _teamCurrency)
  75. {
  76. if (!hotelCurrency.Equals("CNY"))
  77. {
  78. _view.Msg = $"酒店预订费用币种与团组成本币种不一致,请手动审核";
  79. return _view;
  80. }
  81. }
  82. else _rate = 1.0000M;
  83. bool isAutoAudit = true; //是否自动审核
  84. DateTime checkIn = Convert.ToDateTime(hotelCostInfo.CheckInDate),
  85. checkOut = Convert.ToDateTime(hotelCostInfo.CheckOutDate);
  86. if (checkOut > checkIn) checkOut = checkOut.AddDays(-1); //房费计算,结束日期为前一天
  87. var hotelCostInfos = costContents.Where(x => Convert.ToDateTime(x.Date) >= checkIn && Convert.ToDateTime(x.Date) <= checkOut).ToList();
  88. if (hotelCostInfos.Count < 1) isAutoAudit = false;
  89. decimal otherFee = hotelCostDetails.Where(x => x.PriceType != 1).Sum(x => x.Price);
  90. if (otherFee > 0) { otherFee /= (checkOut-checkIn).Days; }
  91. var hotelCostInfosGroup = hotelCostInfos.GroupBy(x => x.Date);
  92. foreach (var item in hotelCostInfosGroup)
  93. {
  94. decimal hotelSingleRoomFee = item.Sum(x => x.HotelSingleRoomFee);
  95. decimal hotelDoubleRoomFee = item.Sum(x => x.HotelDoubleRoomFee);
  96. decimal hotelSuiteFee = item.Sum(x => x.HotelSuiteFee);
  97. decimal hotelSuiteRoomFee = item.Sum(x => x.HotelSuiteRoomFee);
  98. //1.判断费用是否 <= 成本费用
  99. //1.1 判断单间费用
  100. decimal singleRoomPrice = hotelCostInfo.SingleRoomPrice + otherFee;
  101. if (singleRoomPrice > 0) if (singleRoomPrice > hotelSingleRoomFee * _rate) isAutoAudit = false;
  102. //1.2 判断双人间费用
  103. decimal doubleRoomPrice = hotelCostInfo.DoubleRoomPrice + otherFee;
  104. if (doubleRoomPrice > 0) if (doubleRoomPrice > hotelDoubleRoomFee * _rate) isAutoAudit = false;
  105. //1.3 判断套房费用
  106. decimal suiteRoomPrice = hotelCostInfo.SuiteRoomPrice + otherFee;
  107. if (suiteRoomPrice > 0) if (suiteRoomPrice > hotelSuiteFee * _rate) isAutoAudit = false;
  108. //1.4 判断其他房型费用
  109. decimal otherRoomPrice = hotelCostInfo.OtherRoomPrice + otherFee;
  110. if (otherRoomPrice > 0) if (otherRoomPrice > hotelSuiteRoomFee * _rate) isAutoAudit = false;
  111. }
  112. //2.判断是否自动审核
  113. if (isAutoAudit)
  114. {
  115. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  116. .SetColumns(it => it.IsAuditGM == 3)
  117. .SetColumns(it => it.AuditGMOperate == 4)
  118. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  119. .Where(s => s.DIId == diId && s.CTable == 76 && s.CId == dataId)
  120. .ExecuteCommand();
  121. if (ccpUpdate > 0)
  122. {
  123. _view.Code = 200;
  124. _view.Msg = "自动审核执行成功";
  125. return _view;
  126. }
  127. }
  128. else {
  129. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  130. var ccpInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>()
  131. .Where(s => s.DIId == diId && s.CTable == 76 && s.CId == dataId && s.IsAuditGM == 3)
  132. .First();
  133. if (ccpInfo != null)
  134. {
  135. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  136. .SetColumns(it => it.IsAuditGM == 0)
  137. .SetColumns(it => it.AuditGMOperate == 0)
  138. .SetColumns(it => it.AuditGMDate == string.Empty)
  139. .Where(s => s.Id == ccpInfo.Id)
  140. .ExecuteCommand();
  141. if (ccpUpdate > 0)
  142. {
  143. _view.Code = 200;
  144. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  145. return _view;
  146. }
  147. }
  148. }
  149. }
  150. else if (feeType == 2)
  151. {
  152. //1.基础数据参数验证
  153. var priceType = new List<int>() { 1062 };
  154. var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  155. .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId && !priceType.Contains(x.PriceType))
  156. .First();
  157. //var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  158. // .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId )
  159. // .First();
  160. if (opinfos == null)
  161. {
  162. _view.Msg = $"OP费用费用类型属于“尾款”或者 内容未填写";
  163. return _view;
  164. }
  165. //1.含超时费用/超支费用 手动审核
  166. if (opinfos.SelectCheck.Contains("超时") || opinfos.SelectCheck.Contains("超支") || opinfos.SelectCheck.Contains("尾款"))
  167. {
  168. _view.Msg = @$"OP费用含尾款/超支/超时费用,请手动审核";
  169. return _view;
  170. }
  171. //1.参数验证
  172. var opCheckPriceTyeps = opinfos.SelectCheck.Split(',');
  173. var opCheckPriceTyepIds = setData.Where(x => opinfos.SelectCheck.Split(',').Contains(x.Name)).Select(x=>x.Id).ToList();
  174. var opContents = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservationsContent>()
  175. .Where(x => x.IsDel == 0 && x.DiId == diId && x.CTGGRId == dataId && opCheckPriceTyepIds.Contains(x.SId))
  176. .OrderBy(x => x.DatePrice, OrderByType.Asc)
  177. .ToList();
  178. if (opContents.Count < 1)
  179. {
  180. _view.Msg = $"OP费用费用内容未填写";
  181. return _view;
  182. }
  183. string opCurrencyName = setData.Find(x => x.Id == opContents[0].Currency)?.Name ?? "";
  184. //1.1币种验证
  185. if (!opCurrencyName.Equals(_teamCurrency))
  186. {
  187. if (!opCurrencyName.Equals("CNY"))
  188. {
  189. _view.Msg = $"OP费用币种与团组成本币种不一致,请手动审核";
  190. return _view;
  191. }
  192. }
  193. else _rate = 1.0000M;
  194. var opBasicDatas = setData.Where(x => x.STid == 17).ToList(); //费用类型基础数据
  195. bool isAutoAudit = true;
  196. if (!DateTime.TryParse(opinfos.ServiceStartTime, out DateTime startDt1) || !DateTime.TryParse(opinfos.ServiceEndTime, out DateTime endDt1))
  197. {
  198. _view.Msg = $"OP费用服务起止日期格式不正确!";
  199. return _view;
  200. }
  201. DateTime startDt = startDt1;
  202. DateTime endDt = endDt1;
  203. var opCostDatas = costContents.Where(it => Convert.ToDateTime(it.Date) >= startDt && Convert.ToDateTime(it.Date) <= endDt).ToList();
  204. if (opCostDatas.Count < 1)
  205. {
  206. _view.Msg = $"该时间段内团组成本未填写!";
  207. return _view;
  208. }
  209. var noAuditFeeTypeIds = new List<int> {
  210. 982 ,//982 车超时费 -- 暂无
  211. 96 ,//96 接送机费 -- 暂无
  212. 97 ,//97 其他费用 -- 暂无
  213. 992 ,//992 住补费用 -- 暂无
  214. 1059,//1059 导游超时费用 -- 暂无
  215. 1070,//1070 尾款金额 -- 暂无
  216. 1071,//1071 其他额外费用 -- 暂无
  217. 1073,//1073 翻译超时费 -- 暂无
  218. 1074,//1074 早餐超支费用 -- 暂无
  219. 1075,//1075 午餐超支费用 -- 暂无
  220. 1076,//1076 晚餐超支费用 -- 暂无
  221. };
  222. //费用类型筛选 包含 feeTypeIds && 包含这些类型费用大于0
  223. var noAuditFeeContents = opContents.Where(x => x.Price > 0 && noAuditFeeTypeIds.Contains(x.SId)).ToList();
  224. if (noAuditFeeContents.Count > 0)
  225. {
  226. _view.Msg = @$"OP费用含尾款/超支/超时费用,请手动审核";
  227. return _view;
  228. }
  229. //2.按天按项 检查费用是否超过预算
  230. var opDayContent = opContents.GroupBy(x => x.DatePrice);
  231. foreach (var item in opDayContent)
  232. {
  233. var opCostInfo = opCostDatas.Where(x => Convert.ToDateTime(x.Date) == item.Key).ToList();
  234. if (opCostInfo.Count < 1) continue;
  235. //车费 91
  236. var opCarCost = item.FirstOrDefault(x => x.SId == 91);
  237. if (opCarCost != null) if (opCarCost.Price > _rate * opCostInfo.Sum(x => x.CarFee)) isAutoAudit = false;
  238. //982 车超时费 -- 暂无
  239. //92 导游费
  240. var opGuideCost = item.FirstOrDefault(x => x.SId == 92);
  241. if (opGuideCost != null) if (opGuideCost.Price > _rate * opCostInfo.Sum(x => x.GuideFee)) isAutoAudit = false;
  242. //94 导游景点费
  243. var opGuideScenicCost = item.FirstOrDefault(x => x.SId == 94);
  244. if (opGuideScenicCost != null) if (opGuideScenicCost.Price > _rate * opCostInfo.Sum(x => x.GuideScenicFee)) isAutoAudit = false;
  245. //95 导游小费
  246. var opGuideTipCost = item.FirstOrDefault(x => x.SId == 95);
  247. if (opGuideTipCost != null) if (opGuideTipCost.Price > _rate * opCostInfo.Sum(x => x.GuideTipFee)) isAutoAudit = false;
  248. //983 导游餐补
  249. var opGuideMealCost = item.FirstOrDefault(x => x.SId == 983);
  250. if (opGuideMealCost != null) if (opGuideMealCost.Price > _rate * opCostInfo.Sum(x => x.GuideMealFee)) isAutoAudit = false;
  251. //984 导游房补
  252. var opGuideRoomCost = item.FirstOrDefault(x => x.SId == 984);
  253. if (opGuideRoomCost != null) if (opGuideRoomCost.Price > _rate * opCostInfo.Sum(x => x.GuideRoomFee)) isAutoAudit = false;
  254. //985 导游交通
  255. var opGuideTrafficCost = item.FirstOrDefault(x => x.SId == 985);
  256. if (opGuideTrafficCost != null) if (opGuideTrafficCost.Price > _rate * opCostInfo.Sum(x => x.GuideTrafficFee)) isAutoAudit = false;
  257. //96 接送机费 -- 暂无
  258. //97 其他费用 -- 暂无
  259. //979 司机工资
  260. var opDriverCost = item.FirstOrDefault(x => x.SId == 979);
  261. if (opDriverCost != null) if (opDriverCost.Price > _rate * opCostInfo.Sum(x => x.DriverFee)) isAutoAudit = false;
  262. //980 司机小费
  263. var opDriverTipCost = item.FirstOrDefault(x => x.SId == 980);
  264. if (opDriverTipCost != null) if (opDriverTipCost.Price > _rate * opCostInfo.Sum(x => x.DriverTipFee)) isAutoAudit = false;
  265. //981 司机餐补
  266. var opDriverMealCost = item.FirstOrDefault(x => x.SId == 981);
  267. if (opDriverMealCost != null) if (opDriverMealCost.Price > _rate * opCostInfo.Sum(x => x.DriverMealFee)) isAutoAudit = false;
  268. //988 客户早餐费用
  269. var opClientBreakfastCost = item.FirstOrDefault(x => x.SId == 988);
  270. if (opClientBreakfastCost != null) if (opClientBreakfastCost.Price > _rate * opCostInfo.Sum(x => x.ClientBreakfastFee)) isAutoAudit = false;
  271. //93 客户午餐费用
  272. var opClientLunchCost = item.FirstOrDefault(x => x.SId == 93);
  273. if (opClientLunchCost != null) if (opClientLunchCost.Price > _rate * opCostInfo.Sum(x => x.ClientLunchFee)) isAutoAudit = false;
  274. //989 客户晚餐费用
  275. var opClientDinnerCost = item.FirstOrDefault(x => x.SId == 989);
  276. if (opClientDinnerCost != null) if (opClientDinnerCost.Price > _rate * opCostInfo.Sum(x => x.ClientDinnerFee)) isAutoAudit = false;
  277. //990 景点门票费
  278. var opScenicTicketCost = item.FirstOrDefault(x => x.SId == 990);
  279. if (opScenicTicketCost != null) if (opScenicTicketCost.Price > _rate * opCostInfo.Sum(x => x.ScenicTicketFee)) isAutoAudit = false;
  280. //991 饮料/零食/水果
  281. var opDrinkSnackFruitCost = item.FirstOrDefault(x => x.SId == 991);
  282. if (opDrinkSnackFruitCost != null) if (opDrinkSnackFruitCost.Price > _rate * opCostInfo.Sum(x => x.DrinkSnackFruitFee)) isAutoAudit = false;
  283. //992 住补费用 -- 暂无
  284. //994 翻译费
  285. var opTranslatorCost = item.FirstOrDefault(x => x.SId == 994);
  286. if (opTranslatorCost != null) if (opTranslatorCost.Price > _rate * opCostInfo.Sum(x => x.TranslatorFee)) isAutoAudit = false;
  287. //1059 导游超时费用 -- 暂无
  288. //1070 尾款金额 -- 暂无
  289. //1071 其他额外费用 -- 暂无
  290. //1073 翻译超时费 -- 暂无
  291. //1074 早餐超支费用 -- 暂无
  292. //1075 午餐超支费用 -- 暂无
  293. //1076 晚餐超支费用 -- 暂无
  294. }
  295. //更改审核状态
  296. if (isAutoAudit)
  297. {
  298. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  299. .SetColumns(it => it.IsAuditGM == 3)
  300. .SetColumns(it => it.AuditGMOperate == 4)
  301. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  302. .Where(s => s.DIId == diId && s.CTable == 79 && s.CId == dataId)
  303. .ExecuteCommand();
  304. if (ccpUpdate > 0)
  305. {
  306. _view.Code = 200;
  307. _view.Msg = "自动审核执行成功!";
  308. return _view;
  309. }
  310. }
  311. else
  312. {
  313. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  314. var ccpInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>()
  315. .Where(s => s.DIId == diId && s.CTable == 79 && s.CId == dataId && s.IsAuditGM == 3)
  316. .First();
  317. if (ccpInfo != null)
  318. {
  319. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  320. .SetColumns(it => it.IsAuditGM == 0)
  321. .SetColumns(it => it.AuditGMOperate == 0)
  322. .SetColumns(it => it.AuditGMDate == string.Empty)
  323. .Where(s => s.Id == ccpInfo.Id)
  324. .ExecuteCommand();
  325. if (ccpUpdate > 0)
  326. {
  327. _view.Code = 200;
  328. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  329. return _view;
  330. }
  331. }
  332. }
  333. }
  334. else _view.Msg = $"请传入有效的feeType参数";
  335. return _view;
  336. }
  337. }
  338. }