using System; using System.Collections.Generic; using System.Linq; using System.Text; using AutoMapper; using AngleSharp.Html.Parser; using DTO; using IServices; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Bot.Builder.Integration.AspNet.Core; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System.Net.Http; using System.Threading.Tasks; using zhengcaioa.Models; using zhengcaioa.IService; using System.Net.Mail; using static CommonToolsCore.SendMailHelper; using CommonToolsCore; namespace zhengcaioa.Controllers.Customer { [CheckLogin] public class CooperVisitController : Controller { private readonly ILogger _logger; private readonly IProjectService _projectService; private readonly ILiaotianService _liaotianService; private readonly ICooperatecustomCustomerService _cooperatecustomCustomerService; private readonly IUserService _userService; private readonly ICooperVisitService _cooperVisitService; private readonly ICusFangwenjiluService _cusFangwenjiluService; private readonly IPltPageService _pltPageService; public CooperVisitController(ILogger logger, IProjectService projectService, ILiaotianService liaotianService, ICooperatecustomCustomerService cooperatecustomCustomerService, IUserService userService, ICooperVisitService cooperVisitService , ICusFangwenjiluService cusFangwenjiluService , IPltPageService pltPageService) { _logger = logger; _projectService = projectService; _liaotianService = liaotianService; _cooperatecustomCustomerService = cooperatecustomCustomerService; _userService = userService; _cooperVisitService = cooperVisitService; _cusFangwenjiluService = cusFangwenjiluService; _pltPageService = pltPageService; } public IActionResult Index(string Viscustomer = "") { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; List actionlist = new List(); ActionEntity actionEntity = new ActionEntity(); actionEntity.OpenType = 0; actionEntity.ActionUrl = ""; actionEntity.ActionFun = "Search"; actionEntity.PageIco = "fa fa-search"; actionEntity.ActionName = "查询"; actionlist.Add(actionEntity); //ActionEntity actionEntity1 = new ActionEntity(); //actionEntity1.OpenType = 0; //actionEntity1.ActionUrl = ""; //actionEntity1.ActionFun = "Add"; //actionEntity1.PageIco = "fa fa-plus"; //actionEntity1.ActionName = "新增"; //actionlist.Add(actionEntity1); var pageEntities = _pltPageService.GetUserPage(curentuser.Id, "/CooperVisit/Index/"); var pageEntities1 = pageEntities.Where(x => x.PageMethod == "01").ToList(); foreach (var pageEntity in pageEntities1) { ActionEntity actionEntity1 = new ActionEntity(); actionEntity1.OpenType = pageEntity.OpenType; actionEntity1.ActionUrl = ""; actionEntity1.ActionFun = pageEntity.PageShortcut; actionEntity1.PageIco = pageEntity.PageIco; actionEntity1.ActionName = pageEntity.PageName; actionlist.Add(actionEntity1); } ViewData["ActionInfo"] = actionlist; ViewBag.khlx = _liaotianService.GetSYScode("CooperatecustomCustomer", "khlx").Select(x => new { code = x.CodeSn, label = x.Comments }).ToList(); ViewBag.khly = _liaotianService.GetSYScode("CooperatecustomCustomer", "khly").Select(x => new { code = x.CodeSn, label = x.Comments }).ToList(); ViewBag.hyfl = _liaotianService.GetSYScode("CooperatecustomCustomer", "hyfl").Select(x => new { code = x.CodeSn, label = x.Comments }).ToList(); ViewBag.users = _userService.GetList().Where(x => x.IsYwjl == "A").Select(x => new { code = x.Id, label = x.UserName }).ToList(); ViewBag.Viscustomer = Viscustomer; var areaDTOs = _projectService.Getsheng().ToList(); foreach (var areaDTO in areaDTOs) { if (areaDTO.CodeId == "510000") { areaDTOs.Remove(areaDTO); break; } } var areaDTOsic = new Area(); areaDTOsic.CodeId = "510000"; areaDTOsic.Name = "四川省"; areaDTOsic.ParentId = "0 "; areaDTOs.Insert(1, areaDTOsic); ViewBag.ShengDropDown = areaDTOs.Select(x => new { code = x.CodeId, label = x.Name }).ToList(); return View(); } public IActionResult GetList(CooperVisitDTOSearch search) { //JsonResult jsonResult = new JsonResult(_liaotianService.SearchByPaging(search), new Newtonsoft.Json.Serialization.DefaultContractResolver()); return new JsonResult(_cooperVisitService.SearchByPaging(search)); } public IActionResult Edit(string id = null, string Viscustomer = null) { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; var pageEntities = _pltPageService.GetUserPage(curentuser.Id, "/CooperVisit/Index/"); var pageEntities2 = pageEntities.Where(x => x.PageMethod == "02").ToList(); ViewData["ActionInfo2"] = pageEntities2; CooperVisitDTO dto = new CooperVisitDTO(); if (!String.IsNullOrEmpty(id)) { dto = _cooperVisitService.Get(id); var cooperatecustomCustomerDTO = _cooperatecustomCustomerService.Get(dto.Viscustomer); if (cooperatecustomCustomerDTO != null) { dto.ViscustomerName = cooperatecustomCustomerDTO.Name; } } else { dto.Viscustomer = Viscustomer; dto.Vistime = DateTime.Now; if (!string.IsNullOrWhiteSpace(Viscustomer)) { var cooperatecustomCustomerDTO = _cooperatecustomCustomerService.Get(Viscustomer); if (cooperatecustomCustomerDTO != null) { dto.ViscustomerName = cooperatecustomCustomerDTO.Name; } } } if (!string.IsNullOrWhiteSpace(dto.Viscustomer)) { ViewData["customer"] = new List(); } else { ViewData["customer"] = _cooperatecustomCustomerService.GetList(); } ViewData["visType"] = _liaotianService.GetSYScode("CooperVisit", "visType"); ViewData["jtype"] = _liaotianService.GetSYScode("CooperVisit", "jtype"); ViewData["zw"] = _liaotianService.GetSYScode("CooperVisit", "zw"); ViewData["yx"] = _liaotianService.GetSYScode("CooperVisit", "yx"); ViewData["jy"] = _liaotianService.GetSYScode("CooperVisit", "jy"); ViewData.Model = dto; return View(); } /// /// 保存 /// /// 岗位实体类对象 /// /// [HttpPost] public IActionResult Save(CooperVisitDTO data) { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; data.RecStatus = "A"; if (String.IsNullOrEmpty(data.Id)) { data.Creater = curentuser.Id; data.Createtime = DateTime.Now; } data.Modifier = curentuser.Id; data.Modifytime = DateTime.Now; ResultEntity resultEntity = _cooperVisitService.save(data); return new JsonResult(resultEntity); } /// /// 删除主信息 /// /// 实体 /// /// public IActionResult Nullify(string Id = "") { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; return new JsonResult(_cooperVisitService.ModifyStatus(Id, curentuser.Id)); } public IActionResult Duanxin(string id = null) { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; ViewData["baifangid"] = id; return View(); } public IActionResult Fasong(string neirong, string baifangid) { ResultEntity resultEntity = new ResultEntity(); resultEntity.Result = true; var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; List result = new List(); if (baifangid != null) { result = _cooperVisitService.GetList(baifangid.Split(",")); } else { resultEntity.Result = false; resultEntity.Message = "无联系人号码"; } string customename = ""; StringBuilder number = new StringBuilder(); ; if (result != null && result.Count > 0) { for (int i = 0; i < result.Count; i++) { number.Append(string.IsNullOrEmpty(result[i].Phone) ? result[i].Tel : result[i].Phone); customename += result[i].Name + ","; if (i != result.Count - 1) { number.Append(","); } } } else { resultEntity.Result = false; resultEntity.Message = "无联系人"; } string sss = SMSHelper.PushWToUserMES(number.ToString(), neirong ); if (int.Parse(sss) <= 0) { resultEntity.Result = false; resultEntity.Message = "短信发送失败"; return new JsonResult(resultEntity); } CusFangwenjiluDTO dto = new CusFangwenjiluDTO(); dto.Fasongfangshi = "01"; dto.Fasongneirong = neirong; dto.Jieshouren = baifangid.Length>2000? baifangid.Substring(0,2000): baifangid; dto.RecStatus = "A"; dto.Creater = curentuser.Id; dto.Modifytime = DateTime.Now; dto.Modifier = curentuser.Id; dto.Createtime = dto.Modifytime; resultEntity = _cusFangwenjiluService.save(dto); _logger.LogInformation(curentuser.UserName + "发送短信给客户" + customename); return new JsonResult(resultEntity); } public IActionResult Youjian(string id = null) { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; ViewData["baifangid"] = id; return View(); } public IActionResult YoujianFasong(string title, string neirong, string baifangid) { ResultEntity resultEntity = new ResultEntity(); resultEntity.Result = true; var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; List result = new List(); if (baifangid != null) { result = _cooperVisitService.GetList(baifangid.Split(",")); } else { resultEntity.Result = false; resultEntity.Message = "无联系人"; } string customename = ""; List receivers = new List(); if (result != null && result.Count > 0) { for (int i = 0; i < result.Count; i++) { receivers.Add(new MailAddress(result[i].Email, result[i].FName)); customename += result[i].Name + ","; } } else { resultEntity.Result = false; resultEntity.Message = "无联系人号码"; } MailModel model = new MailModel(); //model.ReceiverAddress = result[i].Email;//"272629192@qq.com"; //model.ReceiverName = result[i].FName;// "尧"; model.receivers = receivers; model.Title = title; //"你好"; model.Content = neirong;// "你好呀"; //model.Host = "smtp.qq.com"; //model.Port = 587; //model.SenderName = "尧"; //model.SenderAddress = "272629192@qq.com"; //model.SenderPassword = "mnooicotjaflbhdd"; _logger.LogInformation("准备发送邮件"); resultEntity = SendMailHelper.SendMail(model); _logger.LogInformation("发送邮件完成" + resultEntity.Message); if (resultEntity.Result == false) { return new JsonResult(resultEntity); } CusFangwenjiluDTO dto = new CusFangwenjiluDTO(); dto.Fasongfangshi = "02"; dto.Fasongneirong = neirong; dto.Jieshouren = baifangid.Length > 2000 ? baifangid.Substring(0, 2000) : baifangid; ; dto.RecStatus = "A"; dto.Creater = curentuser.Id; dto.Modifytime = DateTime.Now; dto.Modifier = curentuser.Id; dto.Createtime = dto.Modifytime; resultEntity = _cusFangwenjiluService.save(dto); _logger.LogInformation(curentuser.UserName + "发送邮件给客户" + customename); return new JsonResult(resultEntity); } public IActionResult Xinhan(string id = null) { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; ViewData["baifangid"] = id; List result = new List(); if (id != null) { result = _cooperVisitService.GetList(id.Split(",")); } ViewData["customer"] = result; return View(); } } }