TG.WXCRM.V4/BLL/Soft/SOFT_ACTIVE_BL.cs

155 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Transactions;
using WX.CRM.BLL.Base;
using WX.CRM.BLL.Util;
using WX.CRM.Common;
using WX.CRM.IBLL.Soft;
namespace WX.CRM.BLL.Soft
{
public class SOFT_ACTIVE_BL : ISOFT_ACTIVE, ISOFT_ACTIVE_Q
{
public bool Create(ref ValidationErrors errors, WX.CRM.Model.Entity.SOFT_ACTIVE model)
{
try
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
model.PRODUCTID = new SEQUENCES_BL().Seq_base_get();
db.SOFT_ACTIVE.Add(model);
return db.SaveChanges().GetResult();
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
return false;
}
}
public bool Update(ref ValidationErrors errors, WX.CRM.Model.Entity.SOFT_ACTIVE model)
{
try
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
db.SOFT_ACTIVE.Attach(model);
db.Entry<WX.CRM.Model.Entity.SOFT_ACTIVE>(model).State = EntityState.Modified;
db.SaveChanges();
return true;
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
return false;
}
}
public WX.CRM.Model.Entity.SOFT_ACTIVE GetPRODUCTById(decimal id)
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
WX.CRM.Model.Entity.SOFT_ACTIVE entry = db.SOFT_ACTIVE.FirstOrDefault(m => m.PRODUCTID == id);
return entry;
}
}
public List<WX.CRM.Model.Entity.SOFT_ACTIVE> GetAllPRODUCTS()
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
return db.SOFT_ACTIVE.ToList();
}
}
public List<WX.CRM.Model.Entity.SOFT_ACTIVE> GetPRODUCTList(string productName, string productCode)
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
var list = db.SOFT_ACTIVE.AsQueryable();
if (!string.IsNullOrEmpty(productName))
{
list = list.Where(p => p.PRODUCTNAME.Contains(productName));
}
if (!string.IsNullOrEmpty(productCode))
{
list = list.Where(p => p.PRODUCTCODE == productCode);
}
return list.OrderByDescending(p => p.CTIME).ToList();
}
}
public bool CreateTrans(ref ValidationErrors errors, WX.CRM.Model.Entity.SOFT_ACTIVE model, List<WX.CRM.Model.Entity.SOFT_ACTIVEMODULE> procuctModule)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
model.PRODUCTID = new SEQUENCES_BL().Seq_base_get();
db.SOFT_ACTIVE.Add(model);
foreach (var productmodule in procuctModule)
{
productmodule.PKID = new SEQUENCES_BL().Seq_base_get();
productmodule.PRODUCTID = model.PRODUCTID;
db.SOFT_ACTIVEMODULE.Add(productmodule);
}
db.SaveChanges();
scope.Complete();
return true;
}
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
return false;
}
}
public bool UpdateTrans(ref ValidationErrors errors, WX.CRM.Model.Entity.SOFT_ACTIVE model, List<WX.CRM.Model.Entity.SOFT_ACTIVEMODULE> procuctModule)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
db.SOFT_ACTIVE.Attach(model);
db.Entry<WX.CRM.Model.Entity.SOFT_ACTIVE>(model).State = EntityState.Modified;
var deletemodels = db.SOFT_ACTIVEMODULE.Where(p => p.PRODUCTID == model.PRODUCTID).ToList();
foreach (var deletemodel in deletemodels)
{
db.SOFT_ACTIVEMODULE.Remove(deletemodel);
}
foreach (var productmodule in procuctModule)
{
productmodule.PKID = new SEQUENCES_BL().Seq_base_get();
productmodule.PRODUCTID = model.PRODUCTID;
db.SOFT_ACTIVEMODULE.Add(productmodule);
}
db.SaveChanges();
scope.Complete();
return true;
}
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
return false;
}
}
}
}