ComplianceServer/oldcode/CRMServices/WeiXin/AutoBound.cs

246 lines
11 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 CRM.Core.DTO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WX.CRM.BLL.Base;
using WX.CRM.BLL.Res;
using WX.CRM.BLL.Ww;
using WX.CRM.BLL.Wx;
using WX.CRM.Common;
using WX.CRM.DAL;
using WX.CRM.IBLL.Res;
using WX.CRM.IBLL.Ww;
using WX.CRM.IBLL.Wx;
using WX.CRM.Model.Entity;
namespace WX.CRM.CRMServices.WeiXin
{
/// <summary>
/// 自动绑定
/// </summary>
public static class AutoBound
{
//更新参数
//工作内容
//get wework.ww_extuser json
//手机号转 resid
//insert Orcl_TG.ww_extuser_resid
//update Orcl_TG.res_customerdetail isbound
private static BAS_PARAMETER_BL paramter_bl = new BAS_PARAMETER_BL();
private static IWX_SZZYORDER _wxSzzyorder = new WX_SZZYORDER_BL();
private static IWw_huser _iww_hhuser_service = new Ww_huser_BL();
private static readonly IRES_CUSTOMER res_customer = new RES_CUSTOMER_BL();
public static void Bound()
{
var syskey = paramter_bl.GetModel_Patameter("WW_Extuser_MAXUPDATE");//获取系统上次同步最后更新时间
DateTime preLastUpdate = new DateTime();
DateTime preCtime = new DateTime();
//配置保存 ctimelastupdate
var lastUpdatekey = syskey.PARAVALUE.Split(';')[0];
var ctimekey = syskey.PARAVALUE.Split(';')[1];
var hasCtime = DateTime.TryParse(ctimekey, out preCtime);//上次获取是否 lastupdate 没变
if (DateTime.TryParse(lastUpdatekey, out preLastUpdate))
{
List<DateTime> lastTimeList = new List<DateTime>();
List<DateTime> cTimeList = new List<DateTime>();
var companycode = Utility.GetSettingOrNullByKey("DataClientCode");
Dictionary<string, List<int>> dicDept = _iww_hhuser_service.GetAllCorpDept(companycode);
List<ww_extuser_pares_dept> logs = new List<ww_extuser_pares_dept>();
MySqlDbHelper mySqlhelper = new MySqlDbHelper("MysqlQWConn");
DateTime TODate = DateTime.Now.Date.AddDays(1);
string sql = $@"select * from ww_extuser
where lastupdate >='{preLastUpdate}'
and lastupdate < '{TODate.ToString("yyyy-MM-dd")}'
{(hasCtime ? string.Format("and ctime >='{0}'", preCtime) : "")}
order by lastupdate asc,ctime asc limit 1000";//=必要:保证 一个客户多个用户电话信息在插入时其中一条插入失败,下次能被获取到
DataSet table = mySqlhelper.ExecuteDataTable(sql);//获取外部联系人数据
//处理外部联系人关系数据
foreach (DataRow row in table.Tables[0].Rows)
{
var exinfo = row["exinfo"].ToString();
var corpid = row["corpid"].ToString();
lastTimeList.Add(Convert.ToDateTime(row["lastupdate"]));//记录每条数据的时间
cTimeList.Add(Convert.ToDateTime(row["ctime"]));//记录每条数据的时间
List<string> remark_mobiles = new List<string>();
if (exinfo != "-" && !string.IsNullOrEmpty(exinfo))
{
var model = JsonConvert.DeserializeObject<WwExtUserView>(exinfo);
var unionid = model.external_contact.unionid;
var external_userid = model.external_contact.external_userid;
foreach (var user in model.follow_user)
{
foreach (var mobile in user.remark_mobiles)//遍历json电话
{
try
{
if (string.IsNullOrEmpty(mobile) || mobile.Trim().Length < 8) continue;
string mobileM = Utility.GetMobile(mobile.Trim());//提取号码
if (string.IsNullOrEmpty(mobileM))
continue;
//IDictionary<string, object> r = new Dictionary<string, object>();
//WW_EXTUSER_PARES wep = new WW_EXTUSER_PARES();
//wep.RESID = Common.ResUtil.CreateResId(mobileM);
//wep.EXTERNAL_USERID = external_userid;
//wep.USERID = user.userid;
//wep.UNIONID = unionid ?? "";
//wep.REMAKE = user.remark ?? "";
//wep.CORPID = corpid ?? "";
//logs.Add(wep);
////注册资源
//res_customer.ResgisterCustomer(mobileM, wep.RESID, "qwzy");//企微资源
ww_extuser_pares_dept wep = new ww_extuser_pares_dept
{
RESID = Common.ResUtil.CreateResId(mobileM),
EXTERNAL_USERID = external_userid,
USERID = user.userid,
UNIONID = unionid ?? "",
REMAKE = user.remark ?? "",
CORPID = corpid ?? "",
MOBILE = mobileM
};
logs.Add(wep);
}
catch (Exception ex)
{
WX.CRM.Common.LogHelper.Error("AutoBound错误记录" + exinfo);
throw;
}
}
}
}
}
//
//using (TransactionScope tran = new TransactionScope()) {
//}
//时间由远到近 避免中途出错漏掉数据
//按照顺序 处理获取到的数据
foreach (var item in logs.OrderBy(m => m.CTIME))
{
try
{
var hhuser = _iww_hhuser_service.HHuser_Get(item.USERID, item.CORPID);
if (hhuser != null && hhuser.deptid.HasValue && dicDept.ContainsKey(hhuser.corpid) && dicDept[hhuser.corpid].Contains(hhuser.deptid.Value))
{
//注册资源
res_customer.ResgisterCustomer(item.MOBILE, item.RESID, "qwzy");//企微资源
item.ISINNERDEPT = 1;//是内部资源
var res = _wxSzzyorder.UpdateResIdByWwUserName(item.RESID, item.EXTERNAL_USERID, item.USERID, item.CORPID);
if (!res)
{
continue;//处理失败时跳出
}
else
{
//_wxSzzyorder.UpdateIsBoundByResId(resId);
var host = Utility.GetSettingOrNullByKey("DataSyncApiUrl");
var url = host + "/api/DataSync";
var data = new { resid = item.RESID, userid = item.EXTERNAL_USERID, deptcode = companycode };
var para = new SYNC_PUSH_DTO()
{
bidatatype = "Server_WwextUserResid",
deptcode = companycode,
jsontext = data.ToJson()
};
var rep = Utility.PostAjaxData(url, para.ToJson(), Encoding.UTF8);
var ret2 = Utility.JSONToObject<retMsg>(rep);
if (!ret2.result)
{
LogHelper.Info("同步到中心点异常:" + para.ToJson());
//message = "关联成功!同步到总部失败!";
}
}
}
else
item.ISINNERDEPT = 0;
var logRes = _wxSzzyorder.InsertWW_EXTUSER_PARES(new WW_EXTUSER_PARES() { RESID = item.RESID, EXTERNAL_USERID = item.EXTERNAL_USERID, USERID = item.USERID, UNIONID = item.UNIONID, REMAKE = item.REMAKE, CORPID = item.CORPID, ISINNERDEPT = item.ISINNERDEPT });
}
catch(Exception e)
{
LogHelper.Error(e.ToString());
continue;//处理失败时跳出
}
}
if (lastTimeList.Any())
{//处理到数据才修改配置
if (lastTimeList.Max() == lastTimeList.Min())
{
syskey.PARAVALUE = $"{lastTimeList.Max().ToString("yyyy-MM-dd HH:mm:ss")};{cTimeList.Max().ToString("yyyy-MM-dd HH:mm:ss")}";
}
else
{
syskey.PARAVALUE = $"{lastTimeList.Max().ToString("yyyy-MM-dd HH:mm:ss")};";
}
//更新系统变量 最后处理数据时间
Common.ValidationErrors errors = new Common.ValidationErrors();
paramter_bl.Update_Parameter(ref errors, syskey);
}
}
}
}
public class WwExtUserView
{
public External_Contact external_contact { get; set; }
public List<Follow_User> follow_user { get; set; }
public string corpid { get; set; }
}
public class External_Contact
{
public string external_userid { get; set; }
public string name { get; set; }
public int type { get; set; }
public string avatar { get; set; }
public int gender { get; set; }
public string unionid { get; set; }
}
public class Follow_User
{
public string saleeid { get; set; }
public string userid { get; set; }
public string remark { get; set; }
public string description { get; set; }
public long createtime { get; set; }
public string bindtime { get; set; }
public List<string> remark_mobiles { get; set; }
public string uname { get; set; }
}
public class ww_extuser_pares_dept
{
public string RESID { get; set; }
public string EXTERNAL_USERID { get; set; }
public string USERID { get; set; }
public string UNIONID { get; set; }
public string REMAKE { get; set; }
public string CORPID { get; set; }
public DateTime CTIME { get; set; }
/// <summary>
/// 是否是内部事业部的数据
/// </summary>
public int ISINNERDEPT { get; set; }
public string MOBILE { get; set; }
}
}