public static void SaveHistory(EmployeeEntity employeetity, int departid, IEmployee_History _iplEmployee_History)
        {
            var EmpHis = new Employee_HistoryEntity(); //lưu lại lịch sử đổi phòng

            //trường hợp department ko thay đổi htif ko cập nhập
            if (employeetity.DepartmentId != departid)
            {
                EmpHis.Position     = employeetity.Position;
                EmpHis.DepartmentId = departid;
                EmpHis.EmployeeId   = employeetity.Id;
                EmpHis.TimeIn       = DateTime.Now;
                //EmpHis.TimeOut = DateTime.Now;
                EmpHis.Note = "Chuyển sang từ " + employeetity.DepartmentName;
                var emphId = _iplEmployee_History.Insert(EmpHis);

                IplDepartment depart    = new IplDepartment();
                var           tmpdepart = depart.ViewDetail(departid);

                string departmentTo = tmpdepart.Name;



                EmpHis = _iplEmployee_History.SelectByPositionDepartment(employeetity.Id, employeetity.Position, employeetity.DepartmentId); //sửa lịch sử cũ
                if (EmpHis != null)
                {
                    EmpHis.TimeOut = DateTime.Now;
                    EmpHis.Note    = "Chuyển sang " + departmentTo;
                    var updateEmpHis = _iplEmployee_History.Update(EmpHis);
                }
            }
        }
        //[AuthorizeUser(ModuleName = "Employee", AccessLevel = Constants.Save)]
        public ActionResult Save(EmployeeEntity model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                EmployeeEntity entity       = _iplEmployee.ViewDetail(model.Id);
                var            DepartmentId = form["DepartmentId"];
                var            PositionId   = form["Position"];
                string[]       arrayDepartment;
                string[]       arrayPosition;
                arrayDepartment = DepartmentId.Split(',');
                arrayPosition   = PositionId.Split(',');
                var      DispatchWorkId = form["DispatchWork"];
                string[] arrayDispatchWork;
                arrayDispatchWork = DispatchWorkId.Split(',');

                #region Sửa Employee
                if (model.Id > 0)
                {
                    var sess = SessionSystem.GetUser();
                    Logs.logs("Sửa nhân sự", "Truy cập vào trang Employee", "/Employee/CreateEmployee", sess.UserId);
                    #region Sửa lịch sử Chức Vụ, Phòng Ban
                    if (DepartmentId != null && PositionId != null && arrayDepartment.Count() > 0 && arrayPosition.Count() == arrayDepartment.Count())
                    {
                        //ListHistory là list thêm mới, sửa phòng, chức
                        List <Employee_HistoryEntity> listHistory = ListHistory(model.Id, arrayDepartment, arrayPosition);
                        //ListHistoryOld lấy ra thằng TimeOut = null và IdDepartment và IdPosition không có trong param hoặc bị xóa
                        List <Employee_HistoryEntity> EmpHisOld = ListHistoryOld(model.Id, arrayDepartment, arrayPosition);
                        if (listHistory != null && listHistory.Count() == 0 && EmpHisOld != null && EmpHisOld.Count() > 0)
                        {
                            foreach (var item in EmpHisOld)
                            {
                                var listHistoryOld = _iplEmployee_History.SelectByPositionDepartment(item.EmployeeId, item.Position, item.DepartmentId);
                                if (listHistoryOld != null)
                                {
                                    listHistoryOld.TimeOut = DateTime.Now;
                                    var updateEmpHis = _iplEmployee_History.Update(listHistoryOld);
                                }
                            }
                        }
                        if (listHistory != null && listHistory.Count() > 0)
                        {
                            //Lưu thời gian nghỉ
                            foreach (var item in EmpHisOld)
                            {
                                var listHistoryOld = _iplEmployee_History.SelectByPositionDepartment(item.EmployeeId, item.Position, item.DepartmentId);
                                if (listHistoryOld != null)
                                {
                                    listHistoryOld.TimeOut = DateTime.Now;
                                    var updateEmpHis = _iplEmployee_History.Update(listHistoryOld);
                                }
                            }
                            foreach (var item in listHistory)
                            {
                                var EmpHis = new Employee_HistoryEntity();
                                EmpHis.Position     = item.Position;
                                EmpHis.DepartmentId = item.DepartmentId;
                                EmpHis.EmployeeId   = model.Id;
                                EmpHis.TimeIn       = DateTime.Now;
                                EmpHis.Note         = item.Note != null ? item.Note : form["Department" + item.DepartmentId];
                                _iplEmployee_History.Insert(EmpHis);
                            }
                        }
                        //Xóa liên kết cũ
                        _iplEmployee.DeleteDepartment(model.Id);
                        //Lưu liên kết mới
                        for (int i = 0; i < arrayDepartment.Count(); i++)
                        {
                            Employee_Department a = new Employee_Department();
                            a.IdDepartment = int.Parse(arrayDepartment[i]);
                            a.IdEmployee   = model.Id;
                            a.IdPosition   = int.Parse(arrayPosition[i]);
                            _iplEmployee.InsertDepartment(a);
                        }
                    }
                    else
                    {
                        ReturnFalse(model.Id);
                        ViewBag.ErrPosition = "Chức vụ và phòng ban phải tương đồng.";
                        return(View("CreateEmployee", model));
                    }
                    #endregion
                    #region Sửa Công Việc
                    if (DispatchWorkId != null)
                    {
                        //Xóa liên kết cũ
                        _iplEmployee.DeleteWork(model.Id);
                        //Lưu liên kết mới
                        foreach (var item in arrayDispatchWork)
                        {
                            Employee_DispatchWork a = new Employee_DispatchWork();
                            a.IdDispatchWork = int.Parse(item);
                            a.IdEmployee     = model.Id;
                            _iplEmployee.InsertDispatchWork(a);
                        }
                    }
                    else
                    {
                        ReturnFalse(model.Id);
                        ViewBag.ErrDispatchWork = "Công việc không được để trống.";
                        return(View("CreateEmployee", model));
                    }
                    #endregion
                    if (entity != null && entity.Id > 0)
                    {
                        entity.FirstName            = model.FirstName;
                        entity.LastName             = model.LastName;
                        entity.Phone                = model.Phone;
                        entity.Address              = model.Address;
                        entity.AcademicLevel        = model.AcademicLevel;
                        entity.Birthday             = model.Birthday;
                        entity.Gender               = model.Gender;
                        entity.IsActive             = model.IsActive;
                        entity.Position             = model.Position;
                        entity.DepartmentId         = model.DepartmentId;
                        entity.WageAgreement        = model.WageAgreement;
                        entity.ProbationaryFromDate = model.ProbationaryFromDate;
                        entity.ProbationaryToDate   = model.ProbationaryToDate;
                        entity.WorkFromDate         = model.WorkFromDate;
                        entity.WorkToDate           = model.WorkToDate;

                        if (model.file != null && model.file.ContentLength > 0)
                        {
                            if (!Utility.CheckImageFormat(model.file.FileName))
                            {
                                ViewBag.msgUpload = ConstantMsg.ErrorImageFormat;
                                return(View(model));
                            }
                            var retUpload = Upload.upload(_imagePath, model.file, 250, 250);
                            entity.PicturePath = retUpload.pathThumb;
                        }
                        var retVal = _iplEmployee.Update(entity);

                        if (retVal)
                        {
                            return(RedirectToAction("Index", "Employee"));
                        }
                    }
                }
                #endregion
                #region Thêm mới Employee
                else
                {
                    var sess = SessionSystem.GetUser();
                    Logs.logs("Thêm nhân sự", "Truy cập vào trang Employee", "/Employee/CreateEmployee", sess.UserId);
                    if (model.file != null && model.file.ContentLength > 0)
                    {
                        if (!Utility.CheckImageFormat(model.file.FileName))
                        {
                            ViewBag.msgUpload = ConstantMsg.ErrorImageFormat;
                            return(View(model));
                        }
                        var retUpload = Upload.upload(_imagePath, model.file, 250, 250);
                        model.PicturePath = retUpload.pathThumb;
                    }
                    var empId = _iplEmployee.Insert(model);

                    if (empId > 0)
                    {
                        for (int i = 0; i < arrayDepartment.Count(); i++)
                        {
                            var EmpHis = new Employee_HistoryEntity();
                            EmpHis.Position     = int.Parse(arrayPosition[i]);
                            EmpHis.DepartmentId = int.Parse(arrayDepartment[i]);
                            EmpHis.EmployeeId   = empId;
                            EmpHis.TimeIn       = DateTime.Now;
                            EmpHis.Note         = "Bắt đầu làm việc";
                            _iplEmployee_History.Insert(EmpHis);

                            var employee_Department = new Employee_Department();
                            employee_Department.IdDepartment = int.Parse(arrayDepartment[i]);
                            employee_Department.IdEmployee   = empId;
                            employee_Department.IdPosition   = int.Parse(arrayPosition[i]);
                            _iplEmployee.InsertDepartment(employee_Department);
                        }
                        //Lưu liên kết mới
                        foreach (var item in arrayDispatchWork)
                        {
                            Employee_DispatchWork a = new Employee_DispatchWork();
                            a.IdDispatchWork = int.Parse(item);
                            a.IdEmployee     = empId;
                            _iplEmployee.InsertDispatchWork(a);
                        }
                        return(RedirectToAction("Index", "User", new { id = empId }));
                    }
                }
                #endregion
            }
            ReturnFalse(model.Id);
            return(View("CreateEmployee", model));
        }