using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CY.IDAL;
using System.Data.SqlClient;
using System.Data;
using CY.Model;
using System.Transactions;
namespace CY.SQLDAL
{
public class EC_SellerBusinessLimitsDAL : IEC_SellerBusinessLimitsDAL
{
private Database _dataBase = null;
private SysInquiry_PrintingTypeDAL _sysInquiry_PrintingTypeDAL = null;
public EC_SellerBusinessLimitsDAL()
{
_dataBase = new Database();
_sysInquiry_PrintingTypeDAL = new SysInquiry_PrintingTypeDAL();
}
public EC_SellerBusinessLimitsDAL(Database dataBase)
{
_dataBase = dataBase;
_sysInquiry_PrintingTypeDAL = new SysInquiry_PrintingTypeDAL();
}
///
/// 新增
///
///
///
public bool InserModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
if (trueModel == null)
{
return false;
}
SysInquiry_PrintingTypeDAL _printingTypeDAL = new SysInquiry_PrintingTypeDAL();
SysInquiry_PrintingType _printModel = _printingTypeDAL.GetModelById(trueModel.BusinessId.Value);
trueModel.JoinFlag = _printModel.JoinFlag;
trueModel.SpecialFlag = _printModel.SpecialFlag;
IList sqlParms = new List()
{
new SqlParameter("@SellerId",trueModel.SellerId),
new SqlParameter("@BusinessId",trueModel.BusinessId),
new SqlParameter("@JoinFlag",trueModel.JoinFlag),
new SqlParameter("@SpecialFlag",trueModel.SpecialFlag),
new SqlParameter("@Status",trueModel.Status),
};
try
{
_dataBase.Query("sp_EC_SellerBusinessLimits_Insert", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
//string sqlStr=string.Empty;
//sqlStr=" INSERT INTO EC_SellerBusinessLimits ";
//sqlStr += " SELECT '{0}',KeyId,joinFlag,specialFlag,0 FROM dbo.SysInquiry_PrintingType WHERE ParentId<>0 AND Status=1";
//sqlStr = string.Format(sqlStr, trueModel.SellerId.ToString());
//try
//{
// _dataBase.Query(sqlStr,CommandType.Text,null);
//}
//catch(Exception ex)
//{
// return false;
//}
//return true;
}
///
/// 修改
///
///
///
public bool UpdateModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
if (trueModel == null)
{
return false;
}
IList sqlParms = new List()
{
new SqlParameter("@BusinessId",trueModel.BusinessId),
new SqlParameter("@SellerId",trueModel.SellerId),
new SqlParameter("@Status",trueModel.Status)
};
try
{
_dataBase.Query("EC_SellerBusinessLimits_Update", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
public bool UpdateModelDetail(EC_SellerBusinessLimits model)
{
if (model == null)
return false;
IList sqlParms = new List()
{
new SqlParameter("@BusinessId",model.BusinessId),
new SqlParameter("@SellerId",model.SellerId),
new SqlParameter("@JoinFlag",model.JoinFlag),
new SqlParameter("@SpecialFlag",model.SpecialFlag)
};
try
{
_dataBase.Query("EC_SellerBusinessLimitsDetail_Update", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
///
/// 修改
///
///
///
public bool UpdateALL(Guid firmId)
{
IList sqlParms = new List()
{
new SqlParameter("@SellerId",firmId)
};
try
{
_dataBase.Query("EC_SellerBusinessLimits_UpdateAll", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
///
/// 删除
///
///
///
public bool DeleteModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
if (trueModel == null)
{
return false;
}
IList sqlParms = new List()
{
new SqlParameter("@BusinessId",trueModel.BusinessId)
};
try
{
_dataBase.Query("sp_EC_SellerBusinessLimits_DeleteRow", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
public bool DeleteModel(Guid sellerId)
{
IList sqlParms = new List()
{
new SqlParameter("@SellerId",sellerId)
};
try
{
_dataBase.Query("sp_EC_SellerBusinessLimits_DeleteBySellerId", CommandType.StoredProcedure, sqlParms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return true;
}
public IList GetBusinessRangeByFirmId(Guid firmId)
{
string condition = string.Empty;
condition = " SellerId='" + firmId.ToString() + "' and Status='true' ";
IList limitList = _dataBase.SelectModel("*", "EC_SellerBusinessLimits", condition);
IList list = new List();
foreach (EC_SellerBusinessLimits limitModel in limitList)
{
SysInquiry_PrintingType model = _sysInquiry_PrintingTypeDAL.GetModelById(limitModel.BusinessId.Value);
if (model != null)
list.Add(model);
}
return list;
}
public bool SaveBusinessRangeByFirmId(IList list)
{
bool isSuccess = true;
if (list != null && list.Count > 0)
{
//EC_SellerBusinessLimits m = list[0];
//if (!IsExist(m.SellerId))
//{
// isSuccess = InserModel(m);
//}
////isSuccess = UpdateALL(m.SellerId);
//if (isSuccess)
//{
// foreach (EC_SellerBusinessLimits model in list)
// {
// isSuccess = UpdateModel(model);
// if (!isSuccess)
// break;
// }
//}
EC_SellerBusinessLimits m = list[0];
Guid sellerId=m.SellerId;
isSuccess=DeleteModel(sellerId);
if (isSuccess)
{
foreach (EC_SellerBusinessLimits model in list)
{
isSuccess = InserModel(model);
if (!isSuccess)
break;
}
if (isSuccess)
{
if (IsExistsFastSetByFirm(sellerId))
{
isSuccess = DeleteFastSetByFirm(sellerId);
if (isSuccess)
{
isSuccess = InsertFastSetByFirm(sellerId);
}
}
if (isSuccess)
{
isSuccess = InsertFastSetByCustomer(sellerId);
}
}
}
}
return isSuccess;
}
///
/// 判断厂商是否保存了快速设置
///
///
///
private bool IsExistsFastSetByFirm(Guid firmId)
{
bool isSussess = false;
string sqlStr=@"SELECT * FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId";
SqlParameter par = new SqlParameter("@FirmId",firmId.ToString());
DataTable dt=_dataBase.QueryDataTable(sqlStr,CommandType.Text,par);
if(dt!=null&&dt.Rows.Count>0)
{
isSussess=true;
}
return isSussess;
}
///
/// 判断厂商的客户是否保存了快速设置,有就返回客户ID列表
///
///
///
private bool IsExistsFastSetByCustomer(Guid firmId,IList CustomerIds)
{
bool isSussess = false;
string sqlStr = @"SELECT DISTINCT CustomerId FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId and CustomerId is not null";
SqlParameter par = new SqlParameter("@FirmId", firmId.ToString());
DataTable dt = _dataBase.QueryDataTable(sqlStr, CommandType.Text, par);
if (dt != null && dt.Rows.Count > 0)
{
isSussess = true;
foreach (DataRow dr in dt.Rows)
{
CustomerIds.Add(dr["CustomerId"].ToString());
}
}
return isSussess;
}
///
/// 移除没有权限的快速设置
///
///
///
private bool DeleteFastSetByFirm(Guid firmId)
{
bool isSussess = true;
string sqlStr = @"DELETE inquiry_FastSetByFirm WHERE FirmId=@FirmId AND PrintTypeId NOT IN (SELECT BusinessId FROM EC_SellerBusinessLimits WHERE SellerId=@FirmId)";
SqlParameter par = new SqlParameter("@FirmId",firmId.ToString());
try
{
int i= _dataBase.ExecuteSql(sqlStr,par);
if (i > 0)
{
isSussess = true;
}
}
catch(Exception ex)
{
isSussess = false;
}
return isSussess;
}
///
/// 给厂商新增有权限的快速设置数据
///
///
///
private bool InsertFastSetByFirm(Guid firmId)
{
bool isSussess = true;
string sqlStr = @"INSERT INTO inquiry_FastSetByFirm
SELECT @FirmId,NULL,100,BusinessId
FROM EC_SellerBusinessLimits
WHERE SellerId=@FirmId AND BusinessId NOT IN (
SELECT PrintTypeId FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId AND CustomerId IS NULL
)";
SqlParameter par = new SqlParameter("@FirmId", firmId.ToString());
try
{
int i = _dataBase.ExecuteSql(sqlStr, par);
if (i > 0)
{
isSussess = true;
}
}
catch(Exception ex)
{
isSussess = false;
}
return isSussess;
}
///
/// 给已保存快速设置的客户新增有权限的快速设置数据
///
///
///
private bool InsertFastSetByCustomer(Guid firmId)
{
bool isSussess = true;
IList CustomerIds = new List();
if (IsExistsFastSetByCustomer(firmId, CustomerIds))
{
try
{
foreach (string customerId in CustomerIds)
{
string sqlStr = @"INSERT INTO inquiry_FastSetByFirm
SELECT @FirmId,@CutomerId,100,BusinessId
FROM EC_SellerBusinessLimits
WHERE SellerId=@FirmId AND BusinessId NOT IN (
SELECT PrintTypeId FROM inquiry_FastSetByFirm WHERE FirmId=@FirmId
AND CustomerId=@CutomerId
)";
SqlParameter[] pars = new SqlParameter[]{
new SqlParameter("@FirmId",firmId.ToString()),
new SqlParameter("@CutomerId",customerId),
};
int i = _dataBase.ExecuteSql(sqlStr, pars);
if (i > 0)
{
isSussess = true;
}
}
}
catch(Exception ex)
{
isSussess = false;
}
}
else
{
isSussess = true;
}
return isSussess;
}
///
/// 判断厂商是否有数据
///
///
///
public bool IsExist(Guid firmId)
{
string condition = string.Empty;
condition = " SellerId='" + firmId.ToString() + "' ";
IList limitList = _dataBase.SelectModel("*", "EC_SellerBusinessLimits", condition);
if (limitList != null && limitList.Count > 0)
{
return true;
}
else
{
return false;
}
}
///
/// 根据厂商Id获取其业务范围明细
///
///
///
public IList GetBusinessRangeDetaiByFirmId(Guid firmId)
{
string condition = string.Empty;
condition = " SellerId='" + firmId.ToString() + "' and status='true'";
IList limitList = _dataBase.SelectModel("*", "EC_SellerBusinessLimits", condition);
return limitList;
}
///
/// 保存厂商设置的业务范围明细
///
///
///
public bool SaveBusinessRangeDetailByFirmId(IList list)
{
bool isSuccess = true;
using (TransactionScope scope = new TransactionScope())
{
foreach (EC_SellerBusinessLimits model in list)
{
isSuccess = UpdateModelDetail(model);
if (!isSuccess)
break;
}
if (isSuccess)
scope.Complete();
}
return isSuccess;
}
}
}