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();
|
}
|
|
/// <summary>
|
/// 新增
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
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<SqlParameter> sqlParms = new List<SqlParameter>()
|
{
|
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<SqlParameter>());
|
}
|
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;
|
}
|
|
/// <summary>
|
/// 修改
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
public bool UpdateModel(Infrastructure.Domain.IAggregateRoot model)
|
{
|
Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
|
if (trueModel == null)
|
{
|
return false;
|
}
|
IList<SqlParameter> sqlParms = new List<SqlParameter>()
|
{
|
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<SqlParameter>());
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return true;
|
}
|
|
public bool UpdateModelDetail(EC_SellerBusinessLimits model)
|
{
|
if (model == null)
|
return false;
|
IList<SqlParameter> sqlParms = new List<SqlParameter>()
|
{
|
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<SqlParameter>());
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return true;
|
}
|
|
/// <summary>
|
/// 修改
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
public bool UpdateALL(Guid firmId)
|
{
|
IList<SqlParameter> sqlParms = new List<SqlParameter>()
|
{
|
new SqlParameter("@SellerId",firmId)
|
};
|
try
|
{
|
_dataBase.Query("EC_SellerBusinessLimits_UpdateAll", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return true;
|
}
|
|
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
public bool DeleteModel(Infrastructure.Domain.IAggregateRoot model)
|
{
|
Model.EC_SellerBusinessLimits trueModel = model as Model.EC_SellerBusinessLimits;
|
if (trueModel == null)
|
{
|
return false;
|
}
|
IList<SqlParameter> sqlParms = new List<SqlParameter>()
|
{
|
new SqlParameter("@BusinessId",trueModel.BusinessId)
|
};
|
try
|
{
|
_dataBase.Query("sp_EC_SellerBusinessLimits_DeleteRow", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return true;
|
}
|
|
public bool DeleteModel(Guid sellerId)
|
{
|
IList<SqlParameter> sqlParms = new List<SqlParameter>()
|
{
|
new SqlParameter("@SellerId",sellerId)
|
};
|
try
|
{
|
_dataBase.Query("sp_EC_SellerBusinessLimits_DeleteBySellerId", CommandType.StoredProcedure, sqlParms.ToArray<SqlParameter>());
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return true;
|
}
|
|
public IList<Model.SysInquiry_PrintingType> GetBusinessRangeByFirmId(Guid firmId)
|
{
|
string condition = string.Empty;
|
condition = " SellerId='" + firmId.ToString() + "' and Status='true' ";
|
IList<EC_SellerBusinessLimits> limitList = _dataBase.SelectModel<EC_SellerBusinessLimits>("*", "EC_SellerBusinessLimits", condition);
|
IList<SysInquiry_PrintingType> list = new List<SysInquiry_PrintingType>();
|
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<Model.EC_SellerBusinessLimits> 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;
|
}
|
|
/// <summary>
|
/// 判断厂商是否保存了快速设置
|
/// </summary>
|
/// <param name="firmId"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 判断厂商的客户是否保存了快速设置,有就返回客户ID列表
|
/// </summary>
|
/// <param name="firmId"></param>
|
/// <returns></returns>
|
private bool IsExistsFastSetByCustomer(Guid firmId,IList<string> 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;
|
}
|
|
/// <summary>
|
/// 移除没有权限的快速设置
|
/// </summary>
|
/// <param name="firmId"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 给厂商新增有权限的快速设置数据
|
/// </summary>
|
/// <param name="firmId"></param>
|
/// <returns></returns>
|
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;
|
}
|
|
/// <summary>
|
/// 给已保存快速设置的客户新增有权限的快速设置数据
|
/// </summary>
|
/// <param name="firmId"></param>
|
/// <returns></returns>
|
private bool InsertFastSetByCustomer(Guid firmId)
|
{
|
bool isSussess = true;
|
IList<string> CustomerIds = new List<string>();
|
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;
|
}
|
|
/// <summary>
|
/// 判断厂商是否有数据
|
/// </summary>
|
/// <param name="firmId"></param>
|
/// <returns></returns>
|
public bool IsExist(Guid firmId)
|
{
|
string condition = string.Empty;
|
condition = " SellerId='" + firmId.ToString() + "' ";
|
IList<EC_SellerBusinessLimits> limitList = _dataBase.SelectModel<EC_SellerBusinessLimits>("*", "EC_SellerBusinessLimits", condition);
|
if (limitList != null && limitList.Count > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
|
/// <summary>
|
/// 根据厂商Id获取其业务范围明细
|
/// </summary>
|
/// <param name="firmId"></param>
|
/// <returns></returns>
|
public IList<EC_SellerBusinessLimits> GetBusinessRangeDetaiByFirmId(Guid firmId)
|
{
|
string condition = string.Empty;
|
condition = " SellerId='" + firmId.ToString() + "' and status='true'";
|
IList<EC_SellerBusinessLimits> limitList = _dataBase.SelectModel<EC_SellerBusinessLimits>("*", "EC_SellerBusinessLimits", condition);
|
return limitList;
|
}
|
|
/// <summary>
|
/// 保存厂商设置的业务范围明细
|
/// </summary>
|
/// <param name="list"></param>
|
/// <returns></returns>
|
public bool SaveBusinessRangeDetailByFirmId(IList<EC_SellerBusinessLimits> 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;
|
}
|
}
|
}
|