FeeAuditRepository.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 /= 3; }
  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 = setData.Where(x => x.Name.Contains("尾款")).Select(x =>x.Id).ToList();
  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. if (opinfos == null)
  158. {
  159. _view.Msg = $"OP费用数据未填写";
  160. return _view;
  161. }
  162. //1.含超时费用/超支费用 手动审核
  163. if (opinfos.SelectCheck.Contains("超时") || opinfos.SelectCheck.Contains("超支"))
  164. {
  165. _view.Msg = $"OP费用含超时费用,请手动审核";
  166. return _view;
  167. }
  168. //1.参数验证
  169. var opCheckPriceTyeps = opinfos.SelectCheck.Split(',');
  170. var opCheckPriceTyepIds = setData.Where(x => opinfos.SelectCheck.Split(',').Contains(x.Name)).Select(x=>x.Id).ToList();
  171. var opContents = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservationsContent>()
  172. .Where(x => x.IsDel == 0 && x.DiId == diId && x.CTGGRId == dataId && opCheckPriceTyepIds.Contains(x.SId))
  173. .OrderBy(x => x.DatePrice, OrderByType.Asc)
  174. .ToList();
  175. if (opContents.Count < 1)
  176. {
  177. _view.Msg = $"OP费用费用内容未填写";
  178. return _view;
  179. }
  180. string opCurrencyName = setData.Find(x => x.Id == opContents[0].Currency)?.Name ?? "";
  181. //1.1币种验证
  182. if (!opCurrencyName.Equals(_teamCurrency))
  183. {
  184. if (!opCurrencyName.Equals("CNY"))
  185. {
  186. _view.Msg = $"OP费用币种与团组成本币种不一致,请手动审核";
  187. return _view;
  188. }
  189. }
  190. else _rate = 1.0000M;
  191. var opBasicDatas = setData.Where(x => x.STid == 17).ToList(); //费用类型基础数据
  192. bool isAutoAudit = true;
  193. if (!DateTime.TryParse(opinfos.ServiceStartTime, out DateTime startDt1) || !DateTime.TryParse(opinfos.ServiceEndTime, out DateTime endDt1))
  194. {
  195. _view.Msg = $"OP费用服务起止日期格式不正确!";
  196. return _view;
  197. }
  198. DateTime startDt = startDt1;
  199. DateTime endDt = endDt1;
  200. var opCostDatas = costContents.Where(it => Convert.ToDateTime(it.Date) >= startDt && Convert.ToDateTime(it.Date) <= endDt).ToList();
  201. if (opCostDatas.Count < 1)
  202. {
  203. _view.Msg = $"该时间段内团组成本未填写!";
  204. return _view;
  205. }
  206. //2.按天按项 检查费用是否超过预算
  207. var opDayContent = opContents.GroupBy(x => x.DatePrice);
  208. foreach (var item in opDayContent)
  209. {
  210. var opCostInfo = opCostDatas.Where(x => Convert.ToDateTime(x.Date) == item.Key).ToList();
  211. if (opCostInfo.Count < 1) continue;
  212. //车费 91
  213. var opCarCost = item.FirstOrDefault(x => x.SId == 91);
  214. if (opCarCost != null) if (opCarCost.Price > _rate * opCostInfo.Sum(x => x.CarFee)) isAutoAudit = false;
  215. //982 车超时费 -- 暂无
  216. //92 导游费
  217. var opGuideCost = item.FirstOrDefault(x => x.SId == 92);
  218. if (opGuideCost != null) if (opGuideCost.Price > _rate * opCostInfo.Sum(x => x.GuideFee)) isAutoAudit = false;
  219. //94 导游景点费
  220. var opGuideScenicCost = item.FirstOrDefault(x => x.SId == 94);
  221. if (opGuideScenicCost != null) if (opGuideScenicCost.Price > _rate * opCostInfo.Sum(x => x.GuideScenicFee)) isAutoAudit = false;
  222. //95 导游小费
  223. var opGuideTipCost = item.FirstOrDefault(x => x.SId == 95);
  224. if (opGuideTipCost != null) if (opGuideTipCost.Price > _rate * opCostInfo.Sum(x => x.GuideTipFee)) isAutoAudit = false;
  225. //983 导游餐补
  226. var opGuideMealCost = item.FirstOrDefault(x => x.SId == 983);
  227. if (opGuideMealCost != null) if (opGuideMealCost.Price > _rate * opCostInfo.Sum(x => x.GuideMealFee)) isAutoAudit = false;
  228. //984 导游房补
  229. var opGuideRoomCost = item.FirstOrDefault(x => x.SId == 984);
  230. if (opGuideRoomCost != null) if (opGuideRoomCost.Price > _rate * opCostInfo.Sum(x => x.GuideRoomFee)) isAutoAudit = false;
  231. //985 导游交通
  232. var opGuideTrafficCost = item.FirstOrDefault(x => x.SId == 985);
  233. if (opGuideTrafficCost != null) if (opGuideTrafficCost.Price > _rate * opCostInfo.Sum(x => x.GuideTrafficFee)) isAutoAudit = false;
  234. //96 接送机费 -- 暂无
  235. //97 其他费用 -- 暂无
  236. //979 司机工资
  237. var opDriverCost = item.FirstOrDefault(x => x.SId == 979);
  238. if (opDriverCost != null) if (opDriverCost.Price > _rate * opCostInfo.Sum(x => x.DriverFee)) isAutoAudit = false;
  239. //980 司机小费
  240. var opDriverTipCost = item.FirstOrDefault(x => x.SId == 980);
  241. if (opDriverTipCost != null) if (opDriverTipCost.Price > _rate * opCostInfo.Sum(x => x.DriverTipFee)) isAutoAudit = false;
  242. //981 司机餐补
  243. var opDriverMealCost = item.FirstOrDefault(x => x.SId == 981);
  244. if (opDriverMealCost != null) if (opDriverMealCost.Price > _rate * opCostInfo.Sum(x => x.DriverMealFee)) isAutoAudit = false;
  245. //988 客户早餐费用
  246. var opClientBreakfastCost = item.FirstOrDefault(x => x.SId == 988);
  247. if (opClientBreakfastCost != null) if (opClientBreakfastCost.Price > _rate * opCostInfo.Sum(x => x.ClientBreakfastFee)) isAutoAudit = false;
  248. //93 客户午餐费用
  249. var opClientLunchCost = item.FirstOrDefault(x => x.SId == 93);
  250. if (opClientLunchCost != null) if (opClientLunchCost.Price > _rate * opCostInfo.Sum(x => x.ClientLunchFee)) isAutoAudit = false;
  251. //989 客户晚餐费用
  252. var opClientDinnerCost = item.FirstOrDefault(x => x.SId == 989);
  253. if (opClientDinnerCost != null) if (opClientDinnerCost.Price > _rate * opCostInfo.Sum(x => x.ClientDinnerFee)) isAutoAudit = false;
  254. //990 景点门票费
  255. var opScenicTicketCost = item.FirstOrDefault(x => x.SId == 990);
  256. if (opScenicTicketCost != null) if (opScenicTicketCost.Price > _rate * opCostInfo.Sum(x => x.ScenicTicketFee)) isAutoAudit = false;
  257. //991 饮料/零食/水果
  258. var opDrinkSnackFruitCost = item.FirstOrDefault(x => x.SId == 991);
  259. if (opDrinkSnackFruitCost != null) if (opDrinkSnackFruitCost.Price > _rate * opCostInfo.Sum(x => x.DrinkSnackFruitFee)) isAutoAudit = false;
  260. //992 住补费用 -- 暂无
  261. //994 翻译费
  262. var opTranslatorCost = item.FirstOrDefault(x => x.SId == 994);
  263. if (opTranslatorCost != null) if (opTranslatorCost.Price > _rate * opCostInfo.Sum(x => x.TranslatorFee)) isAutoAudit = false;
  264. //1059 导游超时费用
  265. //1070 尾款金额
  266. //1071 其他额外费用
  267. //1073 翻译超时费
  268. //1074 早餐超支费用
  269. //1075 午餐超支费用
  270. //1076 晚餐超支费用
  271. }
  272. //更改审核状态
  273. if (isAutoAudit)
  274. {
  275. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  276. .SetColumns(it => it.IsAuditGM == 3)
  277. .SetColumns(it => it.AuditGMOperate == 4)
  278. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  279. .Where(s => s.DIId == diId && s.CTable == 79 && s.CId == dataId)
  280. .ExecuteCommand();
  281. if (ccpUpdate > 0)
  282. {
  283. _view.Code = 200;
  284. _view.Msg = "自动审核执行成功!";
  285. return _view;
  286. }
  287. }
  288. else
  289. {
  290. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  291. var ccpInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>()
  292. .Where(s => s.DIId == diId && s.CTable == 79 && s.CId == dataId && s.IsAuditGM == 3)
  293. .First();
  294. if (ccpInfo != null)
  295. {
  296. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  297. .SetColumns(it => it.IsAuditGM == 0)
  298. .SetColumns(it => it.AuditGMOperate == 0)
  299. .SetColumns(it => it.AuditGMDate == string.Empty)
  300. .Where(s => s.Id == ccpInfo.Id)
  301. .ExecuteCommand();
  302. if (ccpUpdate > 0)
  303. {
  304. _view.Code = 200;
  305. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  306. return _view;
  307. }
  308. }
  309. }
  310. }
  311. else _view.Msg = $"请传入有效的feeType参数";
  312. return _view;
  313. }
  314. }
  315. }