using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using SqlSugar; namespace MES.Service.service.BasicData; public class EmployeeInfoManager : Repository { public bool Save(ErpEmployeeInfo unit) { var entity = GetMesItems(unit); // 将 ErpStaff 转换为 MesStaff return UseTransaction(db => { switch (unit.Type) { case "2": case "4": if (InsertOrUpdateEmp(db, entity.employeeInfo, entity.employeeItems)) // 插入或更新员工 return 1; break; case "3": if (DeleteEmp(db, entity.employeeInfo, entity.employeeItems)) // 删除员工 return 1; break; default: throw new ArgumentNullException( $"type没有{unit.Type}这个类型的参数"); } throw new NotImplementedException("操作失败"); }) > 0; } public bool SaveList(List departments) { var result = departments.Select(Save).ToList(); return result.All(b => b); } private bool InsertOrUpdateEmp(SqlSugarScope db, EmployeeInfo entityEmployeeInfo, List entityEmployeeItems) { db.Deleteable().Where( s => s.Id == entityEmployeeInfo.Id).ExecuteCommand(); db.Deleteable() .Where(a => a.Eid == entityEmployeeInfo.Id).ExecuteCommand(); var executeCommand = db.Insertable(entityEmployeeInfo).ExecuteCommand(); var command = db.Insertable(entityEmployeeItems).PageSize(1) .IgnoreColumnsNull().ExecuteCommand(); return executeCommand > 0 && command > 0; } private bool DeleteEmp(SqlSugarScope db, EmployeeInfo entityEmployeeInfo, List entityEmployeeItems) { var command = db.Deleteable().Where( s => s.Id == entityEmployeeInfo.Id).ExecuteCommand(); var executeCommand = db.Deleteable() .Where(a => a.Eid == entityEmployeeInfo.Id).ExecuteCommand(); return executeCommand > 0 && command > 0; } private (EmployeeInfo employeeInfo, List employeeItems) GetMesItems( ErpEmployeeInfo item) { var items = new EmployeeInfo { Id = Convert.ToDecimal(item.Id), EmployeeNo = item.fNumber, EmployeeName = item.fName, DepartmentCode = item.fPostDeptid, DocumentStatus = item.FDocumentStatus, IsDisabled = item.FForbidStatus, Organization = item.FUseOrgId }; var employeeItems = new List(); foreach (var erpEmployeeInfoDto in item.OperatorTypes) { var employee = new EmployeeItem { Eid = items.Id, EType = erpEmployeeInfoDto.OperatorType, CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; employeeItems.Add(employee); } return (items, employeeItems); } }