示例#1
0
        private async Task <bool> ValidateEmployees(KpiGeneral KpiGeneral)
        {
            if (KpiGeneral.EmployeeIds == null || !KpiGeneral.EmployeeIds.Any())
            {
                KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.EmployeeIds), ErrorCode.EmployeeIdsEmpty);
            }
            else
            {
                AppUserFilter AppUserFilter = new AppUserFilter
                {
                    Skip = 0,
                    Take = int.MaxValue,
                    Id   = new IdFilter {
                        In = KpiGeneral.EmployeeIds
                    },
                    OrganizationId = new IdFilter(),
                    Selects        = AppUserSelect.Id
                };

                var EmployeeIdsInDB   = (await UOW.AppUserRepository.List(AppUserFilter)).Select(x => x.Id).ToList();
                var listIdsNotExisted = KpiGeneral.EmployeeIds.Except(EmployeeIdsInDB).ToList();

                if (listIdsNotExisted != null && listIdsNotExisted.Any())
                {
                    foreach (var Id in listIdsNotExisted)
                    {
                        KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.EmployeeIds), ErrorCode.IdNotExisted);
                    }
                }
            }
            return(KpiGeneral.IsValidated);
        }
示例#2
0
        private async Task <bool> ValidateKpiGeneral(KpiGeneral KpiGeneral)
        {
            if (KpiGeneral.EmployeeIds == null || !KpiGeneral.EmployeeIds.Any())
            {
                KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.EmployeeIds), ErrorCode.EmployeeIdsEmpty);
            }
            else
            {
                foreach (var id in KpiGeneral.EmployeeIds)
                {
                    KpiGeneralFilter KpiGeneralFilter = new KpiGeneralFilter
                    {
                        Skip      = 0,
                        Take      = int.MaxValue,
                        AppUserId = new IdFilter {
                            Equal = id
                        },
                        KpiYearId = new IdFilter {
                            Equal = KpiGeneral.KpiYearId
                        },
                        Selects = KpiGeneralSelect.Employee | KpiGeneralSelect.KpiYear
                    };
                    int count = await UOW.KpiGeneralRepository.Count(KpiGeneralFilter);

                    if (count != 0)
                    {
                        KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.KpiYear), ErrorCode.KpiYearExisted);
                    }
                }
            }
            return(KpiGeneral.IsValidated);
        }
示例#3
0
 public async Task <bool> Delete(KpiGeneral KpiGeneral)
 {
     if (await ValidateId(KpiGeneral))
     {
     }
     return(KpiGeneral.IsValidated);
 }
示例#4
0
        public async Task <bool> Delete(KpiGeneral KpiGeneral)
        {
            await DataContext.KpiGeneral.Where(x => x.Id == KpiGeneral.Id).UpdateFromQueryAsync(x => new KpiGeneralDAO {
                DeletedAt = StaticParams.DateTimeNow
            });

            return(true);
        }
示例#5
0
 private async Task <bool> ValidateStatus(KpiGeneral KpiGeneral)
 {
     if (StatusEnum.ACTIVE.Id != KpiGeneral.StatusId && StatusEnum.INACTIVE.Id != KpiGeneral.StatusId)
     {
         KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.Status), ErrorCode.StatusNotExisted);
     }
     return(KpiGeneral.IsValidated);
 }
示例#6
0
        public async Task <KpiGeneral> Get(long Id)
        {
            KpiGeneral KpiGeneral = await UOW.KpiGeneralRepository.Get(Id);

            if (KpiGeneral == null)
            {
                return(null);
            }
            return(KpiGeneral);
        }
示例#7
0
        public async Task <bool> Create(KpiGeneral KpiGeneral)
        {
            await ValidateOrganization(KpiGeneral);
            await ValidateEmployees(KpiGeneral);
            await ValidateStatus(KpiGeneral);
            await ValidateKpiYear(KpiGeneral);
            await ValidateValue(KpiGeneral);
            await ValidateKpiGeneral(KpiGeneral);

            return(KpiGeneral.IsValidated);
        }
示例#8
0
        public async Task <bool> Update(KpiGeneral KpiGeneral)
        {
            if (await ValidateId(KpiGeneral))
            {
                await ValidateOrganization(KpiGeneral);
                await ValidateStatus(KpiGeneral);
                await ValidateValue(KpiGeneral);

                //await ValidateKpiGeneral(KpiGeneral);
            }
            return(KpiGeneral.IsValidated);
        }
示例#9
0
        private async Task SaveReference(KpiGeneral KpiGeneral)
        {
            await DataContext.KpiGeneralContentKpiPeriodMapping
            .Where(x => x.KpiGeneralContent.KpiGeneralId == KpiGeneral.Id)
            .DeleteFromQueryAsync();

            await DataContext.KpiGeneralContent
            .Where(x => x.KpiGeneralId == KpiGeneral.Id)
            .DeleteFromQueryAsync();

            List <KpiGeneralContentDAO> KpiGeneralContentDAOs = new List <KpiGeneralContentDAO>();
            List <KpiGeneralContentKpiPeriodMappingDAO> KpiGeneralContentKpiCriteriaItemMappingDAOs = new List <KpiGeneralContentKpiPeriodMappingDAO>();

            if (KpiGeneral.KpiGeneralContents != null)
            {
                KpiGeneral.KpiGeneralContents.ForEach(x => x.RowId = Guid.NewGuid());
                foreach (KpiGeneralContent KpiGeneralContent in KpiGeneral.KpiGeneralContents)
                {
                    KpiGeneralContentDAO KpiGeneralContentDAO = new KpiGeneralContentDAO();
                    KpiGeneralContentDAO.Id                   = KpiGeneralContent.Id;
                    KpiGeneralContentDAO.KpiGeneralId         = KpiGeneral.Id;
                    KpiGeneralContentDAO.KpiCriteriaGeneralId = KpiGeneralContent.KpiCriteriaGeneralId;
                    KpiGeneralContentDAO.RowId                = KpiGeneralContent.RowId;
                    KpiGeneralContentDAO.StatusId             = KpiGeneralContent.StatusId;
                    KpiGeneralContentDAOs.Add(KpiGeneralContentDAO);
                }
                await DataContext.KpiGeneralContent.BulkMergeAsync(KpiGeneralContentDAOs);


                foreach (KpiGeneralContent KpiGeneralContent in KpiGeneral.KpiGeneralContents)
                {
                    KpiGeneralContent.Id = KpiGeneralContentDAOs.Where(x => x.RowId == KpiGeneralContent.RowId).Select(x => x.Id).FirstOrDefault();
                    if (KpiGeneralContent.KpiGeneralContentKpiPeriodMappings != null)
                    {
                        foreach (KpiGeneralContentKpiPeriodMapping KpiGeneralContentKpiPeriodMapping in KpiGeneralContent.KpiGeneralContentKpiPeriodMappings)
                        {
                            KpiGeneralContentKpiPeriodMappingDAO KpiGeneralContentKpiCriteriaItemMappingDAO = new KpiGeneralContentKpiPeriodMappingDAO
                            {
                                KpiGeneralContentId = KpiGeneralContent.Id,
                                KpiPeriodId         = KpiGeneralContentKpiPeriodMapping.KpiPeriodId,
                                Value = KpiGeneralContentKpiPeriodMapping.Value
                            };
                            KpiGeneralContentKpiCriteriaItemMappingDAOs.Add(KpiGeneralContentKpiCriteriaItemMappingDAO);
                        }
                    }
                }

                await DataContext.KpiGeneralContentKpiPeriodMapping.BulkMergeAsync(KpiGeneralContentKpiCriteriaItemMappingDAOs);
            }
        }
示例#10
0
        public async Task <KpiGeneral> Create(KpiGeneral KpiGeneral)
        {
            if (!await KpiGeneralValidator.Create(KpiGeneral))
            {
                return(KpiGeneral);
            }

            try
            {
                await UOW.Begin();

                List <KpiGeneral> KpiGenerals = new List <KpiGeneral>();
                if (KpiGeneral.EmployeeIds != null && KpiGeneral.EmployeeIds.Any())
                {
                    foreach (var EmployeeId in KpiGeneral.EmployeeIds)
                    {
                        var newObj = Utils.Clone(KpiGeneral);
                        newObj.EmployeeId = EmployeeId;
                        newObj.CreatorId  = CurrentContext.UserId;
                        newObj.RowId      = Guid.NewGuid();
                        KpiGenerals.Add(newObj);
                    }
                }
                await UOW.KpiGeneralRepository.BulkMerge(KpiGenerals);

                await UOW.Commit();

                await Logging.CreateAuditLog(KpiGeneral, new { }, nameof(KpiGeneralService));

                return(KpiGeneral);
            }
            catch (Exception ex)
            {
                await UOW.Rollback();

                if (ex.InnerException == null)
                {
                    await Logging.CreateSystemLog(ex, nameof(KpiGeneralService));

                    throw new MessageException(ex);
                }
                else
                {
                    await Logging.CreateSystemLog(ex.InnerException, nameof(KpiGeneralService));

                    throw new MessageException(ex.InnerException);
                }
            }
        }
示例#11
0
        public async Task <bool> Create(KpiGeneral KpiGeneral)
        {
            KpiGeneralDAO KpiGeneralDAO = new KpiGeneralDAO();

            KpiGeneralDAO.Id             = KpiGeneral.Id;
            KpiGeneralDAO.OrganizationId = KpiGeneral.OrganizationId;
            KpiGeneralDAO.EmployeeId     = KpiGeneral.EmployeeId;
            KpiGeneralDAO.KpiYearId      = KpiGeneral.KpiYearId;
            KpiGeneralDAO.StatusId       = KpiGeneral.StatusId;
            KpiGeneralDAO.CreatorId      = KpiGeneral.CreatorId;
            KpiGeneralDAO.CreatedAt      = StaticParams.DateTimeNow;
            KpiGeneralDAO.UpdatedAt      = StaticParams.DateTimeNow;
            DataContext.KpiGeneral.Add(KpiGeneralDAO);
            await DataContext.SaveChangesAsync();

            KpiGeneral.Id = KpiGeneralDAO.Id;
            await SaveReference(KpiGeneral);

            return(true);
        }
示例#12
0
        public async Task <bool> ValidateId(KpiGeneral KpiGeneral)
        {
            KpiGeneralFilter KpiGeneralFilter = new KpiGeneralFilter
            {
                Skip = 0,
                Take = 10,
                Id   = new IdFilter {
                    Equal = KpiGeneral.Id
                },
                Selects = KpiGeneralSelect.Id
            };

            int count = await UOW.KpiGeneralRepository.Count(KpiGeneralFilter);

            if (count == 0)
            {
                KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.Id), ErrorCode.IdNotExisted);
            }
            return(count == 1);
        }
示例#13
0
        private async Task <bool> ValidateValue(KpiGeneral KpiGeneral)
        {
            bool flag = false;

            foreach (var KpiGeneralContent in KpiGeneral.KpiGeneralContents)
            {
                foreach (var item in KpiGeneralContent.KpiGeneralContentKpiPeriodMappings)
                {
                    if (item.Value != null)
                    {
                        flag = true;
                    }
                }
            }
            if (!flag)
            {
                KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.Id), ErrorCode.ValueCannotBeNull);
            }
            return(KpiGeneral.IsValidated);
        }
示例#14
0
        public KpiGeneral_KpiGeneralDTO(KpiGeneral KpiGeneral)
        {
            this.Id             = KpiGeneral.Id;
            this.OrganizationId = KpiGeneral.OrganizationId;
            this.EmployeeId     = KpiGeneral.EmployeeId;
            this.EmployeeIds    = KpiGeneral.EmployeeIds;
            this.KpiYearId      = KpiGeneral.KpiYearId;
            this.StatusId       = KpiGeneral.StatusId;
            this.CreatorId      = KpiGeneral.CreatorId;
            this.Creator        = KpiGeneral.Creator == null ? null : new KpiGeneral_AppUserDTO(KpiGeneral.Creator);
            this.Employee       = KpiGeneral.Employee == null ? null : new KpiGeneral_AppUserDTO(KpiGeneral.Employee);
            this.KpiYear        = KpiGeneral.KpiYear == null ? null : new KpiGeneral_KpiYearDTO(KpiGeneral.KpiYear);
            this.Organization   = KpiGeneral.Organization == null ? null : new KpiGeneral_OrganizationDTO(KpiGeneral.Organization);
            this.Status         = KpiGeneral.Status == null ? null : new KpiGeneral_StatusDTO(KpiGeneral.Status);

            this.KpiGeneralContents = KpiGeneral.KpiGeneralContents?.Select(x => new KpiGeneral_KpiGeneralContentDTO(x)).ToList();
            this.CreatedAt          = KpiGeneral.CreatedAt;
            this.UpdatedAt          = KpiGeneral.UpdatedAt;
            this.Errors             = KpiGeneral.Errors;
        }
示例#15
0
        private async Task <bool> ValidateKpiYear(KpiGeneral KpiGeneral)
        {
            KpiYearFilter KpiYearFilter = new KpiYearFilter
            {
                Id = new IdFilter {
                    Equal = KpiGeneral.KpiYearId
                }
            };

            int count = await UOW.KpiYearRepository.Count(KpiYearFilter);

            if (count == 0)
            {
                KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.KpiYear), ErrorCode.KpiYearIdNotExisted);
            }
            if (KpiGeneral.KpiYearId < StaticParams.DateTimeNow.Year)
            {
                KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.KpiYear), ErrorCode.KpiYearIdIsInThePast);
            }
            return(KpiGeneral.IsValidated);
        }
示例#16
0
        public async Task <bool> Update(KpiGeneral KpiGeneral)
        {
            KpiGeneralDAO KpiGeneralDAO = DataContext.KpiGeneral.Where(x => x.Id == KpiGeneral.Id).FirstOrDefault();

            if (KpiGeneralDAO == null)
            {
                return(false);
            }
            KpiGeneralDAO.Id             = KpiGeneral.Id;
            KpiGeneralDAO.OrganizationId = KpiGeneral.OrganizationId;
            KpiGeneralDAO.EmployeeId     = KpiGeneral.EmployeeId;
            KpiGeneralDAO.KpiYearId      = KpiGeneral.KpiYearId;
            KpiGeneralDAO.StatusId       = KpiGeneral.StatusId;
            KpiGeneralDAO.CreatorId      = KpiGeneral.CreatorId;
            KpiGeneralDAO.UpdatedAt      = StaticParams.DateTimeNow;
            await DataContext.SaveChangesAsync();

            await SaveReference(KpiGeneral);

            return(true);
        }
示例#17
0
        public async Task <KpiGeneral> Update(KpiGeneral KpiGeneral)
        {
            if (!await KpiGeneralValidator.Update(KpiGeneral))
            {
                return(KpiGeneral);
            }
            try
            {
                var oldData = await UOW.KpiGeneralRepository.Get(KpiGeneral.Id);

                await UOW.Begin();

                await UOW.KpiGeneralRepository.Update(KpiGeneral);

                await UOW.Commit();

                var newData = await UOW.KpiGeneralRepository.Get(KpiGeneral.Id);

                await Logging.CreateAuditLog(newData, oldData, nameof(KpiGeneralService));

                return(newData);
            }
            catch (Exception ex)
            {
                await UOW.Rollback();

                if (ex.InnerException == null)
                {
                    await Logging.CreateSystemLog(ex, nameof(KpiGeneralService));

                    throw new MessageException(ex);
                }
                else
                {
                    await Logging.CreateSystemLog(ex.InnerException, nameof(KpiGeneralService));

                    throw new MessageException(ex.InnerException);
                }
            }
        }
示例#18
0
        public async Task <KpiGeneral> Delete(KpiGeneral KpiGeneral)
        {
            if (!await KpiGeneralValidator.Delete(KpiGeneral))
            {
                return(KpiGeneral);
            }

            try
            {
                await UOW.Begin();

                await UOW.KpiGeneralRepository.Delete(KpiGeneral);

                await UOW.Commit();

                await Logging.CreateAuditLog(new { }, KpiGeneral, nameof(KpiGeneralService));

                return(KpiGeneral);
            }
            catch (Exception ex)
            {
                await UOW.Rollback();

                if (ex.InnerException == null)
                {
                    await Logging.CreateSystemLog(ex, nameof(KpiGeneralService));

                    throw new MessageException(ex);
                }
                else
                {
                    await Logging.CreateSystemLog(ex.InnerException, nameof(KpiGeneralService));

                    throw new MessageException(ex.InnerException);
                }
            }
        }
示例#19
0
        private async Task <bool> ValidateOrganization(KpiGeneral KpiGeneral)
        {
            if (KpiGeneral.OrganizationId == 0)
            {
                KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.Organization), ErrorCode.OrganizationEmpty);
            }
            else
            {
                OrganizationFilter OrganizationFilter = new OrganizationFilter
                {
                    Id = new IdFilter {
                        Equal = KpiGeneral.OrganizationId
                    }
                };

                var count = await UOW.OrganizationRepository.Count(OrganizationFilter);

                if (count == 0)
                {
                    KpiGeneral.AddError(nameof(KpiGeneralValidator), nameof(KpiGeneral.Organization), ErrorCode.OrganizationIdNotExisted);
                }
            }
            return(KpiGeneral.IsValidated);
        }
示例#20
0
        public async Task <KpiGeneral> Get(long Id)
        {
            KpiGeneral KpiGeneral = await DataContext.KpiGeneral.AsNoTracking()
                                    .Where(x => x.Id == Id).Select(x => new KpiGeneral()
            {
                CreatedAt      = x.CreatedAt,
                UpdatedAt      = x.UpdatedAt,
                Id             = x.Id,
                OrganizationId = x.OrganizationId,
                EmployeeId     = x.EmployeeId,
                KpiYearId      = x.KpiYearId,
                StatusId       = x.StatusId,
                CreatorId      = x.CreatorId,
                Creator        = x.Creator == null ? null : new AppUser
                {
                    Id             = x.Creator.Id,
                    Username       = x.Creator.Username,
                    DisplayName    = x.Creator.DisplayName,
                    Address        = x.Creator.Address,
                    Email          = x.Creator.Email,
                    Phone          = x.Creator.Phone,
                    Department     = x.Creator.Department,
                    OrganizationId = x.Creator.OrganizationId,
                    StatusId       = x.Creator.StatusId,
                    Avatar         = x.Creator.Avatar,
                    SexId          = x.Creator.SexId,
                    Birthday       = x.Creator.Birthday,
                },
                Employee = x.Employee == null ? null : new AppUser
                {
                    Id             = x.Employee.Id,
                    Username       = x.Employee.Username,
                    DisplayName    = x.Employee.DisplayName,
                    Address        = x.Employee.Address,
                    Email          = x.Employee.Email,
                    Phone          = x.Employee.Phone,
                    Department     = x.Employee.Department,
                    OrganizationId = x.Employee.OrganizationId,
                    StatusId       = x.Employee.StatusId,
                    Avatar         = x.Employee.Avatar,
                    SexId          = x.Employee.SexId,
                    Birthday       = x.Employee.Birthday,
                },
                KpiYear = x.KpiYear == null ? null : new KpiYear
                {
                    Id   = x.KpiYear.Id,
                    Code = x.KpiYear.Code,
                    Name = x.KpiYear.Name,
                },
                Organization = x.Organization == null ? null : new Organization
                {
                    Id       = x.Organization.Id,
                    Code     = x.Organization.Code,
                    Name     = x.Organization.Name,
                    ParentId = x.Organization.ParentId,
                    Path     = x.Organization.Path,
                    Level    = x.Organization.Level,
                    StatusId = x.Organization.StatusId,
                    Phone    = x.Organization.Phone,
                    Email    = x.Organization.Email,
                    Address  = x.Organization.Address,
                },
                Status = x.Status == null ? null : new Status
                {
                    Id   = x.Status.Id,
                    Code = x.Status.Code,
                    Name = x.Status.Name,
                },
            }).FirstOrDefaultAsync();

            if (KpiGeneral == null)
            {
                return(null);
            }
            KpiGeneral.KpiGeneralContents = await DataContext.KpiGeneralContent.AsNoTracking()
                                            .Where(x => x.KpiGeneralId == KpiGeneral.Id)
                                            .Select(x => new KpiGeneralContent
            {
                Id                   = x.Id,
                KpiGeneralId         = x.KpiGeneralId,
                KpiCriteriaGeneralId = x.KpiCriteriaGeneralId,
                StatusId             = x.StatusId,
                Status               = x.Status == null ? null : new Status
                {
                    Id   = x.Status.Id,
                    Name = x.Status.Name,
                    Code = x.Status.Code,
                },
                KpiCriteriaGeneral = new KpiCriteriaGeneral
                {
                    Id   = x.KpiCriteriaGeneral.Id,
                    Code = x.KpiCriteriaGeneral.Code,
                    Name = x.KpiCriteriaGeneral.Name,
                },
            }).OrderBy(p => p.KpiCriteriaGeneralId).ToListAsync();

            var KpiGeneralContentIds = KpiGeneral.KpiGeneralContents.Select(x => x.Id).ToList();
            List <KpiGeneralContentKpiPeriodMapping> KpiGeneralContentKpiPeriodMappings = await DataContext.KpiGeneralContentKpiPeriodMapping
                                                                                          .Where(x => KpiGeneralContentIds.Contains(x.KpiGeneralContentId))
                                                                                          .Select(x => new KpiGeneralContentKpiPeriodMapping
            {
                KpiGeneralContentId = x.KpiGeneralContentId,
                KpiPeriodId         = x.KpiPeriodId,
                Value             = x.Value,
                KpiGeneralContent = x.KpiGeneralContent == null ? null : new KpiGeneralContent
                {
                    Id = x.KpiGeneralContent.Id,
                    KpiCriteriaGeneralId = x.KpiGeneralContent.KpiCriteriaGeneralId,
                }
            }).ToListAsync();

            foreach (KpiGeneralContent KpiGeneralContent in KpiGeneral.KpiGeneralContents)
            {
                KpiGeneralContent.KpiGeneralContentKpiPeriodMappings = KpiGeneralContentKpiPeriodMappings.Where(x => x.KpiGeneralContentId == KpiGeneralContent.Id).ToList();
            }
            return(KpiGeneral);
        }