heshaofeng
2025-12-29 266e4bf654c55ce2f7e9271048e4625f1b8b49f6
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_BasicService/BasicService.cs
@@ -1,9 +1,21 @@
using WIDESEA_IBasicService;
using SqlSugar;
using WIDESEA_Core.Attributes;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.CodeConfigEnum;
using WIDESEA_Core.DB;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Seed;
using WIDESEA_DTO.Base;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Basic;
namespace WIDESEA_BasicService
{
    public class BasicService : IBasicService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        public IPalletCodeInfoService PalletCodeInfoService { get; }
        public ILocationInfoService LocationInfoService { get; }
@@ -14,13 +26,256 @@
        public IMaterielCodeInfoService MaterielCodeInfoService { get; }
        public BasicService(ILocationInfoService locationInfoService, IMaterielInfoService materielInfoService, IWarehouseService warehouseService, IPalletCodeInfoService palletCodeInfoService, IMaterielCodeInfoService materielCodeInfoService)
        public IMaterialUnitService MaterialUnitService { get; }
        public BasicService(IUnitOfWorkManage unitOfWorkManage, ILocationInfoService locationInfoService, IMaterielInfoService materielInfoService, IWarehouseService warehouseService, IPalletCodeInfoService palletCodeInfoService, IMaterielCodeInfoService materielCodeInfoService, IMaterialUnitService materialUnitService)
        {
            _unitOfWorkManage = unitOfWorkManage;
            LocationInfoService = locationInfoService;
            MaterielInfoService = materielInfoService;
            WarehouseService = warehouseService;
            PalletCodeInfoService = palletCodeInfoService;
            MaterielCodeInfoService = materielCodeInfoService;
            MaterialUnitService = materialUnitService;
        }
        #region
        static object lockObj = new object();
        public string CreateCodeByRule(string ruleCode)
        {
            lock (lockObj)
            {
                string code = string.Empty;
                DateTime dataTime = DateTime.Now;
                DateTime now = DateTime.Now;
                try
                {
                    Dt_CodeRuleConfig ruleConfig = _unitOfWorkManage.Db.Queryable<Dt_CodeRuleConfig>().Where(x => x.RuleCode == ruleCode).First();
                    if (ruleConfig.ModifyDate != null)
                    {
                        dataTime = ruleConfig.ModifyDate.Value;
                    }
                    else
                    {
                        dataTime = ruleConfig.CreateDate;
                    }
                    if (now.Year == dataTime.Year && now.Month == dataTime.Month && now.Day == dataTime.Day)
                    {
                        now = dataTime;
                        ruleConfig.CurrentVal = Convert.ToInt32(ruleConfig.CurrentVal) + 1;
                    }
                    else
                    {
                        ruleConfig.CurrentVal = 1;
                    }
                    ruleConfig.ModifyDate = DateTime.Now;
                    code = ruleConfig.Format;
                    code = code.Replace($"[{CodeFormatTypeEnum.YYYY}]", now.Year.ToString().PadLeft(4, '0'));
                    code = code.Replace($"[{CodeFormatTypeEnum.MM}]", now.Month.ToString().PadLeft(2, '0'));
                    code = code.Replace($"[{CodeFormatTypeEnum.DD}]", now.Day.ToString().PadLeft(2, '0'));
                    code = code.Replace($"[{CodeFormatTypeEnum.ST}]", ruleConfig.StartStr.ToString());
                    code = code.Replace($"[{CodeFormatTypeEnum.NUM}]", ruleConfig.CurrentVal.ToString().PadLeft(ruleConfig.Length, '0'));
                    _unitOfWorkManage.Db.Updateable(ruleConfig).ExecuteCommand();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                return code;
            }
        }
        #endregion
        #region å•位转换
        /// <summary>
        /// å•位转换
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="fromUnit"></param>
        /// <param name="toUnit"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitQuantityConvert(string materialCode, string fromUnit, string toUnit, decimal quantity)
        {
            if (string.IsNullOrEmpty(fromUnit))
            {
                throw new Exception($"转换前单位不能为空");
            }
            if (string.IsNullOrEmpty(toUnit))
            {
                throw new Exception($"转换后单位不能为空");
            }
            decimal ratio = 1;
            if (fromUnit.Trim().ToLower() == toUnit.Trim().ToLower())
            {
                return new UnitConvertResultDTO(materialCode, fromUnit, toUnit, quantity, ratio);
            }
            Dt_MaterialUnit materialUnit = MaterialUnitService.Repository.QueryFirst(x => x.ItemNo == materialCode && x.FromUom == fromUnit && x.ToUom == toUnit);
            if (materialUnit != null)
            {
                ratio = materialUnit.Ratio;
                return new UnitConvertResultDTO(materialCode, fromUnit, toUnit, quantity, ratio);
            }
            else
            {
                materialUnit = MaterialUnitService.Repository.QueryFirst(x => x.ItemNo == materialCode && x.FromUom == toUnit && x.ToUom == fromUnit);
                if (materialUnit == null)
                {
                    throw new Exception($"未找到单位转换关系,物料编号:{materialCode},转换前单位:{fromUnit},转换后单位:{toUnit}");
                }
                ratio = materialUnit.Ratio;
                return new UnitConvertResultDTO(materialCode, fromUnit, toUnit, quantity, ratio);
            }
        }
        /// <summary>
        /// å•位转换,领料转采购
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitUsageToPurchase(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
            return UnitQuantityConvert(materialCode, materielInfo.usageUOM, materielInfo.purchaseUOM, quantity);
        }
        /// <summary>
        /// å•位转换,领料转库存
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitUsageToInventory(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
            return UnitQuantityConvert(materialCode, materielInfo.usageUOM, materielInfo.inventoryUOM, quantity);
        }
        /// <summary>
        /// å•位转换,采购转库存
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitPurchaseToInventory(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
            return UnitQuantityConvert(materialCode, materielInfo.purchaseUOM, materielInfo.inventoryUOM, quantity);
        }
        /// <summary>
        /// å•位转换,采购转领料
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitPurchaseToUsage(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
            return UnitQuantityConvert(materialCode, materielInfo.purchaseUOM, materielInfo.usageUOM, quantity);
        }
        /// <summary>
        /// å•位转换,库存转领料
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitInventoryToUsage(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
            return UnitQuantityConvert(materialCode, materielInfo.inventoryUOM, materielInfo.usageUOM, quantity);
        }
        /// <summary>
        /// å•位转换,库存转采购
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitInventoryToPurchase(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
            return UnitQuantityConvert(materialCode, materielInfo.inventoryUOM, materielInfo.purchaseUOM, quantity);
        }
        #endregion
    }
}