LR-20210131IOQH\Administrator
2021-06-10 6f34ab1ed6f8cb79a012a30a7b687e362c6f16c2
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DTO;
using Microsoft.Extensions.Logging; 
using IServices;
using Newtonsoft.Json;
using zhengcaioa.Models;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using System.Transactions;
using System.Runtime.InteropServices;
 
namespace zhengcaioa.Controllers.AdmManagement
{
    public class SigninController : Controller
    {
        private readonly ILogger<SigninController> _logger;
        private readonly IAdmSigninService _signinService;
        private readonly ISysAttachmentService _sysAttachmentService;
        private readonly IAdmAttendanceRuleService _attendanceRuleService;
        [CheckLogin]
        public SigninController(ILogger<SigninController> logger, IAdmSigninService signinService, ISysAttachmentService sysAttachmentService
            , IAdmAttendanceRuleService attendanceRuleService)
        {
            _logger = logger;
            _signinService = signinService;
            _sysAttachmentService = sysAttachmentService;
            _attendanceRuleService = attendanceRuleService;
        }
 
        #region 考勤签到打卡
        /// <summary>
        /// 上下班签到
        /// </summary>
        /// <returns></returns>
        [CheckLogin]
        public IActionResult Index()
        {       
            return View();
        }
 
        /// <summary>
        /// 我的考勤记录
        /// </summary>
        /// <returns></returns>
        [CheckLogin]
        public IActionResult Signin()
        {
            JArray jar = new JArray();
            for (var i = 1; i <= 12; i++)
            {
                var jo = string.Format("\"code\":{0},  \"label\":\"{1}月\" ", i, i);
                jar.Add(JObject.Parse("{" + jo + "}"));
 
            }
            ViewBag.month =  jar;
            JArray Year = new JArray();
            for (var i = -1; i < 2; i++)
            {
                var jo = string.Format("\"code\":{0},  \"label\":\"{0}年\" ", DateTime.Now.Year + i);
                Year.Add(JObject.Parse("{" + jo + "}"));
            }
            ViewBag.year =  Year;            
            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);
            ViewData["ActionInfo"] = actionlist;     
            
            return View();
        }
 
        /// <summary>
        /// 保存签到信息
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <param name="signinType">10早上上班  11早上下班  20下午上班   21下午下班   30加班上班  31加班下班</param>
        /// <returns></returns>
        [CheckLogin]
        public string Save(string id,int signinType,string mac) 
        {
            ResultEntity resultEntity = new ResultEntity();
            resultEntity.Result = false;
            try
            {
                if (string.IsNullOrEmpty(mac))
                {
                    resultEntity = _signinService.save(id, signinType);
                }
                else
                {
                    var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
                    if(curentuser.IpMac == mac)
                    {
                        //var mac = GetClientMAC(GetClientIP());
 
                    }
                    else
                    {
                        resultEntity.Message = "打卡电脑检查不匹配。";
                    }                    
                }
            }
            catch(Exception er)
            { }
            return JsonConvert.SerializeObject(resultEntity);
        }
 
 
 
        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
 
        [DllImport("Ws2_32.dll")]
        private static extern Int32 inet_addr(string ip);
 
        private string GetClientIP()
        {
            HttpContextAccessor context = new HttpContextAccessor();
            var ip = context.HttpContext?.Connection.RemoteIpAddress.ToString();
            var mac = GetClientMAC(ip);
            var str = "IP:" + ip + "   ,MAC:" + mac;
            return str;
        }
 
        private string GetClientMAC(string ip)
        {
            string mac_dest = string.Empty;
            try
            { 
                Int32 ldest = inet_addr(ip); //目的地的ip 
                Int32 lhost = inet_addr(""); //服务器的ip 
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref macinfo, ref len);
                string mac_src = macinfo.ToString("X");
                while (mac_src.Length < 12)
                {
                    mac_src = mac_src.Insert(0, "0");
                }
                for (int i = 0; i < 11; i++)
                {
                    if (0 == (i % 2))
                    {
                        if (i == 10)
                        {
                            mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
                        }
                        else
                        {
                            mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                        }
 
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return mac_dest;
        }
 
        /// <summary>
        /// 取得今天签到信息
        /// </summary>
        /// <returns></returns>
        [CheckLogin]
        public string GetTodaySignin() 
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));            
            return   JsonConvert.SerializeObject(_signinService.GetTodaySignin(curentuser.Id)); //.ReturnID;
        }
 
 
 
        /// <summary>
        /// 取得考勤记录
        /// </summary>
        /// <returns></returns>
 
        [CheckLogin]
        public string GetSigninList(AdmSignInDTOSearch search)
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            search.userId = curentuser.Id;
            return JsonConvert.SerializeObject(_signinService.SearchByPaging(search));
        }
        #endregion
        #region 请假与销假
         public IActionResult AskLeave(string signinId)
        {
            ViewBag.SigninId = signinId;
            AdmAskLeaveDTO admAskLeaveDTO = _signinService.GetAskLeave(signinId);
            if (admAskLeaveDTO.StratTime.Equals(DateTime.MinValue))
            {
                admAskLeaveDTO.StratTime = DateTime.Now;
            }
            if (admAskLeaveDTO.EndTime.Equals(DateTime.MinValue))
            {
                admAskLeaveDTO.EndTime = DateTime.Now;
            }
            ViewData.Model = admAskLeaveDTO;
            return View();
        }
 
 
        [CheckLogin]
        [HttpPost]
        public string SaveAskLeave(AdmAskLeaveDTO admAsk)
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            admAsk.RecStatus = "A";
            if (String.IsNullOrEmpty(admAsk.Id))
            {
                admAsk.Creater = curentuser.Id;
                admAsk.Createtime = DateTime.Now;
            }
            else
            {
                admAsk.Modifier = curentuser.Id;
                admAsk.Modifytime = DateTime.Now;
            }
 
             ResultEntity resultEntity = _signinService.SaveAskLeave(admAsk);
 
            return JsonConvert.SerializeObject(resultEntity);
             
        }
        public IActionResult AskLeaveOff(string signinId)
        {
            ViewBag.SigninId = signinId;
            AdmAskLeaveOffDTO admAskLeaveOffDTO = _signinService.GetAskLeaveOff(signinId);
            if (admAskLeaveOffDTO.StratTime.Equals(DateTime.MinValue))
            {
                admAskLeaveOffDTO.StratTime = DateTime.Now;
            }
            if (admAskLeaveOffDTO.EndTime.Equals(DateTime.MinValue))
            {
                admAskLeaveOffDTO.EndTime = DateTime.Now;
            }
            ResultDataEntity<SysAttachmentDTO> results = new ResultDataEntity<SysAttachmentDTO>();
            if (!string.IsNullOrEmpty(signinId))
            {
                SysAttachmentDTOSearch searchEntity = new SysAttachmentDTOSearch();
                searchEntity.page = 1;
                searchEntity.rows = 1000;
                searchEntity.AttObj = "AskLeaveOff";
                searchEntity.AttObjid = signinId;
                results = _sysAttachmentService.SearchByPaging(searchEntity);
            }
            ViewBag.attachment = results;
 
 
 
            ViewData.Model = admAskLeaveOffDTO;
            return View();
        }
 
        public string SaveAskLeaveOff(AdmAskLeaveOffDTO leaveOff) 
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            leaveOff.RecStatus = "A";
            if (String.IsNullOrEmpty(leaveOff.Id))
            {
                leaveOff.Creater = curentuser.Id;
                leaveOff.Createtime = DateTime.Now;
            }
            leaveOff.Modifier = curentuser.Id;
            leaveOff.Modifytime = DateTime.Now;
 
            ResultEntity resultEntity = _signinService.SaveAskLeaveOff(leaveOff);
            string[] Filepaths = leaveOff.Filepath;
 
            if(Filepaths!=null&& Filepaths.Length > 0)
            {
                for(int i=0;i< Filepaths.Length; i++)
                {
                    if (string.IsNullOrEmpty(leaveOff.attachmentid[i]))
                    {
                        SysAttachmentDTO sysAttachmentDTO = new SysAttachmentDTO();
                        //sysAttachmentDTO.Id = 
                        sysAttachmentDTO.AttObj = "AskLeaveOff";
                        sysAttachmentDTO.AttObjid = leaveOff.SigninId;
                        sysAttachmentDTO.Filefullname = leaveOff.Filefullname[i];
                        sysAttachmentDTO.Filepath = leaveOff.Filepath[i];
                        sysAttachmentDTO.Creater = curentuser.Id;
                        sysAttachmentDTO.Modifier = curentuser.Id;
                        sysAttachmentDTO.Createtime = DateTime.Now;
                        sysAttachmentDTO.Modifytime = sysAttachmentDTO.Createtime;
 
                        resultEntity = _sysAttachmentService.save(sysAttachmentDTO);
                    }
                  
                }
 
               
            }
 
            
 
            
 
            return JsonConvert.SerializeObject(resultEntity);
        }
        #endregion
 
 
        #region 考勤规则
        public IActionResult AttendanceRule()
        {
            AdmAttendanceRuleDTO admAttendanceRuleDTO =  _attendanceRuleService.Get("");
            admAttendanceRuleDTO.admAttendanceRulesDtlDTOs = _attendanceRuleService.GetDtlList("");
 
            ViewData.Model = admAttendanceRuleDTO;
            return View();
        }
 
        public string SaveAttendanceRule(AdmAttendanceRuleDTO dto)
        {
            ResultEntity resultEntity = new ResultEntity();
            dto.Holiday = "1";
            dto.Rest = "1";
            resultEntity.Result = false;
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            using (TransactionScope scope = new TransactionScope())
            {
                dto.RecStatus = "A";
                if (String.IsNullOrEmpty(dto.Id))
                {
                    dto.Creater = curentuser.Id;
                    dto.Createtime = DateTime.Now;
                }
                dto.Modifier = curentuser.Id;
                dto.Modifytime = DateTime.Now;
 
                resultEntity = _attendanceRuleService.save(dto);
 
 
                _attendanceRuleService.RemoveDtlList(dto.Id);
 
                for (int i = 0; i < dto.holiday_s.Length; i++)
                {
                    if(!String.IsNullOrEmpty(dto.holiday_s[i]) && !String.IsNullOrEmpty(dto.holiday_e[i]))
                    {
                        AdmAttendanceRulesDtlDTO admAttendanceRulesDtlDTO = new AdmAttendanceRulesDtlDTO();
                        admAttendanceRulesDtlDTO.Pid = dto.Id;
                        admAttendanceRulesDtlDTO.ValueStart = dto.holiday_s[i];
                        admAttendanceRulesDtlDTO.ValueEnd = dto.holiday_e[i];
                        admAttendanceRulesDtlDTO.Name = "jiejiari";
                        admAttendanceRulesDtlDTO.RecStatus = "A";
                        admAttendanceRulesDtlDTO.Creater = curentuser.Id;
                        admAttendanceRulesDtlDTO.Createtime = DateTime.Now;
                        admAttendanceRulesDtlDTO.Modifier = admAttendanceRulesDtlDTO.Creater;
                        admAttendanceRulesDtlDTO.Modifytime = admAttendanceRulesDtlDTO.Createtime;
                        resultEntity = _attendanceRuleService.saveDtl(admAttendanceRulesDtlDTO);
                    }
                   
                }
                for (int i = 0; i < dto.rest_s.Length; i++)
                {
                    if (!String.IsNullOrEmpty(dto.rest_s[i]) && !String.IsNullOrEmpty(dto.rest_e[i]))
                    {
                        AdmAttendanceRulesDtlDTO admAttendanceRulesDtlDTO = new AdmAttendanceRulesDtlDTO();
                        admAttendanceRulesDtlDTO.Pid = dto.Id;
                        admAttendanceRulesDtlDTO.ValueStart = dto.rest_s[i];
                        admAttendanceRulesDtlDTO.ValueEnd = dto.rest_e[i];
                        admAttendanceRulesDtlDTO.Name = "tiaoxiuri";
                        admAttendanceRulesDtlDTO.RecStatus = "A";
                        admAttendanceRulesDtlDTO.Creater = curentuser.Id;
                        admAttendanceRulesDtlDTO.Createtime = DateTime.Now;
                        admAttendanceRulesDtlDTO.Modifier = admAttendanceRulesDtlDTO.Creater;
                        admAttendanceRulesDtlDTO.Modifytime = admAttendanceRulesDtlDTO.Createtime;
                        resultEntity = _attendanceRuleService.saveDtl(admAttendanceRulesDtlDTO);
                    }
 
                       
                }
                scope.Complete();
            }
               
 
            //  var ss = leaveOff;
            return JsonConvert.SerializeObject(resultEntity);
        }
        #endregion
    }
}