username@email.com
2022-09-21 78efdf47dccf3d5a04aa25cebc6bed02c0c062db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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<CooperVisitController> _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<CooperVisitController> 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<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            List<ActionEntity> actionlist = new List<ActionEntity>();
            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<PltUser>(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<CooperatecustomCustomerDTO>();
            }
            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();
        }
 
 
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data">岗位实体类对象</param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public IActionResult Save(CooperVisitDTO data)
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(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);
        }
 
 
        /// <summary>
        /// 删除主信息
        /// </summary>
        /// <param name="info">实体</param>
        /// <returns></returns>
        /// 
        public IActionResult Nullify(string Id = "")
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(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<PltUser>(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<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            List<CooperatecustomCustomerDTO> result = new List<CooperatecustomCustomerDTO>();
            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<PltUser>(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<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            List<CooperatecustomCustomerDTO> result = new List<CooperatecustomCustomerDTO>();
 
            if (baifangid != null)
            {
                result = _cooperVisitService.GetList(baifangid.Split(","));
            }
            else
            {
                resultEntity.Result = false;
                resultEntity.Message = "无联系人";
            }
            string customename = "";
            List<MailAddress> receivers = new List<MailAddress>();
            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<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            ViewData["baifangid"] = id;
 
            List<CooperatecustomCustomerDTO> result = new List<CooperatecustomCustomerDTO>();
 
            if (id != null)
            {
                result = _cooperVisitService.GetList(id.Split(","));
            }
            ViewData["customer"] = result;
 
            return View();
        }
    }
}