using MES.Service.DB; using MES.Service.Dto.webApi; using MES.Service.Modes; using SqlSugar; using System.ComponentModel.DataAnnotations; namespace MES.Service.service.BasicData; public class MesFactoryDateManager : Repository { public bool Save(ErpFactoryDate factoryDate) { var entity = ConvertToEntity(factoryDate); return UseTransaction(db => { switch (factoryDate.Type) { case "3": // 删除 return DeleteFactoryDate(db, entity.ErpId) ? 1 : 0; case "2": case "4": // 新增或同步 return InsertOrUpdate(db, entity) ? 1 : 0; default: throw new ArgumentNullException($"type没有{factoryDate.Type}这个类型的参数"); } }) > 0; } public bool SaveList(List factoryDates) { var list = factoryDates.Select(ConvertToEntity).ToList(); var groupBy = list.GroupBy(s => s.Type) .ToDictionary(g => g.Key, g => g.ToList()); return UseTransaction(db => { foreach (var group in groupBy) { switch (group.Key) { case "3": if (!DeleteFactoryDateBatch(db, group.Value)) throw new NotImplementedException("删除失败"); break; case "2": case "4": if (!InsertOrUpdateBatch(db, group.Value)) throw new NotImplementedException("同步失败"); break; default: throw new ArgumentNullException($"type没有{group.Key}这个类型的参数"); } } return 1; }) > 0; } private MesFactoryDate ConvertToEntity(ErpFactoryDate dto) { string DateType = ""; switch (dto.FDateStyle) { case "1": DateType = "工作日"; break; case "2": DateType = "休息日"; break; case "3": DateType = "法定节假日"; break; case "4": DateType = "空"; break; } return new MesFactoryDate { Guid = Guid.NewGuid(), Type = dto.Type, FDocumentStatus = dto.FDocumentStatus, FForbidStatus = dto.FForbidStatus, ErpId = dto.ErpId, Seq = dto.FSeq, OrgId = dto.FUseOrgId, FactoryDate = dto.FDay, DateType = DateType, IsWork = dto.FIsWorkTime, InterId = dto.FInterId }; } private bool DeleteFactoryDate(SqlSugarScope db, string? erpId) { var result = db.Deleteable() .Where(s => s.ErpId == erpId).ExecuteCommand(); if (result > 0) return true; throw new NotImplementedException("删除失败"); } private bool DeleteFactoryDateBatch(SqlSugarScope db, List list) { var erpIds = list.Select(it => it.ErpId).ToArray(); var result = db.Deleteable() .Where(s => erpIds.Contains(s.ErpId)).ExecuteCommand(); if (result > 0) return true; throw new NotImplementedException("删除失败"); } private bool InsertOrUpdate(SqlSugarScope db, MesFactoryDate entity) { db.Deleteable().Where(s => s.ErpId == entity.ErpId) .ExecuteCommand(); var insert = db.Insertable(entity).ExecuteCommand(); return insert > 0; } private bool InsertOrUpdateBatch(SqlSugarScope db, List list) { return list.All(entity => InsertOrUpdate(db, entity)); } }