wangxinhui
2025-10-17 ce40df5daffae0d17b4e9fa7cb6d677afaa4d66f
ÏîÄ¿´úÂë/WMS/WMSServices/WIDESEA_BasicService/SupplierInfoService.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,185 @@
using AutoMapper;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_BasicRepository;
using WIDESEA_Common.MaterielEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_DTO.Basic;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
namespace WIDESEA_BasicService
{
    public class SupplierInfoService : ServiceBase<Dt_SupplierInfo, ISupplierInfoRepository>, ISupplierInfoService
    {
        public ISupplierInfoRepository Repository => BaseDal;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IMapper _mapper;
        public SupplierInfoService(ISupplierInfoRepository BaseDal,IUnitOfWorkManage unitOfWorkManage,IMapper mapper) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _mapper = mapper;
        }
        /// <summary>
        /// æ·»åŠ è€åŽ‚ä¾›åº”å•†ä¿¡æ¯
        /// </summary>
        /// <param name="bSTSupplierDTOs"></param>
        /// <returns></returns>
        public WebResponseContent AddSupplier(List<BSTSupplierDTO> bSTSupplierDTOs)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_SupplierInfo> OldsupplierInfos = BaseDal.QueryData(x => x.InvOrgId == MaterielInvOrgEnum.老厂.ObjToInt());
                //新增
                List<BSTSupplierDTO> bSTSupplierDTOsAdd = bSTSupplierDTOs.Where(x => x.Way == 1).ToList();
                //修改
                List<BSTSupplierDTO> bSTSupplierDTOsUpdate = bSTSupplierDTOs.Where(x => x.Way == 2).ToList();
                //删除
                List<BSTSupplierDTO> bSTSupplierDTOsDTODel = bSTSupplierDTOs.Where(x => x.Way == 3).ToList();
                List<Dt_SupplierInfo> supplierInfosAdd = new List<Dt_SupplierInfo>();
                List<Dt_SupplierInfo> supplierInfosUpdate = new List<Dt_SupplierInfo>();
                //新增
                if (bSTSupplierDTOsAdd.Count > 0)
                {
                    //判断重复插入
                    Dt_SupplierInfo? ExistSupplier = OldsupplierInfos.FirstOrDefault(x => bSTSupplierDTOsAdd.Select(x => x.VendId).Contains(x.SupplierSourceId));
                    if (ExistSupplier != null)
                    {
                        return content.Error($"供应商{nameof(BSTSupplierDTO.VendId)}:{ExistSupplier.SupplierSourceId}已存在");
                    }
                    foreach (var item in bSTSupplierDTOsAdd)
                    {
                        if (bSTSupplierDTOsUpdate.FirstOrDefault(x => x.VendId == item.VendId) != null)
                        {
                            continue;
                        }
                        Dt_SupplierInfo supplierInfo = _mapper.Map<Dt_SupplierInfo>(item);
                        supplierInfosAdd.Add(supplierInfo);
                    }
                }
                //更新
                if (bSTSupplierDTOsUpdate.Count > 0)
                {
                    //获取已存在的物料
                    List<Dt_SupplierInfo>? ExistbSTSuppliersUpdate = OldsupplierInfos.Where(x => bSTSupplierDTOsUpdate.Select(x => x.VendId).Contains(x.SupplierSourceId)).ToList();
                    foreach (var item in bSTSupplierDTOsUpdate)
                    {
                        Dt_SupplierInfo? supplierInfoUpdate = ExistbSTSuppliersUpdate.FirstOrDefault(x => x.SupplierSourceId == item.VendId);
                        //如果物料已存在则进行更改
                        if (supplierInfoUpdate != null)
                        {
                            supplierInfoUpdate.SupplierSourceId = item.VendId;
                            supplierInfoUpdate.SupplierCode = item.VendNo;
                            supplierInfoUpdate.SupplierShortName = item.ShortName;
                            supplierInfoUpdate.SupplierName = item.ShortName;
                            supplierInfoUpdate.Contacts = item.BusinessContact;
                            supplierInfoUpdate.ContactNumber = item.Tel;
                            supplierInfoUpdate.ContactAddress = item.Address;
                            supplierInfosUpdate.Add(supplierInfoUpdate);
                        }
                        else//如果不存在则进行新增
                        {
                            Dt_SupplierInfo supplierInfo = _mapper.Map<Dt_SupplierInfo>(item);
                            supplierInfosAdd.Add(supplierInfo);
                        }
                    }
                }
                //删除
                if (bSTSupplierDTOsDTODel.Count > 0)
                {
                }
                //更新数据
                _unitOfWorkManage.CommitTran();
                BaseDal.AddData(supplierInfosAdd);
                BaseDal.UpdateData(supplierInfosUpdate);
                _unitOfWorkManage.CommitTran();
                content.OK("接收成功");
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// æ·»åŠ äºŒæœŸä¾›åº”å•†ä¿¡æ¯
        /// </summary>
        /// <param name="bSTSupplierDTOs"></param>
        /// <returns></returns>
        public WebResponseContent ReceiveSupplier(ERPSupplierDTO eRPSupplierDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (eRPSupplierDTO == null)
                {
                    return content.Error("供应商信息不能传入为空");
                }
                List<Dt_SupplierInfo> OldsupplierInfos = BaseDal.QueryData(x => x.InvOrgId == MaterielInvOrgEnum.新厂.ObjToInt());
                if (eRPSupplierDTO.OperateType == 1)
                {
                    //判断重复插入
                    Dt_SupplierInfo? supplierInfoOld = OldsupplierInfos.FirstOrDefault(x => x.SupplierCode == eRPSupplierDTO.SupplierCode);
                    if (supplierInfoOld != null)
                    {
                        return content.Error($"供应商{supplierInfoOld.SupplierCode}已存在");
                    }
                    Dt_SupplierInfo supplierInfo = _mapper.Map<Dt_SupplierInfo>(eRPSupplierDTO);
                    //新增
                    BaseDal.AddData(supplierInfo);
                }
                else if (eRPSupplierDTO.OperateType == 2)
                {
                    //判断是否存在
                    Dt_SupplierInfo? ExistsupplierInfo = OldsupplierInfos.FirstOrDefault(x => x.SupplierCode == eRPSupplierDTO.SupplierCode);
                    if (ExistsupplierInfo == null)
                    {
                        return content.Error($"更新供应商{eRPSupplierDTO.SupplierCode}不存在");
                    }
                    Dt_SupplierInfo supplierInfo = _mapper.Map<Dt_SupplierInfo>(eRPSupplierDTO);
                    supplierInfo.Id = ExistsupplierInfo.Id;
                    //更新
                    _unitOfWorkManage.BeginTran();
                    BaseDal.UpdateData(supplierInfo);
                    _unitOfWorkManage.CommitTran();
                }
                else if (eRPSupplierDTO.OperateType == 3)
                {
                    //判断是否存在
                    Dt_SupplierInfo? ExistsupplierInfo = OldsupplierInfos.FirstOrDefault(x => x.SupplierCode == eRPSupplierDTO.SupplierCode);
                    if (ExistsupplierInfo == null)
                    {
                        return content.Error($"更新供应商{eRPSupplierDTO.SupplierCode}不存在");
                    }
                    _unitOfWorkManage.BeginTran();
                    BaseDal.DeleteData(ExistsupplierInfo);
                    _unitOfWorkManage.CommitTran();
                }
                else
                {
                    return content.Error("未找到操作类型");
                }
                //更新数据
                return content.OK("接收成功");
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}