TG.WXCRM.V4/BLL/Wx/WX_USERPERCENT_BL.cs

359 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WX.CRM.BLL.Util;
using WX.CRM.Common;
using WX.CRM.DAL.Wx;
using WX.CRM.IBLL.Wx;
using WX.CRM.Model.Entity;
using WX.CRM.Model.MAP;
namespace WX.CRM.BLL.Wx
{
public class WX_USERPERCENT_BL : DbContextRepository<WX_USERPERCENT>, IWX_USERPERCENT
{
public List<WX_USERPERCENT_Extend> GetList(ref Pager page, decimal[] groupIds)
{
using (var db = new crmContext())
{
var innerUserGroupData = db.BAS_INNERUSERGROUP.AsQueryable();
if (groupIds != null && groupIds.Length > 0)
{
innerUserGroupData = innerUserGroupData.Where(m => groupIds.Contains(m.GID.Value));
}
var returnData = (from a in innerUserGroupData
join b in db.BAS_INNERUSER on a.INNERUSERID equals b.PKID into jiontemp
from b in jiontemp.DefaultIfEmpty()
join c in db.BAS_INNERGROUP on a.GID equals c.GID into jiontemp2
from c in jiontemp2.DefaultIfEmpty()
join d in db.WX_USERPERCENT on b.EID equals d.EID into jiontemp3
from d in jiontemp3.DefaultIfEmpty()
select new WX_USERPERCENT_Extend()
{
EID = b.EID,
GNAME = c.GNAME,
GID = a.GID,
DIV_PERCENT = d.DIV_PERCENT,
UNAME = b.UNAME,
TRUENAME = b.TRUENAME
});
returnData = returnData.OrderBy(m => m.DIV_PERCENT).OrderBy(m => m.GID);
PagerUtil.SetPager<WX_USERPERCENT_Extend>(ref returnData, ref page);
return returnData.ToList();
}
}
public List<WX_USERPERCENT_Extend> GetList(decimal[] groupIds)
{
using (var db = new crmContext())
{
var innerUserGroupData = db.BAS_INNERUSERGROUP.AsQueryable();
if (groupIds != null && groupIds.Length > 0)
{
innerUserGroupData = innerUserGroupData.Where(m => groupIds.Contains(m.GID.Value));
}
var returnData = (from a in innerUserGroupData
join b in db.BAS_INNERUSER on a.INNERUSERID equals b.PKID into jiontemp
from b in jiontemp.DefaultIfEmpty()
join c in db.BAS_INNERGROUP on a.GID equals c.GID into jiontemp2
from c in jiontemp2.DefaultIfEmpty()
join d in db.WX_USERPERCENT on b.EID equals d.EID into jiontemp3
from d in jiontemp3.DefaultIfEmpty()
where c.ISSALEDEPT == 1
select new WX_USERPERCENT_Extend()
{
EID = b.EID,
GNAME = c.GNAME,
GID = a.GID.Value,
DIV_PERCENT = d.DIV_PERCENT,
UNAME = b.UNAME,
TRUENAME = b.TRUENAME,
ISDISMISS = b.ISDISMISS
});
returnData = returnData.OrderBy(m => m.GID);
return returnData.ToList();
}
}
public WX_USERPERCENT_Extend GetWxUserPercentExtend(int eid)
{
throw new NotImplementedException();
}
//public int UpdateWxUserPercent(WX_USERPERCENT model)
//{
// var flag = 0;
// if (Exists(m => m.GID == model.GID))
// {
// if (Update(model))
// flag = (int)WxUpdatePercentFlag.Success;
// }
// else
// {
// flag = Add(model);
// }
// var groupSumPercent = GetList(m => m.GID == model.GID).Sum(m => m.DIV_PERCENT);
// if (groupSumPercent > 100)
// {
// flag = (int)WxUpdatePercentFlag.Overflow;
// }
// return flag;
//}
public int UpdateWxUserPercent(ref ValidationErrors errors, string percents, string gids, string eids)
{
try
{
string[] gidArr = gids.Split(',');
string[] percentArr = percents.Split(',');
string[] eidArr = eids.Split(',');
if (string.IsNullOrWhiteSpace(eids) || string.IsNullOrWhiteSpace(gids) || string.IsNullOrWhiteSpace(percents))
{
return (int)WxUpdatePercentFlag.ParamsError;
}
var distinctGidList = gidArr.Distinct().ToList(); //去重,组ID
Dictionary<string, List<string>> gidToPercentDic = new Dictionary<string, List<string>>(); //组对应的比例
Dictionary<string, List<string>> gidToEidDic = new Dictionary<string, List<string>>(); //组对应的eid
//按组ID进行分类,针对一个用户针对多个组的情况对每个组的所有人的比例和为100%进行限定
foreach (var m in distinctGidList)
{
List<string> percentList = new List<string>();
List<string> eidList = new List<string>();
for (int i = 0; i < eidArr.Length; i++)
{
string eid = eidArr[i];
string percenet = percentArr[i];
string gid = gidArr[i];
if (gid == m)
{
percentList.Add(percenet);
eidList.Add(eid);
}
}
gidToEidDic.Add(m, eidList);
gidToPercentDic.Add(m, percentList);
}
using (var db = new crmContext())
{
foreach (var g in distinctGidList)
{
var eidDicList = gidToEidDic[g];
var percentDicList = gidToPercentDic[g];
var s = percentDicList.Select(m => Convert.ToDecimal(m)).Sum();
if (s != 0)
{
return (int)WxUpdatePercentFlag.NotEqualZero;
}
////先统计出当前gid下面的所有不用修改eid的比例之和
//var excepListSumPercent = GetList().Where(m => (!eidDicList.Contains(m.EID.ToString()) && m.GID.ToString() == g)).Sum(m => m.DIV_PERCENT);
////统计出当前gid下面的需要修改的eid用户的比例之和
//var percentSum = gidToPercentDic[g].Select(m => decimal.Parse(m)).Sum();
//if (excepListSumPercent + percentSum > 100)
//{
// return (int)WxUpdatePercentFlag.Overflow;
//}
//if (excepListSumPercent + percentSum < 100)
//{
// return (int)WxUpdatePercentFlag.Less;
//}
var gid = Convert.ToDecimal(g);
for (int i = 0; i < eidDicList.Count; i++)
{
decimal eid = Convert.ToDecimal(eidDicList[i]);
decimal percent = Convert.ToDecimal(percentDicList[i]);
if (Exists(m => m.EID == eid))
{
Update(new WX_USERPERCENT() { EID = eid, GID = gid, DIV_PERCENT = percent });
}
else
{
Add(new WX_USERPERCENT() { EID = eid, GID = gid, DIV_PERCENT = percent });
}
}
}
return (int)WxUpdatePercentFlag.Success;
}
}
catch (Exception ex) { errors.Add(ex.Message); return (int)WxUpdatePercentFlag.Fail; }
}
public int UserAverageAllot(ref ValidationErrors errors, decimal[] groupIds)
{
try
{
Dictionary<decimal, List<decimal>> gidToEidDic = new Dictionary<decimal, List<decimal>>(); //组对应的eid
var userList = this.GetList(groupIds);
if (userList != null && userList.Count > 0)
{
var gidList = from dbinfo in userList
group dbinfo by dbinfo.GID into list
select new
{
list.First().GID,
};
foreach (var m in gidList.ToList())
{
List<decimal> eidList = new List<decimal>();
for (int i = 0; i < userList.Count; i++)
{
decimal eid = userList[i].EID;
decimal? gid = userList[i].GID;
if (gid.Value == m.GID)
{
eidList.Add(eid);
}
}
gidToEidDic.Add(m.GID.Value, eidList);
}
foreach (var g in gidToEidDic.Keys)
{
var eidList = gidToEidDic[g];
var count = eidList.Count;
if (count > 0)
{
var d = 100 % count; //取余数
decimal percent = 0;
if (d == 0)
{
percent = 100 / count;
}
else
{
percent = Convert.ToDecimal(Math.Round((float)100 * 1.0 / (float)count, 2));
}
for (int i = 0; i < count; i++)
{
var eid = eidList[i];
if (i == count - 1)
{
if (d != 0)
{
percent = 100 - percent * (count - 1);
}
}
if (Exists(m => m.EID == eid))
{
Update(new WX_USERPERCENT() { EID = eid, GID = g, DIV_PERCENT = percent });
}
else
{
Add(new WX_USERPERCENT() { EID = eid, GID = g, DIV_PERCENT = percent });
}
}
}
}
}
return (int)WxUpdatePercentFlag.Success;
}
catch (Exception ex)
{
errors.Add(ex.Message); return (int)WxUpdatePercentFlag.Fail;
}
}
/// <summary>
/// 查询出改变了的 数据,进行修改
/// </summary>
/// <returns></returns>
public List<WX_USERPERCENT_Extend2> GetUserPercentToPush(decimal[] gids)
{
using (crmContext db = new crmContext())
{
if (gids != null && gids.Count() > 0)
{
var userPercent = from a in db.WX_USERPERCENT
join b in db.BAS_INNERUSER on a.EID equals b.EID
where gids.Contains(a.GID)
select new WX_USERPERCENT_Extend2()
{
DIV_PERCENT = a.DIV_PERCENT,
EID = a.EID,
GID = a.GID,
UNAME = b.UNAME,
UserId = b.PKID
};
return userPercent.ToList();
}
else
{
var userPercent = from a in db.WX_USERPERCENT
join b in db.BAS_INNERUSER on a.EID equals b.EID
select new WX_USERPERCENT_Extend2()
{
DIV_PERCENT = a.DIV_PERCENT,
EID = a.EID,
GID = a.GID,
UNAME = b.UNAME,
UserId = b.PKID
};
return userPercent.ToList();
}
}
}
/// <summary>
/// 查询出改变了的 数据,进行修改
/// </summary>
/// <returns></returns>
public List<WX_USERPERCENT_Extend2> GetUserPercentByEidsToPush(string eids)
{
using (crmContext db = new crmContext())
{
List<decimal> neids = new List<decimal>();
foreach (string item in eids.Split(','))
{
neids.Add(Convert.ToDecimal(item));
}
decimal[] eidDecimal = neids.ToArray();
var userPercent = from a in db.WX_USERPERCENT
join b in db.BAS_INNERUSER on a.EID equals b.EID
where eidDecimal.Contains(a.EID)
select new WX_USERPERCENT_Extend2()
{
DIV_PERCENT = a.DIV_PERCENT,
EID = a.EID,
GID = a.GID,
UNAME = b.UNAME,
UserId = b.PKID
};
return userPercent.ToList();
}
}
/// <summary>
/// 读取推送消息
/// </summary>
/// <returns></returns>
public DataTable GetPushData(decimal pici)
{
return new WX_USERPERCENT_DAL().GetPushData(pici);
}
}
}