using AutoMapper; using DTO; using IServices; using System; using System.Collections.Generic; using System.Linq; using System.Text; using zhengcaioa.Models; namespace Services { public class LiaotianService : ILiaotianService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public LiaotianService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity saveLiaotian(LiaotianDTO liaotiandto) { ResultEntity resultEntity = new ResultEntity(); try { var checkUserSn = _context.Liaotians.Where(x => x.Question == liaotiandto.Question && x.RecStatus == "A" && x.Id != liaotiandto.Id).FirstOrDefault(); if (checkUserSn != null && (string.IsNullOrWhiteSpace(liaotiandto.Id) || (!string.IsNullOrWhiteSpace(liaotiandto.Id) && checkUserSn.Id != liaotiandto.Id))) { resultEntity.Result = false; resultEntity.Message = "问题重复"; return resultEntity; } var liaotian = _mapper.Map(liaotiandto); if (String.IsNullOrEmpty(liaotian.Id)) { liaotian.Id = Guid.NewGuid().ToString(); liaotiandto.Id = liaotian.Id; _context.Liaotians.Add(liaotian); } else { var updateliaotian = _context.Liaotians.Find(liaotian.Id); updateliaotian.Question = liaotian.Question; updateliaotian.Anwser = liaotian.Anwser; updateliaotian.RecStatus = liaotian.RecStatus; updateliaotian.Clientid = liaotian.Clientid; // updateliaotian.Creater = liaotian.Creater; //updateliaotian.Createtime = liaotian.Createtime; updateliaotian.Modifier = liaotian.Modifier; updateliaotian.Modifytime = liaotian.Modifytime; updateliaotian.Questiontype = liaotian.Questiontype; updateliaotian.Problemtype = liaotian.Problemtype; updateliaotian.Shiyongfatiao = liaotian.Shiyongfatiao; updateliaotian.ChushuStatus = liaotian.ChushuStatus; updateliaotian.Shenheer = liaotian.Shenheer; updateliaotian.Shenhetime = liaotian.Shenhetime; } _context.SaveChanges(); resultEntity.ReturnID = liaotian.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public LiaotianDTO GetLiaotianEntity(string id) { Liaotian entity = _context.Liaotians.Find(id); if (entity.RecStatus != "A") { entity = new Liaotian(); } var liaotiandto = _mapper.Map(entity); return liaotiandto; } public List GetSYScode(string code_table, string code_field) { List sysCodeDtls = (from e in _context.SysCodes join c in _context.SysCodeDtls on e.Id equals c.CodeId where e.RecStatus == "A" && c.RecStatus == "A" && e.CodeTable == code_table && e.CodeField == code_field select c).OrderBy(x => x.Sort).ToList(); return sysCodeDtls; } public ResultDataEntity SearchByPaging(LiaotianDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); //List list = new List(); DateTime Createtimestart = DateTime.Now; DateTime Createtimeend = DateTime.Now; if (!string.IsNullOrWhiteSpace(searchEntity.Createtime)) { string[] Createtimes = searchEntity.Createtime.Split("|"); DateTime.TryParse(Createtimes[0], out Createtimestart); DateTime.TryParse(Createtimes[1], out Createtimeend); Createtimeend = Createtimeend.AddDays(1); } //var query1 = _context.Liaotians.Where(a => a.RecStatus == "A"); //筛选 var query = (from a in _context.Liaotians // join h in _context.LiaotianShoucangs.Where(x => x.RecStatus == "A" && x.Creater == searchEntity.ShouCangCreater) // on a.Id equals h.LiaotianId //into hsss // from hhh in hsss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.Createtime) || (a.Createtime >= Createtimestart && a.Createtime <= Createtimeend)) && (string.IsNullOrWhiteSpace(searchEntity.Question) || (a.Question.Contains(searchEntity.Question.Trim()))) && (string.IsNullOrWhiteSpace(searchEntity.Questiontype) || (a.Questiontype == searchEntity.Questiontype.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Problemtype) || (a.Problemtype.Contains(searchEntity.Problemtype.Trim()))) && (string.IsNullOrWhiteSpace(searchEntity.Shifoushenhe) || (a.Clientid == searchEntity.Shifoushenhe.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.ChushuStatus) || (a.ChushuStatus == searchEntity.ChushuStatus.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Creater) || (a.Creater == searchEntity.Creater.Trim())) //&& (string.IsNullOrWhiteSpace(searchEntity.ShouCangStatus) || (searchEntity.ShouCangStatus == "A" && hhh.Id !=null) || (searchEntity.ShouCangStatus != "A" && hhh.Id == null)) select new LiaotianDTO { Id = a.Id, Question = a.Question, Anwser = a.Anwser, RecStatus = a.RecStatus, Creater = a.Creater, Createtime = a.Createtime, Modifier = a.Modifier, Modifytime = a.Modifytime, Clientid = a.Clientid, Questiontype = a.Questiontype, Problemtype = a.Problemtype, Shiyongfatiao = a.Shiyongfatiao, ChushuStatus= a.ChushuStatus, Shenheer = a.Shenheer, Shenhetime = a.Shenhetime, } ) .Distinct() .OrderByDescending(x => x.Modifytime).ToList(); //if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); var lianlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); //list = _mapper.Map>(lianlist); data.LoadData(searchEntity, lianlist); return data; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.Liaotians.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 查询打印信息 /// /// 查询条件 /// public List SearchForPrint(LiaotianDTOSearch searchEntity) { List list = new List(); //筛选 var query = _context.Liaotians .Where(b => b.RecStatus == "A") .ToList(); if (!string.IsNullOrEmpty(searchEntity.Question)) { query = query.Where(m => m.Question.Contains(searchEntity.Question)).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Questiontype)) { query = query.Where(m => m.Questiontype == searchEntity.Questiontype).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Problemtype)) { query = query.Where(m => m.Problemtype.Contains(searchEntity.Problemtype)).ToList(); } if (!string.IsNullOrEmpty(searchEntity.Info)) { var check = searchEntity.Info.Split(' '); List liaotians = new List(); if (check != null && check.Length > 0) { for (int i = 0; i < check.Length; i++) { if (!string.IsNullOrWhiteSpace(check[i])) { liaotians = liaotians.Union(query.Where(m => m.Question.Contains(check[i])).ToList()).Distinct().ToList(); } } } query = liaotians; } query = query.OrderByDescending(x => x.Modifytime).ToList(); list = _mapper.Map>(query); return list; } public ResultEntity saveLiaotianShoucang(LiaotianShoucangDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var liaotian = _mapper.Map(dto); if (String.IsNullOrEmpty(liaotian.Id)) { liaotian.Id = Guid.NewGuid().ToString(); dto.Id = liaotian.Id; _context.LiaotianShoucangs.Add(liaotian); } else { var updateliaotian = _context.LiaotianShoucangs.Find(liaotian.Id); updateliaotian.LiaotianId = liaotian.LiaotianId; updateliaotian.RecStatus = liaotian.RecStatus; // updateliaotian.Creater = liaotian.Creater; //updateliaotian.Createtime = liaotian.Createtime; updateliaotian.Modifier = liaotian.Modifier; updateliaotian.Modifytime = liaotian.Modifytime; } _context.SaveChanges(); resultEntity.ReturnID = liaotian.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public ResultEntity ModifyStatusLiaotianShoucang(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.LiaotianShoucangs.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } public List getListLiaotianShoucang(string userId, string LiaotianId) { List result = new List(); var listFiServices = _context.LiaotianShoucangs.Where(r => r.RecStatus == "A" && (string.IsNullOrEmpty(userId) || r.Creater == userId) && (string.IsNullOrEmpty(LiaotianId) || r.LiaotianId == LiaotianId)).OrderBy(x => x.Createtime).ToList(); result = _mapper.Map>(listFiServices); return result; } public ResultEntity saveSysUpdatetime(SysUpdatetime sysUpdatetime) { ResultEntity resultEntity = new ResultEntity(); try { if (String.IsNullOrEmpty(sysUpdatetime.Id)) { sysUpdatetime.Id = Guid.NewGuid().ToString(); _context.SysUpdatetimes.Add(sysUpdatetime); } else { var updateliaotian = _context.SysUpdatetimes.Find(sysUpdatetime.Id); updateliaotian.Updatename = sysUpdatetime.Updatename; updateliaotian.Updatetime = sysUpdatetime.Updatetime; } _context.SaveChanges(); resultEntity.ReturnID = sysUpdatetime.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public SysUpdatetime GetSysUpdatetime(string updatename) { var entity = _context.SysUpdatetimes.Where(x=>x.Updatename == updatename).FirstOrDefault(); return entity; } public List GetListsalary(string userid, DateTime datemin, DateTime datemax) { var listPosition = _context.Liaotians.Where(r => r.RecStatus == "A" && r.Creater == userid && r.Createtime >= datemin && r.Createtime < datemax).ToList(); var list = _mapper.Map>(listPosition); return list; } public List GetListsalaryshenhe(string userid, DateTime datemin, DateTime datemax) { var listPosition = _context.Liaotians.Where(r => r.RecStatus == "A" && r.Shenheer == userid && r.Shenhetime >= datemin && r.Shenhetime < datemax).ToList(); var list = _mapper.Map>(listPosition); return list; } } }