using CRM.Core.BLL.Util;
using CRM.Core.DAL.Soft;
using CRM.Core.DTO;
using CRM.Core.Model.Entity;
using CRM.Core.Res;
using System;
using System.Linq;
using WX.CRM.Common;
namespace CRM.Core.BLL.Soft
{
///
/// 抓取接口数据,旧系统为JMDUSER
///
public class GET_REGUSER
{
private readonly Soft_User_DAL _softUser = new Soft_User_DAL();
private readonly CACHE_BL _cache = new CACHE_BL();
/*接口返回的字符串:18696548545,5001,6201075,5001,2013-8-7 13:54:16【手机号,标签,卡号,用户名,注册时间】*/
///
/// 从接口抓取到的注册用户,旧系统名称为jmdUser
///
/// 手机号
/// 注册渠道
/// 卡号
/// 用户名
/// 注册时间
//public void RegUser(string mobile, string tag, string cardNo, string userName, DateTime? regDate, int companyId, string emial, string ip, string activeMobile = null, string platform = null, string jsonData = null, string dataType = null, string extXML = null, string kWord = null, string eid = null,string openid=null,string openplat=null, string unionid=null)
public void RegUser(SyncRegUserDto dto)
{
//88888888888,5001,6299377,565,2013/9/9 14:44:54,600
ValidationErrors errors = new ValidationErrors();
try
{
//注册customer
string v_resid = string.Empty;
if (!string.IsNullOrEmpty(dto.mobile))
{
v_resid = ResUtil.CreateResId(dto.mobile);
new BLL.Res.RES_CUSTOMER_BL().ResgisterCustomer(dto.mobile, v_resid, dto.ch);
}
DateTime? rgd = null;
if (dto.regDate.HasValue)
{
rgd = DateTimeTool.GetTimeFromLinuxTime(dto.regDate.Value);
}
var ch = Convert.ToInt32(dto.ch);
using (var db = new zxdContext())
{
var regUser = db.SOFT_USER.FirstOrDefault(p => p.USERNAME == dto.username);
if (regUser == null)
{
var softUser = new SOFT_USER
{
USERNAME = dto.username,
USERPASS = "string.Empty",
CTIME = System.DateTime.Now,
COMPANYID = 0,
REGCAMPAINID = ch,
REGDATE = rgd,
RESID = v_resid,
REGPLATFORM = dto.plat,
EMAIL = dto.em,
IP = dto.ip,
OPENID = dto.openid,// ***第三方用户ID * **
OPENPLAT = dto.openplat,//***第三方平台***
LIVECODE = dto.liveCode,
REGSOURCE = dto.regSource
};
if (!string.IsNullOrWhiteSpace(dto.refeid))
{
softUser.EID = Convert.ToInt32(dto.refeid);
dto.eid = dto.refeid;
}
if (!string.IsNullOrEmpty(dto.mobile))
{
softUser.ISACTIVE = 1;
softUser.ACTIVERESID = v_resid;
softUser.ACTIVETIME = rgd;
}
softUser.UNIONID = dto.unionId;
db.SOFT_USER.Add(softUser);
var softUserCH = new Soft_User_CH() { UserName = softUser.USERNAME, CH = Convert.ToInt32(dto.ch), CTime = DateTime.Now };
if (!string.IsNullOrEmpty(dto.eid))
{
softUserCH.Eid = int.Parse(dto.eid);
}
db.Soft_User_CH.Add(softUserCH);
db.SaveChanges();
if (!string.IsNullOrWhiteSpace(dto.eid) && !string.IsNullOrEmpty(dto.mobile))
{
var eid = int.Parse(dto.eid);
SetQuantity(ch, eid, dto.groupid ?? 0, v_resid);
}
}
else
{
if (!string.IsNullOrWhiteSpace(dto.refeid))
{
regUser.EID = Convert.ToInt32(dto.refeid);
}
if (!string.IsNullOrEmpty(dto.unionId))
{
regUser.UNIONID = dto.unionId;
}
var softUserExists = db.Soft_User_CH.Any(p => p.UserName == dto.username && p.CH == ch);
if (!softUserExists)
{
var softUserCH = new Soft_User_CH() { UserName = dto.username, CH = Convert.ToInt32(dto.ch), CTime = DateTime.Now };
if (!string.IsNullOrEmpty(dto.eid))
{
softUserCH.Eid = int.Parse(dto.eid);
}
db.Soft_User_CH.Add(softUserCH);
if (!string.IsNullOrWhiteSpace(dto.eid) && !string.IsNullOrEmpty(dto.mobile))
{
var eid = int.Parse(dto.eid);
SetQuantity(ch, eid, dto.groupid ?? 0, v_resid);
}
}
db.SaveChanges();
//LogHelper.Info("regUser.EID:" + regUser.EID.ToString() + ",regUser.UNIONID:" + regUser.UNIONID);
}
}
if (!string.IsNullOrEmpty(v_resid))
{
//AY.CRM.Common.LogHelper.Error(string.Format("【ContractUser】resid:{0},username:{1}", v_resid, userName));
new BLL.Res.RES_CUSTOMERUSER_BL().ContractUser(v_resid, dto.username);
}
//进入资源系统
bool r = ImportResApply(errors, dto.mobile, dto.ch, string.Empty, dto.username, rgd, null, null, null, null);
if (r == false)
throw new Exception(errors.Error);
}
catch
{
throw;
}
}
public void BindUser(string username, string unionid, int eid, int ch)
{
try
{
using (var db = new zxdContext())
{
var user = new SOFT_USER();
if (!string.IsNullOrEmpty(unionid))
user = db.SOFT_USER.FirstOrDefault(p => p.UNIONID == unionid);
if (!string.IsNullOrEmpty(username))
user = db.SOFT_USER.FirstOrDefault(p => p.USERNAME == username);
if (user == null)
{
LogHelper.Info("找不到客户username:" + username + "|||unionid:" + unionid);
return;
}
var resid = string.Empty;
if (string.IsNullOrEmpty(user.RESID))
{
LogHelper.Info("客户资源resid为空:" + username + " |||unionid:" + unionid);
return;
}
//user.EID = eid;
user.UNIONID = unionid;
db.SaveChanges();
}
}
catch (Exception ex)
{
LogHelper.Error("更新软件用户:" + ex.ToString());
}
}
///
/// (当手机号不为空时时进入资源系统,等清洗)从接口抓取到的注册用户,旧系统名称为jmdUser
///
/// 手机号
/// 注册标签
/// 卡号
/// 用户名
/// 注册时间
public bool ImportResApply(ValidationErrors err, string mobile, string tag, string cardNo, string userName, DateTime? regDate, string jsonData = null, string dataType = null, string extXML = null, string kWord = null)
{
ValidationErrors errors = new ValidationErrors();
try
{
if (string.IsNullOrEmpty(mobile))
return true;
var model = new RES_APPLY
{
RESOURCETAG = tag,
RESID = ResUtil.CreateResId(mobile),
USERNAME = userName,
JSONDATA = jsonData,
RTIME = regDate,
JSONTYPE = 1,
STATUS = 0,
MOBILE = mobile.Trim(),
DATATYPE = dataType,
EXTXML = extXML,
KWORD = kWord
};
new RES_APPLY_BL().Create(ref errors, model);
return true;
}
catch (Exception ex)
{
errors.Add(ex.Message + ex.StackTrace);
//CRM.Core.Common.LogHelper.Error("----------"+ex.Message + ex.StackTrace);
return false;
}
}
public DateTime GetServiceLastExecTime()
{
DateTime rdate = System.DateTime.Now;
try
{
CRM.Core.Model.Entity.zxdContext db = new CRM.Core.Model.Entity.zxdContext();
var par = db.BAS_PARAMETER.FirstOrDefault(p => p.PARAKEY.Trim() == "RegUserServiceExecLastTime" && p.GROUPID.Trim() == "SysServiceExecTime");
if (par != null)
rdate = DateTime.Parse(par.PARAVALUE);
else
throw new Exception("RegUserServiceExecLastTime参数错误");
}
catch
{
rdate = System.DateTime.Now;
}
return rdate;
}
public void UpdateExecLastTime(DateTime lastTime)
{
using (CRM.Core.Model.Entity.zxdContext db = new CRM.Core.Model.Entity.zxdContext())
{
var par = db.BAS_PARAMETER.FirstOrDefault(p => p.PARAKEY.Trim() == "RegUserServiceExecLastTime" && p.GROUPID.Trim() == "SysServiceExecTime");
if (par == null)
{
throw new Exception("RegUserServiceExecLastTime参数错误");
}
par.PARAVALUE = lastTime.ToString();
db.SaveChanges();
}
}
public void SetQuantity(int ch, int eid, int gid, string resid)
{
try
{
//478361560401673707-1001-60974-27
LogHelper.Info("SetQuantity-1:" + resid + "-" + ch.ToString() + "-" + eid.ToString() + "-" + gid.ToString());
var companyCode = _cache.GetCompanyCode();
var company = companyCode.FirstOrDefault(p => p.Min <= ch && p.Max >= ch);
LogHelper.Info("SetQuantity-2:" + company.Defult);
_softUser.SetQuantity(company.Defult, eid, gid);
}
catch (Exception ex)
{
LogHelper.Error(ex);
}
}
}
}