ComplianceServer/oldcode/BLL/Csvr/CSVR_TODOITEM_BL.cs

360 lines
16 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;
namespace WX.CRM.BLL.Csvr
{
public class CSVR_TODOITEM_BL : ICSVR_TODOITEM, ICSVR_TODOITEM_Q
{
#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> GetToDoItemsByParam(ref Pager pager, string resid, string memo, 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 (!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")
{
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);
}
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.CTIME);
PagerUtil.SetPager<CSVR_TODOITEM>(ref list, ref pager);
return list.ToList();
}
}
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);
}
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();
}
}
public List<CSVR_TODOITEM> GetToDoItemsByDateNow()
{
//using (crmContext db = new crmContext())
//{
// var today = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
// var list = db.CSVR_TODOITEM.Where(p => p.DOSTATUS == 0 && p.STARTTIME >= today && p.RESOURCETAG != "softlogin");
// return list.Where(p => p.RESOURCETAG != "RFM").ToList();
//}
string cacheKey = "cache_GetToDoItemsByDateNow";
if (CacheHelper.Exists(cacheKey))
return CacheHelper.Get<List<CSVR_TODOITEM>>(cacheKey);
using (var db = new crmContext())
{
var today = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
var list = db.CSVR_TODOITEM.Where(p => p.DOSTATUS == 0 && p.STARTTIME >= today).ToList();
CacheHelper.Set(cacheKey, list, DateTime.Now.AddSeconds(30), true);
//LogHelper.Info("load:30");
return list;
}
}
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;
}
}
}
}