예제 #1
0
        public async Task <ResponseModel> Insert(EmployeeModel model)
        {
            ResponseModel response = new ResponseModel();

            try
            {
                if (await _context.EmployeeRepository.CountAsync(m => m.EmployeeCode == model.EmployeeCode) > 0)
                {
                    response.ResponseStatus = Core.CommonModel.Enums.ResponseStatus.CodeExists;
                    return(response);
                }

                await _context.BeginTransactionAsync();

                Employee md = new Employee();

                md.EmployeeCode            = model.EmployeeCode.Trim();
                md.ProbationDate           = model.ProbationDate;
                md.StartWorkingDate        = model.StartWorkingDate;
                md.BadgeCardNumber         = model.BadgeCardNumber ?? "";
                md.DateApplyBadge          = model.DateApplyBadge;
                md.FingerSignNumber        = model.FingerSignNumber ?? "";
                md.DateApplyFingerSign     = model.DateApplyFingerSign;
                md.WorkingEmail            = model.WorkingEmail ?? "";
                md.WorkingPhone            = model.WorkingPhone ?? "";
                md.EmployeeWorkingStatusId = model.EmployeeWorkingStatusId;
                md.BasicSalary             = model.BasicSalary;
                md.IsActive   = model.IsActive;
                md.CreateBy   = base.UserId;
                md.CreateDate = DateTime.Now;
                md.Deleted    = false;

                if (model.File != null)
                {
                    FileModel fileModel = new FileModel()
                    {
                        File         = model.File,
                        EmployeeCode = model.EmployeeCode
                    };

                    response = await _imageServerService.Insert(fileModel);

                    if (response.ResponseStatus != Core.CommonModel.Enums.ResponseStatus.Success)
                    {
                        await _context.RollbackTransactionAsync();

                        return(response);
                    }

                    md.AvatarFileId = int.Parse(response.Result.ToString());
                }

                await _context.EmployeeRepository.AddAsync(md).ConfigureAwait(true);

                await _context.SaveChangesAsync();

                EmployeeInfoModel info = new EmployeeInfoModel()
                {
                    EmployeeId = md.Id,
                    FirstName  = model.FirstName.Trim(),
                    LastName   = model.LastName.Trim(),
                    Gender     = model.Gender,
                    Action     = Core.CommonModel.Enums.FormActionStatus.Insert
                };

                response = await _employeeInfoService.Insert(info);

                if (response.ResponseStatus != Core.CommonModel.Enums.ResponseStatus.Success)
                {
                    await _context.RollbackTransactionAsync();

                    return(response);
                }

                await _context.CommitTransactionAsync();

                response = await Item(md.Id);
            }
            catch (Exception ex)
            {
                await _context.RollbackTransactionAsync();

                throw ex;
            }
            return(response);
        }