TG.WXCRM.V4/CRMServices/Res_AlloCate/WwHhuserAct.cs

242 lines
9.0 KiB
C#

using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.CRM.BLL.Base;
using WX.CRM.BLL.Util;
using WX.CRM.BLL.Ww;
using WX.CRM.Common;
using WX.CRM.Model.Entity;
namespace WX.CRM.CRMServices.Res_AlloCate
{
public class WwHhuserAct
{
private string url = "http://post.hc.dn8188.com/Rights/WeworkUser2Eid.html";
private string changeurl = "http://post.hc.dn8188.com/Rights/ChangeWeworkUser2Eid.html";
private WW_HHUSER_EID_ACT_BL _wwHhuserAct = new WW_HHUSER_EID_ACT_BL();
private SecurityHelper sHelper = new SecurityHelper();
private BAS_INNERUSER_BL user_BL = new BAS_INNERUSER_BL();
public void Start()
{
try
{
BAS_BUSSINESSLINE_BL _bussinessLine = new BAS_BUSSINESSLINE_BL();
CACHE_BL _cacheBL = new CACHE_BL();
var deptcode = Utility.GetSettingOrNullByKey("DataClientCode");
var channel = Utility.GetSettingOrNullByKey("Channel");
var acturl = Utility.GetSettingOrNullByKey("UserActUrl");
if (!string.IsNullOrWhiteSpace(acturl))
{
url = acturl;
}
var ch = 0;
if (!string.IsNullOrEmpty(channel))
{
ch = int.Parse(channel);
}
var list = _wwHhuserAct.GetList(p => p.ISEXE == 0 && p.ACT != "changeEid").OrderBy(p => p.CTIME);
if (list.Count() == 0)
{
return;
}
var time = (long)Utility.ConvertDateTimeInt(DateTime.Now);
var message = JsonConvert.SerializeObject(new { account = "dn.crm", time });
var para = sHelper.createSignEncodingStr(message);
var urlUp = url + "?" + para;
List<BAS_INNERUSER> allUser = new List<BAS_INNERUSER>();
Dictionary<decimal, int> res = new Dictionary<decimal, int>();
if (list.Count() > 0)
{
var userList = user_BL.GetAllList();//_cacheBL.GetUserList();
allUser = userList.Where(n => list.Select(x => x.EID).Contains(n.EID)).ToList();
res = _bussinessLine.GetChannelByUser(allUser.Select(n => n.PKID).ToList());
}
var listUp = new List<WeworkUser2Eid>();
foreach (var item in list)
{
try
{
var userId = allUser.FirstOrDefault(n => n.EID == item.EID) == null? 0 : allUser.FirstOrDefault(n => n.EID == item.EID).PKID;
var userChannel = ch;
if (res.ContainsKey(userId))
{
userChannel = res[userId];
}
listUp.Add(new WeworkUser2Eid(item.APPID, item.USERID, Convert.ToInt32(item.EID), 1, deptcode, userChannel, item.ASSIGNSTATUS.HasValue ? Convert.ToInt32(item.ASSIGNSTATUS.Value) : 0));
}
catch (Exception ex)
{
LogHelper.Error($"【{item.APPID}】【{item.USERID}】构建数据失败:{ex.Message}");
}
}
LogHelper.Info($"ActUrl 提交实体为{JsonConvert.SerializeObject(listUp)}");
var rsp = Utility.HttpPostData(urlUp, JsonConvert.SerializeObject(listUp), Encoding.UTF8);
var ret = JsonConvert.DeserializeAnonymousType(rsp, new { errcode = -1, errmsg = string.Empty });
if (ret.errcode == 0)
{
foreach (var item in list)
{
try
{
item.ISEXE = 1;
_wwHhuserAct.Update(item);
}
catch (Exception ex)
{
LogHelper.Error("更新上传关系记录失败:" + item.ToJson() + "---" + ex.ToString());
}
}
}
}
catch (Exception ex)
{
LogHelper.Error(ex);
}
}
public void BuildAction()
{
using (var db = new crmContext())
{
var eidList = db.WW_HHUSER_EID.ToList();
foreach (var item in eidList)
{
WW_HHUSER_EID_ACT model = new WW_HHUSER_EID_ACT
{
ID = new SEQUENCES_BL().Seq_base_get(Model.Enum.PKIDType.LargeTable),
APPID = item.CORPID,
USERID = item.USERID,
EID = item.EID == null ? 0 : item.EID.Value,
ASSIGNSTATUS = item.ASSIGNSTATUS,
CTIME = Convert.ToDateTime("2023-04-17"),
ACT = "addOrupdate",
ISEXE = 0,
};
db.WW_HHUSER_EID_ACT.Add(model);
db.SaveChanges();
}
}
}
public void ChangeEidStart()
{
try
{
var deptcode = Utility.GetSettingOrNullByKey("DataClientCode");
var channel = Utility.GetSettingOrNullByKey("Channel");
var changeEidUrl = Utility.GetSettingOrNullByKey("ChangeEidActUrl");
if (!string.IsNullOrWhiteSpace(changeEidUrl))
{
changeurl = changeEidUrl;
}
var ch = 0;
if (!string.IsNullOrEmpty(channel))
{
ch = int.Parse(channel);
}
var list = _wwHhuserAct.GetList(p => p.ISEXE == 0 && p.ACT == "changeEid").OrderBy(p => p.CTIME).ToList();
if (list.Count() == 0)
{
return;
}
var time = (long)Utility.ConvertDateTimeInt(DateTime.Now);
var message = JsonConvert.SerializeObject(new { account = "dn.crm", time });
var para = sHelper.createSignEncodingStr(message);
var urlUp = changeurl + "?" + para;
var listUp = new List<WeworkUser2Eid>();
foreach (var item in list)
{
listUp.Add(new WeworkUser2Eid(item.APPID, item.USERID, Convert.ToInt32(item.EID), 1, deptcode, ch, item.ASSIGNSTATUS.HasValue ? Convert.ToInt32(item.ASSIGNSTATUS.Value) : 0));
}
var rsp = Utility.HttpPostData(urlUp, JsonConvert.SerializeObject(listUp), Encoding.UTF8);
var ret = JsonConvert.DeserializeAnonymousType(rsp, new { errcode = -1, errmsg = string.Empty });
if (ret.errcode == 0)
{
foreach (var item in list)
{
try
{
item.ISEXE = 1;
_wwHhuserAct.Update(item);
}
catch (Exception ex)
{
LogHelper.Error("更新上传关系记录失败:" + item.ToJson() + "---" + ex.ToString());
}
}
}
}
catch (Exception ex)
{
LogHelper.Error(ex);
}
}
}
public class WeworkUser2Eid
{
public WeworkUser2Eid()
{
}
public WeworkUser2Eid(string appid, string userid, int eid, int status, string deptcode, int ch, int assignstatus)
{
this.appid = appid;
this.userid = userid;
this.eid = eid;
this.status = status;
this.deptcode = deptcode;
this.ch = ch;
this.assignstatus = assignstatus;
}
public string appid { get; set; }
public string userid { get; set; }
public int eid { get; set; }
public int status { get; set; }
public string deptcode { get; set; }
public int ch { get; set; }
public int assignstatus { get; set; }
}
public class WwHhuserActJob : IJob
{
private static bool _isNotice = false;
public void Execute(JobExecutionContext context)
{
if (_isNotice)
return;
_isNotice = true;
try
{
var actBl = new WwHhuserAct();
//同步推广状态
actBl.Start();
//修改工号
actBl.ChangeEidStart();
}
catch (Exception e)
{
WX.CRM.Common.LogHelper.Error("WX.CRM.CRMServices.Res_AlloCate.WwHhuserActJob.WwHhuserAct().Execute:" + e);
}
finally
{
_isNotice = false;
}
}
}
}