using System; using System.Collections.Generic; using System.Linq; using System.Text; using AutoMapper; using CommonToolsCore; using DTO; using IServices; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using zhengcaioa.Models; namespace Services { public class ProjectService : IProjectService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public ProjectService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity saveLiaotian(ProjectDTO projectDTO) { ResultEntity resultEntity = new ResultEntity(); try { var project = _mapper.Map(projectDTO); // var ZhuanjiaList = projectDTO.ZhuanjiaList; //if(ZhuanjiaList!=null&& ZhuanjiaList.Length > 0) //{ // for(int i = 0;i< ZhuanjiaList.Length; i++) // { // project.Zhuanjia += ZhuanjiaList[i] + ","; // } //} if (String.IsNullOrEmpty(project.Id)) { project.Id = Guid.NewGuid().ToString(); _context.Projects.Add(project); } else { var updateproject = _context.Projects.Find(project.Id); updateproject.StartTime = project.StartTime; updateproject.Sheng = project.Sheng; updateproject.City = project.City; updateproject.AreaId = project.AreaId; updateproject.Cgfs = project.Cgfs; updateproject.Number = project.Number; updateproject.Name = project.Name; updateproject.Drhx = project.Drhx; updateproject.DrhxPrice = project.DrhxPrice; updateproject.Dshx = project.Dshx; updateproject.DshxPrice = project.DshxPrice; updateproject.Zhuanjia = project.Zhuanjia; updateproject.Remark = project.Remark; updateproject.Toubiaocishu = project.Toubiaocishu; updateproject.Question = project.Question; updateproject.Zhongbiaocishu = project.Zhongbiaocishu; updateproject.Feibiaocishu = project.Feibiaocishu; updateproject.Zhongbiaojine = project.Zhongbiaojine; updateproject.RecStatus = project.RecStatus; updateproject.Modifier = project.Modifier; updateproject.Modifytime = project.Modifytime; updateproject.Fblx = project.Fblx; updateproject.Fbyy = project.Fbyy; updateproject.Caigourenname = project.Caigourenname; updateproject.Caigourenaddress = project.Caigourenaddress; updateproject.Caigourencontact = project.Caigourencontact; updateproject.Dailiname = project.Dailiname; updateproject.Dailicontact = project.Dailicontact; updateproject.Dailiaddress = project.Dailiaddress; } //子表 var projectreasonListremove = _context.Projectreasons.Where(x => x.ProjectId == project.Id).ToList(); if(projectreasonListremove != null && projectreasonListremove.Count > 0) { foreach (var projectreason in projectreasonListremove) { _context.Projectreasons.Remove(projectreason); } } var ReasonId = projectDTO.ReasonId; if (ReasonId != null&& ReasonId.Length>0) { for(int i = 0;i< ReasonId.Length;i++) { if (!string.IsNullOrEmpty(projectDTO.Reasonname[i])) { Projectreason projectreason = new Projectreason(); if (string.IsNullOrEmpty(ReasonId[i])) { projectreason.ReasonId = Guid.NewGuid().ToString(); } else { projectreason.ReasonId = ReasonId[i]; } projectreason.Reasonname = projectDTO.Reasonname[i]; projectreason.Reasonremark = projectDTO.Reasonremark[i]; projectreason.ProjectId = project.Id; _context.Projectreasons.Add(projectreason); } } } var ProjectyouxiaosListremove = _context.Projectyouxiaos.Where(x => x.ProjectId == project.Id).ToList(); if (ProjectyouxiaosListremove != null && ProjectyouxiaosListremove.Count > 0) { foreach (var projectyouxiao in ProjectyouxiaosListremove) { _context.Projectyouxiaos.Remove(projectyouxiao); } } var YouxiaoId = projectDTO.YouxiaoId; if (YouxiaoId != null && YouxiaoId.Length > 0) { for (int i = 0; i < YouxiaoId.Length; i++) { if (!string.IsNullOrEmpty(projectDTO.Namesyouxiao[i])) { Projectyouxiao projectyouxiao = new Projectyouxiao(); if (string.IsNullOrEmpty(YouxiaoId[i])) { projectyouxiao.YouxiaoId = Guid.NewGuid().ToString(); } else { projectyouxiao.YouxiaoId = YouxiaoId[i]; } projectyouxiao.Namesyouxiao = projectDTO.Namesyouxiao[i]; projectyouxiao.Priceyouxiao = projectDTO.Priceyouxiao[i]; projectyouxiao.ProjectId = project.Id; _context.Projectyouxiaos.Add(projectyouxiao); } } } var ProjectzhongbiaosListremove = _context.Projectzhongbiaos.Where(x => x.ProjectId == project.Id).ToList(); if (ProjectzhongbiaosListremove != null && ProjectzhongbiaosListremove.Count > 0) { foreach (var projectzhongbiao in ProjectzhongbiaosListremove) { _context.Projectzhongbiaos.Remove(projectzhongbiao); } } var ZhongbiaoId = projectDTO.ZhongbiaoId; if (ZhongbiaoId != null && ZhongbiaoId.Length > 0) { for (int i = 0; i < ZhongbiaoId.Length; i++) { if (!string.IsNullOrEmpty(projectDTO.Names[i])) { Projectzhongbiao projectzhongbiao = new Projectzhongbiao(); if (string.IsNullOrEmpty(ZhongbiaoId[i])) { projectzhongbiao.ZhongbiaoId = Guid.NewGuid().ToString(); } else { projectzhongbiao.ZhongbiaoId = ZhongbiaoId[i]; } projectzhongbiao.Names = projectDTO.Names[i]; projectzhongbiao.Price = projectDTO.Price[i]; projectzhongbiao.ProjectId = project.Id; _context.Projectzhongbiaos.Add(projectzhongbiao); } } } CacheHelperNetCore.CacheNull("Projects"); _context.SaveChanges(); resultEntity.ReturnID = project.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public ProjectDTO GetLiaotianEntity(string id) { var entity = _context.Projects.Find(id); if (entity.RecStatus != "A") { entity = new Project(); } var projectreasons = _context.Projectreasons.Where(x => x.ProjectId == entity.Id).ToList(); var Projectyouxiaos = _context.Projectyouxiaos.Where(x => x.ProjectId == entity.Id).ToList(); var Projectzhongbiaos = _context.Projectzhongbiaos.Where(x => x.ProjectId == entity.Id).ToList(); var projectDTO = _mapper.Map(entity); if(projectreasons!=null && projectreasons.Count>0) { projectDTO.projectreasons = projectreasons; } else { var Projectreasonlist = new List(); Projectreasonlist.Add(new Projectreason()); projectDTO.projectreasons = Projectreasonlist; } if (Projectyouxiaos != null && Projectyouxiaos.Count > 0) { projectDTO.Projectyouxiaos = Projectyouxiaos; } else { var Projectyouxiaolist = new List(); Projectyouxiaolist.Add(new Projectyouxiao()); projectDTO.Projectyouxiaos = Projectyouxiaolist; } if (Projectzhongbiaos != null && Projectzhongbiaos.Count > 0) { projectDTO.Projectzhongbiaos = Projectzhongbiaos; } else { var Projectzhongbiaoslist = new List(); Projectzhongbiaoslist.Add(new Projectzhongbiao()); projectDTO.Projectzhongbiaos = Projectzhongbiaoslist; } return projectDTO; } public ResultDataEntity SearchByPaging(ProjectDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); List list = new List(); //筛选 StringBuilder sql = new StringBuilder(); sql.Append("SELECT distinct a.[Id],a.[start_time] ,b.[name] +'-'+c.[name]+'-'+d.[name] Sheng ,a.city ,a.[area_id],a.cgfs, a.[name] ,a.[number],a.creater,a.createtime,a.modifier,a.modifytime ,a.[drhx],a.[drhx_price],a.[dshx],a.[dshx_price],a.[remark],a.[rec_status],a.[toubiaocishu] ,a.[question] ,a.[zhongbiaocishu],a.[feibiaocishu],a.[zhongbiaojine],a.[Fblx],a.[Fbyy] , a.[zhuanjia] , a.[caigourenname], a.[caigourenaddress], a.[caigourencontact], a.[dailiname], a.[dailiaddress], a.[dailicontact] "); //zhuanjia = stuff((select ','+name from [Zhuanjia] f where CHARINDEX(f.id,a.[zhuanjia]) >0 for xml path('')), 1,1, '') sql.Append("FROM [Project] a "); sql.Append("left join [Area] b on a.[sheng] = b.[codeID] "); sql.Append("left join [Area] c on a.[city] = c.[codeID] "); sql.Append("left join [Area] d on a.[area_id] = d.[codeID] "); //sql.Append("left join [Zhuanjia] e on CHARINDEX(e.id,a.[zhuanjia]) >0 "); sql.Append(" where a.rec_status='A' "); if (!string.IsNullOrWhiteSpace(searchEntity.Names)) { sql.Append(" and exists (select b.[ProjectId] from [Projectzhongbiao] b where a.[Id]=b.[ProjectId] and b.[names] like '%"+ searchEntity.Names + "%') "); } if (!string.IsNullOrWhiteSpace(searchEntity.StartTimestar)) { sql.Append(" and a.start_time >='"+ searchEntity.StartTimestar.Trim().Replace("'","''") + "'"); } if (!string.IsNullOrWhiteSpace(searchEntity.StartTimeend)) { sql.Append(" and a.start_time <='" + DateTime.Parse(searchEntity.StartTimeend.Trim().Replace("'", "''")).AddDays(1).ToString("yyyy-MM-dd") + "'"); ; } if (!string.IsNullOrWhiteSpace(searchEntity.Sheng)) { sql.Append(" and a.[sheng] = '" + searchEntity.Sheng.Trim().Replace("'", "''") + "' "); } if (!string.IsNullOrWhiteSpace(searchEntity.City)) { sql.Append(" and a.[city] = '" + searchEntity.City.Trim().Replace("'", "''") + "' "); } if (!string.IsNullOrWhiteSpace(searchEntity.AreaId)) { sql.Append(" and a.[area_id] = '" + searchEntity.AreaId.Trim().Replace("'", "''") + "' "); } if (!string.IsNullOrWhiteSpace(searchEntity.Cgfs)) { sql.Append("and a.[cgfs] ='" + searchEntity.Cgfs.Trim().Replace("'", "''") + "'"); } if (!string.IsNullOrWhiteSpace(searchEntity.Zhuanjia)) { sql.Append("and a.[zhuanjia] like '%" + searchEntity.Zhuanjia.Trim().Replace("'", "''") + "%'"); } if (!string.IsNullOrWhiteSpace(searchEntity.Number)) { sql.Append("and a.[number] like '%" + searchEntity.Number.Trim().Replace("'", "''") + "%'"); } if (!string.IsNullOrWhiteSpace(searchEntity.Name)) { sql.Append("and a.[name] like '%" + searchEntity.Name.Trim().Replace("'", "''") + "%'"); } 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); } if (!string.IsNullOrWhiteSpace(searchEntity.Createtime)) { sql.Append(" and a.Createtime >='" + Createtimestart.ToString("yyyy-MM-dd") + "'"); } if (!string.IsNullOrWhiteSpace(searchEntity.Createtime)) { sql.Append(" and a.Createtime <='" + Createtimeend.AddDays(1).ToString("yyyy-MM-dd") + "'"); } if (!string.IsNullOrWhiteSpace(searchEntity.Creater)) { sql.Append(" and a.Creater ='" + searchEntity.Creater + "'"); } //if (!string.IsNullOrEmpty(searchEntity.pingshenqingkuang) ) //{ // if(searchEntity.pingshenqingkuang.Equals("中标供应商")) // { // sql.Append(" a.[zhongbiaocishu] !='' "); // }else if (searchEntity.pingshenqingkuang.Equals("有效供应商")) // { // sql.Append(" a.[zhongbiaocishu] !='' "); // } // else if (searchEntity.pingshenqingkuang.Equals("无效供应商")) // { // sql.Append(" a.[zhongbiaocishu] !='' "); // } //} var query = _context.Projects .FromSqlRaw(sql.ToString()).OrderByDescending(x=>x.StartTime) .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, list); return data; } public List Getzhuanjie() { var zhuanjia = _context.Zhuanjia.ToList(); return zhuanjia; } public List GetCgfs() { var Cgfs = _context.Cgfs.ToList(); return Cgfs; } public List Getsheng() { var Areas = _context.Areas.Where(x=>x.ParentId == "0").ToList(); return Areas; } public List Getshi(string sheng) { var Areas = _context.Areas.Where(x => x.ParentId == sheng).ToList(); return Areas; } public List Getquxian(string shi) { var Areas = _context.Areas.Where(x => x.ParentId == shi).ToList(); return Areas; } /// /// 获取项目下拉列表 /// /// public List AllcgfsDropDown() { var listProject = _context.Cgfs.ToList(); var items = listProject.Select(x => new SelectListItem { Text = $"{x.Name}", Value = x.Id }).ToList(); return items; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.Projects.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 获取所有有效合作客户 /// /// public List GetList() { List projectDTOs = (List)CacheHelperNetCore.CacheValue("Projects"); if (projectDTOs!=null&& projectDTOs.Count > 0) { return projectDTOs; } var listPosition = _context.Projects.Where(r => r.RecStatus == "A").ToList(); var list = _mapper.Map>(listPosition); CacheHelperNetCore.CacheInsert("Projects", list); return list; } } }