140 lines
5.6 KiB
C#
140 lines
5.6 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 企业微信订单客户操作
|
|
/// </summary>
|
|
public class wx_orderuser_bll
|
|
{
|
|
private string comcode;
|
|
public wx_orderuser_bll(string _comcode)
|
|
{
|
|
comcode = _comcode;
|
|
}
|
|
/// <summary>
|
|
/// 订单客户初始化到微信库(已兼容小事业部)
|
|
/// </summary>
|
|
public void InitHgOrderUserName()
|
|
{
|
|
using (IDbConnection con = ConnectionFactory.CreateConnection(ContextType.crmContext, comcode, false))
|
|
{
|
|
List<wx_ordcustomer> list = new List<wx_ordcustomer>();
|
|
|
|
string dbtype = con.GetType().Name.ToLower();
|
|
if (dbtype.Contains("oracle"))
|
|
{
|
|
list = con.Query<wx_ordcustomer>(@"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<wx_ordcustomer>($@"
|
|
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<wx_ordcustomer> nowlist = conqw.Query<wx_ordcustomer>("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);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化工作微信数据,用于数据过滤
|
|
/// </summary>
|
|
public void InitJobUserName()
|
|
{
|
|
using (IDbConnection con = ConnectionFactory.CreateConnection(ContextType.crmContext, comcode, false))
|
|
{
|
|
List<wx_workalias> list = new List<wx_workalias>();
|
|
string contype = con.GetType().Name.ToLower();
|
|
if (contype.Contains("oracle"))
|
|
{
|
|
list = con.Query<wx_workalias>($@"
|
|
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<wx_workalias>($@"
|
|
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<wx_workalias> nowlist = conqw.Query<wx_workalias>("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);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|