111
啊鑫
2025-09-04 8d771316d68f1523bcdf30c97d3a55107cb5ae56
MES.Service/service/BasicData/MesStaffManager.cs
@@ -346,8 +346,6 @@
        if (unit == null) throw new ArgumentNullException(nameof(unit));
        var entity = GetMesStaff(unit);
        unit.Id = entity.Id.ToString();
        var sysUser = GetUser(unit);
        //var mesStaffPositionLink = GetMesStaffPositionLink(unit);
        List<MesStaffPositionLink> mesStaffPositionLink = null;
@@ -359,19 +357,54 @@
                case "1": return UpdateStaffStatus(db, entity.Id, "B") ? 1 : 0;
                case "2":
                case "4":
                    return InsertOrUpdateStaff(db,
                        new List<SysUser> { sysUser },
                        new List<MesStaff> { entity }, mesStaffPositionLink)
                        ? 1
                        : 0;
                    // return InsertOrUpdateStaff(db,
                    //     new List<SysUser> { sysUser },
                    //     new List<MesStaff> { entity }, mesStaffPositionLink)
                    //     ? 1
                    //     : 0;
                    return InsertUser(db, entity) ? 1 : 0;
                case "3":
                    return DeleteStaff(db, new List<SysUser> { sysUser },
                        new List<MesStaff> { entity })
                        ? 1
                        : 0;
                    // return DeleteStaff(db, new List<SysUser> { sysUser },
                    //     new List<MesStaff> { entity })
                    //     ? 1
                    //     : 0;
                    return DeleteStaff(db, entity) ? 1 : 0;
                default: throw new ArgumentException($"不支持的类型: {unit.Type}");
            }
        }) > 0;
    }
    private bool InsertUser(SqlSugarScope db, MesStaff entity)
    {
        db.Deleteable<MesStaff>()
            .Where(s => s.Id == entity.Id)
            .ExecuteCommand();
        db.Deleteable<SysUser>()
            .Where(s => s.StaffId == entity.Id.ToString())
            .ExecuteCommand();
        var sysUser = GetUser(entity);
        if (entity.Id == null)
        {
            var id = db.Insertable<MesStaff>(entity).ExecuteReturnIdentity();
            sysUser.StaffId = id.ToString();
        }
        return db.Insertable(sysUser).ExecuteCommand() > 0;
    }
    private bool DeleteStaff(SqlSugarScope db, MesStaff entity)
    {
        var executeCommand = db.Deleteable<MesStaff>()
            .Where(s => s.Id == entity.Id)
            .ExecuteCommand();
        db.Deleteable<SysUser>()
            .Where(s => s.StaffId == entity.Id.ToString())
            .ExecuteCommand();
        return executeCommand > 0;
    }
    // 保存多个员工记录
@@ -545,7 +578,7 @@
    }
    // 更新员工状态
    private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId,
    private bool UpdateStaffStatus(SqlSugarScope db, decimal? staffId,
        string status)
    {
        var result = db.Updateable<MesStaff>()
@@ -578,9 +611,9 @@
        {
            var entity = new MesStaff
            {
                Id = string.IsNullOrEmpty(staff.Id)
                    ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                    : Convert.ToDecimal(staff.Id),
                // Id = string.IsNullOrEmpty(staff.Id)
                //     ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                //     : Convert.ToDecimal(staff.Id),
                StaffNo = staff.FStaffNumber,
                StaffName = staff.FName,
                DepartmentName = staff.FPostDept,
@@ -626,30 +659,26 @@
    }
    // 将 ErpStaff 对象转换为 SysUser 对象
    private SysUser GetUser(ErpStaff staff)
    private SysUser GetUser(MesStaff entity)
    {
        if (staff == null) throw new ArgumentNullException(nameof(staff));
        if (entity == null) throw new ArgumentNullException(nameof(entity));
        try
        {
            return new SysUser
            {
                StaffId = staff.Id, // 确保Sid与员工ID一致
                StaffId = entity.Id.ToString(), // 确保Sid与员工ID一致
                IsStatus = true,
                Account = staff.FStaffNumber,
                UserName = staff.FName,
                Account = entity.StaffNo,
                UserName = entity.StaffName,
                Password = "E1ADC3949BA59ABBE56E057F2F883E", // 初始密码
                DepartNo = staff.FPostDept,
                Type = staff.Type,
                DepartNo = entity.DepartmentName,
                CreateTime = DateTime.Now
            };
        }
        catch (Exception ex)
        {
            Console.WriteLine($"转换ErpStaff到SysUser失败: {ex.Message}");
            Console.WriteLine(
                $"输入数据: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
            throw;
            throw new NotImplementedException(ex.Message);
        }
    }