DailyFeePaymentRepository.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. using AutoMapper;
  2. using Microsoft.Extensions.Logging;
  3. using Serilog;
  4. using Newtonsoft.Json;
  5. using OASystem.Domain;
  6. using OASystem.Domain.Dtos;
  7. using OASystem.Domain.Dtos.Financial;
  8. using OASystem.Domain.Dtos.PersonnelModule;
  9. using OASystem.Domain.Entities.Financial;
  10. using OASystem.Domain.Entities.PersonnelModule;
  11. using OASystem.Domain.ViewModels.Financial;
  12. using OASystem.Infrastructure.Repositories.PersonnelModule;
  13. using OASystem.Infrastructure.Repositories.System;
  14. namespace OASystem.Infrastructure.Repositories.Financial
  15. {
  16. /// <summary>
  17. /// 财务 - 日付申请
  18. /// </summary>
  19. public class DailyFeePaymentRepository : BaseRepository<Fin_DailyFeePayment, Fin_DailyFeePaymentView>
  20. {
  21. private readonly IMapper _mapper;
  22. private readonly SetDataTypeRepository _setDataTypeRep;
  23. private readonly UsersRepository _UsersRep;
  24. private readonly CompanyRepository _CompanyRep;
  25. private readonly GoodsRepository _goodsRep;
  26. private readonly ILogger<DailyFeePaymentRepository> _logger;
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. /// <param name="sqlSugar"></param>
  31. /// <param name="mapper"></param>
  32. /// <param name="setDataTypeRep"></param>
  33. /// <param name="usersRep"></param>
  34. /// <param name="companyRep"></param>
  35. /// <param name="goodsRep"></param>
  36. /// <param name="logger"></param>
  37. public DailyFeePaymentRepository(SqlSugarClient sqlSugar,
  38. IMapper mapper,
  39. SetDataTypeRepository setDataTypeRep,
  40. UsersRepository usersRep,
  41. CompanyRepository companyRep,
  42. GoodsRepository goodsRep,
  43. ILogger<DailyFeePaymentRepository> logger)
  44. : base(sqlSugar)
  45. {
  46. _mapper = mapper;
  47. _setDataTypeRep = setDataTypeRep;
  48. _UsersRep = usersRep;
  49. _CompanyRep = companyRep;
  50. _goodsRep = goodsRep;
  51. _logger = logger;
  52. }
  53. /// <summary>
  54. /// 日付申请查询 使用的数据源
  55. /// </summary>
  56. /// <param name="dto"></param>
  57. /// <returns></returns>
  58. public async Task<Result> GetPagePriceTypeData(PortDtoBase dto, int currUserId = 0)
  59. {
  60. Result result = new Result() { Code = -2 };
  61. dynamic? DailyFeePaymentList = null;
  62. var setTypeData = _setDataTypeRep.QueryDto<Sys_SetDataType, Fin_DailyFeePaymentPagePriceTypeView>().ToList();
  63. var setData = _setDataTypeRep.QueryDto<Sys_SetData, Fin_DailyFeePaymentPagePriceSubTypeView>().ToList();
  64. //48人员费用 49办公费用 50 销售费用 51 其他费用 55 大运会
  65. var typeIds = new List<int>() {
  66. 48, //人员费用
  67. 49, //办公费用
  68. 50, //销售费用
  69. 51, //其他费用
  70. 55, //大运会
  71. 90, //各部门基础固定费用明细
  72. 104, //团组费用
  73. 116, //物资采购
  74. };
  75. var priceTypeData = setTypeData.Where(s => typeIds.Contains(s.Id)).ToList();
  76. var priceSubTypeData = setData.Where(s => typeIds.Contains(s.STid)).ToList();
  77. //var isEnable = false;
  78. //员工姓名列表
  79. var userData = _sqlSugar.Queryable<Sys_Users>()
  80. .LeftJoin<Sys_Department>((u, d) => u.DepId == d.Id)
  81. .LeftJoin<Sys_JobPost>((u, d, jp) => u.JobPostId == jp.Id)
  82. .Where((u, d, jp) => u.IsDel == 0)
  83. .Select((u, d, jp) => new
  84. {
  85. u.Id,
  86. u.CnName,
  87. u.DepId,
  88. d.DepName,
  89. u.JobPostId,
  90. jp.JobName
  91. })
  92. .ToList();
  93. //1 经理职位 查看该部门下所有人员
  94. if (currUserId > 0)
  95. {
  96. //42
  97. var auditUserIds = _sqlSugar.Queryable<Sys_UserAuthority>().Where(x => x.IsDel == 0 && x.SmId == 42 && x.FId == 12).Select(x => x.UId).ToList();
  98. if (!auditUserIds.Contains(currUserId))
  99. {
  100. var screenWheres = new List<string>() { "经理", "主管" };
  101. var userInfo = userData.Find(x => x.Id == currUserId && screenWheres.Contains(x.JobName));
  102. if (userInfo != null)
  103. {
  104. userData = userData.Where(x => x.DepName.Equals(userInfo.DepName)).ToList();
  105. }
  106. else
  107. {
  108. userData = userData.Where(x => x.Id == currUserId).ToList();
  109. }
  110. }
  111. else
  112. {
  113. userData.Insert(0, new { Id = -1, CnName = "全部", DepId = 0, DepName = "", JobPostId = 0, JobName = "" });
  114. }
  115. }
  116. var userData1 = userData.Select(x => new { x.Id, x.CnName }).ToList();
  117. //62 公转 63 私转
  118. var feeMarkTypeData = setTypeData.Where(s => s.Id == 62 || s.Id == 63).ToList();
  119. var feeMarkSubTypeData = setData.Where(s => s.STid == 62 || s.STid == 63).ToList();
  120. var companyNameData = await _CompanyRep.GetCompanyNameData();
  121. //审核状态
  122. var auditStatusData = new List<dynamic>()
  123. {
  124. new { Id = -1, Name = "全部" },
  125. new { Id = 0, Name = "未审核" },
  126. new { Id = 1, Name = "已审核" },
  127. new { Id = 2, Name = "审核未通过" },
  128. new { Id = 3, Name = "自动审核" }
  129. };
  130. if (dto.PortType == 1) //web
  131. {
  132. DailyFeePaymentList = new
  133. {
  134. FeeTypeData = priceTypeData,
  135. FeeSubTypeData = priceSubTypeData,
  136. UserNameData = userData1,
  137. FeeMarkTypeData = feeMarkTypeData,
  138. FeeMarkSubTypeData = feeMarkSubTypeData,
  139. CompanyNameData = companyNameData.Data,
  140. AuditStatusData = auditStatusData
  141. };
  142. }
  143. else if (dto.PortType == 2) //安卓
  144. {
  145. DailyFeePaymentList = new
  146. {
  147. UserNameData = userData1,
  148. FeeTypeData = priceTypeData,
  149. FeeTypeSubData = priceSubTypeData
  150. };
  151. }
  152. result.Code = 0;
  153. result.Msg = "查询成功!";
  154. result.Data = DailyFeePaymentList;
  155. return result;
  156. }
  157. /// <summary>
  158. /// 总经理自动审核通过类型
  159. /// 90 Parent 各部门基础固定费用明细
  160. /// 104 Parent 团组费用
  161. /// 672 Sub 办公费用-信息部申请费用
  162. /// 1433 Sub 团组/会务储备物资采购
  163. /// </summary>
  164. /// <param name="typeId"></param>
  165. /// <returns></returns>
  166. public async Task<bool> GMAutoApprovalType(int typeId)
  167. {
  168. if (typeId < 1) return false;
  169. var parentIds = new List<int>()
  170. {
  171. 90, // 各部门基础固定费用明细
  172. 104, // 团组费用
  173. };
  174. var subIds = new List<int>()
  175. {
  176. 672, //办公费用-信息部申请费用
  177. 1433, //团组/会务储备物资采购
  178. };
  179. var parentTypeDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0 && parentIds.Contains(x.STid)).ToListAsync();
  180. var subTypeDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0 && subIds.Contains(x.Id)).ToListAsync();
  181. var typeDatas = new List<Sys_SetData>();
  182. if (parentTypeDatas.Any()) typeDatas.AddRange(parentTypeDatas);
  183. if (subTypeDatas.Any()) typeDatas.AddRange(subTypeDatas);
  184. if (typeDatas.Any(x => x.Id == typeId)) return true;
  185. return false;
  186. }
  187. /// <summary>
  188. /// 日付申请查询 使用的数据源
  189. /// </summary>
  190. /// <param name="dto"></param>
  191. /// <returns></returns>
  192. public async Task<Result> GetPriceTypeAddData(PortDtoBase dto)
  193. {
  194. Result result = new() { Code = -2 };
  195. dynamic? DailyFeePaymentList = null;
  196. var setTypeData = _setDataTypeRep.QueryDto<Sys_SetDataType, Fin_DailyFeePaymentPagePriceTypeView>().ToList();
  197. var setData = _setDataTypeRep.QueryDto<Sys_SetData, Fin_DailyFeePaymentPagePriceSubTypeView>().ToList();
  198. //48人员费用 49办公费用 50 销售费用 51 其他费用 55 大运会
  199. var priceTypeData = setTypeData.Where(s => s.Id == 48 || s.Id == 49 || s.Id == 50 || s.Id == 51 || s.Id == 55).ToList();
  200. var priceSubTypeData = setData.Where(s => s.STid == 48 || s.STid == 49 || s.STid == 50 || s.STid == 51 || s.STid == 55).ToList();
  201. //员工姓名列表
  202. var userNameData = await _UsersRep.GetUserNameList(dto.PortType);
  203. //62 公转 63 私转
  204. var feeMarkTypeData = setTypeData.Where(s => s.Id == 62 || s.Id == 63).ToList();
  205. var feeMarkSubTypeData = setData.Where(s => s.STid == 62 || s.STid == 63).ToList();
  206. var companyNameData = await _CompanyRep.GetCompanyNameData();
  207. if (dto.PortType == 1) //web
  208. {
  209. DailyFeePaymentList = new Fin_DailyFeePaymentPagePriceTypeDataView
  210. {
  211. FeeTypeData = priceTypeData,
  212. FeeSubTypeData = priceSubTypeData,
  213. UserNameData = userNameData.Data,
  214. FeeMarkTypeData = feeMarkTypeData,
  215. FeeMarkSubTypeData = feeMarkSubTypeData,
  216. CompanyNameData = companyNameData.Data
  217. };
  218. }
  219. else if (dto.PortType == 2) //安卓
  220. {
  221. DailyFeePaymentList = new
  222. {
  223. CompanyNameData = companyNameData.Data,
  224. FeeTypeData = feeMarkTypeData,
  225. FeeTypeSubData = feeMarkSubTypeData
  226. };
  227. }
  228. result.Code = 0;
  229. result.Msg = "查询成功!";
  230. result.Data = DailyFeePaymentList;
  231. return result;
  232. }
  233. /// <summary>
  234. /// 日付申请 page 查询
  235. /// </summary>
  236. /// <param name="dto"></param>
  237. /// <returns></returns>
  238. public async Task<Result> GetPageSearchAll(PageDailyFeePaymentDto dto)
  239. {
  240. Result result = new Result() { Code = -2 };
  241. ListViewBase<Fin_DailyFeePaymentPageListView> dailyFeePaymentPageList = new ListViewBase<Fin_DailyFeePaymentPageListView>()
  242. {
  243. ReceiveDt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  244. };
  245. #region sql条件处理
  246. string sqlWhere = "";
  247. if (dto.CompanyId != -1) //公司
  248. sqlWhere += string.Format(" And dfp.CompanyId = {0}", dto.CompanyId);
  249. if (dto.FinancialAuditStatus != -1) //财务审核
  250. sqlWhere += string.Format(" And dfp.FAudit = {0}", dto.FinancialAuditStatus);
  251. if (dto.ManagerAuditStatus != -1) //总经理审核
  252. sqlWhere += string.Format(" And dfp.MAudit = {0}", dto.ManagerAuditStatus);
  253. if (dto.IsPaySign != -1) //付款状态
  254. {
  255. sqlWhere += string.Format(" And dfp.IsPay = {0}", dto.IsPaySign);
  256. }
  257. if (dto.FeeTypeId != -1) //费用类型
  258. {
  259. if (dto.FeeSubTypeId != -1) //子类处理
  260. {
  261. sqlWhere += string.Format(" And dfp.PriceTypeId = {0}", dto.FeeSubTypeId);
  262. }
  263. else
  264. {
  265. var setData = _setDataTypeRep.QueryDto<Sys_SetData, SetDataView>(s => s.STid == dto.FeeTypeId).ToList();
  266. if (setData.Count > 0)
  267. {
  268. string setDataIds = string.Join(",", setData.Select(x => x.Id).ToList());
  269. sqlWhere += string.Format(" And dfp.PriceTypeId In ({0})", setDataIds);
  270. }
  271. }
  272. }
  273. if (!string.IsNullOrEmpty(dto.FeeDesc))
  274. sqlWhere += string.Format(" And dfp.Instructions Like '%{0}%'", dto.FeeDesc);
  275. if (dto.CreateUserId != -1)
  276. sqlWhere += string.Format(" And dfp.CreateUserId = {0}", dto.CreateUserId);
  277. int startIndex = (dto.PageIndex - 1) * dto.PageSize + 1;
  278. int endIndex = startIndex + dto.PageSize - 1;
  279. string sqlPage = string.Format(@"Select * From (
  280. Select row_number() over (order by dfp.Id Desc) as RowNumber,
  281. dfp.Id,dfp.CompanyId,c.CompanyName,dfp.Instructions,dfp.SumPrice,
  282. dfp.CreateUserId,u.CnName CreateUser,dfp.CreateTime,dfp.FAudit,dfp.MAudit,
  283. dfp.PriceTypeId,dfp.IsPay
  284. From Fin_DailyFeePayment dfp
  285. Inner Join Sys_Company c On dfp.CompanyId = c.Id
  286. Left Join Sys_Users u On dfp.CreateUserId = u.Id
  287. Where dfp.IsDel=0 {0}
  288. ) temp Where RowNumber Between {1} and {2}", sqlWhere, startIndex, endIndex);
  289. string sqlCount = string.Format(@"Select COUNT(1) as Count From (
  290. Select dfp.Id,dfp.CompanyId,c.CompanyName,dfp.Instructions,dfp.SumPrice,
  291. dfp.CreateUserId,u.CnName CreateUser,dfp.CreateTime,dfp.FAudit,dfp.MAudit,
  292. dfp.PriceTypeId
  293. From Fin_DailyFeePayment dfp
  294. Inner Join Sys_Company c On dfp.CompanyId = c.Id
  295. Left Join Sys_Users u On dfp.CreateUserId = u.Id
  296. Where dfp.IsDel=0 {0}
  297. ) temp ", sqlWhere);
  298. #endregion
  299. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3) //web
  300. {
  301. //Fin_DailyFeePaymentPageCount
  302. var dailyFeePaymentCount = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentPageCount>(sqlCount).FirstAsync();
  303. var DailyFeePaymentData = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentPageListView>(sqlPage).ToListAsync();
  304. int totalCount = dailyFeePaymentCount.Count;
  305. dailyFeePaymentPageList = new ListViewBase<Fin_DailyFeePaymentPageListView>
  306. {
  307. CurrPageIndex = dto.PageIndex,
  308. CurrPageSize = dto.PageSize,
  309. DataCount = totalCount,
  310. DataList = DailyFeePaymentData
  311. };
  312. }
  313. result.Code = 0;
  314. result.Msg = "查询成功!";
  315. result.Data = dailyFeePaymentPageList;
  316. return result;
  317. }
  318. /// <summary>
  319. /// 日付申请 single 查询 By Id
  320. /// </summary>
  321. /// <param name="dto"></param>
  322. /// <returns></returns>
  323. public async Task<Result> GetSearchById(SearchDailyFeePaymentDto dto)
  324. {
  325. Result result = new Result() { Code = -2 };
  326. if (dto.PortType == 1) //web
  327. {
  328. Fin_DailyFeePaymentInfolView feeData = new Fin_DailyFeePaymentInfolView();
  329. string feeSql = string.Format(@"Select * From Fin_DailyFeePayment
  330. Where IsDel=0 And Id = {0} ", dto.Id);
  331. feeData = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentInfolView>(feeSql).FirstAsync();
  332. string feeContentSql = string.Format(@"Select * From Fin_DailyFeePaymentContent
  333. Where IsDel=0 And DFPId = {0} ", dto.Id);
  334. feeData.FeeContents = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentContentInfolView>(feeContentSql).ToListAsync();
  335. result.Code = 0;
  336. result.Msg = "查询成功!";
  337. result.Data = feeData;
  338. }
  339. else if (dto.PortType == 2 || dto.PortType == 3) //android And ios
  340. {
  341. Fin_DailyFeePaymentInfoAndroidlView feeData = new Fin_DailyFeePaymentInfoAndroidlView();
  342. string feeSql = string.Format(@"Select * From Fin_DailyFeePayment
  343. Where IsDel=0 And Id = {0} ", dto.Id);
  344. feeData = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentInfoAndroidlView>(feeSql).FirstAsync();
  345. //feeData.TransferTypeId = feeData.TransferTypeId == 0 ? 62 : feeData.TransferTypeId == 1 ? 63 : 0;
  346. string feeContentSql = string.Format(@"Select * From Fin_DailyFeePaymentContent
  347. Where IsDel=0 And DFPId = {0} ", dto.Id);
  348. feeData.FeeContents = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentContentInfolView>(feeContentSql).ToListAsync();
  349. result.Code = 0;
  350. result.Msg = "查询成功!";
  351. result.Data = feeData;
  352. }
  353. return result;
  354. }
  355. /// <summary>
  356. /// 日付申请 添加
  357. /// </summary>
  358. /// <param name="dto"></param>
  359. /// <returns></returns>
  360. public async Task<Result> Add(AddDailyFeePaymentDto dto)
  361. {
  362. Result result = new Result() { Code = -2 };
  363. _sqlSugar.BeginTran();
  364. var _fee = _mapper.Map<Fin_DailyFeePayment>(dto);
  365. _fee.CreateUserId = dto.UserId;
  366. _fee.GroupId = dto.GroupId;
  367. //自动审核验证
  368. var auditPer = await GMAutoApprovalType(dto.PriceTypeId);
  369. var priceTypeInfo = await _sqlSugar.Queryable<Sys_SetData>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.PriceTypeId);
  370. if (auditPer)
  371. {
  372. _fee.MAudit = 3;
  373. _fee.MAuditDate = DateTime.Now;
  374. }
  375. int? feeId = await _sqlSugar.Insertable(_fee).ExecuteReturnIdentityAsync();
  376. if (dto.FeeContents.Any())
  377. {
  378. var _feeContents = _mapper.Map<List<Fin_DailyFeePaymentContent>>(dto.FeeContents);
  379. foreach (var item in _feeContents)
  380. {
  381. item.DFPId = feeId == null ? -1 : Convert.ToInt32(feeId);
  382. item.CreateUserId = dto.UserId;
  383. }
  384. await _sqlSugar.Insertable(_feeContents).ExecuteCommandAsync();
  385. }
  386. _sqlSugar.CommitTran();
  387. result.Code = 0;
  388. var data = new { dailyId = feeId, sign = 1 };
  389. result.Data = data;
  390. return result;
  391. }
  392. /// <summary>
  393. /// 日付申请 编辑
  394. /// </summary>
  395. /// <param name="dto"></param>
  396. /// <returns></returns>
  397. public async Task<Result> Edit(EditDailyFeePaymentDto dto)
  398. {
  399. Result result = new Result() { Code = -2 };
  400. //自动审核验证
  401. var auditPer = await GMAutoApprovalType(dto.PriceTypeId);
  402. #region 已审核的数据不可编辑
  403. var dailyFeeInfo = await _sqlSugar.Queryable<Fin_DailyFeePayment>().Where(it => it.Id == dto.Id && it.IsDel == 0).FirstAsync();
  404. if (dailyFeeInfo != null)
  405. {
  406. var auditIds = new List<int>() { 1, 3 };
  407. if (auditIds.Contains(dailyFeeInfo.FAudit) || auditIds.Contains(dailyFeeInfo.MAudit))
  408. {
  409. if (auditPer)
  410. {
  411. if (dailyFeeInfo.IsPay == 1)
  412. {
  413. result.Msg = "该笔费用已付款,不可修改!";
  414. return result;
  415. }
  416. }
  417. else
  418. {
  419. result.Msg = "审核已通过,不可修改!";
  420. return result;
  421. }
  422. }
  423. }
  424. #endregion
  425. _sqlSugar.BeginTran();
  426. try
  427. {
  428. Fin_DailyFeePayment _fee = _mapper.Map<Fin_DailyFeePayment>(dto);
  429. _fee.CreateUserId = dto.UserId;
  430. int? editFeeStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  431. .SetColumns(a => new Fin_DailyFeePayment
  432. {
  433. Instructions = dto.Instructions,
  434. SumPrice = dto.SumPrice,
  435. TransferTypeId = dto.TransferTypeId,
  436. PriceTypeId = dto.PriceTypeId,
  437. CompanyId = dto.CompanyId,
  438. GroupId = dto.GroupId,
  439. })
  440. .SetColumnsIF(auditPer, a => a.MAudit == 3)
  441. .SetColumnsIF(auditPer, a => a.MAuditDate == DateTime.Now)
  442. .Where(a => a.Id == dto.Id)
  443. .ExecuteCommandAsync();
  444. var _feeContents = _mapper.Map<List<Fin_DailyFeePaymentContent>>(dto.FeeContents);
  445. await _sqlSugar.Updateable<Fin_DailyFeePaymentContent>()
  446. .Where(a => a.DFPId == _fee.Id)
  447. .SetColumns(a => new Fin_DailyFeePaymentContent
  448. {
  449. IsDel = 1,
  450. DeleteUserId = _fee.CreateUserId,
  451. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  452. })
  453. .ExecuteCommandAsync();
  454. if (_feeContents.Any())
  455. {
  456. foreach (var item in _feeContents)
  457. {
  458. item.DFPId = _fee.Id;
  459. item.CreateUserId = dto.UserId;
  460. }
  461. await _sqlSugar.Insertable(_feeContents).ExecuteCommandAsync();
  462. }
  463. _sqlSugar.CommitTran();
  464. result.Code = 0;
  465. var data = new { dailyId = dto.Id, sign = 2 };
  466. result.Data = data;
  467. }
  468. catch (Exception ex)
  469. {
  470. _sqlSugar.RollbackTran();
  471. result.Msg = ex.Message;
  472. }
  473. return result;
  474. }
  475. /// <summary>
  476. /// 日付申请 删除
  477. /// </summary>
  478. /// <param name="dto"></param>
  479. /// <returns></returns>
  480. public async Task<Result> Del(DelDailyFeePaymentDto dto)
  481. {
  482. Result result = new Result() { Code = -2 };
  483. int? delFeeStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  484. .Where(a => a.Id == dto.Id)
  485. .SetColumns(a => new Fin_DailyFeePayment
  486. {
  487. IsDel = 1,
  488. DeleteUserId = dto.UserId,
  489. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  490. }).ExecuteCommandAsync();
  491. result.Code = 0;
  492. return result;
  493. }
  494. /// <summary>
  495. /// 日付申请 审核
  496. /// </summary>
  497. /// <param name="dto"></param>
  498. /// <returns></returns>
  499. public async Task<Result> DailyPaymentAudit(DP_AuditStatusDto dto)
  500. {
  501. Result result = new Result() { Code = -2 };
  502. if (dto.AuditType == 1) //财务审核
  503. {
  504. int auditStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  505. .Where(a => a.Id == dto.Id)
  506. .SetColumns(a => new Fin_DailyFeePayment
  507. {
  508. FAudit = dto.AuditStatus,
  509. FAuditDate = DateTime.Now,
  510. }).ExecuteCommandAsync();
  511. if (auditStatus > 0) result.Code = 0;
  512. else result.Msg = "财务审核操作失败";
  513. }
  514. else if (dto.AuditType == 2) //总经理
  515. {
  516. int auditStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  517. .Where(a => a.Id == dto.Id)
  518. .SetColumns(a => new Fin_DailyFeePayment
  519. {
  520. MAudit = dto.AuditStatus,
  521. MAuditDate = DateTime.Now,
  522. }).ExecuteCommandAsync();
  523. if (auditStatus > 0) result.Code = 0;
  524. else result.Msg = "总经理审核操作失败";
  525. }
  526. #region 审核完全通过 指定类型 向物资添加信息
  527. //费用类型
  528. var feeTypeIds = new List<int>() {
  529. 1433, //团组/会务储备物资采购
  530. 1435, //公司固定物资采购
  531. };
  532. //审核状态集合
  533. var auditStatuses = new List<int>() {
  534. 1, //审核通过
  535. 3, //自动审核
  536. };
  537. var dailyInfo = await _sqlSugar.Queryable<Fin_DailyFeePayment>()
  538. .FirstAsync(x => x.Id == dto.Id && x.IsDel == 0 &&
  539. auditStatuses.Contains(x.MAudit) &&
  540. auditStatuses.Contains(x.FAudit) &&
  541. feeTypeIds.Contains(x.PriceTypeId));
  542. if (dailyInfo != null)
  543. {
  544. //执行物资添加、入库、关联信息操作
  545. var linkOpRes = await ApplicationLinkGoodsAddAsync(dailyInfo.Id);
  546. // Updated the logging statement to ensure consistent message templates
  547. _logger.LogInformation("【日付申请单关联物品】请求参数:{DailyInfoId}\r\n返回参数:{LinkOpResData}\r\n返回消息:{LinkOpResMsg}",
  548. dailyInfo.Id,
  549. JsonConvert.SerializeObject(linkOpRes.Data),
  550. linkOpRes.Msg);
  551. }
  552. #endregion
  553. return result;
  554. }
  555. /// <summary>
  556. /// 购买申请单关联物品
  557. /// </summary>
  558. /// <param name="applicatId">日付申请Id</param>
  559. /// <returns></returns>
  560. public async Task<JsonView> ApplicationLinkGoodsAddAsync(int applicatId)
  561. {
  562. if (applicatId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = $"日付申请单ID不正确!" };
  563. //if (currUserId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = $"当前登陆人ID不正确!" };
  564. var dailyInfoRes = await GetSearchById(new Domain.Dtos.Financial.SearchDailyFeePaymentDto() { PortType = 1, Id = applicatId });
  565. if (dailyInfoRes.Code != 0) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = dailyInfoRes?.Msg ?? "日付申请单信息不存在!" };
  566. string dynamicJson = JsonConvert.SerializeObject(dailyInfoRes.Data);
  567. Fin_DailyFeePaymentInfolView dailyInfo = JsonConvert.DeserializeObject<Fin_DailyFeePaymentInfolView>(dynamicJson);
  568. if (dailyInfo == null || !dailyInfo.FeeContents.Any())
  569. {
  570. return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = $"付申请单信息不存在!!" };
  571. }
  572. //审核验证 总经理 && 财务 审核通过 才可进行 物品添加、入库、关联信息 操作
  573. if (dailyInfo.MAudit == 0 || dailyInfo.MAudit == 2 || dailyInfo.FAudit == 0 || dailyInfo.FAudit == 2)
  574. {
  575. return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = $"审核未通过,不可进行物资关联信息!" };
  576. }
  577. _sqlSugar.BeginTran();
  578. //1.验证物品信息
  579. var currGoodsTypeId = dailyInfo.PriceTypeId switch
  580. {
  581. 1433 => 1424, //团组物资
  582. 1435 => 1426, //固定资产
  583. _ => 0
  584. };
  585. var paras = new List<dynamic>();
  586. foreach (var item in dailyInfo.FeeContents)
  587. {
  588. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(item.PriceName) && x.IsDel == 0)
  589. ?? new Pm_GoodsInfo()
  590. {
  591. Id = 0,
  592. Name = item.PriceName,
  593. Type = currGoodsTypeId,
  594. Unit = "-",
  595. CreateUserId = dailyInfo.CreateUserId,
  596. Remark = $"日付申请模块新增物品信息(需自行更改单位):\n{item.Remark}",
  597. };
  598. //物品不存在执行添加
  599. if (goodsInfo.Id == 0)
  600. {
  601. goodsInfo.Id = await _sqlSugar.Insertable(goodsInfo).ExecuteReturnIdentityAsync();
  602. if (goodsInfo.Id < 1)
  603. {
  604. _sqlSugar.RollbackTran();
  605. return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = $"物品添加失败!" };
  606. }
  607. }
  608. //执行入库信息添加
  609. var storageRes = await _goodsRep.GoodsStorageOp(new GoodsStorageOpDto()
  610. {
  611. GoodsId = goodsInfo.Id,
  612. Quantity = item.Quantity,
  613. UnitPrice = item.Price,
  614. TotalPrice = item.ItemTotal,
  615. SupplierName = "-",
  616. SupplierTel = "-",
  617. SupplierAddress = "-",
  618. SupplierSource = "-",
  619. StorageUserId = 374, //固定前台ID为入库人
  620. StorageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  621. Remark = $"日付申请模块新增物品入库信息:{item.Remark}",
  622. }, 374);
  623. if (storageRes.Code != StatusCodes.Status200OK)
  624. {
  625. _sqlSugar.RollbackTran();
  626. return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = $"物品入库信息添加失败!" };
  627. }
  628. var storageId = Convert.ToInt32(storageRes.Data.GetType().GetProperty("sign").GetValue(storageRes.Data, null) ?? 0);
  629. //执行关联表添加
  630. var addStatus = await _sqlSugar.Insertable(new Fin_ApplicationLinkGoods(
  631. id: 0,
  632. dailyId: dailyInfo.Id,
  633. goodsId: goodsInfo.Id,
  634. goodsStorageId: storageId,
  635. remark: "",
  636. userId: dailyInfo.CreateUserId
  637. )).ExecuteCommandAsync();
  638. if (addStatus < 1)
  639. {
  640. _sqlSugar.RollbackTran();
  641. return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = $"日付、物品入库信息关联信息添加失败!" };
  642. }
  643. paras.Add(new
  644. {
  645. dailyId = dailyInfo.Id,
  646. goodsId = goodsInfo.Id,
  647. GoodsStorageId = storageId,
  648. LinkTableId = addStatus
  649. });
  650. }
  651. _sqlSugar.CommitTran();
  652. return new JsonView() { Code = StatusCodes.Status200OK, Data = paras, Msg = $"操作成功!" };
  653. }
  654. }
  655. }