csharp MeasurementUnitConversion - dRetail - BEST&Simple Logic).cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp MeasurementUnitConversion - dRetail - BEST&Simple Logic).cs相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace dRetail.Common
{
  public static class MeasurementUnitConversion
  {
    public static int GetDefaultMeasurementUnit(int measurementUnitID) {
        var _db = Global.dbContext;
        var measurementUnitTypeID = _db.Set_MeasurementUnit.Where(x => x.ID == measurementUnitID && x.IsActive == true).FirstOrDefault().MeasurementUnitTypeID;
        var defaultMeasurementUnitID = _db.Set_MeasurementUnit.Where(x => x.MeasurementUnitTypeID == measurementUnitTypeID && x.IsDefault == true && x.IsActive == true).FirstOrDefault().ID;
        return defaultMeasurementUnitID;
    }
    
    public static decimal Convert(int measurementUnitID, decimal qty)
    {
        try
        {
            if (qty != 0)
            {
                var _db = Global.dbContext;
               var defaultMeasurementUnitID= GetDefaultMeasurementUnit(measurementUnitID);
               if (defaultMeasurementUnitID == measurementUnitID)
                {
                    return qty;
                }
                else
                {                       
                    var unitConversion = _db.Set_UnitConversion.Where(x => x.FromUnitID == measurementUnitID && x.ToUnitID == defaultMeasurementUnitID).FirstOrDefault();
                    if (unitConversion != null) return qty * unitConversion.Value;
                  
                    unitConversion = _db.Set_UnitConversion.Where(x => x.FromUnitID == defaultMeasurementUnitID && x.ToUnitID == measurementUnitID).FirstOrDefault();
                    if (unitConversion != null) return qty/unitConversion.Value;
                    
                    throw new Exception();
                }
            }
            return 0;
        }
        catch(Exception e)
        {
            throw e;
        }
    }
  }
}

以上是关于csharp MeasurementUnitConversion - dRetail - BEST&Simple Logic).cs的主要内容,如果未能解决你的问题,请参考以下文章

csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp渲染,RenderPSDImageAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndex.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndexAsynchronously.cs