啊鑫
2025-06-05 44a7c47a724c154c54e256727c75e79d31fd42e2
StandardPda/MES.Service/service/BasicData/EmployeeInfoManager.cs
@@ -1,110 +1,107 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using MES.Service.DB;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using MES.Service.util;
using SqlSugar;
namespace MES.Service.service
namespace MES.Service.service.BasicData;
public class EmployeeInfoManager : Repository<EmployeeInfo>
{
    public class EmployeeInfoManager : Repository<EmployeeInfo>
    public bool Save(ErpEmployeeInfo unit)
    {
        public bool Save(ErpEmployeeInfo unit)
        {
            var entity = GetMesItems(unit); // 将 ErpStaff 转换为 MesStaff
        var entity = GetMesItems(unit); // 将 ErpStaff 转换为 MesStaff
            return UseTransaction(db =>
        return UseTransaction(db =>
        {
            switch (unit.Type)
            {
                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<ErpEmployeeInfo> departments)
        {
            var result = departments.Select(Save).ToList();
            return result.All(b => b);
        }
        private bool InsertOrUpdateEmp(SqlSugarScope db,
            EmployeeInfo entityEmployeeInfo,
            List<EmployeeItem> entityEmployeeItems)
        {
            db.Deleteable<EmployeeInfo>().Where(
                s => s.Id == entityEmployeeInfo.Id).ExecuteCommand();
            db.Deleteable<EmployeeItem>()
                .Where(a => a.Eid == entityEmployeeInfo.Id).ExecuteCommand();
            var executeCommand =
                db.Insertable(entityEmployeeInfo).ExecuteCommand();
            var command = db.Insertable(entityEmployeeItems).ExecuteCommand();
            return executeCommand > 0 && command > 0;
        }
        private bool DeleteEmp(SqlSugarScope db,
            EmployeeInfo entityEmployeeInfo,
            List<EmployeeItem> entityEmployeeItems)
        {
            var command = db.Deleteable<EmployeeInfo>().Where(
                s => s.Id == entityEmployeeInfo.Id).ExecuteCommand();
            var executeCommand = db.Deleteable<EmployeeItem>()
                .Where(a => a.Eid == entityEmployeeInfo.Id).ExecuteCommand();
            return executeCommand > 0 && command > 0;
        }
        private (EmployeeInfo employeeInfo, List<EmployeeItem> 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<EmployeeItem>();
            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);
                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}这个类型的参数");
            }
            return (items, employeeItems);
            throw new NotImplementedException("操作失败");
        }) > 0;
    }
    public bool SaveList(List<ErpEmployeeInfo> departments)
    {
        var result = departments.Select(Save).ToList();
        return result.All(b => b);
    }
    private bool InsertOrUpdateEmp(SqlSugarScope db,
        EmployeeInfo entityEmployeeInfo,
        List<EmployeeItem> entityEmployeeItems)
    {
        db.Deleteable<EmployeeInfo>().Where(s => s.Id == entityEmployeeInfo.Id)
            .ExecuteCommand();
        db.Deleteable<EmployeeItem>()
            .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<EmployeeItem> entityEmployeeItems)
    {
        var command = db.Deleteable<EmployeeInfo>()
            .Where(s => s.Id == entityEmployeeInfo.Id).ExecuteCommand();
        var executeCommand = db.Deleteable<EmployeeItem>()
            .Where(a => a.Eid == entityEmployeeInfo.Id).ExecuteCommand();
        return executeCommand > 0 && command > 0;
    }
    private (EmployeeInfo employeeInfo, List<EmployeeItem> 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<EmployeeItem>();
        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);
    }
}