Exemplo n.º 1
0
        public ActionResult Create(tblCustomer obj, HttpPostedFileBase FileUpload, bool SaveAndCountinue = false, string key = "", string customergroup = "", string customerstatus = "", string RePassword = "", string hidFinger1 = "", string hidFinger2 = "")
        {
            ViewBag.keyValue            = key;
            ViewBag.CustomerGroupValue  = customergroup;
            ViewBag.customerstatusValue = customerstatus;

            ViewBag.CustomerGroups = GetMenuList();
            ViewBag.ControllerList = _tblAccessControllerService.GetAllActive();
            ViewBag.LevelList      = _tblAccessLevelService.GetAllActive();

            //Kiểm tra
            if (!ModelState.IsValid)
            {
                return(View(obj));
            }

            var existed = _tblCustomerService.GetByCode(obj.CustomerCode);

            if (existed != null)
            {
                ModelState.AddModelError("CustomerCode", "Mã khách hàng đã tồn tại");
                return(View(obj));
            }

            if (!string.IsNullOrWhiteSpace(obj.Password))
            {
                if (obj.Password != RePassword)
                {
                    ModelState.AddModelError("Password", "Vui lòng nhập lại đúng mật khẩu");
                    return(View(obj));
                }

                obj.Password = CryptorEngine.Encrypt(obj.Password, true);
            }

            if (!string.IsNullOrWhiteSpace(obj.DevPass))
            {
                var devpass = _tblCustomerService.GetByDevPass(obj.DevPass);
                if (devpass != null)
                {
                    ModelState.AddModelError("DevPass", "Mật khẩu đã tồn tại");
                    return(View(obj));
                }
            }

            //Gán giá trị
            obj.CustomerID       = Guid.NewGuid();
            obj.AccessExpireDate = Convert.ToDateTime("2099/12/31");
            obj.Finger1          = !string.IsNullOrWhiteSpace(hidFinger1) ? hidFinger1 : "";
            obj.Finger2          = !string.IsNullOrWhiteSpace(hidFinger2) ? hidFinger2 : "";
            obj.DevPass          = !string.IsNullOrEmpty(obj.DevPass) ? obj.DevPass : "";

            if (FileUpload != null)
            {
                var extension = Path.GetExtension(FileUpload.FileName) ?? "";
                var fileName  = Path.GetFileName(string.Format("{0}{1}", StringUtil.RemoveSpecialCharactersVn(FileUpload.FileName.Replace(extension, "")).GetNormalizeString(), extension));

                var url = ConfigurationManager.AppSettings["FileUploadAvatar"];
                obj.Avatar = string.Format("{0}{1}", url, fileName);
            }

            //Thực hiện thêm mới
            var result = _tblCustomerService.Create(obj);

            if (result.isSuccess)
            {
                WriteLog.Write(result, GetCurrentUser.GetUser(), obj.CustomerID.ToString(), obj.CustomerCode, "tblCustomer", ConstField.AccessControlCode, ActionConfigO.Create);

                UploadFile(FileUpload);

                if (SaveAndCountinue)
                {
                    TempData["Success"] = result.Message;
                    return(RedirectToAction("Create", new { key = key, customergroup = customergroup, customerstatus = customerstatus, selectedId = obj.CustomerID }));
                }

                return(RedirectToAction("Index", new { key = key, customergroup = customergroup, customerstatus = customerstatus, selectedId = obj.CustomerID }));
            }
            else
            {
                ModelState.AddModelError("", result.Message);
                return(View(obj));
            }
        }