using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Web; using System.Web.Mvc; using WX.CRM.BLL.Base; using WX.CRM.BLL.Wx; using WX.CRM.Common; using WX.CRM.Model.Entity; using WX.CRM.WebHelper; namespace WX.CRM.WEB.Controllers.WeiXin { public class AttendanceController : BaseController { private readonly WX_ATTENDANCE_BL _wxAttendance; private readonly WX_LEAVE_BL _wxLeave; private readonly BAS_INNERUSER_BL _inneruser; private readonly SEQUENCES_BL _sequences; private readonly WX_AFTERSALE_COMMISSION_BL _wxAftersaleCommisson; public AttendanceController(WX_ATTENDANCE_BL wxAttendance, WX_LEAVE_BL wxLeave, BAS_INNERUSER_BL inneruser, SEQUENCES_BL sequences, WX_AFTERSALE_COMMISSION_BL wxAftersaleCommisson) { _wxAttendance = wxAttendance; _wxLeave = wxLeave; _inneruser = inneruser; _sequences = sequences; _wxAftersaleCommisson = wxAftersaleCommisson; } [HttpGet] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public ActionResult Index() { return View(); } [HttpPost] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public JsonResult Index(string columns) { return null; } [HttpPost] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public JsonResult Upload(HttpPostedFileBase fileData, int year, int month) { if (fileData != null) { try { //文件上传后的保存路径 string filePath = Server.MapPath("~/UploadFile/Attendance/"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileName = Path.GetFileName(fileData.FileName);//原始文件名称 string fileExtension = Path.GetExtension(fileName); //文件扩展名 string saveName = Guid.NewGuid() + fileExtension; //保存文件名称 fileData.SaveAs(filePath + saveName); var ret = handleArrivalUpload(saveName, year, month, fileExtension); return Json(ret ? new { Success = true, Message = "上传成功" } : new { Success = false, Message = "上传失败" }); } catch (Exception ex) { return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet); } } return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet); } public bool handleArrivalUpload(string file, int year, int month, string fileExten) { try { var m = new DateTime(year, month, 1); DataTable dt = new DataTable(); if (fileExten == ".xls") { NPOIHelper.InitializeWorkbook_xls(AppDomain.CurrentDomain.BaseDirectory + "UploadFile\\Attendance\\" + file); dt = NPOIHelper.ConvertToDataTable(); } else if (fileExten == ".xlsx") { NPOIHelper.InitializeWorkbook_xlsx(AppDomain.CurrentDomain.BaseDirectory + "UploadFile\\Attendance\\" + file); dt = NPOIHelper.ConvertToDataTable_xlsx(); } //LogHelper.Error(fileExten); if (dt.Rows.Count > 0) { int start = 0; int end = dt.Rows.Count; var list = new List(); while (start < end) { DataRow model = dt.Rows[start++]; decimal eid = 0; if (!string.IsNullOrWhiteSpace(model[0].ToString())) { eid = Convert.ToDecimal(model[0]); } decimal workday = 0; if (!string.IsNullOrWhiteSpace(model[1].ToString())) { workday = Convert.ToDecimal(model[1]); } decimal trainday = 0; if (!string.IsNullOrWhiteSpace(model[2].ToString())) { trainday = Convert.ToDecimal(model[2]); } decimal sickleave = 0; if (!string.IsNullOrWhiteSpace(model[3].ToString())) { sickleave = Convert.ToDecimal(model[3]); } decimal personalleave = 0; if (!string.IsNullOrWhiteSpace(model[4].ToString())) { personalleave = Convert.ToDecimal(model[4]); } decimal absenteeism = 0; if (!string.IsNullOrWhiteSpace(model[5].ToString())) { absenteeism = Convert.ToDecimal(model[5]); } decimal tousu = 0; if (!string.IsNullOrWhiteSpace(model[6].ToString())) { tousu = Convert.ToDecimal(model[6]); } decimal chufa = 0; if (!string.IsNullOrWhiteSpace(model[7].ToString())) { chufa = Convert.ToDecimal(model[7]); } decimal workday2 = 0; if (!string.IsNullOrWhiteSpace(model[8].ToString())) { workday2 = Convert.ToDecimal(model[8]); } decimal paidleave = 0; if (!string.IsNullOrWhiteSpace(model[9].ToString())) { paidleave = Convert.ToDecimal(model[9]); } var info = new WX_ATTENDANCE() { PKID = _sequences.Seq_base_get(), MONTH = m, EID = eid, WORKDAY = workday, SICKLEAVE = sickleave, PERSONALLEAVE = personalleave, CTIME = DateTime.Now, TRAINDAY = trainday, TOUSU = tousu, CHUFA = chufa, ABSENTEEISM = absenteeism, PAIDLEAVE = paidleave, WORKDAY2 = workday2 }; list.Add(info); } _wxAttendance.Delete(p => p.MONTH == m); _wxAttendance.AddList(list); } return true; } catch (Exception ex) { LogHelper.Error(ex); return false; } } [HttpGet] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public ActionResult Leave() { return View(); } [HttpPost] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public JsonResult Leave(string columns) { return null; } [HttpPost] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public JsonResult LeaveUpload(HttpPostedFileBase fileData, int year, int month) { if (fileData != null) { try { //文件上传后的保存路径 string filePath = Server.MapPath("~/UploadFile/Attendance/"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileName = Path.GetFileName(fileData.FileName);//原始文件名称 string fileExtension = Path.GetExtension(fileName); //文件扩展名 string saveName = Guid.NewGuid() + fileExtension; //保存文件名称 fileData.SaveAs(filePath + saveName); var ret = handleLeavelUpload(saveName, year, month, fileExtension); return Json(ret ? new { Success = true, Message = "上传成功" } : new { Success = false, Message = "上传失败" }); } catch (Exception ex) { return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet); } } return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet); } public bool handleLeavelUpload(string file, int year, int month, string fileExten) { try { var m = new DateTime(year, month, 1); DataTable dt = new DataTable(); if (fileExten == ".xls") { NPOIHelper.InitializeWorkbook_xls(AppDomain.CurrentDomain.BaseDirectory + "UploadFile\\Attendance\\" + file); dt = NPOIHelper.ConvertToDataTable(); } else if (fileExten == ".xlsx") { NPOIHelper.InitializeWorkbook_xlsx(AppDomain.CurrentDomain.BaseDirectory + "UploadFile\\Attendance\\" + file); dt = NPOIHelper.ConvertToDataTable_xlsx(); } //LogHelper.Error(fileExten); if (dt.Rows.Count > 0) { int start = 0; int end = dt.Rows.Count; var list = new List(); while (start < end) { DataRow model = dt.Rows[start++]; decimal eid = 0; if (!string.IsNullOrWhiteSpace(model[0].ToString())) { eid = Convert.ToDecimal(model[0]); } DateTime dismissDate = DateTime.MinValue; if (model[1] != null && !string.IsNullOrWhiteSpace(model[1].ToString())) { dismissDate = DateTime.Parse(DateTime.Parse(model[1].ToString()).ToString("yyyy-MM-dd")); } decimal dismissType = 0; if (!string.IsNullOrWhiteSpace(model[2].ToString())) { dismissType = Convert.ToDecimal(model[2]); } var info = new WX_LEAVE() { PKID = _sequences.Seq_base_get(), MONTH = m, EID = eid, DISMISSDATE = dismissDate, DISMISSTYPE = dismissType, CTIME = DateTime.Now }; list.Add(info); var user = _inneruser.getInnerUserByEid(eid); if (user != null) { user.ISDISMISS = 1; user.DISMISSTIME = dismissDate; user.DISMISSTYPE = dismissType; _inneruser.UpdateDismiss(user, UserId); } } _wxLeave.Delete(p => p.MONTH == m); _wxLeave.AddList(list); } return true; } catch (Exception ex) { LogHelper.Error(ex); return false; } } [HttpGet] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public ActionResult AfterSaleCommission() { return View(); } [HttpPost] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public JsonResult AfterSaleCommission(string columns) { return null; } [HttpPost] [AuthorizeRedirect(Roles = InitRights.CONST_考勤导入)] public JsonResult AfterSaleCommissionUpload(HttpPostedFileBase fileData, int year, int month) { if (fileData != null) { try { //文件上传后的保存路径 string filePath = Server.MapPath("~/UploadFile/Commission/"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileName = Path.GetFileName(fileData.FileName);//原始文件名称 string fileExtension = Path.GetExtension(fileName); //文件扩展名 string saveName = Guid.NewGuid() + fileExtension; //保存文件名称 fileData.SaveAs(filePath + saveName); var ret = handleCommissionUpload(saveName, year, month, fileExtension); return Json(ret ? new { Success = true, Message = "上传成功" } : new { Success = false, Message = "上传失败" }); } catch (Exception ex) { return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet); } } return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet); } public bool handleCommissionUpload(string file, int year, int month, string fileExten) { try { var m = new DateTime(year, month, 1); DataTable dt = new DataTable(); if (fileExten == ".xls") { NPOIHelper.InitializeWorkbook_xls(AppDomain.CurrentDomain.BaseDirectory + "UploadFile\\Commission\\" + file); dt = NPOIHelper.ConvertToDataTable(); } else if (fileExten == ".xlsx") { NPOIHelper.InitializeWorkbook_xlsx(AppDomain.CurrentDomain.BaseDirectory + "UploadFile\\Commission\\" + file); dt = NPOIHelper.ConvertToDataTable_xlsx(); } //LogHelper.Error(fileExten); if (dt.Rows.Count > 0) { int start = 0; int end = dt.Rows.Count; var list = new List(); while (start < end) { DataRow model = dt.Rows[start++]; decimal eid = 0; if (!string.IsNullOrWhiteSpace(model[0].ToString())) { eid = Convert.ToDecimal(model[0]); } var username = string.Empty; if (model[1] != null && !string.IsNullOrWhiteSpace(model[1].ToString())) { username = model[1].ToString(); } decimal commission = 0; if (!string.IsNullOrWhiteSpace(model[2].ToString())) { commission = Convert.ToDecimal(model[2]); } var info = new WX_AFTERSALE_COMMISSION() { PKID = _sequences.Seq_base_get(), MONTH = m, EID = eid, USERNAME = username, COMMISSION = commission, CTIME = DateTime.Now }; list.Add(info); } _wxAftersaleCommisson.Delete(p => p.MONTH == m); _wxAftersaleCommisson.AddList(list); } return true; } catch (Exception ex) { LogHelper.Error(ex); return false; } } } }