211 lines
8.4 KiB
C#
211 lines
8.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
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_GROUPPERCENT_BL : DbContextRepository<WX_GROUPPERCENT>, IWX_GROUPPERCENT
|
||
{
|
||
WX_WORKACCOUNT_DAL dal = new WX_WORKACCOUNT_DAL();
|
||
public WX_GROUPPERCENT_Extend GetGroupPercentExtend(int gid)
|
||
{
|
||
using (var db = new crmContext())
|
||
{
|
||
var groupPercentData = db.WX_GROUPPERCENT.AsQueryable();
|
||
var returnData = (from a in groupPercentData
|
||
join b in db.BAS_INNERGROUP on a.GID equals b.GID
|
||
select new WX_GROUPPERCENT_Extend()
|
||
{
|
||
GID = a.GID,
|
||
GNAME = b.GNAME,
|
||
DIV_PERCENT = a.DIV_PERCENT
|
||
});
|
||
return returnData.FirstOrDefault();
|
||
}
|
||
}
|
||
|
||
public List<WX_GROUPPERCENT_Extend> GetGroupPercentList()
|
||
{
|
||
using (var db = new crmContext())
|
||
{
|
||
var innerGroupData = db.BAS_INNERGROUP.AsQueryable();
|
||
//var innerDepartmentData = db.BAS_INNERDEPARTMENT.Where(m => m.INNERDEPTCODE == "KFZX").AsQueryable();
|
||
var returnData = (from a in innerGroupData
|
||
join b in db.BAS_INNERDEPARTMENT on a.DEPTID equals b.DEPTID
|
||
join c in db.WX_GROUPPERCENT on a.GID equals c.GID into jiontemp
|
||
from c in jiontemp.DefaultIfEmpty()
|
||
where a.ISSALEDEPT == 1
|
||
select new WX_GROUPPERCENT_Extend()
|
||
{
|
||
GNAME = a.GNAME,
|
||
GID = a.GID,
|
||
DIV_PERCENT = c.DIV_PERCENT
|
||
|
||
});
|
||
returnData = returnData.OrderBy(m => m.DIV_PERCENT);
|
||
return returnData.ToList();
|
||
|
||
}
|
||
}
|
||
|
||
public int UpdateWxGroupPercent(ref ValidationErrors errors, string percents, string gids)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(percents) || string.IsNullOrWhiteSpace(gids))
|
||
{
|
||
return (int)WxUpdatePercentFlag.ParamsError;
|
||
}
|
||
string[] gidArr = gids.Split(',');
|
||
string[] percentArr = percents.Split(',');
|
||
using (var db = new crmContext())
|
||
{
|
||
//var excepListSumPercent = GetList().Where(m => !gidArr.Contains(m.GID.ToString())).Sum(m => m.DIV_PERCENT);
|
||
//GetList().All(m => { LogHelper.Info(m.GID+","+ m.DIV_PERCENT.ToString()); return true; });
|
||
//var percentSum = percentArr.Select(m => decimal.Parse(m)).Sum();
|
||
//if (excepListSumPercent + percentSum > 100)
|
||
//{
|
||
// return (int)WxUpdatePercentFlag.Overflow;
|
||
//}
|
||
//if (excepListSumPercent + percentSum < 100)
|
||
//{
|
||
// return (int)WxUpdatePercentFlag.Less;
|
||
//}
|
||
var s = percentArr.Select(m => Convert.ToDecimal(m)).Sum();
|
||
if (s != 0)
|
||
{
|
||
return (int)WxUpdatePercentFlag.NotEqualZero;
|
||
}
|
||
|
||
for (int i = 0; i < gidArr.Length; i++)
|
||
{
|
||
decimal gid = Convert.ToDecimal(gidArr[i]);
|
||
decimal percent = Convert.ToDecimal(percentArr[i]);
|
||
if (Exists(m => m.GID == gid))
|
||
{
|
||
Update(new WX_GROUPPERCENT() { GID = gid, DIV_PERCENT = percent });
|
||
}
|
||
else
|
||
{
|
||
Add(new WX_GROUPPERCENT() { GID = gid, DIV_PERCENT = percent });
|
||
}
|
||
}
|
||
|
||
return (int)WxUpdatePercentFlag.Success;
|
||
}
|
||
}
|
||
catch (Exception ex) { errors.Add(ex.Message); return (int)WxUpdatePercentFlag.Fail; }
|
||
}
|
||
|
||
|
||
public int GroupAverageAllot(ref ValidationErrors errors)
|
||
{
|
||
try
|
||
{
|
||
var groupList = this.GetGroupPercentList();
|
||
if (groupList != null && groupList.Count > 0)
|
||
{
|
||
var count = groupList.Count;
|
||
var d = 100 % count; //取余数
|
||
decimal percnet = 0;
|
||
if (d == 0)
|
||
{
|
||
percnet = 100 / count;
|
||
}
|
||
else
|
||
{
|
||
percnet = Convert.ToDecimal(Math.Round((float)100 * 1.0 / (float)count, 2));
|
||
}
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
var gid = groupList[i].GID;
|
||
if (i == count - 1)
|
||
{
|
||
if (d != 0)
|
||
{
|
||
percnet = 100 - percnet * (count - 1);
|
||
}
|
||
}
|
||
if (Exists(m => m.GID == gid))
|
||
{
|
||
Update(new WX_GROUPPERCENT() { GID = gid, DIV_PERCENT = percnet });
|
||
}
|
||
else
|
||
{
|
||
Add(new WX_GROUPPERCENT() { GID = gid, DIV_PERCENT = percnet });
|
||
}
|
||
}
|
||
}
|
||
|
||
return (int)WxUpdatePercentFlag.Success;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
errors.Add(ex.Message); return (int)WxUpdatePercentFlag.Fail;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 获取已经设置好值的比率(没有gids查询全部)
|
||
/// </summary>
|
||
/// <param name="gids">修改了的组ID</param>
|
||
/// <returns></returns>
|
||
public List<WX_GROUPPERCENT_Extend> GetGroupPercentListJoinGroup(string gids)
|
||
{
|
||
using (crmContext db = new crmContext())
|
||
{
|
||
if (!string.IsNullOrEmpty(gids))
|
||
{
|
||
string[] ngids = gids.Split(',');
|
||
List<decimal> needGetGid = new List<decimal>();
|
||
foreach (string item in ngids)
|
||
{
|
||
if (!string.IsNullOrEmpty(item))
|
||
{
|
||
needGetGid.Add(Convert.ToDecimal(item));
|
||
}
|
||
}
|
||
decimal[] gidDec = needGetGid.ToArray();
|
||
var x = from a in db.WX_GROUPPERCENT
|
||
join b in db.BAS_INNERGROUP on a.GID equals b.GID
|
||
where gidDec.Contains(b.GID)
|
||
select new WX_GROUPPERCENT_Extend()
|
||
{
|
||
DIV_PERCENT = a.DIV_PERCENT,
|
||
GID = a.GID,
|
||
GNAME = b.GNAME
|
||
};
|
||
return x.ToList();
|
||
}
|
||
else
|
||
{
|
||
var x = from a in db.WX_GROUPPERCENT
|
||
join b in db.BAS_INNERGROUP on a.GID equals b.GID
|
||
select new WX_GROUPPERCENT_Extend()
|
||
{
|
||
DIV_PERCENT = a.DIV_PERCENT,
|
||
GID = a.GID,
|
||
GNAME = b.GNAME
|
||
};
|
||
return x.ToList();
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
public List<WX_GroupEmployeeNum> GetSaleGroupEmplyeeNum()
|
||
{
|
||
var ds = dal.GetInnerUserNumByGroup();
|
||
if (ds != null && ds.Tables[0].Rows.Count > 0)
|
||
{
|
||
return ds.Tables[0].ToList<WX_GroupEmployeeNum>();
|
||
}
|
||
return new List<WX_GroupEmployeeNum>();
|
||
}
|
||
}
|
||
}
|