190 lines
6.4 KiB
C#
190 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity.Validation;
|
|
using System.Linq;
|
|
using WX.CRM.BLL.Base;
|
|
using WX.CRM.BLL.Util;
|
|
using WX.CRM.Common;
|
|
using WX.CRM.DAL.Res;
|
|
using WX.CRM.IBLL.Res;
|
|
using WX.CRM.Model.Entity;
|
|
|
|
namespace WX.CRM.BLL.Res
|
|
{
|
|
public class RES_CUSTOMERUSER_BL : IRES_CUSTOMERUSER, IRES_CUSTOMERUSER_Q
|
|
{
|
|
CUSTOMER_DAL custDal = new CUSTOMER_DAL();
|
|
private readonly RedisBL.ReadOrWriteFromRedis_BL redisBl = new RedisBL.ReadOrWriteFromRedis_BL();
|
|
#region 获取单条信息
|
|
/// <summary>
|
|
/// 获取实体
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public WX.CRM.Model.Entity.RES_CUSTOMERUSER GetModelByUserName(string username)
|
|
{
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
WX.CRM.Model.Entity.RES_CUSTOMERUSER entry = db.RES_CUSTOMERUSER.FirstOrDefault(m => m.USERNAME == username);
|
|
return entry;
|
|
}
|
|
}
|
|
|
|
public List<WX.CRM.Model.Entity.RES_CUSTOMERUSER> GetListByUserName(string username)
|
|
{
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
List<WX.CRM.Model.Entity.RES_CUSTOMERUSER> list = db.RES_CUSTOMERUSER.Where(m => m.USERNAME == username).ToList();
|
|
return list;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取实体
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public WX.CRM.Model.Entity.RES_CUSTOMERUSER GetModelByUserNo(decimal userno)
|
|
{
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
var soft_user = db.SOFT_USER.Where(m => m.USERNO == userno);
|
|
var xx = (from a in db.RES_CUSTOMERUSER
|
|
join b in soft_user on a.USERNAME equals b.USERNAME
|
|
select a);
|
|
return xx.FirstOrDefault();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 根据 resid获取实体
|
|
/// </summary>
|
|
/// <param name="ResId"></param>
|
|
/// <returns></returns>
|
|
public List<WX.CRM.Model.Entity.RES_CUSTOMERUSER> GetModelByResId(string ResId)
|
|
{
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
ResId = ResId.Trim();
|
|
List<WX.CRM.Model.Entity.RES_CUSTOMERUSER> entry = db.RES_CUSTOMERUSER.Where(m => m.RESID == ResId).ToList();
|
|
return entry;
|
|
}
|
|
}
|
|
|
|
public List<WX.CRM.Model.Entity.RES_CUSTOMERUSER> GetCustomerListByResId(string Resid)
|
|
{
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
var queryData = db.RES_CUSTOMER.AsQueryable();
|
|
if (!string.IsNullOrEmpty(Resid))
|
|
{
|
|
Resid = Resid.Trim();
|
|
queryData = queryData.Where(m => m.RESID == Resid);
|
|
}
|
|
else
|
|
{
|
|
Resid = Resid.Trim();
|
|
queryData = queryData.Where(m => m.RESID == Resid);
|
|
}
|
|
|
|
List<RES_CUSTOMERUSER> list = (from q in db.RES_CUSTOMER
|
|
join f in queryData on q.CUSTOMERID equals f.CUSTOMERID
|
|
join d in db.RES_CUSTOMERUSER on q.RESID equals d.RESID
|
|
select d
|
|
).ToList();
|
|
return list;
|
|
|
|
}
|
|
}
|
|
#region 添加
|
|
/// <summary>
|
|
/// 添加
|
|
/// </summary>
|
|
/// <param name="errors"></param>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public bool Create(ref ValidationErrors errors, WX.CRM.Model.Entity.RES_CUSTOMERUSER model)
|
|
{
|
|
try
|
|
{
|
|
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
model.PKID = new SEQUENCES_BL().Seq_base_get();
|
|
db.RES_CUSTOMERUSER.Add(model);
|
|
bool result = db.SaveChanges().GetResult();
|
|
//if (result)
|
|
// AddToRedis(model);
|
|
return result;
|
|
}
|
|
}
|
|
catch (DbEntityValidationException dbEx)
|
|
{
|
|
var ex1 = dbEx.EntityValidationErrors.FirstOrDefault();
|
|
if (ex1 != null)
|
|
{
|
|
var ex2 = ex1.ValidationErrors.FirstOrDefault();
|
|
if (ex2 != null)
|
|
{
|
|
errors.Add(ex2.ErrorMessage);
|
|
}
|
|
else
|
|
errors.Add(dbEx.Message);
|
|
}
|
|
else
|
|
errors.Add(dbEx.Message);
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errors.Add(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 关联数据
|
|
/// </summary>
|
|
/// <param name="resid"></param>
|
|
/// <param name="username"></param>
|
|
/// <returns></returns>
|
|
public int ContractUser(string resid, string username)
|
|
{
|
|
|
|
return custDal.ContractUser(resid, username);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
//根据主客户ID获取所有的用户名信息
|
|
public List<RES_CUSTOMERUSER> GetListByCustomerId(string CustomerId)
|
|
{
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
CustomerId = CustomerId.Trim();
|
|
List<WX.CRM.Model.Entity.RES_CUSTOMERUSER> entry = db.RES_CUSTOMERUSER.Where(m => (db.RES_CUSTOMER.Where(b => b.CUSTOMERID == CustomerId).Select(b => b.RESID)).Contains(m.RESID)).ToList();
|
|
return entry;
|
|
}
|
|
}
|
|
|
|
public void Delete(string username, string resid)
|
|
{
|
|
using (var db = new crmContext())
|
|
{
|
|
var model = db.RES_CUSTOMERUSER.FirstOrDefault(p => p.USERNAME == username && p.RESID == resid);
|
|
if (model != null)
|
|
{
|
|
db.RES_CUSTOMERUSER.Remove(model);
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|