using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WMS.Core.ImportExecl
{
public class UnitByConvUnit
{
///
/// 转换单位
///
/// 料号
/// 转换单位
/// 转换值
///
public static JsonConvUnitEntity GetConvUnitByBase(int busTypes, string MatNo, string ConvUnit, decimal ConvValue)
{
SqlSugarClient sugar = null;
JsonConvUnitEntity json = new JsonConvUnitEntity();
try
{
sugar = SysDbCore.GetDbCtx();
ConvUnitEntity entity = null;
if (string.IsNullOrEmpty(MatNo) || string.IsNullOrEmpty(ConvUnit))
throw new Exception("料号或者转换单位不能为空");
if (busTypes != 12)
{
if (ConvValue <= 0)
throw new Exception("参数值不能小于0");
}
var mat = sugar.Queryable().Where(v => v.F_NO == MatNo).First();
if (mat == null)
throw new Exception("料号不存在");
if (string.IsNullOrEmpty(mat.F_BASEUNITNO))
throw new Exception("物料基本单位未维护");
if (string.IsNullOrEmpty(mat.F_DEFINE04))
throw new Exception("最小包装单位未维护");
var convUnit = sugar.Queryable().Where(v => v.F_MATNO == MatNo && v.F_CONVUNIT == ConvUnit).First();
if (convUnit == null)
throw new Exception(MatNo + ":未找到基本转换关系[" + ConvUnit + "-->" + mat.F_BASEUNITNO + "]");
if (convUnit.F_CONVERSION <= 0)
throw new Exception("基本转换率不能小于等于0");
decimal baseValue = ConvValue != 0 ? convUnit.F_CONVERSION * ConvValue : 0;
entity = new ConvUnitEntity();
entity.MatNo = mat.F_NO;
entity.BaseUnit = mat.F_BASEUNITNO;
entity.Conversion = convUnit.F_CONVERSION;
entity.ConvUnit = ConvUnit;
entity.ConvValue = ConvValue;
entity.BaseValue = baseValue;
entity.MinUnit = mat.F_DEFINE04;
entity.MinValue = baseValue;
if (mat.F_BASEUNITNO != mat.F_DEFINE04)
{
var minUnit = sugar.Queryable().Where(v => v.F_MATNO == MatNo && v.F_CONVUNIT == mat.F_DEFINE04).First();
if (minUnit == null)
throw new Exception(MatNo + ":未找到包装转换关系[" + mat.F_BASEUNITNO + "-->" + mat.F_DEFINE04 + "]");
if (minUnit.F_CONVERSION <= 0)
throw new Exception("包装转换率不能小于等于0");
var minValue = baseValue != 0 ? baseValue / minUnit.F_CONVERSION : 0;
if (minValue == 0)
entity.MinValue = 0;
else
entity.MinValue = (baseValue % minUnit.F_CONVERSION) > 0 ? minValue + 1 : minValue;
}
json.Type = 1;
json.Message = "成功";
json.Entity = entity;
}
catch (Exception ex)
{
json.Type = 0;
json.Message = ex.Message;
json.Entity = null;
}
return json;
}
///
/// 转换单位
///
/// 料号
/// 转换单位
/// 转换值
///
public static JsonConvUnitEntity GetConvUnitByBase(int busTypes, string MatNo, string ConvUnit, decimal ConvValue, SqlSugarClient sugar)
{
JsonConvUnitEntity json = new JsonConvUnitEntity();
try
{
ConvUnitEntity entity = null;
if (string.IsNullOrEmpty(MatNo) || string.IsNullOrEmpty(ConvUnit))
throw SysExCore.ThrowFailException("料号或者转换单位不能为空");
if (busTypes != 12)
{
if (ConvValue <= 0)
throw new Exception("参数值不能小于0");
}
var mat = sugar.Queryable().Where(v => v.F_NO == MatNo).First();
if (mat == null)
throw new Exception("料号不存在");
if (string.IsNullOrEmpty(mat.F_BASEUNITNO))
throw new Exception("物料基本单位未维护");
if (string.IsNullOrEmpty(mat.F_DEFINE04))
throw new Exception("最小包装单位未维护");
var convUnit = sugar.Queryable().Where(v => v.F_MATNO == MatNo && v.F_CONVUNIT == ConvUnit).First();
if (convUnit == null)
throw new Exception(MatNo + ":未找到基本转换关系[" + ConvUnit + "-->" + mat.F_BASEUNITNO + "]");
if (convUnit.F_CONVERSION <= 0)
throw new Exception("基本转换率不能小于等于0");
decimal baseValue = ConvValue != 0 ? convUnit.F_CONVERSION * ConvValue : 0;
entity = new ConvUnitEntity();
entity.MatNo = mat.F_NO;
entity.BaseUnit = mat.F_BASEUNITNO;
entity.Conversion = convUnit.F_CONVERSION;
entity.ConvUnit = ConvUnit;
entity.ConvValue = ConvValue;
entity.BaseValue = baseValue;
entity.MinUnit = mat.F_DEFINE04;
entity.MinValue = baseValue;
if (mat.F_BASEUNITNO != mat.F_DEFINE04)
{
var minUnit = sugar.Queryable().Where(v => v.F_MATNO == MatNo && v.F_CONVUNIT == mat.F_DEFINE04).First();
if (minUnit == null)
throw new Exception(MatNo + ":未找到包装转换关系[" + mat.F_BASEUNITNO + "-->" + mat.F_DEFINE04 + "]");
if (minUnit.F_CONVERSION <= 0)
throw new Exception("包装转换率不能小于等于0");
var minValue = baseValue != 0 ? baseValue / minUnit.F_CONVERSION : 0;
if (minValue == 0)
entity.MinValue = 0;
else
entity.MinValue = (baseValue % minUnit.F_CONVERSION) > 0 ? minValue + 1 : minValue;
}
json.Type = 1;
json.Message = "成功";
json.Entity = entity;
}
catch (Exception ex)
{
json.Type = 0;
json.Message = ex.Message;
json.Entity = null;
}
return json;
}
}
}