TG.WXCRM.V4/BLL/WeWork/WxResource_BL.cs

132 lines
5.3 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WX.CRM.BLL.Base;
using WX.CRM.BLL.Util;
using WX.CRM.Common;
using WX.CRM.Common.StockHelper;
using WX.CRM.IBLL.MsgTool;
using WX.CRM.IBLL.WeWork;
using WX.CRM.Model.Entity;
using WX.CRM.Model.Enum;
namespace WX.CRM.BLL.WeWork
{
public class WxResource_BL : IWxResource
{
private CACHE_BL _cache = new CACHE_BL();
private BAS_BUSSINESSLINE_BL line_bl = new BAS_BUSSINESSLINE_BL();
public WxResource_BL()
{
}
public List<UserGroupModel> GetGroupList(decimal userid)
{
//获取事业线
var busineeLine = line_bl.GetBusinessLineByRoot(userid);
List<UserGroupModel> result = new List<UserGroupModel>();
var url = _cache.GetValue_Parameter(Parameter.ZXD_CORE_WEBAPI);
//url = "http://localhost:5244";
url = $"{url}/Api/WeWorkResource/UserGroupList";
var para = $"deptid={string.Join(",", busineeLine.Select(n => n.BUSINESSID))}";
Dictionary<string, string> header = new Dictionary<string, string>();
var appid = System.Configuration.ConfigurationManager.AppSettings["appid"];
header.Add("appid", appid);
var res = Utility.GetData(url, para, header, Encoding.UTF8, 5000);
var reqInfo = JsonConvert.DeserializeObject<ApiResult<List<UserGroupDto>>>(res);
if (reqInfo.Code == 0)
{
foreach (var item in reqInfo.Data)
{
var nameTxt = item.Name;
if (!string.IsNullOrWhiteSpace(item.catekeyName))
{
nameTxt = $"({item.catekeyName}) {item.Name}";
}
result.Add(new UserGroupModel
{
id = item.Id.ToString(),
text = nameTxt,
description = item.Description,
UserCount = item.UserCount,
CustomerCount = item.CustomerCount,
deptId = item.deptId
});
}
return result;
}
else
{
throw new Exception(reqInfo.Message);
}
}
public bool SubmitResourceTask(ResourceConfigCreateDto model)
{
using (var db = new crmContext())
{
var user = db.BAS_INNERUSER.FirstOrDefault(n => n.EID == model.eid);
var busineeLine = line_bl.GetBusinessLineByRoot(user.PKID);
var url = _cache.GetValue_Parameter(Parameter.ZXD_CORE_WEBAPI);
var fromUser = JsonConvert.DeserializeObject<List<FromUser>>(model.fromUser);
var toUser = JsonConvert.DeserializeObject<List<ToUser>>(model.toUser);
if (string.IsNullOrWhiteSpace(model.appid))
{
throw new Exception("请选择企微和人群包");
}
if (model.total <= 0)
{
throw new Exception("没有可分配的数量");
}
if (model.total < toUser.Sum(n => n.flowCount))
{
throw new Exception("总分配人数不能大于可分配资源数");
}
if (toUser.Count == 0 || toUser.Sum(n => n.flowCount) <= 0)
{
throw new Exception("请选择接替成员和填写分配数量");
}
var containUser = fromUser.Where(m => toUser.Select(n => n.userid).Contains(m.userid)).ToList();
if (containUser.Count > 0)
{
throw new Exception($"存在相同的转移成员与接替成员【{string.Join(",", containUser.Select(n => n.userid))}】");
}
if (model.deptId <= 0 && busineeLine.Count > 0)
{
model.deptId = Convert.ToInt32(busineeLine.FirstOrDefault().BUSINESSID);
}
//构建对象
ResourceConfigPostDto postdata = new ResourceConfigPostDto
{
appid = model.appid,
gname = model.gname,
groupid = model.groupid,
eid = model.eid,
ename = user?.UNAME,
flowCount = toUser.Sum(n => n.flowCount),
deptId = model.deptId,
FromUser = fromUser,
ToUser = toUser
};
url = $"{url}/Api/WeWorkResource/SubmitSourceTask";
//url = "http://localhost:5244/Api/WeWorkResource/SubmitSourceTask";
var para = postdata.ToJson();
Dictionary<string, string> header = new Dictionary<string, string>();
var res = Utility.PostAjaxData(url, para, header, Encoding.UTF8, 5000);
var reqInfo = JsonConvert.DeserializeObject<ApiResult>(res);
if (reqInfo.Code == 0)
{
return true;
}
else
{
throw new Exception(reqInfo.Message);
}
}
}
}
}