using Dapper;
using NetCore.Model.enums;
using NetCore.Model.wx;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
//using Kogel.Dapper.Extension.MySql;
namespace NetCore.BLL.wx
{
///
/// 企业微信订单客户操作
///
public class wx_orderuser_bll
{
private string comcode;
public wx_orderuser_bll(string _comcode)
{
comcode = _comcode;
}
///
/// 订单客户初始化到微信库(已兼容小事业部)
///
public void InitHgOrderUserName()
{
using (IDbConnection con = ConnectionFactory.CreateConnection(ContextType.crmContext, comcode, false))
{
List list = new List();
string dbtype = con.GetType().Name.ToLower();
if (dbtype.Contains("oracle"))
{
list = con.Query(@"select distinct t.customerusername username from wx_szzyorder t
join res_customer x on t.resid=x.resid
where t.orderstatus in (200, 205, 220, 80, 90)
and t.arrivalpay > 100
and t.customerusername is not null
union
select a.username from wx_rcontact a where a.resid in(
select distinct t.resid from wx_szzyorder t
join res_customer x on t.resid=x.resid
where t.orderstatus in (200, 205, 220, 80, 90)
and t.arrivalpay > 100
)", null, buffered: false).ToList();
}
else if (dbtype.Contains("mysql"))
{
list = con.Query($@"
select distinct t.customerusername username,b.CHANNEL from wx_szzyorder t
join res_customer x on t.resid=x.resid
join WX_ORDEREXT b on b.orderid=t.orderid
where t.orderstatus in (200, 205, 220, 80, 90)
and t.arrivalpay > 100
and t.customerusername is not null
and t.customerusername !=''
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.wxContext, comcode))
{
List nowlist = conqw.Query("select username from hg_orderuser;", null, buffered: false).ToList();
var welist = list.Select(m => m.username).ToArray();
var ollist = nowlist.Select(m => m.username).ToArray();
var needDel = nowlist.Where(m => !welist.Contains(m.username));
foreach (wx_ordcustomer item in needDel)//删除多余的订单客户
{
conqw.Execute("delete from hg_orderuser where username=@username", item);
}
var needInsert = list.Where(m => !ollist.Contains(m.username));
foreach (var item in needInsert)
{
conqw.Execute("insert into hg_orderuser(username)values(@username)", item);
}
}
}
}
///
/// 初始化工作微信数据,用于数据过滤
///
public void InitJobUserName()
{
using (IDbConnection con = ConnectionFactory.CreateConnection(ContextType.crmContext, comcode, false))
{
List list = new List();
string contype = con.GetType().Name.ToLower();
if (contype.Contains("oracle"))
{
list = con.Query($@"
select * from (select username jobusername,alias,nickname,row_number() over(partition by username order by pkid)non from wx_workaccount T WHERE
T.USERNAME IS NOT NULL
AND T.INNERUSERID IN(
select A.INNERUSERID from bas_innerusergroup a where a.companyid=(select companyid from bas_company where COMPANYCODE='{comcode}' )
)
)ac where ac.non=1
", null, buffered: false).ToList();
}
else if (contype.Contains("mysql"))
{
list = con.Query($@"
select username jobusername,alias,nickname from wx_workaccount T WHERE
T.USERNAME IS NOT NULL
AND 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.wxContext, comcode))
{
List nowlist = conqw.Query("select jobusername,alias,nickname from hg_jobalias;", null, buffered: false).ToList();
var welist = list.Select(m => m.jobusername).ToArray();
var ollist = nowlist.Select(m => m.jobusername).ToArray();
var needDel = nowlist.Where(m => !welist.Contains(m.jobusername));
foreach (wx_workalias item in needDel)//删除多余的订单客户
{
conqw.Execute("delete from hg_jobalias where jobusername=@jobusername", item);
}
var needInsert = list.Where(m => !ollist.Contains(m.jobusername));
foreach (var item in needInsert)
{
conqw.Execute("insert into hg_jobalias(jobusername,alias,nickname)values(@jobusername,@alias,@nickname)", item);
}
}
}
}
}
}