DailyFeePaymentRepository.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using AutoMapper;
  2. using NPOI.SS.Formula.Functions;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos;
  5. using OASystem.Domain.Dtos.Financial;
  6. using OASystem.Domain.Entities.Financial;
  7. using OASystem.Domain.ViewModels.Financial;
  8. using OASystem.Infrastructure.Repositories.System;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel.Design;
  12. using System.Linq;
  13. using System.Runtime.Intrinsics.Arm;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace OASystem.Infrastructure.Repositories.Financial
  17. {
  18. /// <summary>
  19. /// 财务 - 日付申请
  20. /// </summary>
  21. public class DailyFeePaymentRepository : BaseRepository<Fin_DailyFeePayment, Fin_DailyFeePaymentView>
  22. {
  23. private readonly IMapper _mapper;
  24. private readonly System.SetDataTypeRepository _setDataTypeRep;
  25. private readonly System.UsersRepository _UsersRep;
  26. private readonly System.CompanyRepository _CompanyRep;
  27. public DailyFeePaymentRepository(SqlSugarClient sqlSugar, IMapper mapper, System.SetDataTypeRepository setDataTypeRep,
  28. UsersRepository usersRep, CompanyRepository companyRep)
  29. : base(sqlSugar)
  30. {
  31. this._mapper = mapper;
  32. this._setDataTypeRep = setDataTypeRep;
  33. this._UsersRep = usersRep;
  34. this._CompanyRep = companyRep;
  35. }
  36. /// <summary>
  37. /// 日付申请查询 使用的数据源
  38. /// </summary>
  39. /// <param name="dto"></param>
  40. /// <returns></returns>
  41. public async Task<Result> GetPagePriceTypeData(PortDtoBase dto, int currUserId = 0)
  42. {
  43. Result result = new Result() { Code = -2 };
  44. dynamic? DailyFeePaymentList = null;
  45. var setTypeData = _setDataTypeRep.QueryDto<Sys_SetDataType, Fin_DailyFeePaymentPagePriceTypeView>().ToList();
  46. var setData = _setDataTypeRep.QueryDto<Sys_SetData, Fin_DailyFeePaymentPagePriceSubTypeView>().ToList();
  47. //48人员费用 49办公费用 50 销售费用 51 其他费用 55 大运会
  48. var priceTypeData = setTypeData.Where(s => s.Id == 48 || s.Id == 49 || s.Id == 50 || s.Id == 51 || s.Id == 55).ToList();
  49. var priceSubTypeData = setData.Where(s => s.STid == 48 || s.STid == 49 || s.STid == 50 || s.STid == 51 || s.STid == 55).ToList();
  50. var isEnable = false;
  51. //员工姓名列表
  52. var userData = _sqlSugar.Queryable<Sys_Users>()
  53. .Where(x => x.IsDel == 0)
  54. .Select(x => new { x.Id, x.DepId, x.CnName })
  55. .ToList();
  56. //1 经理职位 查看该部门下所有人员
  57. if (currUserId > 0)
  58. {
  59. //42
  60. var auditUserIds = _sqlSugar.Queryable<Sys_UserAuthority>().Where(x => x.IsDel == 0 && x.SmId == 42 && x.FId == 12).Select(x => x.UId).ToList();
  61. if (!auditUserIds.Contains(currUserId))
  62. {
  63. var screenWheres = new List<string>() { "经理", "主管" };
  64. var userInfo = _sqlSugar.Queryable<Sys_Users>()
  65. .LeftJoin<Sys_Department>((u, d) => u.DepId == d.Id)
  66. .LeftJoin<Sys_JobPost>((u, d, jp) => u.JobPostId == jp.Id)
  67. .Where((u, d, jp) => u.IsDel == 0 && u.Id == currUserId && screenWheres.Contains(jp.JobName))
  68. .Select((u, d, jp) => new
  69. {
  70. u.Id,
  71. u.CnName,
  72. u.DepId,
  73. d.DepName,
  74. u.JobPostId,
  75. jp.JobName
  76. })
  77. .First();
  78. if (userInfo != null)
  79. {
  80. userData = userData.Where(x => x.DepId == userInfo.DepId).ToList();
  81. }
  82. else
  83. {
  84. userData = userData.Where(x => x.Id == currUserId).ToList();
  85. }
  86. }
  87. }
  88. var userData1 = userData.Select(x => new { x.Id, x.CnName }).ToList();
  89. //62 公转 63 私转
  90. var feeMarkTypeData = setTypeData.Where(s => s.Id == 62 || s.Id == 63).ToList();
  91. var feeMarkSubTypeData = setData.Where(s => s.STid == 62 || s.STid == 63).ToList();
  92. var companyNameData = await _CompanyRep.GetCompanyNameData();
  93. if (dto.PortType == 1) //web
  94. {
  95. DailyFeePaymentList = new
  96. {
  97. FeeTypeData = priceTypeData,
  98. FeeSubTypeData = priceSubTypeData,
  99. UserNameData = userData1,
  100. FeeMarkTypeData = feeMarkTypeData,
  101. FeeMarkSubTypeData = feeMarkSubTypeData,
  102. CompanyNameData = companyNameData.Data
  103. };
  104. }
  105. else if (dto.PortType == 2) //安卓
  106. {
  107. DailyFeePaymentList = new
  108. {
  109. UserNameData = userData1,
  110. FeeTypeData = priceTypeData,
  111. FeeTypeSubData = priceSubTypeData
  112. };
  113. }
  114. result.Code = 0;
  115. result.Msg = "查询成功!";
  116. result.Data = DailyFeePaymentList;
  117. return result;
  118. }
  119. /// <summary>
  120. /// 日付申请查询 使用的数据源
  121. /// </summary>
  122. /// <param name="dto"></param>
  123. /// <returns></returns>
  124. public async Task<Result> GetPriceTypeAddData(PortDtoBase dto)
  125. {
  126. Result result = new Result() { Code = -2 };
  127. dynamic? DailyFeePaymentList = null;
  128. var setTypeData = _setDataTypeRep.QueryDto<Sys_SetDataType, Fin_DailyFeePaymentPagePriceTypeView>().ToList();
  129. var setData = _setDataTypeRep.QueryDto<Sys_SetData, Fin_DailyFeePaymentPagePriceSubTypeView>().ToList();
  130. //48人员费用 49办公费用 50 销售费用 51 其他费用 55 大运会
  131. var priceTypeData = setTypeData.Where(s => s.Id == 48 || s.Id == 49 || s.Id == 50 || s.Id == 51 || s.Id == 55).ToList();
  132. var priceSubTypeData = setData.Where(s => s.STid == 48 || s.STid == 49 || s.STid == 50 || s.STid == 51 || s.STid == 55).ToList();
  133. //员工姓名列表
  134. var userNameData = await _UsersRep.GetUserNameList(dto.PortType);
  135. //62 公转 63 私转
  136. var feeMarkTypeData = setTypeData.Where(s => s.Id == 62 || s.Id == 63).ToList();
  137. var feeMarkSubTypeData = setData.Where(s => s.STid == 62 || s.STid == 63).ToList();
  138. var companyNameData = await _CompanyRep.GetCompanyNameData();
  139. if (dto.PortType == 1) //web
  140. {
  141. DailyFeePaymentList = new Fin_DailyFeePaymentPagePriceTypeDataView
  142. {
  143. FeeTypeData = priceTypeData,
  144. FeeSubTypeData = priceSubTypeData,
  145. UserNameData = userNameData.Data,
  146. FeeMarkTypeData = feeMarkTypeData,
  147. FeeMarkSubTypeData = feeMarkSubTypeData,
  148. CompanyNameData = companyNameData.Data
  149. };
  150. }
  151. else if (dto.PortType == 2) //安卓
  152. {
  153. DailyFeePaymentList = new
  154. {
  155. CompanyNameData = companyNameData.Data,
  156. FeeTypeData = feeMarkTypeData,
  157. FeeTypeSubData = feeMarkSubTypeData
  158. };
  159. }
  160. result.Code = 0;
  161. result.Msg = "查询成功!";
  162. result.Data = DailyFeePaymentList;
  163. return result;
  164. }
  165. /// <summary>
  166. /// 日付申请 page 查询
  167. /// </summary>
  168. /// <param name="dto"></param>
  169. /// <returns></returns>
  170. public async Task<Result> GetPageSearchAll(PageDailyFeePaymentDto dto)
  171. {
  172. Result result = new Result() { Code = -2 };
  173. ListViewBase<Fin_DailyFeePaymentPageListView> dailyFeePaymentPageList = new ListViewBase<Fin_DailyFeePaymentPageListView>()
  174. {
  175. ReceiveDt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  176. };
  177. #region sql条件处理
  178. string sqlWhere = "";
  179. if (dto.CompanyId != -1) //公司
  180. sqlWhere += string.Format(" And dfp.CompanyId = {0}", dto.CompanyId);
  181. if (dto.FinancialAuditStatus != -1) //财务审核
  182. sqlWhere += string.Format(" And dfp.FAudit = {0}", dto.FinancialAuditStatus);
  183. if (dto.ManagerAuditStatus != -1) //总经理审核
  184. sqlWhere += string.Format(" And dfp.MAudit = {0}", dto.ManagerAuditStatus);
  185. if (dto.IsPaySign != -1) //付款状态
  186. {
  187. sqlWhere += string.Format(" And dfp.IsPay = {0}", dto.IsPaySign);
  188. }
  189. if (dto.FeeTypeId != -1) //费用类型
  190. {
  191. if (dto.FeeSubTypeId != -1) //子类处理
  192. {
  193. sqlWhere += string.Format(" And dfp.PriceTypeId = {0}", dto.FeeSubTypeId);
  194. }
  195. else
  196. {
  197. var setData = _setDataTypeRep.QueryDto<Sys_SetData, SetDataView>(s => s.STid == dto.FeeTypeId).ToList();
  198. if (setData.Count > 0)
  199. {
  200. string setDataIds = "";
  201. foreach (var item in setData)
  202. {
  203. setDataIds += item.Id + ",";
  204. }
  205. if (setDataIds.Length > 0)
  206. {
  207. setDataIds = setDataIds.Substring(0, setDataIds.Length - 1);
  208. sqlWhere += string.Format(" And dfp.PriceTypeId In ({0})", setDataIds);
  209. }
  210. }
  211. }
  212. }
  213. if (!string.IsNullOrEmpty(dto.FeeDesc))
  214. sqlWhere += string.Format(" And dfp.Instructions Like '%{0}%'", dto.FeeDesc);
  215. if (dto.CreateUserId != -1)
  216. sqlWhere += string.Format(" And dfp.CreateUserId = {0}", dto.CreateUserId);
  217. int startIndex = (dto.PageIndex - 1) * dto.PageSize + 1;
  218. int endIndex = startIndex + dto.PageSize - 1;
  219. string sqlPage = string.Format(@"Select * From (
  220. Select row_number() over (order by dfp.Id Desc) as RowNumber,
  221. dfp.Id,dfp.CompanyId,c.CompanyName,dfp.Instructions,dfp.SumPrice,
  222. dfp.CreateUserId,u.CnName CreateUser,dfp.CreateTime,dfp.FAudit,dfp.MAudit,
  223. dfp.PriceTypeId,dfp.IsPay
  224. From Fin_DailyFeePayment dfp
  225. Inner Join Sys_Company c On dfp.CompanyId = c.Id
  226. Left Join Sys_Users u On dfp.CreateUserId = u.Id
  227. Where dfp.IsDel=0 {0}
  228. ) temp Where RowNumber Between {1} and {2}", sqlWhere, startIndex, endIndex);
  229. string sqlCount = string.Format(@"Select COUNT(1) as Count From (
  230. Select dfp.Id,dfp.CompanyId,c.CompanyName,dfp.Instructions,dfp.SumPrice,
  231. dfp.CreateUserId,u.CnName CreateUser,dfp.CreateTime,dfp.FAudit,dfp.MAudit,
  232. dfp.PriceTypeId
  233. From Fin_DailyFeePayment dfp
  234. Inner Join Sys_Company c On dfp.CompanyId = c.Id
  235. Left Join Sys_Users u On dfp.CreateUserId = u.Id
  236. Where dfp.IsDel=0 {0}
  237. ) temp ", sqlWhere);
  238. #endregion
  239. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3) //web
  240. {
  241. //Fin_DailyFeePaymentPageCount
  242. var dailyFeePaymentCount = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentPageCount>(sqlCount).FirstAsync();
  243. var DailyFeePaymentData = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentPageListView>(sqlPage).ToListAsync();
  244. int totalCount = dailyFeePaymentCount.Count;
  245. dailyFeePaymentPageList = new ListViewBase<Fin_DailyFeePaymentPageListView>
  246. {
  247. CurrPageIndex = dto.PageIndex,
  248. CurrPageSize = dto.PageSize,
  249. DataCount = totalCount,
  250. DataList = DailyFeePaymentData
  251. };
  252. }
  253. result.Code = 0;
  254. result.Msg = "查询成功!";
  255. result.Data = dailyFeePaymentPageList;
  256. return result;
  257. }
  258. /// <summary>
  259. /// 日付申请 single 查询 By Id
  260. /// </summary>
  261. /// <param name="dto"></param>
  262. /// <returns></returns>
  263. public async Task<Result> GetSearchById(SearchDailyFeePaymentDto dto)
  264. {
  265. Result result = new Result() { Code = -2 };
  266. if (dto.PortType == 1) //web
  267. {
  268. Fin_DailyFeePaymentInfolView feeData = new Fin_DailyFeePaymentInfolView();
  269. string feeSql = string.Format(@"Select * From Fin_DailyFeePayment
  270. Where IsDel=0 And Id = {0} ", dto.Id);
  271. feeData = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentInfolView>(feeSql).FirstAsync();
  272. string feeContentSql = string.Format(@"Select * From Fin_DailyFeePaymentContent
  273. Where IsDel=0 And DFPId = {0} ", dto.Id);
  274. feeData.FeeContents = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentContentInfolView>(feeContentSql).ToListAsync();
  275. result.Code = 0;
  276. result.Msg = "查询成功!";
  277. result.Data = feeData;
  278. }
  279. else if (dto.PortType == 2 || dto.PortType == 3) //android And ios
  280. {
  281. Fin_DailyFeePaymentInfoAndroidlView feeData = new Fin_DailyFeePaymentInfoAndroidlView();
  282. string feeSql = string.Format(@"Select * From Fin_DailyFeePayment
  283. Where IsDel=0 And Id = {0} ", dto.Id);
  284. feeData = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentInfoAndroidlView>(feeSql).FirstAsync();
  285. //feeData.TransferTypeId = feeData.TransferTypeId == 0 ? 62 : feeData.TransferTypeId == 1 ? 63 : 0;
  286. string feeContentSql = string.Format(@"Select * From Fin_DailyFeePaymentContent
  287. Where IsDel=0 And DFPId = {0} ", dto.Id);
  288. feeData.FeeContents = await _sqlSugar.SqlQueryable<Fin_DailyFeePaymentContentInfolView>(feeContentSql).ToListAsync();
  289. result.Code = 0;
  290. result.Msg = "查询成功!";
  291. result.Data = feeData;
  292. }
  293. return result;
  294. }
  295. /// <summary>
  296. /// 日付申请 添加
  297. /// </summary>
  298. /// <param name="dto"></param>
  299. /// <returns></returns>
  300. public async Task<Result> Add(AddDailyFeePaymentDto dto)
  301. {
  302. Result result = new Result() { Code = -2 };
  303. _sqlSugar.BeginTran();
  304. try
  305. {
  306. Fin_DailyFeePayment _fee = _mapper.Map<Fin_DailyFeePayment>(dto);
  307. _fee.CreateUserId = dto.UserId;
  308. int? feeId = await _sqlSugar.Insertable(_fee).ExecuteReturnIdentityAsync();
  309. if (dto.FeeContents.Count > 0)
  310. {
  311. List<Fin_DailyFeePaymentContent> _feeContents = _mapper.Map<List<Fin_DailyFeePaymentContent>>(dto.FeeContents);
  312. foreach (var item in _feeContents)
  313. {
  314. item.DFPId = feeId == null ? -1 : Convert.ToInt32(feeId);
  315. item.CreateUserId = dto.UserId;
  316. }
  317. await _sqlSugar.Insertable(_feeContents).ExecuteCommandAsync();
  318. }
  319. _sqlSugar.CommitTran();
  320. result.Code = 0;
  321. var data = new { dailyId = feeId, sign = 1 };
  322. result.Data = data;
  323. }
  324. catch (Exception ex)
  325. {
  326. _sqlSugar.RollbackTran();
  327. result.Msg = ex.Message;
  328. }
  329. return result;
  330. }
  331. /// <summary>
  332. /// 日付申请 编辑
  333. /// </summary>
  334. /// <param name="dto"></param>
  335. /// <returns></returns>
  336. public async Task<Result> Edit(EditDailyFeePaymentDto dto)
  337. {
  338. Result result = new Result() { Code = -2 };
  339. #region 已审核过的日付申请不可编辑
  340. Fin_DailyFeePayment _DailyFeePayment = await _sqlSugar.Queryable<Fin_DailyFeePayment>().Where(it => it.Id == dto.Id && it.IsDel == 0).FirstAsync();
  341. if (_DailyFeePayment != null)
  342. {
  343. //if (_DailyFeePayment.FAudit != 0 && _DailyFeePayment.MAudit != 0)
  344. //{
  345. // result.Msg = "财务和总经理均已审核,不可修改!";
  346. // return result;
  347. //}
  348. //else if (_DailyFeePayment.FAudit != 0)
  349. //{
  350. // result.Msg = "财务已审核,不可修改!";
  351. // return result;
  352. //}
  353. //else if (_DailyFeePayment.MAudit != 0)
  354. //{
  355. // result.Msg = "总经理已审核,不可修改!";
  356. // return result;
  357. //}
  358. if (_DailyFeePayment.FAudit == 1 || _DailyFeePayment.MAudit == 1)
  359. {
  360. result.Msg = "审核已通过,不可修改!";
  361. return result;
  362. }
  363. }
  364. #endregion
  365. _sqlSugar.BeginTran();
  366. try
  367. {
  368. Fin_DailyFeePayment _fee = _mapper.Map<Fin_DailyFeePayment>(dto);
  369. _fee.CreateUserId = dto.UserId;
  370. int? editFeeStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  371. .Where(a => a.Id == dto.Id)
  372. .SetColumns(a => new Fin_DailyFeePayment
  373. {
  374. Instructions = dto.Instructions,
  375. SumPrice = dto.SumPrice,
  376. TransferTypeId = dto.TransferTypeId,
  377. PriceTypeId = dto.PriceTypeId,
  378. CompanyId = dto.CompanyId,
  379. }).ExecuteCommandAsync();
  380. List<Fin_DailyFeePaymentContent> _feeContents = _mapper.Map<List<Fin_DailyFeePaymentContent>>(dto.FeeContents);
  381. await _sqlSugar.Updateable<Fin_DailyFeePaymentContent>()
  382. .Where(a => a.DFPId == _fee.Id)
  383. .SetColumns(a => new Fin_DailyFeePaymentContent
  384. {
  385. IsDel = 1,
  386. DeleteUserId = _fee.CreateUserId,
  387. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  388. }).ExecuteCommandAsync();
  389. if (_feeContents.Count > 0)
  390. {
  391. foreach (var item in _feeContents)
  392. {
  393. item.DFPId = _fee.Id;
  394. item.CreateUserId = dto.UserId;
  395. }
  396. if (_feeContents.Count > 0)
  397. {
  398. await _sqlSugar.Insertable(_feeContents).ExecuteCommandAsync();
  399. }
  400. }
  401. _sqlSugar.CommitTran();
  402. result.Code = 0;
  403. var data = new { dailyId = dto.Id, sign = 2 };
  404. result.Data = data;
  405. }
  406. catch (Exception ex)
  407. {
  408. _sqlSugar.RollbackTran();
  409. result.Msg = ex.Message;
  410. }
  411. return result;
  412. }
  413. /// <summary>
  414. /// 日付申请 删除
  415. /// </summary>
  416. /// <param name="dto"></param>
  417. /// <returns></returns>
  418. public async Task<Result> Del(DelDailyFeePaymentDto dto)
  419. {
  420. Result result = new Result() { Code = -2 };
  421. int? delFeeStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  422. .Where(a => a.Id == dto.Id)
  423. .SetColumns(a => new Fin_DailyFeePayment
  424. {
  425. IsDel = 1,
  426. DeleteUserId = dto.Id,
  427. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  428. }).ExecuteCommandAsync();
  429. result.Code = 0;
  430. return result;
  431. }
  432. /// <summary>
  433. /// 日付申请 审核
  434. /// </summary>
  435. /// <param name="dto"></param>
  436. /// <returns></returns>
  437. public async Task<Result> DailyPaymentAudit(DP_AuditStatusDto dto)
  438. {
  439. Result result = new Result() { Code = -2 };
  440. if (dto.AuditType == 1) //财务审核
  441. {
  442. int? auditStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  443. .Where(a => a.Id == dto.Id)
  444. .SetColumns(a => new Fin_DailyFeePayment
  445. {
  446. FAudit = dto.AuditStatus,
  447. FAuditDate = DateTime.Now,
  448. }).ExecuteCommandAsync();
  449. if (auditStatus != null && auditStatus > 0)
  450. result.Code = 0;
  451. else
  452. result.Msg = "财务审核操作失败";
  453. }
  454. else if (dto.AuditType == 2) //总经理
  455. {
  456. int? auditStatus = await _sqlSugar.Updateable<Fin_DailyFeePayment>()
  457. .Where(a => a.Id == dto.Id)
  458. .SetColumns(a => new Fin_DailyFeePayment
  459. {
  460. MAudit = dto.AuditStatus,
  461. MAuditDate = DateTime.Now,
  462. }).ExecuteCommandAsync();
  463. if (auditStatus != null && auditStatus > 0)
  464. result.Code = 0;
  465. else
  466. result.Msg = "总经理审核操作失败";
  467. }
  468. return result;
  469. }
  470. }
  471. }