using System; using System.Collections.Generic; using System.Linq; using System.Text; using CY.IDAL.Inquiry; using CY.Model; using System.Data.SqlClient; using System.Data; namespace CY.SQLDAL { public class ReduceWorkCountSetDAL : IReduceWorkCountSetDAL { Database _dataBase = null; public ReduceWorkCountSetDAL() { _dataBase = new Database(); } public Model.Inquiry_ReduceWorkCountSet GetModel(Guid firmId, Guid customerId) { Inquiry_ReduceWorkCountSet model = null; //获取厂商设置的价格 if (customerId == Guid.Empty) { model = GetModelByFirm(firmId); } else { model = GetModelByCustomer(firmId, customerId); if (model == null) { model = GetModelByFirm(firmId); } } return model; } public Inquiry_ReduceWorkCountSet GetModelByFirm(Guid firmId) { Inquiry_ReduceWorkCountSet model = null; string selectTarget = " KeyId,Count "; string fromSouce = " Inquiry_ReduceWorkCountSet "; string condition = " FirmId='" + firmId.ToString() + "' and CustomerId is null "; IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition); if (result != null && result.Count > 0) { model = result[0]; } return model; } public Inquiry_ReduceWorkCountSet GetModelByCustomer(Guid firmId, Guid customerId) { Inquiry_ReduceWorkCountSet model = null; string selectTarget = " KeyId,Count "; string fromSouce = " Inquiry_ReduceWorkCountSet "; string condition = " FirmId='" + firmId.ToString() + "' and CustomerId='" + customerId.ToString() + "'"; IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition); if (result != null && result.Count > 0) { model = result[0]; } return model; } public bool IsExits(Guid firmId, Guid customerId) { string selectTarget = " KeyId "; string fromSouce = " Inquiry_ReduceWorkCountSet "; string condition = string.Empty; if (customerId == Guid.Empty) { condition = " FirmId='" + firmId.ToString() + "'"; } else { condition = " FirmId='" + firmId.ToString() + "' and CustomerId='" + customerId.ToString() + "'"; } IList result = _dataBase.SelectModel(selectTarget, fromSouce, condition); if (result == null || result.Count == 0) return false; else return true; } public bool SaveModel(Model.Inquiry_ReduceWorkCountSet model) { bool isSuccess = true; if (IsExits(model.FirmId, model.CustomerId)) { isSuccess = UpdateModel(model); } else { isSuccess = InserModel(model); } return isSuccess; } public bool InserModel(Infrastructure.Domain.IAggregateRoot model) { Model.Inquiry_ReduceWorkCountSet trueModel = model as Model.Inquiry_ReduceWorkCountSet; 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("@Count",SqlDbType.Int), cusPar }; parameters[0].Value = trueModel.FirmId; parameters[1].Value = trueModel.Count; try { _dataBase.Query("Inquiry_ReduceWorkCountSet_ADD", CommandType.StoredProcedure, parameters); } catch (Exception ex) { throw ex; } return true; } public bool UpdateModel(Infrastructure.Domain.IAggregateRoot model) { Model.Inquiry_ReduceWorkCountSet trueModel = model as Model.Inquiry_ReduceWorkCountSet; 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("@Count", SqlDbType.Int), cusPar }; parameters[0].Value = trueModel.FirmId; parameters[1].Value = trueModel.Count; try { _dataBase.Query("Inquiry_ReduceWorkCountSet_Update", CommandType.StoredProcedure, parameters); } catch (Exception ex) { throw ex; } return true; } } }