using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CY.IDAL.Inquiry;
using System.Transactions;
using CY.Infrastructure.Common;
using System.Data.SqlClient;
using System.Data;
using CY.Model.Inquiry;
using CY.Model;
namespace CY.SQLDAL
{
///
/// 厂商快速设置表操作接口--SQL实现
///
public class FastSetByFirmDAL : IFastSetByFirmDAL
{
Database _dataBase = null;
public FastSetByFirmDAL()
{
_dataBase = new Database();
}
///
/// 获取设置的快速价格
///
///
///
///
public IList GetModelList(Guid firmId,Guid customerId)
{
IList list = new List();
//获取厂商设置的价格
if (customerId == Guid.Empty)
{
list = GetModelListByFirm(firmId);
if (list == null || list.Count == 0)
{
list = GetModelListByAdmin();
}
}
else //获取客户设置的价格
{
list = GetModelListByCustomer(firmId,customerId);
if (list == null || list.Count == 0)
{
list = GetModelListByFirm(firmId);
if (list == null || list.Count == 0)
{
list = GetModelListByAdmin();
}
}
}
return list;
}
///
/// 获取系统设置的快速价格
///
///
public IList GetModelListByAdmin()
{
//string selectTarget = " a.FastValue,a.PrintTypeId,b.PrintName AS PrintTypeName ";
//string fromSouce = " Inquiry_FastSetByFirm a INNER JOIN SysInquiry_PrintingType b ON a.PrintTypeId=b.KeyId ";
//string condition = " a.FirmId='" + UtilConst.AdminFirmId + "' ";
//IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition);
//return result;
string selectTarget = " 100 AS FastValue,KeyId AS PrintTypeId,PrintName AS PrintTypeName ";
string fromSouce = " SysInquiry_PrintingType ";
string condition = " ParentId<>0 AND Status=1 ORDER BY orderNum ";
IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition);
return result;
}
///
/// 获取厂商设置的快速价格
///
///
public IList GetModelListByFirm(Guid firmId)
{
string selectTarget = " a.FastValue,a.PrintTypeId,b.PrintName AS PrintTypeName ";
string fromSouce = " Inquiry_FastSetByFirm a INNER JOIN SysInquiry_PrintingType b ON a.PrintTypeId=b.KeyId ";
string condition = " a.FirmId='" + firmId.ToString() + "' and a.CustomerId is null ";
IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition);
return result;
}
///
/// 获取厂商的客户设置的快速价格
///
///
public IList GetModelListByCustomer(Guid firmId,Guid customerId)
{
string selectTarget = " a.FastValue,a.PrintTypeId,b.PrintName AS PrintTypeName ";
string fromSouce = " Inquiry_FastSetByFirm a INNER JOIN SysInquiry_PrintingType b ON a.PrintTypeId=b.KeyId ";
string condition = " a.FirmId='" + firmId.ToString() + "' and a.CustomerId='" + customerId.ToString() + "'";
IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition);
return result;
}
public bool SaveModelList(IList modelList)
{
bool isSuccess = true;
using (TransactionScope scope = new TransactionScope())
{
foreach (Inquiry_FastSetByFirm model in modelList)
{
if(IsExits(model.FirmId,model.CustomerId,model.PrintTypeId))
{
isSuccess=UpdateModel(model);
if(!isSuccess)
break;
}
else
{
isSuccess=InserModel(model);
if(!isSuccess)
break;
}
}
if (isSuccess)
scope.Complete();
}
return isSuccess;
}
///
/// 判断是否存在
///
///
///
///
///
public bool IsExits(Guid firmId, Guid customerId, int printTypeId)
{
string selectTarget = " FirmId ";
string fromSouce = " Inquiry_FastSetByFirm ";
string condition = string.Empty;
if (customerId == Guid.Empty)
{
condition = " FirmId='" + firmId.ToString() + "' and printTypeId=" + printTypeId;
}
else
{
condition = " FirmId='" + firmId.ToString() + "' and CustomerId='" + customerId.ToString() + "' and printTypeId=" + printTypeId;
}
IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition);
if (result == null || result.Count == 0)
return false;
else
return true;
}
public bool InserModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.Inquiry_FastSetByFirm trueModel = model as Model.Inquiry_FastSetByFirm;
if (trueModel == null)
{
return false;
}
SqlParameter cusPar = null;
if (trueModel.CustomerId == Guid.Empty)
{
cusPar = new SqlParameter("@CustomerId", DBNull.Value);
}
else
{
cusPar = new SqlParameter("@CustomerId", trueModel.CustomerId);
}
SqlParameter[] parameters = {
new SqlParameter("@FirmId", SqlDbType.UniqueIdentifier,16),
new SqlParameter("@FastValue", SqlDbType.Float,8),
new SqlParameter("@PrintTypeId",SqlDbType.Int),
cusPar
};
parameters[0].Value = trueModel.FirmId;
parameters[1].Value = trueModel.FastValue;
parameters[2].Value = trueModel.PrintTypeId;
try
{
_dataBase.Query("Inquiry_FastSetByFirm_ADD", CommandType.StoredProcedure, parameters);
}
catch (Exception ex)
{
throw ex;
}
return true;
}
public bool UpdateModel(Infrastructure.Domain.IAggregateRoot model)
{
Model.Inquiry_FastSetByFirm trueModel = model as Model.Inquiry_FastSetByFirm;
if (trueModel == null)
{
return false;
}
SqlParameter cusPar = null;
if (trueModel.CustomerId == Guid.Empty)
{
cusPar = new SqlParameter("@CustomerId", DBNull.Value);
}
else
{
cusPar = new SqlParameter("@CustomerId", trueModel.CustomerId);
}
SqlParameter[] parameters = {
new SqlParameter("@FirmId", SqlDbType.UniqueIdentifier,16),
new SqlParameter("@FastValue", SqlDbType.Float,8),
new SqlParameter("@PrintTypeId",SqlDbType.Int),
cusPar
};
parameters[0].Value = trueModel.FirmId;
parameters[1].Value = trueModel.FastValue;
parameters[2].Value = trueModel.PrintTypeId;
try
{
_dataBase.Query("Inquiry_FastSetByFirm_Update", CommandType.StoredProcedure, parameters);
}
catch (Exception ex)
{
throw ex;
}
return true;
}
public bool DeleteModel(Infrastructure.Domain.IAggregateRoot model)
{
throw new NotImplementedException();
}
}
}