public static void ExcelToDataBase(IDbSession cn, DataSet dataSet, long ManagerId)
{
if (cn.Transaction == null)
{
throw new BusinessException("调用此方法需要先开启事务");
}
if (dataSet != null)
{
//表
for (int i = 0; i < dataSet.Tables.Count; i++)
{
//1.找分类
DataTable table = dataSet.Tables[i];
QueryExpression q1 = new QueryExpression();
q1.Selects.Add(Category_.CategoryId);
q1.Selects.Add(Category_.CategoryName);
q1.Wheres.Add(Category_.CategoryName.TEqual(table.TableName));
q1.Wheres.Add(Category_.IsActive.TEqual(1));
Categorys categorys = CategoryService.GetCategorys(cn, q1);
if (categorys != null && categorys.Items.Count > 0)
{
Category category = categorys.Items[0];
ProductGlobal productGlobal = new ProductGlobal();
//行
for (int j = 1; j < table.Rows.Count; j++)
{
DataRow row = table.Rows[j];
string ProductName = row[0].ToString().Trim();//产品名称
string Spec = row[1].ToString().Trim();//规格
string Unit = row[2].ToString().Trim();//计价单位
string DisplayUnitTypeId = row[3].ToString().Trim();//单位是否一致
string DisplayUnit = row[4].ToString().Trim();//购买单位
string UnitAmount = row[5].ToString().Trim();//计量数
if (string.IsNullOrEmpty(ProductName))
{
throw new BusinessException("产品名称不能为空");
}
if (string.IsNullOrEmpty(DisplayUnitTypeId))
{
throw new BusinessException("单位是否一致不能为空");
}
if ("否".Equals(DisplayUnitTypeId))
{
productGlobal.DisplayUnitTypeId = DisplayUnitType.None;
}
else if ("是".Equals(DisplayUnitTypeId))
{
productGlobal.DisplayUnitTypeId = DisplayUnitType.Replace;
if (string.IsNullOrEmpty(DisplayUnit))
{
throw new BusinessException("单位不一致时,购买单位不能为空");
}
if (string.IsNullOrEmpty(UnitAmount))
{
throw new BusinessException("单位不一致时,计量数不能为空");
}
try
{
productGlobal.UnitAmount = decimal.Parse(UnitAmount);
}
catch (Exception e)
{
throw new BusinessException("计量数必须为数字");
}
}
else
{
throw new BusinessException("单位是否不一致请填写是或否");
}
productGlobal.ProductName = ProductName;
productGlobal.Spec = Spec;
productGlobal.Unit = ProductName;
productGlobal.DisplayUnit = DisplayUnit;
//2.去重、添加商品分类
ConditionExpressionCollection cs = new ConditionExpressionCollection();
cs.Add(ProductGlobal_.ProductName.TEqual(ProductName));
cs.Add(ProductGlobal_.Spec.TEqual(Spec));
cs.Add(ProductGlobal_.IsActive.TEqual(true));
if (cn.Exists<ProductGlobal>(cs))
{
throw new BusinessException(ProductName + "-" + Spec + ":已经存在,不能重复添加");
}
productGlobal.CategoryId = category.CategoryId;
productGlobal.ManagerId = ManagerId;
ProductService.InsertProductGlobal(cn, productGlobal);
}
}
else
{
throw new BusinessException(table.TableName + "-分类不存在");
}
}
}
}
{
if (cn.Transaction == null)
{
throw new BusinessException("调用此方法需要先开启事务");
}
if (dataSet != null)
{
//表
for (int i = 0; i < dataSet.Tables.Count; i++)
{
//1.找分类
DataTable table = dataSet.Tables[i];
QueryExpression q1 = new QueryExpression();
q1.Selects.Add(Category_.CategoryId);
q1.Selects.Add(Category_.CategoryName);
q1.Wheres.Add(Category_.CategoryName.TEqual(table.TableName));
q1.Wheres.Add(Category_.IsActive.TEqual(1));
Categorys categorys = CategoryService.GetCategorys(cn, q1);
if (categorys != null && categorys.Items.Count > 0)
{
Category category = categorys.Items[0];
ProductGlobal productGlobal = new ProductGlobal();
//行
for (int j = 1; j < table.Rows.Count; j++)
{
DataRow row = table.Rows[j];
string ProductName = row[0].ToString().Trim();//产品名称
string Spec = row[1].ToString().Trim();//规格
string Unit = row[2].ToString().Trim();//计价单位
string DisplayUnitTypeId = row[3].ToString().Trim();//单位是否一致
string DisplayUnit = row[4].ToString().Trim();//购买单位
string UnitAmount = row[5].ToString().Trim();//计量数
if (string.IsNullOrEmpty(ProductName))
{
throw new BusinessException("产品名称不能为空");
}
if (string.IsNullOrEmpty(DisplayUnitTypeId))
{
throw new BusinessException("单位是否一致不能为空");
}
if ("否".Equals(DisplayUnitTypeId))
{
productGlobal.DisplayUnitTypeId = DisplayUnitType.None;
}
else if ("是".Equals(DisplayUnitTypeId))
{
productGlobal.DisplayUnitTypeId = DisplayUnitType.Replace;
if (string.IsNullOrEmpty(DisplayUnit))
{
throw new BusinessException("单位不一致时,购买单位不能为空");
}
if (string.IsNullOrEmpty(UnitAmount))
{
throw new BusinessException("单位不一致时,计量数不能为空");
}
try
{
productGlobal.UnitAmount = decimal.Parse(UnitAmount);
}
catch (Exception e)
{
throw new BusinessException("计量数必须为数字");
}
}
else
{
throw new BusinessException("单位是否不一致请填写是或否");
}
productGlobal.ProductName = ProductName;
productGlobal.Spec = Spec;
productGlobal.Unit = ProductName;
productGlobal.DisplayUnit = DisplayUnit;
//2.去重、添加商品分类
ConditionExpressionCollection cs = new ConditionExpressionCollection();
cs.Add(ProductGlobal_.ProductName.TEqual(ProductName));
cs.Add(ProductGlobal_.Spec.TEqual(Spec));
cs.Add(ProductGlobal_.IsActive.TEqual(true));
if (cn.Exists<ProductGlobal>(cs))
{
throw new BusinessException(ProductName + "-" + Spec + ":已经存在,不能重复添加");
}
productGlobal.CategoryId = category.CategoryId;
productGlobal.ManagerId = ManagerId;
ProductService.InsertProductGlobal(cn, productGlobal);
}
}
else
{
throw new BusinessException(table.TableName + "-分类不存在");
}
}
}
}