DailyFeePaymentRepository.cs 32 KB

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