Exemplo n.º 1
0
 public ActionResult Add(ClockModels model)
 {
     try
     {
         model.CardId        = model.CardId.Trim();
         model.PassWord      = model.PassWord.Trim();
         model.CreatTime     = DateTime.Now;
         model.flag          = true;
         model.ClockStateAM  = false;
         model.ClockStatePM  = false;
         model.LastClockTime = Convert.ToDateTime("2000-1-1 00:00:00");
         Db.ClockModels.Add(model);
         Db.Entry <ClockModels>(model).State = EntityState.Added;
         if (Db.SaveChanges() > 0)
         {
             return(Json("OK"));
         }
         else
         {
             return(Json("添加失败"));
         }
     }
     catch (Exception ex)
     {
         return(Json($"添加失败,请检查数据格式以及工号是否重复!异常信息:{ex}"));
     }
 }
Exemplo n.º 2
0
 public ActionResult Delete(string Id)
 {
     try
     {
         string[] ids = Id.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
         if (ids == null)
         {
             return(View("Index"));
         }
         foreach (var id in ids)
         {
             var model1 = new ClockModels()
             {
                 CardId = id
             };
             Db.ClockModels.Attach(model1);
             Db.Entry <ClockModels>(model1).State = EntityState.Deleted;
         }
         Db.SaveChanges();
         return(Json("OK"));
     }
     catch (Exception ex)
     {
         return(Json(string.Format("删除失败!异常信息:{0}", ex)));
     }
 }
Exemplo n.º 3
0
 public ActionResult Edit(ClockModels model)
 {
     try
     {
         model.PassWord = model.PassWord.Trim();
         Db.ClockModels.Attach(model);
         Db.Entry <ClockModels>(model).State = EntityState.Modified;
         Db.Entry(model).Property("LastClockTime").IsModified = false;
         Db.Entry(model).Property("ClockStateAM").IsModified  = false;
         Db.Entry(model).Property("ClockStatePM").IsModified  = false;
         Db.Entry(model).Property("CreatTime").IsModified     = false;
         Db.Entry(model).Property("flag").IsModified          = false;
         if (Db.SaveChanges() > 0)
         {
             return(Json("OK"));
         }
         else
         {
             return(Json("修改失败"));
         }
     }
     catch (Exception ex)
     {
         return(Json(string.Format("添加失败,请检查数据格式输入!异常信息:{0}", ex)));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 批量设置周六周日员工
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadBatchExcel()
        {
            int           count     = 0;
            StringBuilder failCadID = new StringBuilder();
            StringBuilder errorMsg  = new StringBuilder(); // 错误信息

            try
            {
                #region 1.获取Excel文件并转换为一个List集合

                // 1.1存放Excel文件到本地服务器
                HttpPostedFile filePost = System.Web.HttpContext.Current.Request.Files[0];
                string         filePath = ExcelHelper.SaveExcelFile(filePost); // 保存文件并获取文件路径

                // 单元格抬头
                // key:实体对象属性名称,可通过反射获取值
                // value:属性对应的中文注解
                Dictionary <string, string> cellheader = new Dictionary <string, string> {
                    { "CardId", "工号" },
                    { "NeedSta", "周六" },
                    { "NeedSun", "周日" }
                };

                // 1.2解析文件,存放到一个List集合里
                List <ClockModels> enlist = ExcelHelper.ExcelToEntityList <ClockModels>(cellheader, filePath, out errorMsg);
                if (errorMsg.Length != 0)
                {
                    return(Json($"excel格式不对,请使用模板格式导入!!《预期内的错误》"));
                }
                #endregion
                // 获取所有数据
                IEnumerable <ClockModels> allModels = Db.ClockModels;
                for (int i = 0; i < enlist.Count; i++)
                {
                    ClockModels toupdateitem = (from r in allModels
                                                where r.CardId == enlist[i].CardId
                                                select r).FirstOrDefault();
                    if (toupdateitem == null)
                    {
                        failCadID.AppendLine(enlist[i].CardId);
                        continue;
                    }
                    toupdateitem.CardId  = enlist[i].CardId.Trim();
                    toupdateitem.NeedSta = enlist[i].NeedSta;
                    toupdateitem.NeedSun = enlist[i].NeedSun;
                    enlist[i]            = null;

                    Db.ClockModels.Attach(toupdateitem);
                    Db.Entry <ClockModels>(toupdateitem).State = EntityState.Modified;
                    try
                    {
                        count += Db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        if (toupdateitem.CardId != null)
                        {
                            failCadID.AppendLine(toupdateitem.CardId);
                        }
                        continue;
                    }
                }
                return(Json($"批量设置成功!成功导入{count}条数据!异常的工号“{failCadID.ToString()}”"));
            }
            catch (Exception ex)
            {
                return(Json($"批量设置出错,已成功{count}条《预期外的错误》"));
            }
        }
Exemplo n.º 5
0
        public ActionResult UploadFileExcel()
        {
            int           count     = 0;
            StringBuilder failCadID = new StringBuilder();
            StringBuilder errorMsg  = new StringBuilder(); // 错误信息

            try
            {
                #region 1.获取Excel文件并转换为一个List集合

                // 1.1存放Excel文件到本地服务器
                HttpPostedFile filePost = System.Web.HttpContext.Current.Request.Files[0];
                string         filePath = ExcelHelper.SaveExcelFile(filePost); // 保存文件并获取文件路径

                // 单元格抬头
                // key:实体对象属性名称,可通过反射获取值
                // value:属性对应的中文注解
                Dictionary <string, string> cellheader = new Dictionary <string, string> {
                    { "CardId", "工号" },
                    { "PassWord", "密码" },
                    { "EmployeeName", "名称" },
                    { "TotalDays", "需要打卡总天数" },
                    { "OnTimeStart", "OnStart" },
                    { "OnTimeEnd", "OnEnd" },
                    { "OffTimeStart", "OffStart" },
                    { "OffTimeEnd", "OffEnd" },
                    { "NeedSta", "周六" },
                    { "NeedSun", "周日" }
                };

                // 1.2解析文件,存放到一个List集合里
                List <ClockModels> enlist = ExcelHelper.ExcelToEntityList <ClockModels>(cellheader, filePath, out errorMsg);
                if (errorMsg.Length != 0)
                {
                    return(Json($"excel格式不对,请使用模板格式导入!!《预期内的错误》"));
                }
                #endregion
                // 获取所有数据
                IEnumerable <ClockModels> allModels = Db.ClockModels.ToList();
                for (int i = 0; i < enlist.Count; i++)
                {
                    ClockModels toupdateitem = (from r in allModels
                                                where r.CardId == enlist[i].CardId
                                                select r).FirstOrDefault();
                    if (toupdateitem != null)
                    {
                        toupdateitem.CardId       = enlist[i].CardId.Trim();
                        toupdateitem.PassWord     = enlist[i].PassWord.Trim();
                        toupdateitem.EmployeeName = enlist[i].EmployeeName ?? "NULL";
                        toupdateitem.TotalDays    = enlist[i].TotalDays;
                        toupdateitem.OnTimeStart  = enlist[i].OnTimeStart;
                        toupdateitem.OnTimeEnd    = enlist[i].OnTimeEnd;
                        toupdateitem.OffTimeStart = enlist[i].OffTimeStart;
                        toupdateitem.OffTimeEnd   = enlist[i].OffTimeEnd;
                        toupdateitem.NeedSta      = enlist[i].NeedSta;
                        toupdateitem.NeedSun      = enlist[i].NeedSun;

                        toupdateitem.CreatTime     = DateTime.Now;
                        toupdateitem.LastClockTime = Convert.ToDateTime("2000-1-1 00:00");
                        toupdateitem.ClockStateAM  = false;
                        toupdateitem.ClockStatePM  = false;
                        toupdateitem.flag          = true;

                        enlist[i] = null;

                        Db.ClockModels.Attach(toupdateitem);
                        Db.Entry <ClockModels>(toupdateitem).State = EntityState.Modified;
                        try
                        {
                            count += Db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            if (toupdateitem.CardId != null)
                            {
                                failCadID.AppendLine(toupdateitem.CardId);
                            }
                            continue;
                        }
                    }
                }
                foreach (var model in enlist)
                {
                    if (model == null)
                    {
                        continue;
                    }
                    //默认数据
                    model.CardId        = model.CardId.Trim();
                    model.CreatTime     = DateTime.Now;
                    model.LastClockTime = Convert.ToDateTime("2000-1-1 00:00");
                    model.ClockStateAM  = false;
                    model.ClockStatePM  = false;
                    model.flag          = true;
                    Db.ClockModels.Add(model);
                    Db.Entry <ClockModels>(model).State = EntityState.Added;
                    try
                    {
                        count += Db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        if (model.CardId != null)
                        {
                            failCadID.AppendLine(model.CardId);
                        }
                        continue;
                    }
                }
                return(Json($"成功导入{count}条数据!异常的工号“{failCadID.ToString()}”"));
            }
            catch (Exception ex)
            {
                return(Json($"导入出错,已成功{count}条《预期外的错误》"));
            }
        }