using Dapper;
using NetCore.Model.crm;
using NetCore.Model.enums;
using NetCore.Model.qw;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
//using Kogel.Dapper.Extension.MySql;
namespace NetCore.BLL
{
///
/// 企业微信订单客户操作
///
public class qw_orderuser_bll
{
private string comcode;
///
///企业微信订单客户操作
///
/// 公司编码
public qw_orderuser_bll(string _comcode)
{
comcode = _comcode;
}
///
/// 订单客户初始化(已经兼容小事业部)
///
public void InitHgOrderUser()
{
using (IDbConnection con = ConnectionFactory.CreateConnection(ContextType.crmContext, comcode, false))
{
List list = new List();
var aax = con.GetType().Name.ToLower();
if (aax.Contains("oracle"))//oracle数据库
{
list = con.Query(@" select userid external_userid from ww_extuser_resid x
where userid is not null and exists
(select * from wx_szzyorder t where t.orderstatus in (200, 205, 220, 80, 90)
and t.arrivalpay > 100 and x.resid = t.resid
)
union
select t.external_userid from wx_szzyorder t where t.orderstatus in (200, 205, 220, 80, 90)
and t.arrivalpay > 100 and t.external_userid is not null", null, buffered: false).ToList();
}
else if (aax.Contains("mysql"))//mysql版本的涉及到多个事业部
{
list = con.Query($@"
select DISTINCT t.external_userid from wx_szzyorder t
join WX_ORDEREXT b on b.orderid=t.orderid
where t.orderstatus in (200, 205, 220, 80, 90)
and t.arrivalpay > 100 and t.external_userid is not null and t.external_userid!=''
and exists(select 1 from bas_company aa where aa.companycode='{comcode}' and b.CHANNEL>=aa.channelMin and b.channel<=aa.channelMax);
", null, buffered: false).ToList();
}
using (IDbConnection conqw = ConnectionFactory.CreateConnection(ContextType.qwContext, comcode))
{
List nowlist = conqw.Query("select external_userid from hg_orderuser;", null, buffered: false).ToList();
var welist = list.Select(m => m.external_userid).ToArray();
var ollist = nowlist.Select(m => m.external_userid).ToArray();
var needDel = nowlist.Where(m => !welist.Contains(m.external_userid));
foreach (wx_order_userid item in needDel)//删除多余的订单客户
{
conqw.Execute("delete from hg_orderuser where external_userid=@external_userid", item);
}
var needInsert = list.Where(m => !ollist.Contains(m.external_userid));
foreach (var item in needInsert)
{
conqw.Execute("insert into hg_orderuser(external_userid)values(@external_userid)", item);
}
}
}
}
///
/// 初始化工作企业微信信息(已经兼容小事业部)
///
public void InitHgJobUser()
{
using (IDbConnection con = ConnectionFactory.CreateConnection(ContextType.crmContext, comcode, false))
{
List list = con.Query($@"select distinct userid,corpid from ww_hhuser_eid t
where t.eid in(
select x.eid from bas_innerusergroup a
join bas_inneruser x on x.pkid=a.inneruserid
where a.companyid=(select companyid from bas_company where COMPANYCODE='{comcode}' )
)
", null, buffered: false).ToList();
using (IDbConnection conqw = ConnectionFactory.CreateConnection(ContextType.qwContext, comcode))
{
List nowlist = conqw.Query("select userid,corpid from hg_jobuser;", null, buffered: false).ToList();
var needDel = nowlist.Where(m => !list.Contains(m));
foreach (hg_jobuser item in needDel)//删除多余的订单客户
{
conqw.Execute("delete from hg_jobuser where userid=@userid and corpid=@corpid", item);
}
var needInsert = list.Where(m => !nowlist.Contains(m));
foreach (var item in needInsert)
{
conqw.Execute("insert into hg_jobuser(userid,corpid)values(@userid,@corpid)", item);
}
}
}
}
}
}