TG.WXCRM.V4/BLL/Csvr/CSVR_TODOITEM_BL.cs

414 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using WX.CRM.BLL.Base;
using WX.CRM.BLL.Util;
using WX.CRM.Common;
using WX.CRM.IBLL.Csvr;
using WX.CRM.Model.Entity;
using WX.CRM.Model.MAP;
using WX.CRM.BLL.Application.UserComBox;
using System.Diagnostics;
namespace WX.CRM.BLL.Csvr
{
public class CSVR_TODOITEM_BL : ICSVR_TODOITEM, ICSVR_TODOITEM_Q
{
private CACHE_BL cache_BL = new CACHE_BL();
#region
public bool Create(ref ValidationErrors errors, WX.CRM.Model.Entity.CSVR_TODOITEM model)
{
try
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
model.PKID = new SEQUENCES_BL().Seq_base_get();
model.CTIME = DateTime.Now;
db.CSVR_TODOITEM.Add(model);
return db.SaveChanges().GetResult();
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
return false;
}
}
#endregion
#region
public bool Update(ref ValidationErrors errors, CSVR_TODOITEM model)
{
try
{
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
{
db.CSVR_TODOITEM.Attach(model);
db.Entry<WX.CRM.Model.Entity.CSVR_TODOITEM>(model).State = EntityState.Modified;
db.SaveChanges();
return true;
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
return false;
}
}
#endregion
public List<WX.CRM.Model.Entity.CSVR_TODOITEM> GetAllToDoItems()
{
using (crmContext db = new crmContext())
{
List<CSVR_TODOITEM> list = db.CSVR_TODOITEM.ToList();
return list;
}
}
public List<CSVR_TODOITEM_extend> GetToDoItemsByParam(ref Pager pager, string resid, string memo, DateTime? ctime, DateTime? Etime, decimal status, QueryUserComboDto usercomboDto, string resourcetag, string activity, int? deptlineid)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var deptList = cache_BL.GetDeptNameMapList();
using (crmContext db = new crmContext())
{
//var list = db.CSVR_TODOITEM.AsQueryable();
var list = from a in db.CSVR_TODOITEM
join c in db.BAS_INNERUSER on a.RECEIVEDUSERID equals c.PKID
select new CSVR_TODOITEM_extend
{
RESID = a.RESID,
DOSTATUS = a.DOSTATUS,
DOREMARK = a.DOREMARK,
DOTIME = a.DOTIME,
DOUSERID = a.DOUSERID,
MEMO = a.MEMO,
PKID = a.PKID,
RECEIVEDUSERID = a.RECEIVEDUSERID,
REMARK = a.REMARK,
RESOURCETAG = a.RESOURCETAG,
SENDEDUSERID = a.SENDEDUSERID,
STARTTIME = a.STARTTIME,
URL = a.URL,
URLTITLE = a.URLTITLE,
DEPTID = c.DEPTID,
EXTXML = a.EXTXML
};
if (!string.IsNullOrEmpty(activity))
{
list = list.Where(m => m.EXTXML!=null && m.EXTXML.Contains(activity));
}
if (deptlineid.HasValue)
{
list = list.Where(m => m.DEPTID == deptlineid);
}
if (!string.IsNullOrEmpty(resid))
{
resid = resid.Trim();
list = list.Where(m => m.RESID == resid);
}
if (!string.IsNullOrEmpty(memo))
{
memo = memo.Trim();
list = list.Where(m => m.MEMO.Contains(memo));
}
if (ctime.HasValue && Etime.HasValue)
{
Etime = Etime.Value.AddDays(1);
list = list.Where(p => p.STARTTIME >= ctime && p.STARTTIME <= Etime);
}
if (status > -1)
{
list = list.Where(p => p.DOSTATUS == status);
}
if (!string.IsNullOrEmpty(resourcetag))
{
if (resourcetag == "softlogin")
{
list = list.Where(p => p.RESOURCETAG == "softlogin");
}
else if (resourcetag == "RFM")
{
list = list.Where(p => p.RESOURCETAG == "RFM");
}
else if (resourcetag == "web")
{
string[] sx = { "softlogin", "RFM" };
list = list.Where(p => p.RESOURCETAG != null && !sx.Contains(p.RESOURCETAG) && !p.RESOURCETAG.StartsWith("UserCenterEvent"));
}
else if (resourcetag == "other")
{
list = list.Where(p => p.RESOURCETAG == null);
}
else
{
list = list.Where(p => p.RESOURCETAG == resourcetag);
}
}
if (usercomboDto.userId.HasValue)
{
decimal _userid = usercomboDto.userId.Value;
list = list.Where(m => m.SENDEDUSERID == _userid || m.RECEIVEDUSERID == _userid);
}
else if (!string.IsNullOrEmpty(usercomboDto.groupIds))
{
decimal[] _groupids = OperationUtil.ConvertToDecimal(usercomboDto.groupIds.Split(','));
var userIds = db.BAS_INNERUSERGROUP.Where(m => _groupids.Contains(m.GID.Value)).Select(m => m.INNERUSERID);
list = list.Where(m => userIds.Contains(m.SENDEDUSERID) || userIds.Contains(m.RECEIVEDUSERID));
}
else if (!string.IsNullOrEmpty(usercomboDto.deptId))
{
var depts = OperationUtil.ConvertToDecimal(usercomboDto.deptId.Split(','));
List<decimal> ALLdeptIDS = new List<decimal>();
List<decimal> ALLgidS = new List<decimal>();
UserServices userservices = new UserServices();
foreach (var item in depts)
{
List<decimal> deptIDS = new List<decimal>();
List<decimal> gidS = new List<decimal>();
userservices.GetAllDeptIDOrGidByDeptId(item, ref deptIDS, ref gidS);//获取所有的组别和gid
ALLdeptIDS.AddRange(deptIDS);
ALLgidS.AddRange(gidS);
}
//var depts = OperationUtil.ConvertToDecimal(usercomboDto.deptId.Split(','));
list = (
from a in list
join u in db.BAS_INNERUSER on a.SENDEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
//join g in db.BAS_INNERGROUP on ug.GID equals g.GID
where ALLdeptIDS.Contains(ug.DEPTID.Value) || ALLgidS.Contains(ug.GID.Value)
select a
)
.Union(
from a in list
join u in db.BAS_INNERUSER on a.RECEIVEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
//join g in db.BAS_INNERGROUP on ug.GID equals g.GID
where ALLdeptIDS.Contains(ug.DEPTID.Value) || ALLgidS.Contains(ug.GID.Value)
select a
);
}
else if (usercomboDto.companyId.HasValue)
{
list = (
from a in list
join u in db.BAS_INNERUSER on a.SENDEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
join g in db.BAS_INNERGROUP on ug.GID equals g.GID
join m in db.BAS_SALESDEPARTMENT on g.SALEDEPTID equals m.SALEDEPTID
where m.COMPANYID == usercomboDto.companyId.Value
select a
)
.Union(
from a in list
join u in db.BAS_INNERUSER on a.RECEIVEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
join g in db.BAS_INNERGROUP on ug.GID equals g.GID
join m in db.BAS_SALESDEPARTMENT on g.SALEDEPTID equals m.SALEDEPTID
where m.COMPANYID == usercomboDto.companyId.Value
select a
);
}
list = list.OrderByDescending(p => p.PKID);
PagerUtil.SetPager<CSVR_TODOITEM_extend>(ref list, ref pager);
var res = list.ToList();
stopwatch.Stop();
LogHelper.Info($"待办事项查询花费{stopwatch.ElapsedMilliseconds}");
foreach (var item in res)
{
item.DEPTNAME = deptList.FirstOrDefault(n => n.id == item.DEPTID)?.title;
}
return res;
}
}
public List<CSVR_TODOITEM_MAP> GetToDoItemsByParamExport(ref Pager pager, string resid, DateTime? ctime, DateTime? Etime, decimal status, QueryUserComboDto usercomboDto, string resourcetag)
{
using (crmContext db = new crmContext())
{
var list = db.CSVR_TODOITEM.AsQueryable();
if (!string.IsNullOrEmpty(resid))
{
resid = resid.Trim();
list = list.Where(m => m.RESID == resid);
}
if (ctime.HasValue && Etime.HasValue)
{
Etime = Etime.Value.AddDays(1);
list = list.Where(p => p.STARTTIME >= ctime && p.STARTTIME <= Etime);
}
if (status > -1)
{
list = list.Where(p => p.DOSTATUS == status);
}
if (!string.IsNullOrEmpty(resourcetag))
{
if (resourcetag == "softlogin")
{
list = list.Where(p => p.RESOURCETAG == "softlogin");
}
else if (resourcetag == "RFM")
{
list = list.Where(p => p.RESOURCETAG == "RFM");
}
else if (resourcetag == "web")
{
list = list.Where(p => p.RESOURCETAG.StartsWith("web_"));
}
else if (resourcetag == "other")
{
list = list.Where(p => p.RESOURCETAG == null);
}
else
{
list = list.Where(p => p.RESOURCETAG == resourcetag);
}
}
if (usercomboDto.userId.HasValue)
{
decimal _userid = usercomboDto.userId.Value;
//list = list.Where(m => m.SENDEDUSERID == _userid || m.RECEIVEDUSERID == _userid);
list = list.Where(m => m.RECEIVEDUSERID == _userid);//只按照接收人来过滤数据,加快速度
}
else if (!string.IsNullOrEmpty(usercomboDto.groupIds))
{
decimal[] _groupids = OperationUtil.ConvertToDecimal(usercomboDto.groupIds.Split(','));
var userIds = db.BAS_INNERUSERGROUP.Where(m => _groupids.Contains(m.GID.Value)).Select(m => m.INNERUSERID);
list = list.Where(m => userIds.Contains(m.SENDEDUSERID) || userIds.Contains(m.RECEIVEDUSERID));
}
else if (!string.IsNullOrEmpty(usercomboDto.deptId))
{
var depts = OperationUtil.ConvertToDecimal(usercomboDto.deptId.Split(','));
list = (
from a in list
join u in db.BAS_INNERUSER on a.SENDEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
join g in db.BAS_INNERGROUP on ug.GID equals g.GID
where depts.Contains(g.SALEDEPTID.Value)
select a
)
.Union(
from a in list
join u in db.BAS_INNERUSER on a.RECEIVEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
join g in db.BAS_INNERGROUP on ug.GID equals g.GID
where depts.Contains(g.SALEDEPTID.Value)
select a
);
}
else if (usercomboDto.companyId.HasValue)
{
list = (
from a in list
join u in db.BAS_INNERUSER on a.SENDEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
join g in db.BAS_INNERGROUP on ug.GID equals g.GID
join m in db.BAS_SALESDEPARTMENT on g.SALEDEPTID equals m.SALEDEPTID
where m.COMPANYID == usercomboDto.companyId.Value
select a
)
.Union(
from a in list
join u in db.BAS_INNERUSER on a.RECEIVEDUSERID equals u.PKID
join ug in db.BAS_INNERUSERGROUP on u.PKID equals ug.INNERUSERID
join g in db.BAS_INNERGROUP on ug.GID equals g.GID
join m in db.BAS_SALESDEPARTMENT on g.SALEDEPTID equals m.SALEDEPTID
where m.COMPANYID == usercomboDto.companyId.Value
select a
);
}
var maplist = from a in list
//join userGroup in db.BAS_INNERUSERGROUP on model.PKID equals userGroup.INNERUSERID into jiontemp
//from userGroup in jiontemp.DefaultIfEmpty()//左关联(用户组/部门 关系表)
join u in db.RES_CUSTOMERDETAIL on a.RESID equals u.RESID into jiontemp
from u in jiontemp.DefaultIfEmpty()//左关联(用户组/部门 关系表)
select new CSVR_TODOITEM_MAP
{
CNAME = u.CNAME,
todoitem = a
};
maplist = maplist.OrderByDescending(p => p.todoitem.CTIME);
//PagerUtil.SetPager<CSVR_TODOITEM>(ref maplist, ref pager);
return maplist.ToList();
}
}
private static bool islock = false;
private static object noticeLock = new object();
public List<CSVR_TODOITEM> GetToDoItemsByDateNow()
{
string cacheKey = "cache_GetToDoItemsByDateNow";
if (CacheHelper.Exists(cacheKey))
return CacheHelper.Get<List<CSVR_TODOITEM>>(cacheKey);
List<CSVR_TODOITEM> res = new List<CSVR_TODOITEM>();
if (islock)
{
//被锁了返回
return res;
}
lock (noticeLock)
{
if (islock)
{
LogHelper.Info($"【GetToDoItemsByDateNow】was innerlocked ");
return res;
}
islock = true;
using (var db = new crmContext())
{
var today = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
res = db.CSVR_TODOITEM.Where(p => p.DOSTATUS == 0 && p.STARTTIME >= today).ToList();
CacheHelper.Set(cacheKey, res, DateTime.Now.AddSeconds(120), true);
//LogHelper.Info("load:30");
}
islock = false;
return res;
}
}
public List<CSVR_TODOITEM> GetToDoItemSoftLogin()
{
//using (crmContext db = new crmContext())
//{
// var today = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
// return db.CSVR_TODOITEM.Where(p => p.DOSTATUS == 0 && p.STARTTIME >= today && p.RESOURCETAG == "softlogin").ToList();
//}
var list = GetToDoItemsByDateNow();
return list.Where(p => p.RESOURCETAG == "softlogin").ToList();
}
public List<CSVR_TODOITEM> GetToDoItemRFM()
{
//using (crmContext db = new crmContext())
//{
// var today = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
// return db.CSVR_TODOITEM.Where(p => p.DOSTATUS == 0 && p.STARTTIME >= today && p.RESOURCETAG == "RFM").ToList();
//}
var list = GetToDoItemsByDateNow();
return list.Where(p => p.RESOURCETAG == "RFM").ToList();
}
public CSVR_TODOITEM GetToDoItemById(decimal id)
{
using (crmContext db = new crmContext())
{
CSVR_TODOITEM list = db.CSVR_TODOITEM.FirstOrDefault(p => p.PKID == id);
return list;
}
}
}
}