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 partial class ExpertService: IExpertService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public ExpertService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(ExpertDTO expertDTO) { ResultEntity resultEntity = new ResultEntity(); try { var list = _context.Experts.Where(e => e.IdCard == expertDTO.IdCard && e.RecStatus == "A" && e.Id != expertDTO.Id).ToList(); if (list.Count > 0 ) { resultEntity.Result = false; resultEntity.Message = "该专家信息已经存在"; return resultEntity; } Expert expert = _mapper.Map(expertDTO); if (string.IsNullOrEmpty(expert.Id)) { expert.Id = Guid.NewGuid().ToString(); _context.Experts.Add(expert); } else { var updateplExpert = _context.Experts.Find(expert.Id); updateplExpert.Name = expert.Name; updateplExpert.Sex = expert.Sex; updateplExpert.InfoSource = expert.InfoSource; updateplExpert.IdCard = expert.IdCard; updateplExpert.Province = expert.Province; updateplExpert.AreaId = expert.AreaId; updateplExpert.City = expert.City; updateplExpert.Address = expert.Address; updateplExpert.Postal = expert.Postal; updateplExpert.ExpertType = expert.ExpertType; updateplExpert.Level = expert.Level; updateplExpert.Phone1 = expert.Phone1; updateplExpert.Phone2 = expert.Phone2; updateplExpert.Qq = expert.Qq; updateplExpert.Wechat = expert.Wechat; updateplExpert.ReviewItem = expert.ReviewItem; updateplExpert.CertiNumber = expert.CertiNumber; updateplExpert.CertiFont = expert.CertiFont; updateplExpert.CertiBack = expert.CertiBack; updateplExpert.TitileCerti = expert.TitileCerti; updateplExpert.Remark = expert.Remark; updateplExpert.TitileCerti1 = expert.TitileCerti1; updateplExpert.TitileCerti2 = expert.TitileCerti2; updateplExpert.TitileCerti3 = expert.TitileCerti3; updateplExpert.TitileCerti4 = expert.TitileCerti4; updateplExpert.TitileCerti5 = expert.TitileCerti5; updateplExpert.TitileCerti6 = expert.TitileCerti6; updateplExpert.TitileCerti7 = expert.TitileCerti7; updateplExpert.TitileCerti8 = expert.TitileCerti8; updateplExpert.TitileCerti9 = expert.TitileCerti9; updateplExpert.TitileCerti10 = expert.TitileCerti10; } _context.SaveChanges(); resultEntity.ReturnID = expert.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; ex.Message.ToString(); } return resultEntity; } public ExpertDTO Get(string id) { Expert entity = _context.Experts.Find(id); if (entity.RecStatus != "A") { entity = new Expert(); } ExpertDTO exDTO = _mapper.Map(entity); return exDTO; } public ResultDataEntity SearchByPaging(ExpertDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); List list = new List(); //筛选 var query = (from a in _context.Experts join h in _context.ExpertOrderDispatches.Where(x => x.RecStatus == "A").GroupBy(q => new { q.ExpertId }).Select(q => new { ExpertId = q.Key.ExpertId, JoinCount = q.Count(), } ) on a.Id equals h.ExpertId into hsss from hhh in hsss.DefaultIfEmpty() where a.RecStatus == "A" && (searchEntity.JoinCount<=0 || hhh.JoinCount >= searchEntity.JoinCount) select a).ToList(); if (!string.IsNullOrEmpty(searchEntity.Name)) { query = query.Where(m => m.Name.Contains(searchEntity.Name)).ToList(); } if (!string.IsNullOrEmpty(searchEntity.ExpertType)) { query = query.Where(m => m.ExpertType == searchEntity.ExpertType).ToList(); } DateTime startDate = DateTime.Now; DateTime endDate = DateTime.Now; if (!string.IsNullOrEmpty(searchEntity.Createtime)) { string[] arr = searchEntity.Createtime.Split("|"); startDate = Convert.ToDateTime(arr[0]); endDate = Convert.ToDateTime(arr[1]).AddDays(1); query = query.Where(m => m.Createtime >= startDate && m.Createtime < endDate).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Province)) { query = query.Where(m => m.Province == searchEntity.Province).ToList(); } if (!string.IsNullOrEmpty(searchEntity.City)) { query = query.Where(m => m.City == searchEntity.City).ToList(); } if (!string.IsNullOrEmpty(searchEntity.ReviewItem)) { query = query.Where(m => m.ReviewItem.Contains(searchEntity.ReviewItem)).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Level)) { query = query.Where(m => m.Level == searchEntity.Level).ToList(); } if (!string.IsNullOrEmpty(searchEntity.CertiNumber)) { query = query.Where(m => m.CertiNumber.Contains(searchEntity.CertiNumber)).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Phone1)) { query = query.Where(m => m.Phone1.Contains(searchEntity.Phone1)).ToList(); } query = query.OrderByDescending(x => x.Modifytime).ToList(); if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); var expertlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); list = _mapper.Map>(expertlist); data.LoadData(searchEntity, list); return data; } public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.Experts.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } public List GetList() { var listExperts = _context.Experts.Where(e => e.RecStatus == "A").ToList(); var list = _mapper.Map>(listExperts); return list; } #region 保存专家推广记录 public ResultEntity SaveExpPromote(ExpertPromoteDTO expertPromoteDTO) { ResultEntity resultEntity = new ResultEntity(); try { ExpertPromote expPromote = _mapper.Map(expertPromoteDTO); if (string.IsNullOrEmpty(expPromote.Id)) { expPromote.Id = Guid.NewGuid().ToString(); _context.ExpertPromotes.Add(expPromote); } _context.SaveChanges(); resultEntity.ReturnID = expPromote.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; ex.Message.ToString(); } return resultEntity; } public ResultDataEntity SearchExpPromote(ExpertPromoteDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); List list = new List(); //筛选 var query = _context.ExpertPromotes .Where(b => b.RecStatus == "A") .ToList(); if (!string.IsNullOrEmpty(searchEntity.ExpertId)) { query = query.Where(m => m.ExpertId.Contains(searchEntity.ExpertId)).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Createtime)) { DateTime startDate = DateTime.Now; DateTime endDate = DateTime.Now; string[] arr = searchEntity.Createtime.Split("|"); startDate = Convert.ToDateTime(arr[0]); endDate = Convert.ToDateTime(arr[1]).AddDays(1); query = query.Where(m => m.Createtime >= startDate && m.Createtime < endDate).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Creater)) { query = query.Where(m => m.Creater.Contains(searchEntity.Creater)).ToList(); } query = query.OrderByDescending(x => x.Modifytime).ToList(); if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); var expertlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); list = _mapper.Map>(expertlist); data.LoadData(searchEntity, list); return data; } #endregion #region 订单派工 /// /// 查询工单专家派工记录 /// /// /// public ResultDataEntity SearchOrderDispatch(ExpertOrderDipatchDTOSearch 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 XdTimestart = DateTime.Now; DateTime XdTimeend = DateTime.Now; if (!string.IsNullOrWhiteSpace(searchEntity.XdTime)) { string[] XdTimes = searchEntity.XdTime.Split("|"); DateTime.TryParse(XdTimes[0], out XdTimestart); DateTime.TryParse(XdTimes[1], out XdTimeend); //XdTimeend = XdTimeend.AddDays(1); } var query = (from a in _context.CooperOrders join b in listCode.Where(x => x.CodeTable == "CooperOrder" && x.CodeField == "shouli_status") on a.ShouliStatus equals b.CodeSn into bsss from bbb in bsss.DefaultIfEmpty() join f in listCode.Where(x => x.CodeTable == "CooperVisit" && x.CodeField == "jtype") on a.OrderType equals f.CodeSn into fsssss from fff in fsssss.DefaultIfEmpty() join g in _context.CooperatecustomCustomers.Where(x => x.RecStatus == "A") on a.Khdw equals g.Id into gsss from ggg in gsss.DefaultIfEmpty() join h in _context.ExpertOrderDispatches.Where(x => x.RecStatus == "A") on a.Id equals h.Orderid into ho from hod in ho.DefaultIfEmpty() join j in _context.Experts.Where(x => x.RecStatus == "A") on hod.ExpertId equals j.Id into exp from expt in exp.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.XdTime) || (a.XdTime >= XdTimestart && a.XdTime <= XdTimeend)) && (string.IsNullOrWhiteSpace(searchEntity.Ywjl) || ggg.Ywjl == searchEntity.Ywjl.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.OrderType) || a.OrderType == searchEntity.OrderType.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.OrderNo) || a.OrderNo.Contains(searchEntity.OrderNo.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.KhdwName) || ggg.Name.Contains(searchEntity.KhdwName.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Khlx) || ggg.Khlx == searchEntity.Khlx.Trim()) // && (string.IsNullOrWhiteSpace(searchEntity.HuifangStatus) || a.HuifangStatus.Contains(searchEntity.HuifangStatus.Trim())) // && (string.IsNullOrWhiteSpace(searchEntity.PingjiaStatus) || a.PingjiaStatus.Contains(searchEntity.PingjiaStatus.Trim())) select new ExpertOrderDipatchDTO { Id = a.Id, XdTime = a.XdTime, XdTimeName = a.XdTime.ToString("yyyy-MM-dd"), OrderId = a.Id, OrderNo = a.OrderNo, Khdw = a.Khdw, KhdwName = ggg.Name, OrderType = a.OrderType, OrderTypeName = fff.Comments, //OrderPro = a.OrderPro, //OrderProName = hhh.Name, //OrderNum = a.OrderNum, Shr = a.Shr, ShrAddress = a.ShrAddress, ShrTel = a.ShrTel, Money = hod.Money, Creater = hod.Creater, Createtime = hod.Createtime, ExpertId = hod.ExpertId, ExpertName = expt.Name, RecStatus = hod.RecStatus, Modifier = hod.Modifier, Modifytime = hod.Modifytime } ).OrderByDescending(x => x.XdTime).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; } public ResultEntity SaveDispatch(ExpertOrderDipatchDTO dipatchDTO) { ResultEntity resultEntity = new ResultEntity(); try { ExpertOrderDispatch dispatch = _mapper.Map(dipatchDTO); if (string.IsNullOrEmpty(dispatch.Id)) { dispatch.Id = Guid.NewGuid().ToString(); _context.ExpertOrderDispatches.Add(dispatch); } _context.SaveChanges(); resultEntity.ReturnID = dispatch.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; ex.Message.ToString(); } return resultEntity; } #endregion } }