C# 员工排班系统: 保存排班信息并处理更新操作

本文将介绍使用 C# 开发的员工排班系统中,如何保存排班信息并处理更新操作。

代码示例

public async Task SaveAsync(SaveWorkforceTimeDto input)
{
    try
    {
        List<Workforce> workforces = new();
        foreach (var item in input.Workforce)
        {
            Workforce workforce1 = ObjectMapper.Map<SaveWorkforceDto, Workforce>(item);
            if (workforce1.Id > 0)
            {
                // 给 Id 属性赋值,以便进行更新操作
                workforce1.Id = item.Id;
                _WorkforceRepository.UpdateAsync(workforce1);
                // 或者使用下面的方法进行更新
                // var workforce = await _WorkforceRepository.FindAsync(workforce1.Id);
                // ObjectMapper.Map(workforce1, workforce);
                // await _WorkforceRepository.UpdateAsync(workforce);
            }
            else
            {
                workforce1.Time = input.Time.Date;
                await _WorkforceRepository.InsertAsync(workforce1);
            }
        }
    }
    catch (Exception ex)
    {
        throw new AbpException('保存失败:' + ex.Message);
    }
}

public class SaveWorkforceDto
{
    public long Id { get; set; }
    /// <summary>
    /// 岗位
    /// </summary>
    public string Post { get; set; }
    public ICollection<SaveWorkforceRelationDto> Morning { get; set; }
    public ICollection<SaveWorkforceRelationDto> Noon { get; set; }
    public ICollection<SaveWorkforceRelationDto> Night { get; set; }
    
}

public class SaveWorkforceTimeDto : ExtensibleObject
{
    public Guid? TenantId { get; set; }
    public ICollection<SaveWorkforceDto> Workforce { get; set; }
    /// <summary>
    /// 时间
    /// </summary>
    public DateTime Time { get; set; }
}

public class SaveWorkforceRelationDto : ExtensibleObject
{
    public long Id { get; set; }
    public long? StaffFileId { get; set; }
}

public class Workforce : FullAuditedAggregateRoot<long>, IMultiTenant //租户
{
    public Guid? TenantId { get; set; }
    //public long? PostId { get; set; }
    [Comment('岗位')]
    public string Post { set; get; }
    public ICollection<WorkforceRelation> Morning { get; set; }
    public ICollection<WorkforceRelation> Noon { get; set; }
    public ICollection<WorkforceRelation> Night { get; set; }
    public DateTime? Time { get; set; }
}

public class WorkforceRelation : FullAuditedAggregateRoot<long>
{
    public long? StaffFileId { get; set; }
    public StaffFile StaffFile { get; set; }
}

代码解释

  1. SaveAsync 方法: 该方法用于保存排班信息。它遍历 input.Workforce 集合,并使用 ObjectMapperSaveWorkforceDto 对象映射到 Workforce 对象。
  2. 更新操作: 当 workforce1.Id 大于 0 时,表示需要更新已有数据。需要给 workforce1.Id 属性赋值,以便使用 _WorkforceRepository.UpdateAsync(workforce1) 方法进行更新操作。
  3. 插入操作: 当 workforce1.Id 为 0 时,表示需要插入新数据。将 workforce1.Time 属性设置为 input.Time.Date,然后使用 _WorkforceRepository.InsertAsync(workforce1) 方法进行插入操作。
  4. 数据持久化: _WorkforceRepository 用于数据持久化操作,它提供了 UpdateAsyncInsertAsync 方法。

总结

本文介绍了使用 C# 开发的员工排班系统中,如何保存排班信息并处理更新操作。文中展示了代码示例,并解释了如何使用 ObjectMapper 进行对象映射,以及如何使用 _WorkforceRepository 进行数据持久化。

希望本文对您有所帮助。

C# 员工排班系统: 保存排班信息并处理更新操作

原文地址: https://www.cveoy.top/t/topic/pdnU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录