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 ExpertTestTopicService: IExpertTestTopicService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public ExpertTestTopicService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(ExpertTestTopicDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var checkUserSn = _context.ExpertTestTopics.Where(x => x.Topic == dto.Topic && x.RecStatus == "A" && x.Id != dto.Id).FirstOrDefault(); if (checkUserSn != null && (string.IsNullOrWhiteSpace(dto.Id) || (!string.IsNullOrWhiteSpace(dto.Id) && checkUserSn.Id != dto.Id))) { resultEntity.Result = false; resultEntity.Message = "试题重复"; return resultEntity; } var entity = _mapper.Map(dto); if (String.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); dto.Id = entity.Id; _context.ExpertTestTopics.Add(entity); } else { var updateproject = _context.ExpertTestTopics.Find(entity.Id); updateproject.Topictype = entity.Topictype; updateproject.Zhishitype = entity.Zhishitype; updateproject.Topic = entity.Topic; updateproject.Area = entity.Area; updateproject.Zhongdian = entity.Zhongdian; updateproject.JiexiStatus = entity.JiexiStatus; updateproject.ShenpiStatus = entity.ShenpiStatus; updateproject.RecStatus = entity.RecStatus; updateproject.Modifier = entity.Modifier; updateproject.Modifytime = entity.Modifytime; updateproject.Jiexi = entity.Jiexi; } //子表 var expertTestTopicanwsers = _context.ExpertTestTopicanwsers.Where(x => x.Topic == dto.Id).ToList(); if (expertTestTopicanwsers != null && expertTestTopicanwsers.Count > 0) { foreach (var expertTestTopicanwser in expertTestTopicanwsers) { _context.ExpertTestTopicanwsers.Remove(expertTestTopicanwser); } } var anwserno = dto.anwserno; if (anwserno != null && anwserno.Length > 0) { for (int i = 0; i < anwserno.Length; i++) { if (!string.IsNullOrEmpty(dto.anwserno[i])) { ExpertTestTopicanwser expertTestTopicanwser = new ExpertTestTopicanwser(); expertTestTopicanwser.Id = Guid.NewGuid().ToString(); expertTestTopicanwser.Topic = dto.Id; expertTestTopicanwser.Anwserno = dto.anwserno[i]; expertTestTopicanwser.Anwser = dto.Anwser[i]; expertTestTopicanwser.Shifouzhengqu = dto.Shifouzhengqu[i]; expertTestTopicanwser.Creater = dto.Modifier; expertTestTopicanwser.Createtime = DateTime.Now; expertTestTopicanwser.Modifier = dto.Modifier; expertTestTopicanwser.Modifytime = DateTime.Now; _context.ExpertTestTopicanwsers.Add(expertTestTopicanwser); } } } //解析 var expertTestTopicjiexis = _context.ExpertTestTopicjiexis.Where(x => x.Topic == dto.Id).ToList(); if (expertTestTopicjiexis != null && expertTestTopicjiexis.Count > 0) { foreach (var expertTestTopicjiexi in expertTestTopicjiexis) { _context.ExpertTestTopicjiexis.Remove(expertTestTopicjiexi); } } var Falv = dto.Falv; if (Falv != null && Falv.Length > 0) { for (int i = 0; i < Falv.Length; i++) { if (!string.IsNullOrEmpty(dto.Falv[i])) { ExpertTestTopicjiexi expertTestTopicjiexi = new ExpertTestTopicjiexi(); expertTestTopicjiexi.Id = Guid.NewGuid().ToString(); expertTestTopicjiexi.Topic = dto.Id; expertTestTopicjiexi.Falv = dto.Falv[i]; expertTestTopicjiexi.Fatiao = dto.Fatiao[i]; expertTestTopicjiexi.Flag = dto.Flag[i]; expertTestTopicjiexi.Creater = dto.Modifier; expertTestTopicjiexi.Createtime = DateTime.Now; expertTestTopicjiexi.Modifier = dto.Modifier; expertTestTopicjiexi.Modifytime = DateTime.Now; _context.ExpertTestTopicjiexis.Add(expertTestTopicjiexi); } } } _context.SaveChanges(); resultEntity.ReturnID = entity.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public ExpertTestTopicDTO Get(string id) { var entity = _context.ExpertTestTopics.Find(id); if (entity.RecStatus != "A") { entity = new ExpertTestTopic(); } var result = _mapper.Map(entity); return result; } public ResultDataEntity SearchByPaging(ExpertTestTopicDTOSearch 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 } ); ///ExpertTestTopics var query = (from a in _context.ExpertTestTopics join e in listCode.Where(x => x.CodeTable == "expert_test_topic" && x.CodeField == "topictype") on a.Topictype equals e.CodeSn into esssss from eee in esssss.DefaultIfEmpty() join f in listCode.Where(x => x.CodeTable == "expert_test_topic" && x.CodeField == "zhishitype") on a.Zhishitype equals f.CodeSn into fsssss from fff in fsssss.DefaultIfEmpty() join g in listCode.Where(x => x.CodeTable == "system" && x.CodeField == "shifou") on a.Zhongdian equals g.CodeSn into gsssss from ggg in gsssss.DefaultIfEmpty() join h in listCode.Where(x => x.CodeTable == "expert_test_topic" && x.CodeField == "jiexi_status") on a.JiexiStatus equals h.CodeSn into hsssss from hhh in hsssss.DefaultIfEmpty() join i in listCode.Where(x => x.CodeTable == "expert_test_topic" && x.CodeField == "shenpi_status") on a.ShenpiStatus equals i.CodeSn into isssss from iii in isssss.DefaultIfEmpty() join j in listCode.Where(x => x.CodeTable == "expert_test_topicjiexi" && x.CodeField == "falv") on a.ShenpiStatus equals j.CodeSn into jsssss from jjj in jsssss.DefaultIfEmpty() join k in _context.Areas on a.Area equals k.CodeId into ksssss from kkk in ksssss.DefaultIfEmpty() //join ll in ( from aaa in _context.ExpertTestTopicanwsers.Where(x => x.RecStatus == "A").Select(e => new { Topic = e.Topic, Items = e.Anwserno + " " + e.Anwser + "
" }).GroupBy(e => new { e.Topic }) // let ids = aaa.Select(b => b.Items.ToString()).ToArray() // select new { Topic = aaa.Key.Topic, Items = String.Join(" ", ids) } // //.Select(eg => new // //{ // // Topic = eg.Key.Topic, // // //EmployeeName = eg.First().EmployeeName, // // Items = eg.Select(i => i.Anwserno) + " " + eg.Select(i => i.Anwser) + "
", // //}) // ) //on a.Id equals ll.Topic // into llsssss //from llll in llsssss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.Topictype) || a.Topictype == searchEntity.Topictype.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Zhishitype) || a.Zhishitype == searchEntity.Zhishitype.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Zhongdian) || a.Zhongdian == searchEntity.Zhongdian.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.JiexiStatus) || a.JiexiStatus == searchEntity.JiexiStatus.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.ShenpiStatus) || a.ShenpiStatus == searchEntity.ShenpiStatus.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Topic) || a.Topic.Contains(searchEntity.Topic.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Area) || a.Area == searchEntity.Area.Trim()) select new ExpertTestTopicDTO { Id = a.Id, Topictype = a.Topictype, TopictypeName = eee.Comments, Zhishitype = a.Zhishitype, ZhishitypeName = fff.Comments, Topic = a.Topic + "
" /*+ llll.Items*/, Zhongdian = a.Zhongdian, ZhongdianName = ggg.Comments, JiexiStatus = a.JiexiStatus, JiexiStatusName = hhh.Comments, Jiexi = a.Jiexi, ShenpiStatus = a.ShenpiStatus, ShenpiStatusName = iii.Comments, Area = a.Area, AreaName = kkk.Name, Creater = a.Creater, Createtime = a.Createtime, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, } ).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(); var lianlist11 = lianlist.Select(x => x.Id).ToList(); var querydels = (from aaa in _context.ExpertTestTopicanwsers.Where(x => x.RecStatus == "A" && lianlist11.Contains(x.Topic) ) select aaa).ToList(); foreach(var lian in lianlist) { var querydel = querydels.Where(x => x.Topic == lian.Id).OrderBy(x=>x.Anwserno).ToList(); foreach (var quer in querydel) { lian.Topic += quer.Anwserno + " " + quer.Anwser + "
"; } } data.LoadData(searchEntity, lianlist); return data; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.ExpertTestTopics.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 获取所有有效角色 /// /// public List GetList() { var listRole = _context.ExpertTestTopics.Where(r => r.RecStatus == "A").ToList(); var list = _mapper.Map>(listRole); return list; } public ResultEntity saveanwser(ExpertTestTopicanwserDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var entity = _mapper.Map(dto); if (String.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); dto.Id = entity.Id; _context.ExpertTestTopicanwsers.Add(entity); } else { var updateproject = _context.ExpertTestTopicanwsers.Find(entity.Id); updateproject.Topic = entity.Topic; updateproject.Anwserno = entity.Anwserno; updateproject.Anwser = entity.Anwser; updateproject.Shifouzhengqu = entity.Shifouzhengqu; 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 List GetListanwser(string topicId = "") { var listRole = _context.ExpertTestTopicanwsers.Where(r => r.RecStatus == "A").ToList(); if (!string.IsNullOrEmpty(topicId)) { listRole = listRole.Where(x=>x.Topic == topicId).OrderBy(x=>x.Anwserno).ToList(); } var list = _mapper.Map>(listRole); return list; } public ResultEntity savejiexi(ExpertTestTopicjiexiDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var entity = _mapper.Map(dto); if (String.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); dto.Id = entity.Id; _context.ExpertTestTopicjiexis.Add(entity); } else { var updateproject = _context.ExpertTestTopicjiexis.Find(entity.Id); updateproject.Topic = entity.Topic; updateproject.Falv = entity.Falv; updateproject.Fatiao = entity.Fatiao; updateproject.Jiexi = entity.Jiexi; updateproject.Flag = entity.Flag; 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 List GetListjiexi(string topicId = "") { 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 } ); var listRole = (from a in _context.ExpertTestTopicjiexis join e in listCode.Where(x => x.CodeTable == "expert_test_topicjiexi" && x.CodeField == "falv") on a.Falv equals e.CodeSn into esssss from eee in esssss.DefaultIfEmpty() join f in listCode.Where(x => x.CodeTable == "system" && x.CodeField == "shifou") on a.Flag equals f.CodeSn into fsssss from fff in fsssss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(topicId) || a.Topic == topicId.Trim()) select new ExpertTestTopicjiexiDTO { Id = a.Id, Topic = a.Topic, Falv = a.Falv, FalvName = eee.Comments, Fatiao = a.Fatiao, Flag = a.Flag, FlagName = fff.Comments, Jiexi = a.Jiexi, Creater = a.Creater, Createtime = a.Createtime, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, } ).OrderBy(x => x.Flag).ThenByDescending(x=>x.Modifytime).ToList(); return listRole; } public ResultEntity shenpi(ExpertTestTopicDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var updateproject = _context.ExpertTestTopics.Find(dto.Id); updateproject.ShenpiStatus = dto.ShenpiStatus; updateproject.Modifier = dto.Modifier; updateproject.Modifytime = dto.Modifytime; _context.SaveChanges(); resultEntity.ReturnID = dto.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public ResultDataEntity SearchByPagingjiexi(ExpertTestTopicDTOSearch 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 } ); var query = (from a in _context.ExpertTestTopicjiexis join e in listCode.Where(x => x.CodeTable == "expert_test_topicjiexi" && x.CodeField == "falv") on a.Falv equals e.CodeSn into esssss from eee in esssss.DefaultIfEmpty() join f in listCode.Where(x => x.CodeTable == "system" && x.CodeField == "shifou") on a.Flag equals f.CodeSn into fsssss from fff in fsssss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.TopicId) || a.Topic == searchEntity.TopicId.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Falv) || a.Falv == searchEntity.Falv.Trim()) select new ExpertTestTopicjiexiDTO { Id = a.Id, Topic = a.Topic, Falv = a.Falv, FalvName = eee.Comments, Fatiao = a.Fatiao, Flag = a.Flag, FlagName = fff.Comments, Jiexi = a.Jiexi, Creater = a.Creater, Createtime = a.Createtime, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, } ).OrderBy(x => x.Flag).ThenByDescending(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(); data.LoadData(searchEntity, lianlist); return data; } public ExpertTestTopicjiexiDTO Getjiexi(string id) { var entity = _context.ExpertTestTopicjiexis.Find(id); if (entity.RecStatus != "A") { entity = new ExpertTestTopicjiexi(); } var result = _mapper.Map(entity); return result; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatusjiexi(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.ExpertTestTopicjiexis.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 获取所有有效角色 /// /// public List GetListResult() { 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 } ); var listRole = (from a in _context.ExpertTestResults join e in listCode.Where(x => x.CodeTable == "expert_test_result" && x.CodeField == "resulttype") on a.Resulttype equals e.CodeSn into esssss from eee in esssss.DefaultIfEmpty() where a.RecStatus == "A" //&& (string.IsNullOrWhiteSpace(topicId) || a.Topic == topicId.Trim()) select new ExpertTestResultDTO { Id = a.Id, Resulttype = a.Resulttype, ResulttypeName = eee.Comments, Result = a.Result, Creater = a.Creater, Createtime = a.Createtime, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, } ).OrderBy(x => x).ToList(); return listRole; } /// /// 获取所有有效角色 /// /// public List GetListpeizhi() { 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 } ); var listRole = (from a in _context.ExpertTestTopicpeizhis join e in listCode.Where(x => x.CodeTable == "expert_test_topic" && x.CodeField == "topictype") on a.Topictype equals e.CodeSn into esssss from eee in esssss.DefaultIfEmpty() join f in listCode.Where(x => x.CodeTable == "expert_test_topic" && x.CodeField == "zhishitype") on a.Zhishitype equals f.CodeSn into fsssss from fff in fsssss.DefaultIfEmpty() where a.RecStatus == "A" //&& (string.IsNullOrWhiteSpace(topicId) || a.Topic == topicId.Trim()) select new ExpertTestTopicpeizhiDTO { Id = a.Id, ShijianId = a.ShijianId, Topictype = a.Topictype, TopictypeName = eee.Comments, Zhishitype = a.Zhishitype, ZhishitypeName = fff.Comments, Fenshu = a.Fenshu, Fenzhi = a.Fenzhi, Creater = a.Creater, Createtime = a.Createtime, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, } ).OrderBy(x => x.Topictype).ThenBy(x => x.Zhishitype).ToList(); return listRole; } public ResultEntity saveResult(ExpertTestResultDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var entity = _mapper.Map(dto); if (String.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); dto.Id = entity.Id; _context.ExpertTestResults.Add(entity); } else { var updateproject = _context.ExpertTestResults.Find(entity.Id); updateproject.Resulttype = entity.Resulttype; updateproject.Result = entity.Result; 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 ResultEntity savepeizhi(ExpertTestTopicpeizhiDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var entity = _mapper.Map(dto); if (String.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); dto.Id = entity.Id; _context.ExpertTestTopicpeizhis.Add(entity); } else { var updateproject = _context.ExpertTestTopicpeizhis.Find(entity.Id); updateproject.ShijianId = entity.ShijianId; updateproject.Topictype = entity.Topictype; updateproject.Zhishitype = entity.Zhishitype; updateproject.Fenshu = entity.Fenshu; updateproject.Fenzhi = entity.Fenzhi; 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; } } }