| | |
| | | public bool Save(ErpStaff unit) |
| | | { |
| | | var entity = GetMesStaff(unit); // 将 ErpStaff 转换为 MesStaff |
| | | var sysUser = GetUser(unit); // 获取 SysUser 实例 |
| | | |
| | | return UseTransaction(db => |
| | | { |
| | |
| | | break; |
| | | case "2": |
| | | case "4": |
| | | if (InsertOrUpdateStaff(db, sysUser, entity)) // 插入或更新员工 |
| | | if (InsertOrUpdateStaff(db, entity)) // 插入或更新员工 |
| | | return 1; |
| | | break; |
| | | case "3": |
| | | if (DeleteStaff(db, sysUser, entity.Id)) // 删除员工 |
| | | if (DeleteStaff(db, entity.Id)) // 删除员工 |
| | | return 1; |
| | | break; |
| | | default: |
| | |
| | | } |
| | | |
| | | // 更新员工状态的方法 |
| | | private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId, |
| | | string status) |
| | | private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId,string status) |
| | | { |
| | | var result = db.Updateable<MesStaff>() |
| | | .SetColumns(s => s.FforbidStatus == status) |
| | | .Where(s => s.Id == staffId).ExecuteCommand(); |
| | | var result = db.Updateable<MesStaff>().SetColumns(s => s.FforbidStatus == status).Where(s => s.Id == staffId).ExecuteCommand(); |
| | | |
| | | if (result > 0) |
| | | { |
| | | return true; |
| | | |
| | | } |
| | | throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败"); |
| | | } |
| | | |
| | | // 插入或更新员工的方法 |
| | | private bool InsertOrUpdateStaff(SqlSugarScope db, SysUser sysUser, |
| | | MesStaff entity) |
| | | private bool InsertOrUpdateStaff(SqlSugarScope db,MesStaff entity) |
| | | { |
| | | var exists = db.Queryable<SysUser>().Any(u => u.Sid == sysUser.Sid); |
| | | var exists = db.Queryable<MesStaff>().Any(u => u.StaffNo == entity.StaffNo); |
| | | if (exists) |
| | | { |
| | | var updateUser = db.Updateable<SysUser>() |
| | | .SetColumns(s => s.Fcode == sysUser.Fcode) |
| | | .SetColumns(s => s.Fname == sysUser.Fname) |
| | | .Where(s => s.Sid == sysUser.Sid) |
| | | .ExecuteCommand(); |
| | | if (updateUser > 0) |
| | | var updateStaff = db.Updateable(entity).ExecuteCommand(); |
| | | if (updateStaff > 0) |
| | | { |
| | | var updateStaff = db.Updateable(entity).ExecuteCommand(); |
| | | if (updateStaff > 0) |
| | | return true; |
| | | return true; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | var insertUser = db.Insertable(sysUser).ExecuteCommand(); |
| | | if (insertUser > 0) |
| | | var insertStaff = db.Insertable(entity).ExecuteCommand(); |
| | | if (insertStaff > 0) |
| | | { |
| | | var insertStaff = db.Insertable(entity).ExecuteCommand(); |
| | | if (insertStaff > 0) |
| | | return true; |
| | | return true; |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // 删除员工的方法 |
| | | private bool DeleteStaff(SqlSugarScope db, SysUser sysUser, decimal staffId) |
| | | private bool DeleteStaff(SqlSugarScope db,decimal staffId) |
| | | { |
| | | var deleteUser = db.Deleteable<SysUser>() |
| | | .Where(s => s.Sid == sysUser.Sid).ExecuteCommand(); |
| | | if (deleteUser > 0) |
| | | var deleteStaff =db.Deleteable<MesStaff>().In(staffId).ExecuteCommand(); |
| | | if (deleteStaff > 0) |
| | | { |
| | | var deleteStaff = |
| | | db.Deleteable<MesStaff>().In(staffId).ExecuteCommand(); |
| | | if (deleteStaff > 0) |
| | | return true; |
| | | return true; |
| | | } |
| | | |
| | | throw new NotImplementedException("反审核失败"); |
| | |
| | | Id = Convert.ToDecimal(staff.Id), |
| | | StaffNo = staff.FStaffNumber, |
| | | StaffName = staff.FName, |
| | | DepartmentName = staff.FPostDept, |
| | | PositionName = staff.FPost, |
| | | DepartmentNo = staff.FPostDept, |
| | | PositionCode = staff.FPost, |
| | | PhoneNumber = staff.FMobile, |
| | | CreateOrg = Convert.ToDecimal(staff.FCreateOrgId), |
| | | UseOrg = Convert.ToDecimal(staff.FUseOrgId), |
| | | CreateDate = DateTime.Now, |
| | | Remark = staff.FDescription, |
| | | FforbidStatus = staff.FForbidStatus |
| | | }; |
| | | |
| | | if (staff.FStaffStartDate != null) |
| | | entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate, |
| | | "yyyy-MM-dd HH:mm:ss", null); |
| | | { |
| | | entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate, "yyyy-MM-dd HH:mm:ss", null); |
| | | } |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | // 将 ErpStaff 对象转换为 SysUser 对象的方法 |
| | | private SysUser GetUser(ErpStaff staff) |
| | | { |
| | | return new SysUser |
| | | { |
| | | Sid = Convert.ToDecimal(staff.Id), |
| | | Fcode = staff.FStaffNumber, |
| | | Fname = staff.FName, |
| | | Fpassword = "8+()./,", // 初始密码 |
| | | Fsystem = "上格", |
| | | Lever = 0, |
| | | Fcompany = "1000", |
| | | Ffactory = "1000", |
| | | DelFlag = 0, |
| | | Ismanager = 0, |
| | | Isacq = 0, |
| | | ExpField9 = "1020", |
| | | QmUser = 0, |
| | | Status = 0, |
| | | Isdeparture = 0, |
| | | CheckFlag = 0, |
| | | ExpFiel12 = 0 |
| | | }; |
| | | } |
| | | |
| | | // SaveList 方法用于保存多个员工记录,根据类型批量执行不同的操作 |
| | | public bool SaveList(List<ErpStaff> departments) |
| | | { |
| | | var list = new List<MesStaff>(); |
| | | var userList = new List<SysUser>(); |
| | | |
| | | departments.ForEach(s => |
| | | { |
| | |
| | | entity.Type = s.Type; |
| | | list.Add(entity); |
| | | |
| | | var sysUser = GetUser(s); |
| | | sysUser.Type = s.Type; |
| | | userList.Add(sysUser); |
| | | }); |
| | | |
| | | var groupBy = list.GroupBy(s => s.Type) |
| | | .ToDictionary(g => g.Key, g => g.ToList()); |
| | | var groupBy = list.GroupBy(s => s.Type).ToDictionary(g => g.Key, g => g.ToList()); |
| | | return UseTransaction(db => |
| | | { |
| | | foreach (var staffGroup in groupBy) |
| | | switch (staffGroup.Key) |
| | | { |
| | | case "0": |
| | | if (!UpdateStaffStatusBatch(db, staffGroup.Value, |
| | | "A")) // 批量启用员工 |
| | | if (!UpdateStaffStatusBatch(db, staffGroup.Value,"A")) // 批量启用员工 |
| | | throw new NotImplementedException("启用失败"); |
| | | break; |
| | | case "1": |
| | | if (!UpdateStaffStatusBatch(db, staffGroup.Value, |
| | | "B")) // 批量禁用员工 |
| | | if (!UpdateStaffStatusBatch(db, staffGroup.Value,"B")) // 批量禁用员工 |
| | | throw new NotImplementedException("禁用失败"); |
| | | break; |
| | | case "2": |
| | | if (!InsertStaffBatch(db, userList, |
| | | staffGroup.Value)) // 批量插入员工 |
| | | if (!InsertStaffBatch(db,staffGroup.Value)) // 批量插入员工 |
| | | throw new NotImplementedException("插入失败"); |
| | | break; |
| | | case "3": |
| | | if (!DeleteStaffBatch(db, userList, |
| | | staffGroup.Value)) // 批量删除员工 |
| | | if (!DeleteStaffBatch(db,staffGroup.Value)) // 批量删除员工 |
| | | throw new NotImplementedException("删除失败"); |
| | | break; |
| | | case "4": |
| | | if (!InsertOrUpdateBatch(db, userList, |
| | | staffGroup.Value)) // 批量插入或更新员工 |
| | | if (!InsertOrUpdateBatch(db,staffGroup.Value)) // 批量插入或更新员工 |
| | | throw new NotImplementedException("同步失败"); |
| | | break; |
| | | default: |
| | | throw new ArgumentNullException( |
| | | $"type没有{staffGroup.Key}这个类型的参数"); |
| | | throw new ArgumentNullException($"type没有{staffGroup.Key}这个类型的参数"); |
| | | } |
| | | |
| | | return 1; |
| | |
| | | } |
| | | |
| | | // 批量更新员工状态的方法 |
| | | private bool UpdateStaffStatusBatch(SqlSugarScope db, |
| | | List<MesStaff> staffList, string status) |
| | | private bool UpdateStaffStatusBatch(SqlSugarScope db,List<MesStaff> staffList, string status) |
| | | { |
| | | var ids = staffList.Select(it => it.Id).ToArray(); |
| | | var result = db.Updateable<MesStaff>() |
| | | .SetColumns(s => s.FforbidStatus == status) |
| | | .Where(s => ids.Contains(s.Id)).ExecuteCommand(); |
| | | var result = db.Updateable<MesStaff>().SetColumns(s => s.FforbidStatus == status).Where(s => ids.Contains(s.Id)).ExecuteCommand(); |
| | | |
| | | if (result > 0) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败"); |
| | | } |
| | | |
| | | // 批量插入员工的方法 |
| | | private bool InsertStaffBatch(SqlSugarScope db, List<SysUser> userList, |
| | | List<MesStaff> staffList) |
| | | private bool InsertStaffBatch(SqlSugarScope db,List<MesStaff> staffList) |
| | | { |
| | | var userInsert = userList.FindAll(s => s.Type == "INSERT"); |
| | | var userInsert = staffList.FindAll(s => s.Type == "INSERT"); |
| | | var executeCommand = db.Insertable(userInsert).ExecuteCommand(); |
| | | if (executeCommand > 0) |
| | | if (db.Insertable(staffList).ExecuteCommand() > 0) |
| | |
| | | } |
| | | |
| | | // 批量删除员工的方法 |
| | | private bool DeleteStaffBatch(SqlSugarScope db, List<SysUser> userList, |
| | | List<MesStaff> staffList) |
| | | private bool DeleteStaffBatch(SqlSugarScope db, List<MesStaff> staffList) |
| | | { |
| | | var sid = userList.FindAll(s => s.Type == "DELETE").Select(s => s.Sid) |
| | | .ToArray(); |
| | | var result = db.Deleteable<SysUser>() |
| | | .Where(s => sid.Contains(s.Sid)).ExecuteCommand(); |
| | | |
| | | if (result > 0) |
| | | var ids = staffList.Select(it => it.Id).ToArray(); |
| | | if (db.Deleteable<MesStaff>().In(ids).ExecuteCommand() > 0) |
| | | { |
| | | var ids = staffList.Select(it => it.Id).ToArray(); |
| | | if (db.Deleteable<MesStaff>().In(ids).ExecuteCommand() > 0) |
| | | return true; |
| | | return true; |
| | | } |
| | | |
| | | throw new ArgumentNullException("反审核失败"); |
| | | } |
| | | |
| | | // 批量插入或更新员工的方法 |
| | | private bool InsertOrUpdateBatch(SqlSugarScope db, List<SysUser> userList, |
| | | List<MesStaff> staffList) |
| | | private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesStaff> staffList) |
| | | { |
| | | foreach (var sysUser in userList) |
| | | foreach (var staff in staffList) |
| | | { |
| | | var entity = staffList.First(s => s.Id == sysUser.Sid); |
| | | if (!InsertOrUpdateStaff(db, sysUser, entity)) return false; |
| | | var entity = staffList.First(s => s.StaffNo == staff.StaffNo); |
| | | if (!InsertOrUpdateStaff(db, entity)) |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | return true; |