183 lines
8.3 KiB
C#
183 lines
8.3 KiB
C#
using CRM.Core.DTO;
|
|
using CRM.Core.DTO.AliYunSub;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using WX.CRM.BLL.Res;
|
|
using WX.CRM.BLL.Wx;
|
|
using WX.CRM.Common;
|
|
using WX.CRM.IBLL.Soft;
|
|
using WX.CRM.Model.Entity;
|
|
|
|
namespace WX.CRM.BLL.Soft
|
|
{
|
|
public class SOFT_USERINFO_SUB_BL : ISOFT_USERINFO_SUB
|
|
{
|
|
/// <summary>
|
|
/// 日志入庫
|
|
/// </summary>
|
|
/// <param name="log"></param>
|
|
/// <returns></returns>
|
|
public bool UserInfoSubLog(SOFT_USERINFO_SUBLOG log)
|
|
{
|
|
bool result = false;
|
|
try
|
|
{
|
|
using (var db = new crmContext())
|
|
{
|
|
log.DATA = LogHelper.encryPhone(log.DATA);
|
|
db.SOFT_USERINFO_SUBLOG.Add(log);
|
|
db.SaveChanges();
|
|
}
|
|
result = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogHelper.Error(e.ToString());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
//合并或者解绑
|
|
public bool UserInfoSubInDb(AliYunSubInfo info)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new crmContext())
|
|
{
|
|
if (info.data == null)
|
|
return false;
|
|
foreach (var item in info.data)
|
|
{
|
|
var entry = db.SOFT_USERINFO_SUB.FirstOrDefault(m => m.USID == item.uid);
|
|
|
|
//var company = companyList.FirstOrDefault(p => p.Min <= item.ch && p.Max >= item.ch);
|
|
////LogHelper.Info("根据渠道获取推送码:" + company.ToJson());
|
|
//if (company != null)
|
|
//{
|
|
// item.deptcode = company.CompanyCode;
|
|
//}
|
|
if (entry == null)//无数据新增
|
|
{
|
|
LogHelper.Info(item.ToJson());
|
|
db.SOFT_USERINFO_SUB.Add(new SOFT_USERINFO_SUB()
|
|
{
|
|
APPID = item.appid,
|
|
APPUSERID = item.appuserid,
|
|
CH = item.ch,
|
|
CID = item.cid,
|
|
MOBILE = LogHelper.encryPhone(item.mobile),
|
|
TYPE = item.type,
|
|
USID = item.uid,
|
|
UNIONID = item.unionid,
|
|
CTIME = DateTime.Now
|
|
});
|
|
db.SaveChanges();//入库
|
|
}
|
|
else//有数据修改
|
|
{
|
|
entry.MOBILE = item.mobile;
|
|
entry.TYPE = item.type;
|
|
entry.UNIONID = item.unionid;
|
|
entry.CID = item.cid;
|
|
entry.CH = item.ch;
|
|
entry.UTIME = DateTime.Now;
|
|
db.SaveChanges();//入库
|
|
}
|
|
}
|
|
|
|
List<string> resids = new List<string>();
|
|
var data = info.data.Where(m => !string.IsNullOrEmpty(m.resid));//数据合并
|
|
int cid = 0;
|
|
if (data.Count() > 1)//数据超过两条,执行合并
|
|
{
|
|
RES_CUSTOMER_BL rescustomerbll = new RES_CUSTOMER_BL();
|
|
foreach (var dto in data)
|
|
{
|
|
cid = dto.cid;
|
|
string v_resid = string.Empty;
|
|
if (!string.IsNullOrEmpty(dto.resid))
|
|
{
|
|
resids.Add(dto.resid);
|
|
}
|
|
if (!string.IsNullOrEmpty(dto.mobile))
|
|
{
|
|
v_resid = ResUtil.CreateResId(dto.mobile);
|
|
dto.resid = v_resid;
|
|
resids.Add(v_resid);
|
|
rescustomerbll.ResgisterCustomer(dto.mobile, v_resid, dto.ch.ToString());//先执行一次注册
|
|
}
|
|
}
|
|
}
|
|
|
|
//不管合并和解绑,都看数据一样就合并,不一样就解绑
|
|
var nowlist = db.RES_CUSTOMER.Where(m => resids.Contains(m.RESID));//遇到的ID
|
|
var allresid = nowlist.Select(m => m.CUSTOMERID).Union(nowlist.Select(m => m.RESID));
|
|
var alllist = db.RES_CUSTOMER.Where(m => allresid.Contains(m.CUSTOMERID));//查出了所有的资源
|
|
foreach (var item in info.data.GroupBy(m => m.cid))
|
|
{
|
|
List<string> newresid = info.data.Where(m => m.cid == item.Key && !string.IsNullOrEmpty(m.resid)).Select(m => m.resid).ToList();
|
|
if (newresid == null || newresid.Count == 0)//如果没有号码,就直接跳过
|
|
{
|
|
continue;
|
|
}
|
|
var bklist = nowlist.Where(m => newresid.Contains(m.RESID)).ToList();
|
|
if (info.type == 20)
|
|
{
|
|
bklist = alllist.Where(m => newresid.Contains(m.RESID) || newresid.Contains(m.CUSTOMERID)).ToList();//如果是合并就将全部数据查出来
|
|
}
|
|
LogHelper.Info(bklist.ToList().ToJson());
|
|
var ax = bklist.OrderBy(m => m.CTIME).FirstOrDefault(m => m.CUSTOMERID != m.RESID);//假如入已经有了customerid 就取时间小的id作为主ID
|
|
var mainresid = ax != null ? ax.CUSTOMERID : "";
|
|
var mainumid = ax != null ? ax.UMID : "";
|
|
bool isreal = true;
|
|
if (info.type == 10 && ax != null)
|
|
{
|
|
if (bklist.FirstOrDefault(m => m.RESID == mainresid) == null)
|
|
{
|
|
isreal = false;
|
|
}
|
|
}
|
|
if (ax != null && isreal)
|
|
{
|
|
foreach (var aa in bklist)
|
|
{
|
|
var customer = db.RES_CUSTOMER.FirstOrDefault(p => p.RESID == aa.RESID);
|
|
if (customer != null)
|
|
{
|
|
customer.CUSTOMERID = mainresid;
|
|
customer.CUSTOMERMID = mainumid;
|
|
db.SaveChanges();
|
|
LogHelper.Info("key:" + item.Key + ",找到了旧的customerid:" + mainresid + ",resid:" + customer.RESID);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var ax2 = info.data.OrderBy(m => m.uid).FirstOrDefault(m => m.cid == item.Key && !string.IsNullOrEmpty(m.resid));//之前没有合并过,就取 用户中心传递过来的最小id的号码作为主要ID
|
|
var nbresid = ax2.resid;
|
|
foreach (var ba in bklist)
|
|
{
|
|
var customer = db.RES_CUSTOMER.FirstOrDefault(p => p.RESID == ba.RESID);
|
|
var mainCust = db.RES_CUSTOMER.FirstOrDefault(n => n.RESID == nbresid);
|
|
if (customer != null)
|
|
{
|
|
customer.CUSTOMERID = nbresid;
|
|
customer.CUSTOMERMID = mainCust?.UMID;
|
|
LogHelper.Info("key: " + item.Key + "没找到的customerid:" + nbresid + ",resid:" + customer.RESID);
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogHelper.Error(e.ToString());
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
} |