273 lines
9.7 KiB
C#
273 lines
9.7 KiB
C#
using CRM.Core.DTO.Res;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity.Validation;
|
|
using System.Linq;
|
|
using WX.CRM.BLL.Base;
|
|
using WX.CRM.BLL.Soft;
|
|
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();
|
|
SOFT_USER_BL soft_user_bl = new SOFT_USER_BL();
|
|
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<SOFT_USER> GetListByCustomerId(string CustomerId)
|
|
{
|
|
List<SOFT_USER> res = new List<SOFT_USER>();
|
|
if(string.IsNullOrWhiteSpace(CustomerId))
|
|
{
|
|
return res;
|
|
}
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
CustomerId = CustomerId.Trim();
|
|
var entry = db.SOFT_USER.Where(m => (db.RES_CUSTOMER.Where(b => b.CUSTOMERID == CustomerId).Select(b => b.RESID)).Contains(m.RESID)).ToList();
|
|
return entry;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取用户绑定状态
|
|
/// </summary>
|
|
/// <param name="resid"></param>
|
|
/// <param name=""></param>
|
|
/// <returns></returns>
|
|
public bool GetResidBound(string resid) {
|
|
using (var db = new WX.CRM.Model.Entity.crmContext())
|
|
{
|
|
var entry = db.RES_RESID_WEWORKUSER.Where(m => m.RESID== resid).ToList();
|
|
return entry.Any(m=>m.STATUS==1);
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 手机号 用户名修改
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool ChangeUserMobile(string resid, ChangeMobile model)
|
|
{
|
|
var result = false;
|
|
using (var db = new crmContext())
|
|
{
|
|
//解绑
|
|
var oldResId = "";
|
|
//if(model.type == 2 && !string.IsNullOrWhiteSpace(model.oldsoftusername))
|
|
//有传 旧的软件用户名 则解绑
|
|
if (!string.IsNullOrWhiteSpace(model.oldsoftusername))
|
|
{
|
|
var softUser = soft_user_bl.GetUser_userName(model.oldsoftusername);
|
|
if (softUser != null)
|
|
{
|
|
oldResId = softUser.RESID;
|
|
softUser.RESID = string.Empty;
|
|
soft_user_bl.Update(softUser);
|
|
result = true;
|
|
SOFT_USER_LOG userlog = new SOFT_USER_LOG
|
|
{
|
|
NEWRESID = string.Empty,
|
|
OLDRESID = oldResId,
|
|
CTIME = DateTime.Now,
|
|
TYPE = 1,
|
|
USERNAME = softUser?.USERNAME,
|
|
PKID = new SEQUENCES_BL().Seq_base_get(),
|
|
};
|
|
soft_user_bl.InsertLog(userlog);
|
|
}
|
|
}
|
|
//换绑 绑定
|
|
if (model.type == 1)
|
|
{
|
|
//绑定新的软件名
|
|
var softUser = soft_user_bl.GetUser_userName(model.newsoftusername);
|
|
if (softUser != null && softUser.RESID != resid)
|
|
{
|
|
oldResId = softUser.RESID;
|
|
softUser.RESID = resid;
|
|
soft_user_bl.Update(softUser);
|
|
result = true;
|
|
ContractUser(resid, model.newsoftusername);//关联用户 流水日志
|
|
SOFT_USER_LOG userlog = new SOFT_USER_LOG
|
|
{
|
|
NEWRESID = resid,
|
|
OLDRESID = oldResId,
|
|
CTIME = DateTime.Now,
|
|
TYPE = 2,
|
|
USERNAME = softUser?.USERNAME,
|
|
PKID = new SEQUENCES_BL().Seq_base_get()
|
|
};
|
|
soft_user_bl.InsertLog(userlog);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|