using System; using System.Collections.Generic; using System.Linq; using System.Text; using AutoMapper; using DTO; using IServices; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using zhengcaioa.Models; namespace Services { public class IntentionOrderService: IIntentionOrderService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public IntentionOrderService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(IntentionOrderDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var entity = _mapper.Map(dto); if (String.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); _context.IntentionOrders.Add(entity); } else { var updateproject = _context.IntentionOrders.Find(entity.Id); updateproject.ZxTime = entity.ZxTime; updateproject.Uid = entity.Uid; updateproject.Cjyx = entity.Cjyx; updateproject.Lxr = entity.Lxr; updateproject.Tel = entity.Tel; updateproject.Name = entity.Name; updateproject.Yxyw = entity.Yxyw; updateproject.Remark = entity.Remark; updateproject.RecStatus = entity.RecStatus; updateproject.Modifier = entity.Modifier; updateproject.Modifytime = entity.Modifytime; } _context.SaveChanges(); resultEntity.ReturnID = entity.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public IntentionOrderDTO Get(string id) { var entity = _context.IntentionOrders.Find(id); if (entity.RecStatus != "A") { entity = new IntentionOrder(); } var result = _mapper.Map(entity); return result; } public ResultDataEntity SearchByPaging(IntentionOrderDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); List list = new List(); 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 ZxTimestart = DateTime.Now; DateTime ZxTimeend = DateTime.Now; if (!string.IsNullOrWhiteSpace(searchEntity.ZxTime)) { string[] ZxTimes = searchEntity.ZxTime.Split("|"); DateTime.TryParse(ZxTimes[0], out ZxTimestart); DateTime.TryParse(ZxTimes[1], out ZxTimeend); //ZxTimeend = ZxTimeend.AddDays(1); } var query = (from a in _context.IntentionOrders join b in listCode.Where(x => x.CodeTable == "CooperVisit" && x.CodeField == "yx") on a.Cjyx equals b.CodeSn join e in _context.PltUsers.Where(x => x.RecStatus == "A" && x.Zhiwustatus == "A" && x.IsYwjl == "A") on a.Uid equals e.Id into esss from abi in esss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.ZxTime) || (a.ZxTime >= ZxTimestart && a.ZxTime <= ZxTimeend)) && (string.IsNullOrWhiteSpace(searchEntity.Name) || a.Name.Contains(searchEntity.Name.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Cjyx) || a.Cjyx == searchEntity.Cjyx.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Uid) || a.Uid == searchEntity.Uid.Trim()) select new IntentionOrderDTO { Id = a.Id, ZxTime = a.ZxTime, ZxTimeName = a.ZxTime.ToString("yyyy-MM-dd"), Uid = a.Uid, UidName = abi.UserName, Cjyx = a.Cjyx, CjyxName = b.Comments, Lxr = a.Lxr, Tel = a.Tel, Name = a.Name, Yxyw = a.Yxyw, Remark = a.Remark, Creater = a.Creater, Createtime = a.Createtime, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, } ).OrderByDescending(x => x.ZxTime).ToList(); //if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); var lianlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); data.LoadData(searchEntity, lianlist); return data; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.IntentionOrders.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } } }