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 CooperatecustomCustomerService: ICooperatecustomCustomerService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public CooperatecustomCustomerService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(CooperatecustomCustomerDTO cooperatecustomCustomerDTO) { ResultEntity resultEntity = new ResultEntity(); try { var checkUserSn = _context.CooperatecustomCustomers.Where(x => x.Name == cooperatecustomCustomerDTO.Name && x.RecStatus == "A" && x.Id != cooperatecustomCustomerDTO.Id).FirstOrDefault(); if (checkUserSn != null && (string.IsNullOrWhiteSpace(cooperatecustomCustomerDTO.Id) || (!string.IsNullOrWhiteSpace(cooperatecustomCustomerDTO.Id) && checkUserSn.Id != cooperatecustomCustomerDTO.Id))) { resultEntity.Result = false; resultEntity.Message = "客户单位重复"; return resultEntity; } if(!cooperatecustomCustomerDTO.zhuanyi && string.IsNullOrWhiteSpace(cooperatecustomCustomerDTO.Id)) { var checkyixiang = _context.IntentionCustomers.Where(x => x.Name == cooperatecustomCustomerDTO.Name && x.RecStatus == "A").FirstOrDefault(); if (checkyixiang != null) { resultEntity.Result = false; resultEntity.Message = "已存在意向客户,请转移客户!"; return resultEntity; } } var cooperatecustomCustomer = _mapper.Map(cooperatecustomCustomerDTO); if (String.IsNullOrEmpty(cooperatecustomCustomer.Id)) { cooperatecustomCustomer.Id = Guid.NewGuid().ToString(); _context.CooperatecustomCustomers.Add(cooperatecustomCustomer); } else { var updateproject = _context.CooperatecustomCustomers.Find(cooperatecustomCustomer.Id); updateproject.Sheng = cooperatecustomCustomer.Sheng; updateproject.City = cooperatecustomCustomer.City; updateproject.AreaId = cooperatecustomCustomer.AreaId; updateproject.Khly = cooperatecustomCustomer.Khly; updateproject.Hyfl = cooperatecustomCustomer.Hyfl; updateproject.Khlx = cooperatecustomCustomer.Khlx; updateproject.Ywjl = cooperatecustomCustomer.Ywjl; updateproject.Name = cooperatecustomCustomer.Name; updateproject.Postal = cooperatecustomCustomer.Postal; updateproject.Address = cooperatecustomCustomer.Address; updateproject.Url = cooperatecustomCustomer.Url; updateproject.Tel = cooperatecustomCustomer.Tel; updateproject.Phone = cooperatecustomCustomer.Phone; updateproject.Qq = cooperatecustomCustomer.Qq; updateproject.Email = cooperatecustomCustomer.Email; updateproject.FName = cooperatecustomCustomer.FName; updateproject.FTel = cooperatecustomCustomer.FTel; updateproject.FQq = cooperatecustomCustomer.FQq; updateproject.YwName = cooperatecustomCustomer.YwName; updateproject.YwTel = cooperatecustomCustomer.YwTel; updateproject.YwQq = cooperatecustomCustomer.YwQq; updateproject.JgCode = cooperatecustomCustomer.JgCode; updateproject.Khh = cooperatecustomCustomer.Khh; updateproject.Count = cooperatecustomCustomer.Count; updateproject.ShrName = cooperatecustomCustomer.ShrName; updateproject.ShrTel = cooperatecustomCustomer.ShrTel; updateproject.ShrAddress = cooperatecustomCustomer.ShrAddress; updateproject.Remark = cooperatecustomCustomer.Remark; updateproject.RecStatus = cooperatecustomCustomer.RecStatus; updateproject.Modifier = cooperatecustomCustomer.Modifier; updateproject.Modifytime = cooperatecustomCustomer.Modifytime; updateproject.Weixin = cooperatecustomCustomer.Weixin; updateproject.HuiyuanId = cooperatecustomCustomer.HuiyuanId; updateproject.Shifoutianjiaweixin = cooperatecustomCustomer.Shifoutianjiaweixin; updateproject.Weixintime = cooperatecustomCustomer.Weixintime; updateproject.Weixiner = cooperatecustomCustomer.Weixiner; updateproject.Shouxinedu = cooperatecustomCustomer.Shouxinedu; updateproject.Taocanjianshu = cooperatecustomCustomer.Taocanjianshu; } _context.SaveChanges(); resultEntity.ReturnID = cooperatecustomCustomer.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public ResultEntity saveyufukuan(CooperatecustomCustomerDTO cooperatecustomCustomerDTO) { ResultEntity resultEntity = new ResultEntity(); try { var cooperatecustomCustomer = _mapper.Map(cooperatecustomCustomerDTO); var updateproject = _context.CooperatecustomCustomers.Find(cooperatecustomCustomer.Id); updateproject.Yufukuan = cooperatecustomCustomer.Yufukuan; updateproject.Shenyushouxin = cooperatecustomCustomer.Shenyushouxin; updateproject.Shouxinedu = cooperatecustomCustomer.Shouxinedu; _context.SaveChanges(); resultEntity.ReturnID = cooperatecustomCustomer.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public CooperatecustomCustomerDTO Get(string id) { var entity = _context.CooperatecustomCustomers.Find(id); if(entity.RecStatus != "A") { entity = new CooperatecustomCustomer(); } var cooperatecustomCustomerDTO = _mapper.Map(entity); return cooperatecustomCustomerDTO; } public ResultDataEntity SearchByPaging(CooperatecustomCustomerDTOSearch 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.CooperatecustomCustomers join b in listCode.Where(x => x.CodeTable == "CooperatecustomCustomer" && x.CodeField == "khly") on a.Khly equals b.CodeSn into bsss from bbb in bsss.DefaultIfEmpty() join c in listCode.Where(x => x.CodeTable == "CooperatecustomCustomer" && x.CodeField == "hyfl") on a.Hyfl equals c.CodeSn into csss from ccc in csss.DefaultIfEmpty() join d in listCode.Where(x => x.CodeTable == "CooperatecustomCustomer" && x.CodeField == "khlx") on a.Khlx equals d.CodeSn into dsss from ddd in dsss.DefaultIfEmpty() 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.CooperOrders.Where(x => x.RecStatus == "A" && x.ChedanStatus != "A").GroupBy(q => new { q.Khdw }).Select(q => new { Khdw = q.Key.Khdw, Moneycount = q.Sum(i => i.Money), Ordercount = q.Count(), } ) on a.Id equals i.Khdw into isss from iii in isss.DefaultIfEmpty() join f in _context.Areas on a.Sheng equals f.CodeId into fsss from fff in fsss.DefaultIfEmpty() join g in _context.Areas on a.City equals g.CodeId into gsss from ggg in gsss.DefaultIfEmpty() join h in _context.Areas on a.AreaId equals h.CodeId into hsss from hhh in hsss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.Createtime) || (a.Createtime >= Createtimestart && a.Createtime <= Createtimeend)) && (string.IsNullOrWhiteSpace(searchEntity.Name) || a.Name.Contains(searchEntity.Name.Trim())) && (string.IsNullOrWhiteSpace(searchEntity.Khly) || a.Khly == searchEntity.Khly.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Hyfl) || a.Hyfl == searchEntity.Hyfl.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Khlx) || a.Khlx == searchEntity.Khlx.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Ywjl) || a.Ywjl == searchEntity.Ywjl.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Sheng) || a.Sheng == searchEntity.Sheng.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.City) || a.City == searchEntity.City.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.AreaId) || a.AreaId == searchEntity.AreaId.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.Shifouweixin) || a.Shifoutianjiaweixin == searchEntity.Shifouweixin.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.HuiyuanId) || a.HuiyuanId == searchEntity.HuiyuanId.Trim()) select new CooperatecustomCustomerDTO { Id = a.Id, Sheng = a.Sheng, City = a.City, AreaId = a.AreaId, Khly = a.Khly, Hyfl = a.Hyfl, Khlx = a.Khlx, Ywjl = a.Ywjl, ShengName = fff.Name + "-" + ggg.Name + "-" + hhh.Name, CityName = ggg.Name, AreaIdName = hhh.Name, KhlyName = bbb.Comments, HyflName = ccc.Comments, KhlxName = ddd.Comments, YwjlName = abi.UserName, Name = a.Name, Postal = a.Postal, Address = a.Address, Url = a.Url, Tel = a.Tel, Ordercount = iii.Ordercount.ToString(), Moneycount = iii.Moneycount.ToString(), Moneycountde = iii.Moneycount, Phone = a.Phone, Qq = a.Qq, Email = a.Email, FName = a.FName, FTel = a.FTel, FQq = a.FQq, YwName = a.YwName, YwTel = a.YwTel, YwQq = a.YwQq, JgCode = a.JgCode, Khh = a.Khh, Count = a.Count, ShrName = a.ShrName, ShrTel = a.ShrTel, ShrAddress = a.ShrAddress, Remark = a.Remark, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, CreatetimeName = a.Createtime.ToString("yyyy-MM-dd"), Weixin = a.Weixin, Shifoutianjiaweixin = a.Shifoutianjiaweixin, HuiyuanId = a.HuiyuanId, Weixiner = a.Weixiner, Weixintime = a.Weixintime, Yufukuan = a.Yufukuan ?? 0, Shouxinedu = a.Shouxinedu ?? 0, Shenyushouxin = a.Shenyushouxin ?? 0, YufukuanName = a.Yufukuan.HasValue ? a.Yufukuan.Value.ToString("f2") : "", ShouxineduName = a.Shouxinedu.HasValue ? a.Shouxinedu.Value.ToString("f2") : "", ShenyushouxinName = a.Shenyushouxin.HasValue ? a.Shenyushouxin.Value.ToString("f2") : "", Taocanjianshu = a.Taocanjianshu ?? 0, } ).OrderByDescending(x => x.Modifytime).ToList(); //if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); data.Heji1 = Math.Round(query.Sum(x => x.Yufukuan) ?? 0, 2); data.Heji2 = Math.Round(query.Sum(x => x.Shouxinedu) ?? 0, 2); data.Heji3 = Math.Round(query.Sum(x => x.Shenyushouxin) ?? 0, 2); data.Heji4 = Math.Round(query.Sum(x => x.Moneycountde) ?? 0, 2); var lianlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); data.DangyeHeji1 = Math.Round(lianlist.Sum(x => x.Yufukuan) ?? 0, 2); data.DangyeHeji2 = Math.Round(lianlist.Sum(x => x.Shouxinedu) ?? 0, 2); data.DangyeHeji3 = Math.Round(lianlist.Sum(x => x.Shenyushouxin) ?? 0, 2); data.DangyeHeji4 = Math.Round(lianlist.Sum(x => x.Moneycountde) ?? 0, 2); data.LoadData(searchEntity, lianlist); return data; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.CooperatecustomCustomers.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 获取所有有效合作客户 /// /// public List GetList(string HuiyuanId = "", string Name = "") { var listPosition = _context.CooperatecustomCustomers.Where(r => r.RecStatus == "A").ToList(); if (!string.IsNullOrEmpty(HuiyuanId)) { listPosition = listPosition.Where(x => x.HuiyuanId == HuiyuanId).ToList(); } if (!string.IsNullOrEmpty(Name)) { listPosition = listPosition.Where(x => x.Name == Name).ToList(); } var list = _mapper.Map>(listPosition); return list; } public List GetListsalary(string userid, DateTime datemin, DateTime datemax) { var listPosition = _context.CooperatecustomCustomers.Where(r => r.RecStatus == "A" && r.Creater == userid && r.Createtime >= datemin && r.Createtime < datemax).ToList(); var list = _mapper.Map>(listPosition); return list; } public List GetListsalaryqiandan(string userid, DateTime datemin, DateTime datemax) { var listPosition =( from a in _context.CooperatecustomCustomers.Where(r => r.RecStatus == "A" && r.Creater == userid) join b in _context.CooperOrders.Where(x => x.RecStatus == "A" && x.Createtime >= datemin && x.Createtime < datemax) on a.Id equals b.Khdw select a ).Distinct().ToList(); var list = _mapper.Map>(listPosition); return list; } public List GetListsalaryweixin(string userid, DateTime datemin, DateTime datemax) { var listPosition = _context.CooperatecustomCustomers.Where(r => r.RecStatus == "A" && r.Weixiner == userid && r.Weixintime >= datemin && r.Weixintime < datemax).ToList(); var list = _mapper.Map>(listPosition); return list; } } }