using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace libEShangPB
{
[ComVisible(true)]
[Guid("5FB09E5B-EA28-4106-9C63-2124E1D4F865")]
public interface IZJCoupon
{
string VoucherCheck(string webServiceUrl, string serverPartCode, string shopCode, string couponCode);
bool VoucherVerification(string webServiceUrl, string serverPartCode, string shopCode, string couponCode);
bool VoucherCancel(string webServiceUrl, string serverPartCode, string shopCode, string couponCode);
bool ReportUpload(string webServiceUrl, string serverPartCode, string shopCode, string machineCode, string reportTime);
bool ThirdOrderSync(string webServiceUrl, string storeCode, string userCode, string orderCode, string orderDate,
decimal orderAmount, decimal discountAmount, decimal couponAmount, string payInfo, string couponInfo, string goodsTable);
string GetMemberInfo(string webServiceUrl, string dynamicCode, string phone);
string GetByMemberCode(string webServiceUrl, string storeCode, string dynamicCode, string orderCode, decimal orderAmount, string goodsTable);
string GetByCode(string webServiceUrl, string storeCode, string couponCode, string orderCode, decimal orderAmount, string goodsTable);
string ImmediateCost(string webServiceUrl, string storeCode, string couponCode, string orderCode, decimal orderAmount, string goodsTable);
bool Undo(string webServiceUrl, string couponCode);
void BusinessLogCheck(string webServiceUrl, string serverpartCode, string shopCode, string machineCode);
bool CreateYsCouponAnalysis(string webServiceUrl, string serverpartCode, string shopCode, string machineCode, string analysisDate, int analysisType);
bool ZJCouponPrint(string orderCode);
}
[ComVisible(true)]
[Guid("C3137870-7DD8-4817-A8D5-97EDA64C6862")]
[ProgId("libEShangPB.ZJCoupon")]
public class ZJCoupon : IZJCoupon
{
///
/// 请求账号(固定值:gssy_ys)
///
private const string RequestAccount = "gssy_ys";
///
/// 券核销接口主地址
///
private const string ApiUrl = "https://gssy.cjene.com/center/coupon";
///
/// 券核验接口
///
///
///
public string VoucherCheck(string webServiceUrl, string serverPartCode, string shopCode, string couponCode)
{
object o_Temp = new
{
//待验证的券号
voucherNumber = couponCode,
//门店号 + 账号 + 密钥 MD5加密32位大写
signParam = ApiParamSign(serverPartCode + shopCode + RequestAccount, "200DAEE7E80DB68E4E194ACC762DAF82"),
//门店编号
storeNumber = serverPartCode + shopCode,
//请求账号(固定值:gssy_ys)
requestAccount = RequestAccount
};
string str_PostData = JsonConvert.SerializeObject(o_Temp);
Hashtable hashtable = new Hashtable
{
{ "requestURL", ApiUrl+"/VoucherCheck" },
{ "postData", str_PostData },
{ "contentType", "application/json" }
};
Model.GSSYCouponModel cm_Result = new Model.GSSYCouponModel { ResultCode = "0" };
try
{
//调用接口获取交易结果
string retString = SoapWSHelper.QuerySoapWebServiceString(webServiceUrl, "MobilePayProxy", hashtable);
cm_Result = JsonConvert.DeserializeObject(retString);
LogHelper.WriteServiceLog($"高速石油券验证接口调用成功。接口返回值:{retString}");
}
catch (Exception ex)
{
LogHelper.WriteServiceLog($"高速石油券验证接口调用失败:{ex.Message}");
}
return JsonConvert.SerializeObject(cm_Result);
}
///
/// 券核销接口
///
///
///
public bool VoucherVerification(string webServiceUrl, string serverPartCode, string shopCode, string couponCode)
{
object o_Temp = new
{
//待验证的券号
voucherNumber = couponCode,
//门店号 + 账号 + 密钥 MD5加密32位大写
signParam = ApiParamSign(serverPartCode + shopCode + RequestAccount, "200DAEE7E80DB68E4E194ACC762DAF82"),
//门店编号
storeNumber = serverPartCode + shopCode,
//请求账号(固定值:gssy_ys)
requestAccount = RequestAccount
};
string str_PostData = JsonConvert.SerializeObject(o_Temp);
Hashtable hashtable = new Hashtable
{
{ "requestURL", ApiUrl+"/VoucherVerification" },
{ "postData", str_PostData },
{ "contentType", "application/json" }
};
Model.GSSYCouponModel cm_Result = new Model.GSSYCouponModel { ResultCode = "0" };
try
{
//调用接口获取交易结果
string retString = SoapWSHelper.QuerySoapWebServiceString(webServiceUrl, "MobilePayProxy", hashtable);
cm_Result = JsonConvert.DeserializeObject(retString);
LogHelper.WriteServiceLog($"高速石油券核销接口调用成功。接口返回值:{retString}");
}
catch (Exception ex)
{
LogHelper.WriteServiceLog($"高速石油券核销接口调用失败:{ex.Message}");
}
return cm_Result.ResultCode == "1";
}
///
/// 券撤销接口
///
///
///
public bool VoucherCancel(string webServiceUrl, string serverPartCode, string shopCode, string couponCode)
{
object o_Temp = new
{
//待验证的券号
voucherNumber = couponCode,
//门店号 + 账号 + 密钥 MD5加密32位大写
signParam = ApiParamSign(serverPartCode + shopCode + RequestAccount, "200DAEE7E80DB68E4E194ACC762DAF82"),
//门店编号
storeNumber = serverPartCode + shopCode,
//请求账号(固定值:gssy_ys)
requestAccount = RequestAccount
};
string str_PostData = JsonConvert.SerializeObject(o_Temp);
Hashtable hashtable = new Hashtable
{
{ "requestURL", ApiUrl+"/VoucherCancel" },
{ "postData", str_PostData },
{ "contentType", "application/json" }
};
Model.GSSYCouponModel cm_Result = new Model.GSSYCouponModel { ResultCode = "0" };
try
{
//调用接口获取交易结果
string retString = SoapWSHelper.QuerySoapWebServiceString(webServiceUrl, "MobilePayProxy", hashtable);
cm_Result = JsonConvert.DeserializeObject(retString);
LogHelper.WriteServiceLog($"高速石油券撤销接口调用成功。接口返回值:{retString}");
}
catch (Exception ex)
{
LogHelper.WriteServiceLog($"高速石油券撤销接口调用失败:{ex.Message}");
}
return cm_Result.ResultCode == "1";
}
///
/// 接口参数签名
///
/// 待签名参数集
/// 签名密钥
///
private string ApiParamSign(string signParm, string MD5Key)
{
using (MD5 _MD5 = new MD5CryptoServiceProvider())
{
return BitConverter.ToString(_MD5.ComputeHash(
Encoding.UTF8.GetBytes(signParm + MD5Key))).Replace("-", "").ToUpper();
}
}
#region AES加密
///
/// AES加密
///
/// 明文
/// 密钥,长度为16的字符串
/// 偏移量,长度为16的字符串
///
/// True:输出Hex格式密文
/// False:输出Base64格式密文
///
/// 密文
public string AESEncrypt(string text, string key, string iv = "", bool isHex = false)
{
//对密钥先进行一次MD5加密
MD5 md5 = new MD5CryptoServiceProvider();
byte[] pwdBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
//使用加密后的密钥对数据进行AES加密
RijndaelManaged rijndaelCipher = new RijndaelManaged
{
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
Key = pwdBytes
};
if (!string.IsNullOrWhiteSpace(iv))
{
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
rijndaelCipher.IV = ivBytes;//需要IV的启用这两句
}
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = Encoding.UTF8.GetBytes(text);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
if (isHex)
return ToHex(cipherBytes);//输出为hex即启用此句,注释上一句
else
return Convert.ToBase64String(cipherBytes);//输出为Base64即启用此句,注释下一句
}
#endregion
#region AES解密
///
/// AES解密
///
/// 密文
/// 密钥,长度为16的字符串
/// 偏移量,长度为16的字符串
///
/// True:输入Hex格式密文
/// False:输入Base64格式密文
/// 明文
public string AESDecrypt(string text, string key, string iv = "", bool isHex = false)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] pwdBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
RijndaelManaged rijndaelCipher = new RijndaelManaged
{
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
Key = pwdBytes
};
if (!string.IsNullOrWhiteSpace(iv))
{
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
rijndaelCipher.IV = ivBytes;//需要IV的启用这两句
}
byte[] encryptedData;
if (isHex)
encryptedData = UnHex(text);//输出为hex即启用此句,注释上一句
else
encryptedData = Convert.FromBase64String(text);//输出为Base64即启用此句,注释下一句
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
#endregion
#region Hex与byte转码
///
/// 从字符串转换到16进制表示的字符串
///
/// 需要转码的byte
/// 返回结果
private string ToHex(byte[] bytes)
{
string str = string.Empty;
if (bytes != null || bytes.Length > 0)
{
for (int i = 0; i < bytes.Length; i++)
{
str += string.Format("{0:X2}", bytes[i]);
}
}
return str.ToLower();
}
///
/// 从16进制转换成utf编码的字符串
///
/// 需要转码的hex
///
private byte[] UnHex(string hex)
{
if (hex == null)
throw new ArgumentNullException("hex");
hex = hex.Replace(",", "");
hex = hex.Replace("\n", "");
hex = hex.Replace("\\", "");
hex = hex.Replace(" ", "");
if (hex.Length % 2 != 0)
{
hex += "20";//空格
throw new ArgumentException("hex is not a valid number!", "hex");
}
// 需要将 hex 转换成 byte 数组。
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
try
{
// 每两个字符是一个 byte。
bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
System.Globalization.NumberStyles.HexNumber);
}
catch
{
// Rethrow an exception with custom message.
throw new ArgumentException("hex is not a valid hex number!", "hex");
}
}
return bytes;
}
#endregion
#region 方法 -> 打印油品优惠领取券
///
/// 打印油品优惠领取券
///
///
///
public bool ZJCouponPrint(string orderCode)
{
//9.28起活动结束,不再打印领券二维码
if (DateTime.Now >= new DateTime(2022, 09, 28)) return true;
//初始化二维码图片对象
ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = 400,
Height = 400
}
};
Thread.Sleep(300);
PrintDeviceHelper.PrintList(new List
{
PrintDeviceHelper.CreateLeftLine("微信扫码领取加油红包:"),
PrintDeviceHelper.CreateLeftLine("1、汽油满150元减3元"),
PrintDeviceHelper.CreateLeftLine("2、汽油满300元减6元"),
PrintDeviceHelper.CreateLeftLine("3、汽油满400元减9元")
}, 1, 1, false);
Thread.Sleep(300);
string str_CouponQRCode = $"https://eshangtech.com/zj?c=YS3{orderCode}&t=3";
System.Drawing.Bitmap bitmap = writer.Write(str_CouponQRCode);
PrintDeviceHelper.PrintQRCode(bitmap);
LogHelper.WriteServiceLog($"【{DateTime.Now}】小票号:{orderCode};150-3油券信息:{str_CouponQRCode}");
Thread.Sleep(300);
str_CouponQRCode = $"https://eshangtech.com/zj?c=YS6{orderCode}&t=6";
bitmap = writer.Write(str_CouponQRCode);
PrintDeviceHelper.PrintQRCode(bitmap);
LogHelper.WriteServiceLog($"【{DateTime.Now}】小票号:{orderCode};300-6油券信息:{str_CouponQRCode}");
Thread.Sleep(300);
str_CouponQRCode = $"https://eshangtech.com/zj?c=YS9{orderCode}&t=9";
bitmap = writer.Write(str_CouponQRCode);
PrintDeviceHelper.PrintQRCode(bitmap);
Thread.Sleep(300);
PrintDeviceHelper.PrintList(new List
{
PrintDeviceHelper.CreateCenterLine("二维码有效期24小时"),
PrintDeviceHelper.CreateCenterLine("领券后有效期7天,过期失效。" )
}, 1, 6, false);
LogHelper.WriteServiceLog($"【{DateTime.Now}】小票号:{orderCode};400-9油券信息:{str_CouponQRCode}");
return true;
}
#endregion
#region 方法 -> 上传报表
///
/// 上传报表
///
///
public bool ReportUpload(string webServiceUrl, string serverPartCode, string shopCode, string machineCode, string reportTime)
{
try
{
DateTime date_Report = DateTime.Parse(reportTime);
//9.28起活动结束,不再统计
if (date_Report >= new DateTime(2022, 10, 01)) return true;
DataTable table_ActiveLogging = SyBaseHelper.QueryOdbc(
$@"Select ActiveLogging_ID From T_ActiveLogging
Where Logging_Type = 9009 And Logging_Date = DateTime('{date_Report}')").Tables[0];
if (table_ActiveLogging.Rows.Count == 0)
{
//客单总数
int int_TicketCount = 0;
decimal dec_SellAmount = 0;
decimal dec_ZJCoupon = 0;
decimal dec_CouponCount = 0;
decimal dec_CouponAmount = 0;
//读取流水记录
string str_SelectSQL = $@"Select SellData_Date,Sum(IsNull(FactAmount,0)) As SellMaster_Amount,
Sum(IsNull(CostBill,0)) As CouponPay From T_SellData
Where SellData_Date >= DateTime('{date_Report.ToString("yyyy/MM/dd HH:mm:ss")}') And
SellData_Date < DateTime('{date_Report.AddDays(1).ToString("yyyy/MM/dd HH:mm:ss")}')
Group By SellData_Date";
DataTable table_SellMaster = SyBaseHelper.QueryOdbc(str_SelectSQL).Tables[0];
if (table_SellMaster.Rows.Count > 0)
{
int_TicketCount = table_SellMaster.Rows.Count;
object object_Temp;
//销售总额
object_Temp = table_SellMaster.Compute("Sum(SellMaster_Amount)", "");
if (object_Temp != null && !string.IsNullOrWhiteSpace(object_Temp.ToString()))
dec_SellAmount = (decimal)object_Temp;
//发券客单数
object_Temp = table_SellMaster.Compute("Count(SellData_Date)", "SellMaster_Amount >= 37");
if (object_Temp != null && !string.IsNullOrWhiteSpace(object_Temp.ToString()))
dec_ZJCoupon = (int)object_Temp;
//用券客单数量
object_Temp = table_SellMaster.Compute("Count(SellData_Date)", "CouponPay <> 0");
if (object_Temp != null && !string.IsNullOrWhiteSpace(object_Temp.ToString()))
dec_CouponCount = (int)object_Temp;
//用券销售金额
object_Temp = table_SellMaster.Compute("Sum(SellMaster_Amount)", "CouponPay <> 0");
if (object_Temp != null && !string.IsNullOrWhiteSpace(object_Temp.ToString()))
dec_CouponAmount = (decimal)object_Temp;
}
//客单总数|销售总额|发券客单数量|用券客单数量|用券销售金额
string str_NotifyMessage = $"{date_Report.ToString("yyyyMMdd")}|{int_TicketCount}|{dec_SellAmount}|{dec_ZJCoupon}|{dec_CouponCount}|{dec_CouponAmount}";
//日志接口
string str_HttpURL = $"http://pos.eshangtech.com:7198/webApi_publish/WebSocket/RecordLogToOracle";
//发送数据
string str_PostData = $"ServerPartCode={serverPartCode}&ShopCode={shopCode}&" +
$"MachineCode={machineCode}&LogCentent={str_NotifyMessage}&LogType=1&" +
$"TableName=9009&OperateTime={DateTime.Now.ToString("yyyyMMddHHmmss")}";
//拼接服务参数
Hashtable hashtable = new Hashtable
{
{ "requestURL", str_HttpURL+"?"+str_PostData },
{ "postData", str_PostData },
{ "contentType", "application/x-www-form-urlencoded" }
};
//调用接口上传记录
string retString = SoapWSHelper.QuerySoapWebServiceString(webServiceUrl, "MobilePayProxy", hashtable);
int int_ActiveLoggingID = SyBaseHelper.CreateNextSequence("T_ActiveLogging", "ActiveLogging_ID");
string str_InsertSQL = $@"Insert Into T_ActiveLogging (ActiveLogging_ID,Logging_Date,Logging_Type,Logging_Desc)
Values ({int_ActiveLoggingID},DateTime('{date_Report}'),9009,'{str_NotifyMessage}')";
SyBaseHelper.ExecuteSqlTran(str_InsertSQL);
LogHelper.WriteServiceLog($"交易统计日志上报成功。日志内容:{str_NotifyMessage};接口返回值:{retString}");
}
return true;
}
catch (Exception ex)
{
LogHelper.WriteServiceLog($"交易统计日志上报失败:{ex.Message}");
}
return false;
}
#endregion
#region 方法 -> 浙江商业集团会员优惠券核销
///
/// 浙江商业集团会员优惠券核销(根据动态券码)
///
///
///
///
///
///
///
///
public string GetByCode(string webServiceUrl, string storeCode, string couponCode, string orderCode, decimal orderAmount, string goodsTable)
{
Model.GSSYCouponModel cm_Result = new Model.GSSYCouponModel { ResultCode = "0" };
var query = ZJZCGCMemberHelper.GetByCode(webServiceUrl, storeCode, couponCode, orderCode, orderAmount, goodsTable);
if (query.Success)
{
var pay = ZJZCGCMemberHelper.ImmediateCost(webServiceUrl, storeCode, query.Data.CouponCode, orderCode, orderAmount, goodsTable);
cm_Result.ResultCode = pay.Success ? "1" : "0";
cm_Result.VoucherNumber = query.Data.CouponCode;
cm_Result.VoucherTypeName = query.Data.CouponName;
cm_Result.FaceAmount = query.Data.CouponValue;
cm_Result.RequestAccount = query.Data.UserCode;
}
return JsonConvert.SerializeObject(cm_Result);
}
#endregion
#region 方法 -> 浙江商业集团会员可用优惠券查询
///
/// 浙江商业集团会员可用优惠券查询
///
///
///
///
///
///
///
///
public string GetByMemberCode(string webServiceUrl, string storeCode, string dynamicCode, string orderCode, decimal orderAmount, string goodsTable)
{
List cm_Result = new List();
var query = ZJZCGCMemberHelper.GetByMemberCode(webServiceUrl, storeCode, dynamicCode, orderCode, orderAmount, goodsTable);
if (query.Success && query.Data != null)
{
cm_Result = query.Data;
}
return JsonConvert.SerializeObject(cm_Result);
}
#endregion
#region 方法 -> 浙江商业集团会员信息查询
///
/// 浙江商业集团会员信息查询
///
///
///
///
public string GetMemberInfo(string webServiceUrl, string dynamicCode, string phone)
{
string str_MemberCode = "";
var info = ZJZCGCMemberHelper.MemberInfo(webServiceUrl, dynamicCode, phone);
if (info.Success && info.Data != null)
{
str_MemberCode = info.Data.MemberCode;
}
return str_MemberCode;
}
#endregion
#region 方法 -> 浙江商业集团会员优惠券核销(根据系统券码)
///
/// 浙江商业集团会员优惠券核销(根据系统券码)
///
///
///
///
///
///
///
///
public string ImmediateCost(string webServiceUrl, string storeCode, string couponCode, string orderCode, decimal orderAmount, string goodsTable)
{
Model.GSSYCouponModel cm_Result = new Model.GSSYCouponModel { ResultCode = "0" };
var pay = ZJZCGCMemberHelper.ImmediateCost(webServiceUrl, storeCode, couponCode, orderCode, orderAmount, goodsTable);
if (pay.Success)
{
cm_Result.ResultCode = pay.Success ? "1" : "0";
cm_Result.VoucherNumber = pay.Data.CouponCode;
cm_Result.VoucherTypeName = pay.Data.CouponName;
cm_Result.FaceAmount = pay.Data.CouponValue;
cm_Result.RequestAccount = pay.Data.UserCode;
}
return JsonConvert.SerializeObject(cm_Result);
}
#endregion
#region 方法 -> 浙江商业集团会员优惠券撤销
///
/// 浙江商业集团会员优惠券撤销
///
/// 服务地址
/// 待撤销的券码
///
public bool Undo(string webServiceUrl, string couponCode)
{
var undo = ZJZCGCMemberHelper.Undo(webServiceUrl, couponCode);
return undo.Success;
}
///
/// 浙江商业集团会员优惠券订单同步
///
/// 服务地址
/// 门店ID
/// 会员编码
/// 订单编码
/// 交易时间(格式:yyyy-MM-dd HH:mm:ss)
/// 实付金额
/// 系统优惠金额
/// 券抵扣金额
/// 付款明细
/// 券明细
/// 商品明细
///
public bool ThirdOrderSync(string webServiceUrl, string storeCode, string userCode, string orderCode, string orderDate,
decimal orderAmount, decimal discountAmount, decimal couponAmount, string payInfo, string couponInfo, string goodsTable)
{
var thirdOrderSync = ZJZCGCMemberHelper.ThirdOrderSync(webServiceUrl, storeCode,
userCode, orderCode, orderDate, orderAmount, discountAmount, couponAmount, payInfo, couponInfo, goodsTable);
return thirdOrderSync.Success;
}
#endregion
#region 方法 -> 券核销统计和上传控制日志处理
///
/// 券核销统计和上传控制日志处理
///
public void BusinessLogCheck(string webServiceUrl, string serverpartCode, string shopCode, string machineCode)
{
new System.Threading.Tasks.Task(() =>
{
string str_MessageLog = "";
try
{
//拼接完整接口地址
string str_HttpURL = $"http://pos.eshangtech.com:7198/webApi_publish/BusinessLog/GetBusinessLogList";
//拼接接口参数字符串,读取当前收银机需要处理的数据传输异常日志
string str_PostData = $"ServerpartCode={serverpartCode}&ShopCode={shopCode}" +
$"&MachineCode={machineCode}&CheckState=0";
//拼接服务参数
Hashtable hashtable = new Hashtable
{
{ "requestURL", str_HttpURL+"?"+str_PostData },
{ "postData", str_PostData },
{ "contentType", "application/x-www-form-urlencoded" }
};
//调用接口并解析返回的数据
//调用接口上传记录
string retString = SoapWSHelper.QuerySoapWebServiceString(webServiceUrl, "MobilePayProxy", hashtable);
ResultListModel rlm_ResultModel = JsonConvert.DeserializeObject>(retString);
if (rlm_ResultModel.Result_Data.List.Count > 0)
{
//对数据传输差异记录进行逐条分类处理
foreach (var blm_BusinessLog in rlm_ResultModel.Result_Data.List)
{
str_MessageLog += $"日志内容:{blm_BusinessLog.BusinessLog_Content}\r\n";
//这里对需要重新上传数据文件的记录进行处理
switch (blm_BusinessLog.BusinessLog_Type)
{
case 5://统计指定时间的优惠券使用情况
if (DateTime.TryParseExact(blm_BusinessLog.Statistics_Date.ToString(), "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date_Analysis))
{
//生成当天统计截止时间参数
DateTime date_Today = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, date_Analysis.Hour, date_Analysis.Minute, date_Analysis.Second);
//在指定日期内,每天定时执行一次
if (date_Analysis.Date >= DateTime.Today && date_Today < DateTime.Now)
{
//生成指定时间统计数据并上传到云端
CreateYsCouponAnalysis(webServiceUrl, serverpartCode, shopCode, machineCode, date_Today.ToString(), 3000);
}
}
break;
case 6://上传指定日期的优惠券核销记录(时间格式:yyyyMMdd)
if (DateTime.TryParseExact(blm_BusinessLog.Statistics_Date.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date_Sync))
{
//上传指定日期全部优惠券核销明细数据
SynchroYsCoupon(webServiceUrl, date_Sync);
}
break;
}
}
}
}
catch (Exception ex)
{
str_MessageLog += $"数据传输异常日志查询处理失败。原因:{ex.Message}\r\n";
}
//本地记录文本日志
LogHelper.WriteServiceLog(str_MessageLog);
}).Start();
}
#endregion
#region 方法 -> 券使用情况统计
///
/// 券使用情况统计
///
/// 统计截止时间
/// 统计类型(1000:账期统计;2000:自然日统计;3000:指定时间统计)
///
public bool CreateYsCouponAnalysis(string webServiceUrl, string serverpartCode, string shopCode, string machineCode, string analysisDate, int analysisType)
{
try
{
//数据唯一标识
DateTime date_Analysis = DateTime.Parse(analysisDate);
string str_CouponAnalysisCode = serverpartCode + shopCode + machineCode + date_Analysis.ToString("yyyyMMddHHmmss") + analysisType.ToString("F0");
LogHelper.WriteServiceLog($"收到统计请求【{analysisDate}】,类型:{analysisType}");
//查询是否已经统计过指定时间指定类型的数据
string str_SelectSQL = $@"Select CouponAnalysis_Code From T_YsCouponAnalysis
Where CouponAnalysis_Code Like '{str_CouponAnalysisCode}%' ";
DataTable table_YsCouponAnalysis = SyBaseHelper.QueryOdbc(str_SelectSQL).Tables[0];
//指定时间类型的统计数据未生成,开始生成数据
if (table_YsCouponAnalysis.Rows.Count == 0)
{
//计算时间范围内交易客单数、交易总金额
string str_Select = $@"Select Count(SellData_Date) As TicketCount,
IsNull(Sum(FactAmount),0) As TotalAmount
From T_SellData Where ServerpartCode = '{serverpartCode}' And
ShopCode = '{shopCode}' And MachineCode = '{machineCode}' And
SellData_Date >= DateTime('{date_Analysis.Date.ToString("yyyy/MM/dd HH:mm:ss")}') And
SellData_Date <= DateTime('{date_Analysis.ToString("yyyy/MM/dd HH:mm:ss")}')
Group By SellData_Date";
DataTable table_SellMaster = SyBaseHelper.QueryOdbc(str_Select).Tables[0];
//交易客单数
decimal dec_TicketCount = 0;
//交易总金额
decimal dec_TotalAmount = 0;
if (table_SellMaster.Rows.Count > 0)
{
dec_TicketCount = decimal.Parse(table_SellMaster.Rows[0]["TicketCount"].ToString());
dec_TotalAmount = decimal.Parse(table_SellMaster.Rows[0]["TotalAmount"].ToString());
}
//计算时间范围内,各优惠券使用数量、抵扣金额、交易金额
str_Select = $@"Select Coupon_Name,Count(CouponPay_Code) As CouponCount,
Sum(Coupon_Amount) As CouponAmount,Sum(SellMaster_Amount) As CouponTotalAmount
From T_YsCouponPay Where ServerpartCode = '{serverpartCode}' And
ShopCode = '{shopCode}' And MachineCode = '{machineCode}' And
CouponPay_Date >= {date_Analysis.Date.ToString("yyyyMMddHHmmss")} And
CouponPay_Date <= {date_Analysis.ToString("yyyyMMddHHmmss")} And CouponPay_State = 1
Group By Coupon_Name";
DataTable table_YsCouponPay = SyBaseHelper.QueryOdbc(str_Select).Tables[0];
if (table_YsCouponPay.Rows.Count > 0)
{
List list_InsertSQL = new List();
for (int i = 0; i < table_YsCouponPay.Rows.Count; i++)
{
//拼接统计数据插入SQL语句
list_InsertSQL.Add($@"Insert Into T_YsCouponAnalysis (CouponAnalysis_Code,ServerpartCode,
Serverpart_Name,ShopCode,ShopName,MachineCode,CouponAnalysis_Date,TicketCount,
TotalAmount,CouponCount,CouponAmount,CouponTotalAmount,CouponName,CouponAnalysis_Type)
Values ('{str_CouponAnalysisCode + i.ToString().PadLeft(4, '0')}','{serverpartCode}','','{shopCode}','',
'{machineCode}',{date_Analysis.ToString("yyyyMMddHHmmss")},{dec_TicketCount},{dec_TotalAmount},
{table_YsCouponPay.Rows[i]["CouponCount"].ToString()},{table_YsCouponPay.Rows[i]["CouponAmount"].ToString()},
{table_YsCouponPay.Rows[i]["CouponTotalAmount"]},'{table_YsCouponPay.Rows[i]["Coupon_Name"].ToString()}',{analysisType})");
}
//保存数据到数据库
SyBaseHelper.ExecuteSqlTran(list_InsertSQL);
LogHelper.WriteServiceLog($"【{analysisDate}】统计类型:{analysisType} ,共生成{list_InsertSQL.Count}条数据");
}
}
//统计结束,启动数据上传
new System.Threading.Tasks.Task(() => SynchroYsCouponAnalysis(webServiceUrl)).Start();
return true;
}
catch (Exception ex)
{
LogHelper.WriteServiceLog($"【{analysisDate}】券使用统计失败。原因:{ex.Message}");
return false;
}
}
///
/// 上传优惠券统计数据到云端
///
private void SynchroYsCouponAnalysis(string webServiceUrl)
{
int i_Success = 0;
int i_Failure = 0;
//读取全部未上传的优惠券统计数据
string str_SelectSQL = $@"Select CouponAnalysis_Code,ServerpartCode,Serverpart_Name,ShopCode,
ShopName,MachineCode,CouponAnalysis_Date,TicketCount,TotalAmount,CouponCount,
CouponAmount,CouponTotalAmount,CouponName,CouponAnalysis_Type
From T_YsCouponAnalysis Where IsNull(Transfer_State,0) = 0";
DataTable table_YsCouponAnalysis = SyBaseHelper.QueryOdbc(str_SelectSQL).Tables[0];
if (table_YsCouponAnalysis.Rows.Count > 0)
{
//逐条拼接上传到云端
foreach (DataRow dataRow in table_YsCouponAnalysis.Rows)
{
//拼接接口数据格式
object o_Data = new
{
CouponAnalysis_Code = dataRow["CouponAnalysis_Code"].ToString(),
CouponAnalysis_Type = int.Parse(dataRow["CouponAnalysis_Type"].ToString()),
Serverpart_Code = dataRow["ServerpartCode"].ToString(),
Serverpart_Name = dataRow["Serverpart_Name"].ToString(),
ShopCode = dataRow["ShopCode"].ToString(),
ShopName = dataRow["ShopName"].ToString(),
MachineCode = dataRow["MachineCode"].ToString(),
CouponAnalysis_Date = long.Parse(dataRow["CouponAnalysis_Date"].ToString()),
TicketCount = int.Parse(dataRow["TicketCount"].ToString()),
TotalAmount = decimal.Parse(dataRow["TotalAmount"].ToString()),
CouponCount = int.Parse(dataRow["CouponCount"].ToString()),
CouponAmount = decimal.Parse(dataRow["CouponAmount"].ToString()),
CouponTotalAmount = decimal.Parse(dataRow["CouponTotalAmount"].ToString()),
CouponName = dataRow["CouponName"].ToString()
};
//拼接完整接口地址
string str_HttpURL = $"http://pos.eshangtech.com:7198/webApi_publish/Member/SynchroYsCouponAnalysis";
//拼接接口参数字符串,读取当前收银机需要处理的数据传输异常日志
string str_PostData = JsonConvert.SerializeObject(o_Data);
//拼接服务参数
Hashtable hashtable = new Hashtable
{
{ "requestURL", str_HttpURL+"?"+str_PostData },
{ "postData", str_PostData },
{ "contentType", "application/json" }
};
try
{
//调用接口上传记录
string retString = SoapWSHelper.QuerySoapWebServiceString(webServiceUrl, "MobilePayProxy", hashtable);
//调用接口并解析返回的数据
ResultDataModel rdm_ResultModel = JsonConvert.DeserializeObject(retString);
//判断是否上传成功
if (rdm_ResultModel.Result_Code == 100)
{
i_Success++;
//更新上传成功状态
string str_UpdateSQL = $@"Update T_YsCouponAnalysis Set Transfer_State = 1
Where CouponAnalysis_Code = '{dataRow["CouponAnalysis_Code"].ToString()}'";
SyBaseHelper.ExecuteSqlTran(str_UpdateSQL);
}
else
{
i_Failure++;
}
}
catch (Exception ex)
{
i_Failure++;
LogHelper.WriteServiceLog($"券统计数据【{dataRow["CouponAnalysis_Code"].ToString()}】上传失败。原因:{ex.Message}");
}
}
LogHelper.WriteServiceLog($"券统计数据上传完成。共:{table_YsCouponAnalysis.Rows.Count}条,失败(等待下次重传):{i_Failure}条。");
}
}
#endregion
///
/// 上传指定日期优惠券核销数据
///
/// 指定的券核销日期
///
private bool SynchroYsCoupon(string webServiceUrl, DateTime couponPayDate)
{
string str_SelectSQL = $@"Select CouponPay_Code,ServerpartCode,ServerpartName,
ShopCode,ShopName,MachineCode,CouponPay_Date,Coupon_Code,
Coupon_Amount,SellMaster_Amount,Coupon_Name,CouponPay_State
From T_YsCouponPay Where IsNull(Transfer_State,0) = 0 And
CouponPay_Date >= {couponPayDate.Date.ToString("yyyyMMddHHmmss")} And
CouponPay_Date < {couponPayDate.Date.AddDays(1).ToString("yyyyMMddHHmmss")}";
DataTable table_YsCouponPay = SyBaseHelper.QueryOdbc(str_SelectSQL).Tables[0];
if (table_YsCouponPay.Rows.Count > 0)
{
//日志接口
string str_HttpURL = $"http://pos.eshangtech.com:7198/webApi_publish/WebSocket/RecordLogToOracle";
foreach (DataRow dataRow in table_YsCouponPay.Rows)
{
//订单号|订单时间|券编码|券名称|交易金额|券抵扣金额|券状态
string str_NotifyMessage = $"{dataRow["CouponPay_Code"].ToString()}|{dataRow["CouponPay_Date"].ToString()}" +
$"|{dataRow["Coupon_Code"].ToString()}|{dataRow["Coupon_Name"].ToString()}|{dataRow["SellMaster_Amount"].ToString()}" +
$"|{dataRow["Coupon_Amount"].ToString()}|{dataRow["CouponPay_State"].ToString()}";
//发送数据
string str_PostData = $"ServerPartCode={dataRow["ServerpartCode"].ToString()}&ShopCode={dataRow["ShopCode"].ToString()}&" +
$"MachineCode={dataRow["MachineCode"].ToString()}&LogCentent={str_NotifyMessage}&LogType=1&" +
$"TableName=9012&OperateTime={DateTime.Now.ToString("yyyyMMddHHmmss")}";
//拼接服务参数
Hashtable hashtable = new Hashtable
{
{ "requestURL", str_HttpURL+"?"+str_PostData },
{ "postData", str_PostData },
{ "contentType", "application/x-www-form-urlencoded" }
};
try
{
//调用接口上传记录
string retString = SoapWSHelper.QuerySoapWebServiceString(webServiceUrl, "MobilePayProxy", hashtable);
//调用接口并解析返回的数据
ResultDataModel rdm_ResultModel = JsonConvert.DeserializeObject(retString);
//判断是否上传成功
if (rdm_ResultModel.Result_Code == 100)
{
//更新上传成功状态
string str_UpdateSQL = $@"Update T_YsCouponPay Set Transfer_State = 1
Where CouponPay_Code = '{dataRow["CouponPay_Code"].ToString()}'";
SyBaseHelper.ExecuteSqlTran(str_UpdateSQL);
}
}
catch (Exception ex)
{
LogHelper.WriteServiceLog($"券核销记录【{dataRow["CouponPay_Code"].ToString()}】上传失败。原因:{ex.Message}");
}
}
}
return true;
}
///
/// 返回object数据对象使用
///
private class ResultDataModel
{
public int Result_Code { get; set; }
public string Result_Desc { get; set; }
public string Result_Data { get; set; }
}
}
}