using System;
using System.Threading;
using System.Linq;
using System.Data;
using Library;
using MySystem.Models;
using System.Collections.Generic;
///
/// 广电卡交易数据导入
///
namespace MySystem
{
public class SIMPosMerchantTradeService
{
public readonly static SIMPosMerchantTradeService Instance = new SIMPosMerchantTradeService();
private SIMPosMerchantTradeService()
{
}
public void Start()//启动
{
Thread thread = new Thread(ImportPostDo);
thread.IsBackground = true;
thread.Start();
}
public void ImportPostDo()
{
while (true)
{
try
{
string data = RedisDbconn.Instance.RPop("SIMPosMerchantTradeQueue");
if (!string.IsNullOrEmpty(data))
{
string[] dataList = data.Split("#cut#");
string _ExcelPath = dataList[0];
string _Kind = dataList[1];
string checkKey = dataList[2];
string Operator = dataList[3]; // 操作人
int SuccessCount = 0;
int DoCount = 0;
string FullExcelPath = function.getPath(_ExcelPath);
FullExcelPath = FullExcelPath.Replace("//", "/");
DataTable list = new PublicFunction().ExcelToDataTable(FullExcelPath);
int TotalCount = list.Rows.Count;
while (DoCount < list.Rows.Count)
{
WebCMSEntities db = new WebCMSEntities();
SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
//导激活
if (_Kind == "1")
{
var tran = db.Database.BeginTransaction();
try
{
int Size = 100;
if (list.Rows.Count - DoCount < 100)
{
Size = list.Rows.Count - DoCount;
}
Dictionary storeData = new Dictionary();
for (int i = DoCount; i < DoCount + Size; i++)
{
DataRow dr = list.Rows[i];
string OrderNo = dr["订单编码"].ToString();
string CreateDate = dr["订单创建时间"].ToString();
string PhoneNumber = dr["选择号码"].ToString();
string HasFirstCharge = dr["是否有首充"].ToString();
string FirstChargeAmount = dr["首充金额(元)"].ToString();
string Province = dr["号码归属省份"].ToString();
string City = dr["号码归属地市"].ToString();
string ProductName = dr["商品名称"].ToString();
string ActiveName = dr["促销活动名称"].ToString();
string ProductType = dr["商品类型名称"].ToString();
string ActualAmount = dr["实付金额(元)"].ToString();
string OrderStatus = dr["订单状态"].ToString();
string ActStatus = dr["激活状态名称"].ToString();
string ActDate = dr["激活时间"].ToString();
string WorkNo = dr["BOSS员工工号"].ToString();
string AccountName = dr["账号姓名"].ToString();
string SimNo = dr["SIM卡号"].ToString();
// PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == PhoneNumber && m.Field5 == ActStatus);
// if (checkMer == null)
// {
string json = "{";
json += "\"OrderNo\":\"" + OrderNo + "\",";
json += "\"CreateDate\":\"" + CreateDate + "\",";
json += "\"PhoneNumber\":\"" + PhoneNumber + "\",";
json += "\"HasFirstCharge\":\"" + HasFirstCharge + "\",";
json += "\"FirstChargeAmount\":\"" + FirstChargeAmount + "\",";
json += "\"Province\":\"" + Province + "\",";
json += "\"City\":\"" + City + "\",";
json += "\"ProductName\":\"" + ProductName + "\",";
json += "\"ActiveName\":\"" + ActiveName + "\",";
json += "\"ProductType\":\"" + ProductType + "\",";
json += "\"ActualAmount\":\"" + ActualAmount + "\",";
json += "\"OrderStatus\":\"" + OrderStatus + "\",";
json += "\"ActStatus\":\"" + ActStatus + "\",";
json += "\"ActDate\":\"" + ActDate + "\",";
json += "\"WorkNo\":\"" + WorkNo + "\",";
json += "\"AccountName\":\"" + AccountName + "\",";
json += "\"SimNo\":\"" + SimNo + "\"";
json += "}";
spdb.BindRecord.Add(new SpModels.BindRecord()
{
CreateDate = DateTime.Parse(CreateDate),
UpdateTime = DateTime.Parse(CreateDate), //机具绑定、解绑时间
CreateTime = DateTime.Parse(CreateDate), //商户操作时间
MerNewSnNo = PhoneNumber, //商户手机号
MerSnNo = SimNo, //序列号
MerName = AccountName, //商户名称
MerNo = PhoneNumber, //商户编号
Remark = ProductType,
Field1 = Province,
Field2 = ActiveName,
Field3 = ProductName,
Field4 = WorkNo,
Field5 = ActStatus,
SeoTitle = ActualAmount,
SeoKeyword = ActDate,
SeoDescription = json,
ProductType = "14",
Status = 1,
});
spdb.Merchants.Add(new SpModels.Merchants()
{
SnNo = SimNo,
CreateTime = DateTime.Parse(CreateDate),
UpdateTime = DateTime.Parse(CreateDate),
AgentName = AccountName,
MerRealName = AccountName,
MerMobile = PhoneNumber,
MerNo = PhoneNumber,
MerName = AccountName,
ProductType = "14",
Status = 1,
});
SuccessCount += 1;
}
DoCount += Size;
spdb.SaveChanges();
tran.Commit();
if (DoCount >= list.Rows.Count)
{
RedisDbconn.Instance.Set("SIMPosMerchantTradeCheckImport:" + checkKey, "success|" + SuccessCount);
RedisDbconn.Instance.SetExpire("SIMPosMerchantTradeCheckImport:" + checkKey, 60000);
}
}
catch (Exception ex)
{
DoCount = list.Rows.Count;
function.WriteLog(ex.ToString(), "导入广电卡激活数据");
tran.Rollback();
ErrorMsg msg = new ErrorMsg()
{
Time = DateTime.Now,
ErrorContent = ex.ToString(),
};
function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入广电卡激活数据Excel文件异常");
}
tran.Dispose();
}
//导交易
if (_Kind == "2")
{
var tran = db.Database.BeginTransaction();
try
{
int Size = 100;
if (list.Rows.Count - DoCount < 100)
{
Size = list.Rows.Count - DoCount;
}
Dictionary storeData = new Dictionary();
for (int i = DoCount; i < DoCount + Size; i++)
{
DataRow dr = list.Rows[i];
string Mobile = dr["用户手机号"].ToString();
string SimNo = dr["SIM卡号"].ToString();
string CreateMonth = dr["入网月份"].ToString();
string SettleMonth = dr["结算月份"].ToString();
string SettleTime = dr["结算账期"].ToString();
string SettleSpace = dr["结算场景"].ToString();
string BusinessType = dr["业务类型"].ToString();
string Province = dr["用户省份"].ToString();
string City = dr["用户地市"].ToString();
string Team = dr["团队归属"].ToString();
string WorkName = dr["行销宝姓名"].ToString();
string WorkNo = dr["行销宝工号"].ToString();
string ProductName = dr["商品名称"].ToString();
string TotalAmount = dr["账单总金额"].ToString();
string InAmount = dr["套内金额"].ToString();
string OutAmount = dr["套外金额"].ToString();
string UserStatus = dr["用户状态"].ToString();
string ChannelCode = dr["渠道编码"].ToString();
string ChannelName = dr["渠道名称"].ToString();
string OrderCreateTime = dr["订单创建时间"].ToString();
string OrderStatus = dr["订单状态"].ToString();
string ActTime = dr["激活时间"].ToString();
string SourceProductName = dr["原订单商品名称"].ToString();
string ProductType = dr["商品类型名称"].ToString();
string ActivityName = dr["促销活动名称"].ToString();
string Percent = dr["分成比例"].ToString();
string OrgDivi = dr["机构分成"].ToString();
string json = "{";
json += "\"Mobile\":\"" + Mobile + "\",";
json += "\"SimNo\":\"" + SimNo + "\",";
json += "\"CreateMonth\":\"" + CreateMonth + "\",";
json += "\"SettleMonth\":\"" + SettleMonth + "\",";
json += "\"SettleTime\":\"" + SettleTime + "\",";
json += "\"SettleSpace\":\"" + SettleSpace + "\",";
json += "\"BusinessType\":\"" + BusinessType + "\",";
json += "\"Province\":\"" + Province + "\",";
json += "\"City\":\"" + City + "\",";
json += "\"Team\":\"" + Team + "\",";
json += "\"WorkName\":\"" + WorkName + "\",";
json += "\"WorkNo\":\"" + WorkNo + "\",";
json += "\"ProductName\":\"" + ProductName + "\",";
json += "\"TotalAmount\":\"" + TotalAmount + "\",";
json += "\"InAmount\":\"" + InAmount + "\",";
json += "\"OutAmount\":\"" + OutAmount + "\",";
json += "\"UserStatus\":\"" + UserStatus + "\",";
json += "\"ChannelCode\":\"" + ChannelCode + "\",";
json += "\"ChannelName\":\"" + ChannelName + "\",";
json += "\"OrderCreateTime\":\"" + OrderCreateTime + "\",";
json += "\"OrderStatus\":\"" + OrderStatus + "\",";
json += "\"ActTime\":\"" + ActTime + "\",";
json += "\"SourceProductName\":\"" + SourceProductName + "\",";
json += "\"ProductType\":\"" + ProductType + "\",";
json += "\"ActivityName\":\"" + ActivityName + "\",";
json += "\"Percent\":\"" + Percent + "\",";
json += "\"OrgDivi\":\"" + OrgDivi + "\"";
json += "}";
bool op = true;
string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
decimal TotalAmountNum = decimal.Parse(function.CheckNum(numberString: TotalAmount));
DateTime CreateDate = DateTime.Parse(OrderCreateTime);
string CheckDate = DateTime.Now.AddMonths(-3).ToString("yyyyMM");
if (int.Parse(CreateMonth) >= int.Parse(CheckDate)) op = false;
if (OrderStatus == "待激活") op = false;
if (UserStatus.Contains("注销") || UserStatus.Contains("欠费"))
{
op = false;
SimposMerchantInfo checkSimMer = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
if (checkSimMer != null)
{
checkSimMer.Status = -1;
if (UserStatus.Contains("欠费") && checkSimMer.Status != -1)
{
checkSimMer.Status = -2;
}
db.SaveChanges();
}
PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
if (checkMer != null)
{
checkMer.Status = -1;
if (UserStatus.Contains("欠费") && checkMer.Status != -1)
{
checkMer.Status = -2;
}
db.SaveChanges();
}
PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == SimNo);
if (pos != null)
{
pos.Status = -1;
if (UserStatus.Contains("欠费") && pos.Status != -1)
{
pos.Status = -2;
}
db.SaveChanges();
}
}
if (UserStatus.Contains("正常"))
{
SimposMerchantInfo checkSimMer = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
if (checkSimMer != null)
{
if (checkSimMer.Status == -2) checkSimMer.Status = 0;
db.SaveChanges();
}
PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
if (checkMer != null)
{
if (checkMer.Status == -2) checkMer.Status = 1;
db.SaveChanges();
}
PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == SimNo);
if (pos != null && pos.Status < 0)
{
if (pos.Status == -2)
{
pos.Status = 0;
}
db.SaveChanges();
}
}
if (TotalAmountNum > 0 && op)
{
spdb.TradeRecord.Add(new SpModels.TradeRecord()
{
CreateDate = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-20 HH:mm:ss")), //创建时间
TradeSerialNo = OrderNo, //订单编号
AgentNo = WorkName + WorkNo, //一级服务商编号
MerNo = Mobile, //商户编
TradeSnNo = SimNo, //sim卡号
ChannelSerial = OrderNo, //请求号
TradeAmount = TotalAmountNum, //账单总金额
TradeStatus = OrderStatus, //订单状态
TradeDate = CreateMonth,
TradeTime = CreateDate.ToString("yyyyMMdd"),
BankCardType = "01",
TradeType = "01",
ProductType = "14",
Status = 1,
Field1 = ProductName,
Field2 = SourceProductName,
Field3 = ProductType,
Field4 = BusinessType,
SeoDescription = json,
});
spdb.SaveChanges();
}
}
DoCount += Size;
db.SaveChanges();
tran.Commit();
if (DoCount >= list.Rows.Count)
{
RedisDbconn.Instance.Set("SIMPosMerchantTradeCheckImport:" + checkKey, "success|");
RedisDbconn.Instance.SetExpire("SIMPosMerchantTradeCheckImport:" + checkKey, 60000);
}
}
catch (Exception ex)
{
DoCount = list.Rows.Count;
function.WriteLog(ex.ToString(), "导入广电卡交易数据");
tran.Rollback();
ErrorMsg msg = new ErrorMsg()
{
Time = DateTime.Now,
ErrorContent = ex.ToString(),
};
function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入广电卡交易数据Excel文件异常");
}
tran.Dispose();
}
}
}
else
{
Thread.Sleep(5000);
}
}
catch (Exception ex)
{
function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "后台导入Excel文件队列异常");
}
}
}
}
}