using AutoMapper; using DTO; using IServices; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using zhengcaioa.Models; namespace Services { public class FiAccountRecordService: IFiAccountRecordService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public FiAccountRecordService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(FiAccountRecordDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var fiAccountRecord = _mapper.Map(dto); if (String.IsNullOrEmpty(fiAccountRecord.Id)) { fiAccountRecord.Id = Guid.NewGuid().ToString(); dto.Id = fiAccountRecord.Id; _context.FiAccountRecords.Add(fiAccountRecord); } else { var updatepltRole = _context.FiAccountRecords.Find(fiAccountRecord.Id); updatepltRole.SubjectId = fiAccountRecord.SubjectId; updatepltRole.AccountId = fiAccountRecord.AccountId; updatepltRole.RecordTypeId = fiAccountRecord.RecordTypeId; updatepltRole.Department = fiAccountRecord.Department; updatepltRole.Money = fiAccountRecord.Money; updatepltRole.PaymentUnit = fiAccountRecord.PaymentUnit; updatepltRole.OperationalMatters = fiAccountRecord.OperationalMatters; updatepltRole.Remark = fiAccountRecord.Remark; updatepltRole.QiyongStatus = fiAccountRecord.QiyongStatus; updatepltRole.RecStatus = fiAccountRecord.RecStatus; // updatepltRole.Creater = pltRole.Creater; //updatepltRole.Createtime = pltRole.Createtime; updatepltRole.Modifier = fiAccountRecord.Modifier; updatepltRole.Modifytime = fiAccountRecord.Modifytime; } _context.SaveChanges(); resultEntity.ReturnID = fiAccountRecord.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public FiAccountRecordDTO Get(string id) { var entity = _context.FiAccountRecords.Find(id); if (entity.RecStatus != "A") { entity = new FiAccountRecord(); } var FiAccountRecordDTO = _mapper.Map(entity); return FiAccountRecordDTO; } public ResultDataEntity SearchByPaging(FiAccountRecordDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); var listCode = (from a in _context.SysCodeDtls join b in _context.SysCodes on a.CodeId equals b.Id where a.RecStatus == "A" && b.RecStatus == "A" select new CodeDataEntity() { CodeId = b.Id, CodeTable = b.CodeTable, CodeField = b.CodeField, CodeSn = a.CodeSn, Comments = a.Comments, Contents = a.Contents, RecStatus = a.RecStatus, Sort = a.Sort } ); DateTime XdTimestart = DateTime.Now; DateTime XdTimeend = DateTime.Now; if (!string.IsNullOrWhiteSpace(searchEntity.Createtime)) { string[] XdTimes = searchEntity.Createtime.Split("|"); DateTime.TryParse(XdTimes[0], out XdTimestart); DateTime.TryParse(XdTimes[1], out XdTimeend); XdTimeend = XdTimeend.AddDays(1); } var query = (from a in _context.FiAccountRecords//.Where(x => x.RecStatus == "A") join z in _context.FiAccounts on a.AccountId equals z.Id join b in listCode.Where(x => x.CodeTable == "fi_account" && x.CodeField == "accounttype") on z.Accounttype equals b.CodeSn into bsss from bbb in bsss.DefaultIfEmpty() join c in listCode.Where(x => x.CodeTable == "fi_account_record" && x.CodeField == "RecordTypeId") on a.RecordTypeId equals c.CodeSn into csss from ccc in csss.DefaultIfEmpty() join f in _context.FiSubjects on a.SubjectId equals f.Id into fsss from fff in fsss.DefaultIfEmpty() join y in _context.PltUsers on a.Creater equals y.Id into ysss from yyy in ysss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.Createtime) || (a.Createtime >= XdTimestart && a.Createtime <= XdTimeend)) && (string.IsNullOrWhiteSpace(searchEntity.Accounttype) || z.Accounttype == searchEntity.Accounttype.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.AccountId) || a.AccountId == searchEntity.AccountId.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.SubjectId) || a.SubjectId == searchEntity.SubjectId.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.OperationalMatters) || a.OperationalMatters.Contains(searchEntity.OperationalMatters.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.PaymentUnit) || a.PaymentUnit.Contains(searchEntity.PaymentUnit.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.CreaterName) || yyy.UserName.Contains(searchEntity.CreaterName.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.RecordTypeId) || a.RecordTypeId == searchEntity.RecordTypeId.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.RecordTypeId) || a.RecordTypeId == searchEntity.RecordTypeId.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Remark) || a.Remark.Contains(searchEntity.Remark.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Creater) || (a.Creater != "ZCUserCentre" && a.Creater != "shop")) select new FiAccountRecordDTO { Id = a.Id, SubjectId = a.SubjectId, SubjectName = fff.Subjectname, AccountId = a.AccountId, AccountName = z.Accountname, Accounttype = z.Accounttype, AccounttypeName = bbb.Comments, RecordTypeId = a.RecordTypeId, RecordTypeName = ccc.Comments, Department = a.Department, Money = a.Money, PaymentUnit = a.PaymentUnit, OperationalMatters = a.OperationalMatters, Remark = a.Remark, CreatetimeName = a.Createtime.ToString("yyyy-MM-dd HH:mm:ss"), Creater = a.Creater, CreaterName = yyy.UserName, Createtime = a.Createtime, Modifier = a.Modifier, Modifytime = a.Modifytime, QiyongStatus = a.QiyongStatus, RecStatus = a.RecStatus, Balance = z.Balance??0, AccountMoney = a.AccountMoney??0, }).OrderByDescending(x => x.Modifytime).ToList(); //if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); data.Heji1 = Math.Round(query.Sum(x => x.Money) ?? 0, 2); var lianlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); data.DangyeHeji1 = Math.Round(lianlist.Sum(x => x.Money) ?? 0, 2); data.LoadData(searchEntity, lianlist); return data; } public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.FiAccountRecords.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 获取所有有效角色 /// /// public List GetList(string OperationalMatters = null, string Remark = null) { var listPosition = _context.FiAccountRecords.Where(r => r.RecStatus == "A" ).ToList(); if (!string.IsNullOrEmpty(OperationalMatters)&& !string.IsNullOrEmpty(Remark)) { listPosition = listPosition.Where(x => x.OperationalMatters == OperationalMatters || x.Remark == Remark).ToList(); } var list = _mapper.Map>(listPosition); return list; } } }