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 GroupGroupService: IGroupGroupService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public GroupGroupService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(GroupGroupDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var checkUserSn = _context.GroupGroups.Where(x => x.Qunnumber == dto.Qunnumber && 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 groupGroup = _mapper.Map(dto); if (String.IsNullOrEmpty(groupGroup.Id)) { groupGroup.Id = Guid.NewGuid().ToString(); _context.GroupGroups.Add(groupGroup); } else { var updateproject = _context.GroupGroups.Find(groupGroup.Id); updateproject.Grouptype = groupGroup.Grouptype; updateproject.Hylb = groupGroup.Hylb; updateproject.Qunnumber = groupGroup.Qunnumber; updateproject.Qunzhuname = groupGroup.Qunzhuname; updateproject.Qunqq = groupGroup.Qunqq; updateproject.Qunwx = groupGroup.Qunwx; updateproject.Ywjl = groupGroup.Ywjl; updateproject.Tel = groupGroup.Tel; updateproject.Topic = groupGroup.Topic; updateproject.Ywjl = groupGroup.Ywjl; updateproject.Remark = groupGroup.Remark; updateproject.RecStatus = groupGroup.RecStatus; updateproject.Modifier = groupGroup.Modifier; updateproject.Modifytime = groupGroup.Modifytime; } _context.SaveChanges(); resultEntity.ReturnID = groupGroup.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public GroupGroupDTO Get(string id) { var entity = _context.GroupGroups.Find(id); if (entity.RecStatus != "A") { entity = new GroupGroup(); } var GroupGroupDTO = _mapper.Map(entity); return GroupGroupDTO; } public ResultDataEntity SearchByPaging(GroupGroupDTOSearch 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 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 query = (from a in _context.GroupGroups join b in _context.GroupGrouptypes.Where(x => x.RecStatus == "A") on a.Grouptype equals b.Id join d in _context.GroupTopics.Where(x => x.RecStatus == "A") on a.Topic equals d.Id into dsss from ddd in dsss.DefaultIfEmpty() join c in listCode.Where(x => x.CodeTable == "CooperatecustomCustomer" && x.CodeField == "hyfl") on a.Hylb equals c.CodeSn join e in _context.PltUsers.Where(x => x.RecStatus == "A" && x.Zhiwustatus == "A" && x.IsYwjl == "A") on a.Ywjl equals e.Id into esss from abi in esss.DefaultIfEmpty() join i in _context.GroupTuiguangs.Where(x => x.RecStatus == "A").GroupBy(q => new { q.GroupId }).Select(q => new { Khdw = q.Key.GroupId, Ordercount = q.Count(), } ) on a.Id equals i.Khdw into isss from iii in isss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.Createtime) || (a.Createtime >= Createtimestart && a.Createtime <= Createtimeend)) && (string.IsNullOrWhiteSpace(searchEntity.Qunnumber) || a.Qunnumber.Contains(searchEntity.Qunnumber.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Qunzhuname) || a.Qunzhuname.Contains(searchEntity.Qunzhuname.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Hylb) || a.Hylb == searchEntity.Hylb.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Topic) || a.Topic == searchEntity.Topic.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Ywjl) || a.Ywjl == searchEntity.Ywjl.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Ywjl) || a.Ywjl == searchEntity.Ywjl.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Grouptype) || a.Grouptype == searchEntity.Grouptype.Trim()) select new GroupGroupDTO { Id = a.Id, Grouptype = a.Grouptype, GrouptypeName = b.Grouptypename, Hylb = a.Hylb, HylbName = c.Comments, Qunnumber = a.Qunnumber, Qunzhuname = a.Qunzhuname, Qunqq = a.Qunqq, Qunwx = a.Qunwx, Tel = a.Tel, Topic = a.Topic, TopicName = ddd.Topicname, Ywjl = a.Ywjl, YwjlName = abi.UserName, Remark = a.Remark, Ordercount = iii.Ordercount.ToString(), RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, CreatetimeName = a.Createtime.ToString("yyyy-MM-dd"), } ).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(); data.LoadData(searchEntity, lianlist); return data; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.GroupGroups.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } } }