using DTO; using IServices; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Transactions; using zhengcaioa.IService; using zhengcaioa.Models; namespace zhengcaioa.Controllers.Finance { [CheckLogin] public class FiAccountTransferController : Controller { private readonly ILogger _logger; private readonly IFiAccountService _fiAccountService; private readonly ILiaotianService _liaotianService; private readonly IFiAccountRecordService _iFiAccountRecordService; private readonly IFiSubjectService _ifiSubjectService; public FiAccountTransferController(ILogger logger, IFiAccountService fiAccountService, ILiaotianService liaotianService, IFiAccountRecordService iFiAccountRecordService, IFiSubjectService ifiSubjectService) { _logger = logger; _fiAccountService = fiAccountService; _liaotianService = liaotianService; _iFiAccountRecordService = iFiAccountRecordService; _ifiSubjectService = ifiSubjectService; } public IActionResult Index() { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; FiAccountRecordDTO fiAccountRecordDTO = new FiAccountRecordDTO(); ViewBag.accounttype = _liaotianService.GetSYScode("fi_account", "accounttype"); ViewBag.FiSubject = _ifiSubjectService.GetList().Where(x => x.Subjecttype == "02").ToList(); ViewBag.FiSubjectIn = _ifiSubjectService.GetList().Where(x => x.Subjecttype == "01").ToList(); ViewData.Model = fiAccountRecordDTO; return View(); } /// /// 保存 /// /// 岗位实体类对象 /// /// [HttpPost] public IActionResult Save(FiAccountRecordDTO data) { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; data.RecStatus = "A"; data.Creater = curentuser.Id; data.Createtime = DateTime.Now; data.Modifier = curentuser.Id; data.Modifytime = DateTime.Now; ResultEntity resultEntity = new ResultEntity(); resultEntity.Result = false; using (TransactionScope scope = new TransactionScope()) { FiAccountDTO firmAccount = _fiAccountService.Get(data.AccountId); data.RecordTypeId = "2";//支出 data.Department = ""; data.PaymentUnit = ""; firmAccount.AllExpenses = (firmAccount.AllExpenses ?? 0) + data.Money; firmAccount.Balance = (firmAccount.Balance ?? 0) - data.Money; data.AccountMoney = firmAccount.Balance; resultEntity = _iFiAccountRecordService.save(data); if (!resultEntity.Result) { return new JsonResult(resultEntity); } resultEntity = _fiAccountService.save(firmAccount); if (!resultEntity.Result) { return new JsonResult(resultEntity); } FiAccountDTO firmAccountIn = _fiAccountService.Get(data.InAccountId); FiAccountRecordDTO dataIn = new FiAccountRecordDTO(); dataIn.Accounttype = data.InAccounttype; dataIn.AccountId = data.InAccountId; dataIn.SubjectId = data.InSubjectId; dataIn.Money = data.Money; dataIn.OperationalMatters = data.OperationalMatters; dataIn.RecordTypeId = "1";//收入 dataIn.Department = ""; dataIn.PaymentUnit = ""; dataIn.Creater = curentuser.Id; dataIn.Createtime = data.Createtime; dataIn.Modifier = curentuser.Id; dataIn.Modifytime = data.Modifytime; firmAccountIn.AllIncome = (firmAccountIn.AllIncome ?? 0) + dataIn.Money; firmAccountIn.Balance = (firmAccountIn.Balance ?? 0) + dataIn.Money; dataIn.AccountMoney = firmAccountIn.Balance; resultEntity = _iFiAccountRecordService.save(dataIn); if (!resultEntity.Result) { return new JsonResult(resultEntity); } resultEntity = _fiAccountService.save(firmAccountIn); if (!resultEntity.Result) { return new JsonResult(resultEntity); } scope.Complete(); } return new JsonResult(resultEntity); } } }