using System;
|
using System.IO;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Net;
|
using Newtonsoft.Json;
|
using Microsoft.Practices.EnterpriseLibrary.Data;
|
using System.Data;
|
using System.Data.Sql;
|
using Rdream.Core.Model;
|
using System.Text.RegularExpressions;
|
using System.Runtime.Serialization.Formatters.Binary;
|
using System.Net.Sockets;
|
|
|
|
namespace Rdream.Core.DataHandler
|
{
|
public class SendMsgByAccessControlRecord
|
{
|
Database da = DatabaseFactory.CreateDatabase("ConnectionString"); //人脸识别系统数据源
|
//主服务URL
|
public static string mainServiceUrl = System.Configuration.ConfigurationManager.AppSettings["mainServiceUrl"];
|
|
//学生门禁记录推送企业微信URL
|
public static string _sendMsgByAccessControlRecordUrl = System.Configuration.ConfigurationManager.AppSettings["sendMsgByAccessControlRecordUrl"];
|
string sendMsgByAccessControlRecordUrl = mainServiceUrl + _sendMsgByAccessControlRecordUrl;
|
|
//检查服务端门禁人脸图片文件前是否存在URL
|
public static string _checkDoorImageUrl = System.Configuration.ConfigurationManager.AppSettings["checkDoorImageUrl"];
|
string checkDoorImageUrl = mainServiceUrl + _checkDoorImageUrl;
|
|
//上传门禁系统人脸图片URL
|
public static string _uploadDoorImageUrl = System.Configuration.ConfigurationManager.AppSettings["uploadDoorImageUrl"];
|
string uploadDoorImageUrl = mainServiceUrl + _uploadDoorImageUrl;
|
|
public static string corpId = System.Configuration.ConfigurationManager.AppSettings["CorpId"];
|
|
//门禁设备供应商:1-依时利;2-中控
|
public static string _device_type = System.Configuration.ConfigurationManager.AppSettings["supplier"];
|
//SQL:数据获取
|
public static string sql_extract_1 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_1"];
|
public static string sql_extract_2 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_2"];
|
public static string sql_extract_3 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_3"];
|
public static string sql_extract_4 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_4"];
|
public static string sql_extract_5 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_5"];
|
public static string sql_extract_6 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_6"];
|
public static string sql_extract_7 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_7"];
|
public static string sql_extract_8 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_8"];
|
public static string sql_extract_9 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_9"];
|
public static string sql_extract_judge_1 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_judge_1"];
|
public static string sql_extract_judge_2 = System.Configuration.ConfigurationManager.AppSettings["sql_extract_judge_2"];
|
|
//SQL:微信发送检验
|
public static string sql_WXCheck_def_1 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_def_1"];
|
public static string sql_WXCheck_def_2 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_def_2"];
|
public static string sql_WXCheck_def_3 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_def_3"];
|
public static string sql_WXCheck_def_4 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_def_4"];
|
public static string sql_WXCheck_def_5 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_def_5"];
|
//SQL:微信发送检验、相同方向间隔时间控制
|
public static string sql_WXCheck_judge1_1 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_1"];
|
public static string sql_WXCheck_judge1_2 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_2"];
|
public static string sql_WXCheck_judge1_3 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_3"];
|
public static string sql_WXCheck_judge1_4 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_4"];
|
public static string sql_WXCheck_judge1_5 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_5"];
|
public static string sql_WXCheck_judge1_6 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_6"];
|
public static string sql_WXCheck_judge1_7 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_7"];
|
public static string sql_WXCheck_judge1_8 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_8"];
|
public static string sql_WXCheck_judge1_9 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge1_9"];
|
//SQL:微信发送检验、相反方向间隔时间控制
|
public static string sql_WXCheck_judge2_1 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_1"];
|
public static string sql_WXCheck_judge2_2 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_2"];
|
public static string sql_WXCheck_judge2_3 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_3"];
|
public static string sql_WXCheck_judge2_4 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_4"];
|
public static string sql_WXCheck_judge2_5 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_5"];
|
public static string sql_WXCheck_judge2_6 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_6"];
|
public static string sql_WXCheck_judge2_7 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_7"];
|
public static string sql_WXCheck_judge2_8 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_8"];
|
public static string sql_WXCheck_judge2_9 = System.Configuration.ConfigurationManager.AppSettings["sql_WXCheck_judge2_9"];
|
|
//SQL:短信发送检验
|
public static string sql_SMSCheck_def_1 = System.Configuration.ConfigurationManager.AppSettings["sql_SMSCheck_def_1"];
|
public static string sql_SMSCheck_def_2 = System.Configuration.ConfigurationManager.AppSettings["sql_SMSCheck_def_2"];
|
public static string sql_SMSCheck_def_3 = System.Configuration.ConfigurationManager.AppSettings["sql_SMSCheck_def_3"];
|
public static string sql_SMSCheck_def_4 = System.Configuration.ConfigurationManager.AppSettings["sql_SMSCheck_def_4"];
|
public static string sql_SMSCheck_def_5 = System.Configuration.ConfigurationManager.AppSettings["sql_SMSCheck_def_5"];
|
//SQL:短信发送检验、间隔时间控制
|
public static string sql_SMSCheck_judge1_1 = System.Configuration.ConfigurationManager.AppSettings["sql_SMSCheck_judge1_1"];
|
public static string sql_SMSCheck_judge1_2 = System.Configuration.ConfigurationManager.AppSettings["sql_SMSCheck_judge1_2"];
|
|
//SQL:更新同步状态
|
public static string sql_update_SycFlag = System.Configuration.ConfigurationManager.AppSettings["sql_update_SycFlag"];
|
|
|
//生成图片规则1:学号 + "_" + 刷卡时间(yyyyMMddHHmmss)+ ".jpg"
|
public static string picture_file_name_rules_1 = System.Configuration.ConfigurationManager.AppSettings["picture_file_name_rules_1"];
|
//生成图片规则2:卡号 + 刷卡时间(yyyyMMddHHmmss)+ ".jpg"
|
public static string picture_file_name_rules_2 = System.Configuration.ConfigurationManager.AppSettings["picture_file_name_rules_2"];
|
|
|
//获取近5秒的门禁学生进出记录,注意该值必须大于轮循休眠时间
|
public static string seconds = System.Configuration.ConfigurationManager.AppSettings["seconds"];
|
//两次门禁考勤记录间相同方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
public static string _twoRecordSameTimes = System.Configuration.ConfigurationManager.AppSettings["twoRecordSameTimes"];
|
//两次门禁考勤记录间相反方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
public static string _twoRecordContraryTimes = System.Configuration.ConfigurationManager.AppSettings["twoRecordContraryTimes"];
|
|
//两次门禁考勤记录间在60秒(1分钟)内,不推送短信;为0时都推送。
|
public static string _twoRecordSMSTimes = System.Configuration.ConfigurationManager.AppSettings["twoRecordSMSTimes"];
|
|
//请假签到有效时间内1800秒(30分钟)
|
public static string _validTimeLeave = System.Configuration.ConfigurationManager.AppSettings["validTimeLeave"];
|
|
//人脸识别照片路径
|
public static string frpicPath = System.Configuration.ConfigurationManager.AppSettings["frpicPath"];
|
//刷卡抓拍路径
|
public static string doorPicPath = System.Configuration.ConfigurationManager.AppSettings["doorPicPath"];
|
|
//温度控制不推送消息,由于门禁配置大于等于这个温度不开门。
|
public static string _temperature = System.Configuration.ConfigurationManager.AppSettings["_temperature"];
|
|
//<!-- 走读生类型 -->
|
public static string extern_student = System.Configuration.ConfigurationManager.AppSettings["extern_student"];
|
//<!-- 住宿生类型 -->
|
public static string boarder_student = System.Configuration.ConfigurationManager.AppSettings["boarder_student"];
|
|
//<!-- 时间段规则如下,时间跨度以连接符“~”连接,多个时间以分号“;”分隔-->
|
//<!-- 走读生、进门、周一到周日推送消息时间段 -->
|
public static string in_extern_monday = System.Configuration.ConfigurationManager.AppSettings["in_extern_monday"];
|
public static string in_extern_tuesday = System.Configuration.ConfigurationManager.AppSettings["in_extern_tuesday"];
|
public static string in_extern_wednesday = System.Configuration.ConfigurationManager.AppSettings["in_extern_wednesday"];
|
public static string in_extern_thursday = System.Configuration.ConfigurationManager.AppSettings["in_extern_thursday"];
|
public static string in_extern_friday = System.Configuration.ConfigurationManager.AppSettings["in_extern_friday"];
|
public static string in_extern_saturday = System.Configuration.ConfigurationManager.AppSettings["in_extern_saturday"];
|
public static string in_extern_sunday = System.Configuration.ConfigurationManager.AppSettings["in_extern_sunday"];
|
|
//<!-- 走读生、出门、周一到周日出门推送消息时间段 -->
|
public static string out_extern_monday = System.Configuration.ConfigurationManager.AppSettings["out_extern_monday"];
|
public static string out_extern_tuesday = System.Configuration.ConfigurationManager.AppSettings["out_extern_tuesday"];
|
public static string out_extern_wednesday = System.Configuration.ConfigurationManager.AppSettings["out_extern_wednesday"];
|
public static string out_extern_thursday = System.Configuration.ConfigurationManager.AppSettings["out_extern_thursday"];
|
public static string out_extern_friday = System.Configuration.ConfigurationManager.AppSettings["out_extern_friday"];
|
public static string out_extern_saturday = System.Configuration.ConfigurationManager.AppSettings["out_extern_saturday"];
|
public static string out_extern_sunday = System.Configuration.ConfigurationManager.AppSettings["out_extern_sunday"];
|
|
//<!-- 住宿生、进门、周一到周日推送消息时间段 -->
|
public static string in_boarder_monday = System.Configuration.ConfigurationManager.AppSettings["in_boarder_monday"];
|
public static string in_boarder_tuesday = System.Configuration.ConfigurationManager.AppSettings["in_boarder_tuesday"];
|
public static string in_boarder_wednesday = System.Configuration.ConfigurationManager.AppSettings["in_boarder_wednesday"];
|
public static string in_boarder_thursday = System.Configuration.ConfigurationManager.AppSettings["in_boarder_thursday"];
|
public static string in_boarder_friday = System.Configuration.ConfigurationManager.AppSettings["in_boarder_friday"];
|
public static string in_boarder_saturday = System.Configuration.ConfigurationManager.AppSettings["in_boarder_saturday"];
|
public static string in_boarder_sunday = System.Configuration.ConfigurationManager.AppSettings["in_boarder_sunday"];
|
|
//<!-- 住宿生、出门、周一到周日推送消息时间段 -->
|
public static string out_boarder_monday = System.Configuration.ConfigurationManager.AppSettings["out_boarder_monday"];
|
public static string out_boarder_tuesday = System.Configuration.ConfigurationManager.AppSettings["out_boarder_tuesday"];
|
public static string out_boarder_wednesday = System.Configuration.ConfigurationManager.AppSettings["out_boarder_wednesday"];
|
public static string out_boarder_thursday = System.Configuration.ConfigurationManager.AppSettings["out_boarder_thursday"];
|
public static string out_boarder_friday = System.Configuration.ConfigurationManager.AppSettings["out_boarder_friday"];
|
public static string out_boarder_saturday = System.Configuration.ConfigurationManager.AppSettings["out_boarder_saturday"];
|
public static string out_boarder_sunday = System.Configuration.ConfigurationManager.AppSettings["out_boarder_sunday"];
|
|
|
private static SendMsgByAccessControlRecord _instance;
|
public static SendMsgByAccessControlRecord Instance
|
{
|
get
|
{
|
if (_instance == null)
|
{
|
_instance = new SendMsgByAccessControlRecord();
|
}
|
return _instance;
|
}
|
}
|
|
|
/**
|
* 调用掌骏后端接口发送家长企业消息和短信
|
*
|
* */
|
public ResponseMsgMo sendMsgByAccessControlRecord()
|
{
|
|
//在门禁记录表中增加字段dataSycFlag数据同步标志:0-未同步;1-同步中;2-已同步
|
//alter table eastRiver.dbo.PassTime add dataSycFlag int default 0;
|
//update eastRiver.dbo.PassTime set dataSycFlag = 0 where dataSycFlag is null;
|
|
//获取近2秒的门禁学生进出记录
|
/**
|
string sql = "select DISTINCT a.id, a.card_id, a.emp_id, a.door_id, a.passTime, a.inOutFlag, a.evenFlag "
|
//+ ", b.picture "
|
+ " from eastRiver.dbo.PassTime a, eastRiver.dbo.PassPicRecords b "
|
+ " where a.card_id = b.Card_No and a.passTime = b.PassTime "
|
+ " and a.EvenFlag in (0, 1, 2, 3, 4, 5, 6, 7, 211) "
|
+ " and DATEDIFF(ss, a.passTime, GETDATE() ) <= " + seconds + " ";
|
**/
|
|
/**
|
string sql = "select aa.*, b.status_id, b.enrollNumber "
|
+ " from ("
|
+ " select a.id, a.card_id, a.emp_id, a.door_id, a.temperature, a.passTime, a.inOutFlag, a.evenFlag "
|
+ ", b.picture "
|
+ " from eastRiver.dbo.PassTime a, eastRiver.dbo.PassPicRecords b "
|
+ " where a.card_id = b.Card_No and a.passTime = b.PassTime "
|
+ " and a.EvenFlag in (0, 1, 2, 3, 4, 5, 6, 7, 211) "
|
+ " and DATEDIFF(ss, a.passTime, GETDATE() ) <= " + seconds + " "
|
+ " ) aa "
|
+ " left join [EastRiver].[dbo].[Employee] b "
|
+ " on aa.emp_id=b.emp_id ";**/
|
|
/**
|
//延时5秒获取记录
|
int _seconds = (seconds == null || seconds.Length <= 0) ? 0 : int.Parse(seconds);
|
_seconds += 5;
|
|
string sql = "select aa.*, b.status_id "
|
+ " from ("
|
+ " select a.id, a.card_id, a.emp_id, a.door_id, a.temperature, a.passTime, a.inOutFlag, a.evenFlag "
|
//+ ", b.picture "
|
+ " from eastRiver.dbo.PassTime a "
|
//+ " where a.EvenFlag in (0, 1, 2, 3, 4, 5, 6, 7, 211) "
|
+ " where 1=1 and dataSycFlag = 0 ";
|
if (_seconds > 0)
|
{
|
sql += " and DATEDIFF(ss, a.passTime, GETDATE() ) <= " + _seconds + " ";
|
sql += " and DATEDIFF(ss, a.passTime, GETDATE() ) >= " + 5 + " ";
|
}
|
sql += " ) aa ";
|
sql += " left join [EastRiver].[dbo].[Employee] b ";
|
sql += " on aa.emp_id=b.emp_id ";
|
sql += " order by aa.passTime ";
|
**/
|
|
|
//延时5秒获取记录
|
int _seconds = (seconds == null || seconds.Length <= 0) ? 0 : int.Parse(seconds);
|
_seconds += 5;
|
|
string sql = "";
|
if (sql_extract_1.Length>0)
|
{
|
sql += sql_extract_1;
|
}
|
if (sql_extract_2.Length > 0)
|
{
|
sql += sql_extract_2;
|
}
|
if (sql_extract_3.Length > 0)
|
{
|
sql += sql_extract_3;
|
}
|
if (sql_extract_4.Length > 0)
|
{
|
sql += sql_extract_4;
|
}
|
if (sql_extract_5.Length > 0)
|
{
|
sql += sql_extract_5;
|
}
|
|
string sql_extract_judge = "";
|
if (_seconds > 0)
|
{
|
sql_extract_judge += sql_extract_judge_1 + _seconds + " ";
|
//sql_extract_judge += sql_extract_judge_2 + 5 + " ";
|
sql_extract_judge += sql_extract_judge_2 ;
|
}
|
if (sql_extract_judge.Length > 0)
|
{
|
sql += sql_extract_judge;
|
}
|
|
if (sql_extract_6.Length > 0)
|
{
|
sql += sql_extract_6;
|
}
|
if (sql_extract_7.Length > 0)
|
{
|
sql += sql_extract_7;
|
}
|
if (sql_extract_8.Length > 0)
|
{
|
sql += sql_extract_8;
|
}
|
if (sql_extract_9.Length > 0)
|
{
|
sql += sql_extract_9;
|
}
|
|
|
//log4netService.Debug("sql:" + sql);
|
|
DataSet ds = da.ExecuteDataSet(System.Data.CommandType.Text, sql);
|
DataTable dt = ds.Tables[0];
|
List<AccessControlRecord> list = new List<AccessControlRecord>();
|
AccessControlRecord accessControlRecord = new AccessControlRecord();
|
|
//Console.WriteLine("待处理记录数:" + dt.Rows.Count);
|
if (dt.Rows.Count > 0)
|
{
|
foreach (DataRow dr in dt.Rows)
|
{
|
|
int id = int.Parse(dr["id"].ToString());
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 1);
|
|
string card_id = dr["card_id"].ToString(); //人事资料卡号
|
string emp_id = dr["emp_id"].ToString();
|
int door_id = dr["door_id"].ToString()=="" ? 0 : int.Parse(dr["door_id"].ToString());
|
|
double temperature = Convert.ToDouble(dr["temperature"].ToString());
|
double temperatureTmp = (_temperature == null || _temperature == "") ? 0.0 : Convert.ToDouble(_temperature);
|
|
|
//双精度误差
|
var DOUBLE_DELTA = 1E-6;
|
|
//温度控制不推送消息,由于门禁配置大于等于这个温度不开门。
|
if (temperature - temperatureTmp > DOUBLE_DELTA || temperature == temperatureTmp)
|
{
|
|
log4netService.Debug("门禁记录温度[" + temperature + "]大于等于控制温度[" + temperatureTmp + "]");
|
Console.WriteLine("门禁记录温度[" + temperature + "]大于等于控制温度[" + temperatureTmp + "]");
|
|
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 2);
|
continue;
|
}
|
DateTime passTime = Convert.ToDateTime(dr["passTime"].ToString());
|
int inOutFlag = dr["inOutFlag"].ToString() == "" ? 0 : int.Parse(dr["inOutFlag"].ToString());
|
int evenFlag = dr["evenFlag"].ToString() == "" ? 0 : int.Parse(dr["evenFlag"].ToString());
|
|
//两次门禁考勤记录间相同方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
int twoRecordSameTimes = (_twoRecordSameTimes == null || _twoRecordSameTimes == "") ? 0 : int.Parse(_twoRecordSameTimes);
|
//两次门禁考勤记录间相反方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
int twoRecordContraryTimes = (_twoRecordContraryTimes == null || _twoRecordContraryTimes == "") ? 0 : int.Parse(_twoRecordContraryTimes);
|
//两次门禁考勤记录间在60秒(1分钟)内,不推送短信;为0时都推送。
|
int twoRecordSMSTimes = (_twoRecordSMSTimes == null || _twoRecordSMSTimes == "") ? 0 : int.Parse(_twoRecordSMSTimes);
|
string status_id = dr["status_id"].ToString(); //学生类型:55 住宿;66 走读;其他(0,77,null)
|
int device_type = (_device_type == null || _device_type == "") ? 1 : int.Parse(_device_type); //门禁设备类型:1-依时利、2-中控
|
int validTimeLeave = (_validTimeLeave == null || _validTimeLeave == "") ? 0 : int.Parse(_validTimeLeave); //请假签到有效时间内1800秒(30分钟)
|
int isAttendanceSend = 1; //是否考勤发送微信消息:0-不推送; 1-推送。
|
int isAttendanceSMSSend = 1; //是否考勤发送短信消息:0-不推送; 1-推送。
|
string wxMessageFailReason = ""; //微信消息失败原因:如:黑名单限制、间隔时间限制、学生类型推送时间限制
|
string smsMessageFailReason = ""; //短信消息失败原因:如:黑名单限制、间隔时间限制、学生类型推送时间限制
|
int dealFlag = 0; //处理标志:0-未处理; 1-处理中;2-已处理.
|
//string enrollNumber = dr["enrollNumber"] == null ? null : dr["enrollNumber"].ToString(); //人事资料登记号
|
|
//byte[] imagePicture = dr["picture"] == null ? null : (byte[])dr["picture"];
|
//image转换为string
|
//string picture = imagePicture == null ? null : Convert.ToBase64String(imagePicture);
|
//string picture = "";
|
//byte[]转换为字符串
|
//picture = System.Text.ASCIIEncoding.Unicode.GetString(pictureTmp);
|
|
string fileName = "";
|
//人脸识别照片路径(人事资料登记号+时间命名)D:\Program Files (x86)\EastRiver\Documents\FRPic\[yyyyMMddhhmmss]
|
string enrollNumberFileName = frpicPath + "/" + passTime.ToString("yyyy-MM-dd") + "/" + emp_id + "_" + passTime.ToString("yyyyMMddHHmmss") + ".jpg";
|
|
//刷卡抓拍路径 (人事资料卡号+时间命名)D:\Program Files (x86)\EastRiver\Documents\DoorPic\[yyyyMMddhhmmss]
|
string cardIdFileName = doorPicPath + "/" + passTime.ToString("yyyyMMdd") + "/" + card_id + passTime.ToString("yyyyMMddHHmmss") + ".jpg";
|
|
//文件是否存在
|
if (File.Exists(enrollNumberFileName))
|
{
|
fileName = enrollNumberFileName;
|
}
|
else
|
{
|
|
if (File.Exists(cardIdFileName))
|
{
|
fileName = cardIdFileName;
|
}
|
else
|
{
|
fileName = null;
|
}
|
}
|
|
//Console.WriteLine("文件名:" + fileName);
|
if (fileName != null && fileName.Length > 0)
|
{
|
//上传图片文件前检查服务端文件是否存在。
|
Boolean fileCheckFlag = checkDoorImage(passTime.ToString("yyyyMMdd"), Path.GetFileName(fileName));
|
|
//检查通过时,上传图片
|
if (fileCheckFlag)
|
{
|
try
|
{
|
UploadRequest(passTime.ToString("yyyyMMdd"), fileName);
|
}
|
catch (Exception ex)
|
{
|
log4netService.Error("上传图片异常:" + ex.Message);
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 0);
|
continue;
|
}
|
|
log4netService.Info("上传门禁人脸识别图片成功,文件名:" + Path.GetFileName(fileName));
|
}
|
}
|
|
Boolean bl = false;
|
|
//log4netService.Debug("是学生类型:" + status_id + "温度:" + temperature);
|
Console.WriteLine("学生学号:" + emp_id + " 学生类型:" + status_id + " 温度:" + temperature);
|
if (passTime != null && (inOutFlag == 1 || inOutFlag == 2))
|
{
|
|
//第一步规则检查,
|
//两次门禁考勤记录间相同方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
//两次门禁考勤记录间相反方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
if (twoRecordSameTimes == 0 && twoRecordContraryTimes == 0)
|
{
|
isAttendanceSend = 1; //推送微信(门禁考勤)
|
}
|
else
|
{
|
//检查与本次刷卡的方向
|
int checkInOutFlag = 0 ;
|
/**
|
if (inOutFlag == 1)
|
{
|
checkInOutFlag = 2;
|
}
|
else if (inOutFlag == 2)
|
{
|
checkInOutFlag = 1;
|
}**/
|
checkInOutFlag = inOutFlag;
|
|
//门禁考勤系统记录检验,检查本次记录与上次记录的时间范围
|
bl = doTwoRecordTimesCheck(passTime, checkInOutFlag, twoRecordSameTimes, twoRecordContraryTimes, emp_id, id);
|
//Console.WriteLine("检查本次记录与上次记录的时间范围。。。。" + bl);
|
if (!bl)
|
{
|
isAttendanceSend = 0; //不推送微信(门禁考勤)
|
wxMessageFailReason = "间隔时间限制";
|
}
|
else
|
{
|
isAttendanceSend = 1; //推送微信(门禁考勤)
|
}
|
|
}
|
|
|
|
//第一步规则检查,
|
//两次门禁考勤记录间在60秒(1分钟)内,不推送短信;为0时都推送。
|
if (twoRecordSMSTimes == 0 )
|
{
|
isAttendanceSMSSend = 1; //推送短信(门禁考勤)
|
}
|
else
|
{
|
|
//门禁考勤系统记录检验,检查本次记录与上次记录的时间范围
|
bl = doTwoRecordTimesSMSCheck(passTime, twoRecordSMSTimes, emp_id, id);
|
if (!bl)
|
{
|
isAttendanceSMSSend = 0; //不推送微信(门禁考勤)
|
smsMessageFailReason = "间隔时间限制";
|
}
|
else
|
{
|
isAttendanceSMSSend = 1; //推送微信(门禁考勤)
|
}
|
//log4netService.Debug("isAttendanceSMSSend:" + isAttendanceSMSSend);
|
|
}
|
}
|
|
|
//是学生时,作住宿生、走读生按二次检查(周一到周日开放时间段)
|
//if (status_id == "55" || status_id == "66")
|
if (status_id == boarder_student || status_id == extern_student)
|
{
|
|
if (isAttendanceSend == 1)
|
{
|
//门禁考勤系统记录检验,根据刷卡或刷脸时间检查是否需要推送消息。
|
bl = doCheck(passTime, inOutFlag, status_id);
|
if (!bl)
|
{
|
isAttendanceSend = 0; //不推送微信(门禁考勤)
|
wxMessageFailReason = "学生类型推送时间限制";
|
}
|
else
|
{
|
isAttendanceSend = 1; //推送微信(门禁考勤)
|
|
}
|
}
|
|
//第一步规则检查结果为推送消息
|
if (isAttendanceSMSSend == 1)
|
{
|
//门禁考勤系统记录检验,根据刷卡或刷脸时间检查是否需要推送消息。
|
bl = doCheck(passTime, inOutFlag, status_id);
|
if (!bl)
|
{
|
isAttendanceSMSSend = 0; //不推送微信(门禁考勤)
|
smsMessageFailReason = "学生类型推送时间限制";
|
}
|
else
|
{
|
isAttendanceSMSSend = 1; //推送微信(门禁考勤)
|
|
}
|
}
|
|
|
}
|
//除了学生类型55-住宿生;66-走读生需要控制;77-全日通行类型等其他类型
|
else
|
{
|
; //无需作二次检查(周一到周日开放时间段)
|
}
|
|
//accessControlRecord = new AccessControlRecord();
|
|
//log4netService.Info("门禁记录ID:" + id );
|
list.Add(new AccessControlRecord()
|
{
|
id = id,
|
card_id = card_id,
|
emp_id = emp_id,
|
door_id = door_id,
|
temperature = temperature, //测温
|
passTime = passTime,
|
inOutFlag = inOutFlag, //进出门标志:1-出门;2-进门
|
evenFlag = evenFlag,
|
//picture = picture, //图片抓拍二进制
|
picture = fileName == null ? null : Path.GetFileName(fileName), //存放文件名
|
//twoRecordTimes = twoRecordSMSTimes, //两次门禁考勤记录间在60秒(1分钟)内,不推送短信;为0时都推送。
|
twoRecordSameTimes = twoRecordSameTimes, //两次门禁考勤记录间相同方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
twoRecordContraryTimes = twoRecordContraryTimes, //两次门禁考勤记录间相反方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
twoRecordSMSTimes = twoRecordSMSTimes, //两次门禁考勤记录间在60秒(5分钟)内,不推送短信;为0时都推送。
|
status_id = status_id, //学生类型:55 住宿;66 走读;77-全天通行;其他(0,77,null)
|
device_type = device_type, //门禁设备类型:1-依时利
|
validTimeLeave = validTimeLeave, //请假签到有效时长(秒)
|
isAttendanceSend = isAttendanceSend, //是否考勤发送微信消息:0-不推送; 1-推送。
|
isAttendanceSMSSend = isAttendanceSMSSend, //是否考勤发送微信消息:0-不推送; 1-推送。
|
wxMessageFailReason = wxMessageFailReason, //微信消息失败原因:如:黑名单限制、间隔时间限制、学生类型推送时间限制
|
smsMessageFailReason = smsMessageFailReason, //短信消息失败原因:如:黑名单限制、间隔时间限制、学生类型推送时间限制
|
dealFlag = dealFlag, //处理标志:0-未处理; 1-处理中;2-已处理.
|
corpId = corpId
|
});
|
|
|
}
|
|
}
|
else
|
{
|
//log4netService.Info(DateTime.Now.ToString("u") + "门禁监控中......");
|
return null;
|
|
}
|
|
try
|
{
|
string errmsg = "";
|
|
if (string.IsNullOrEmpty(sendMsgByAccessControlRecordUrl))
|
{
|
if (list != null && list.Count > 0)
|
{
|
foreach (AccessControlRecord acr in list)
|
{
|
//int id = int.Parse(acr.card_id.ToString());
|
int id = int.Parse(acr.id.ToString());
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 0);
|
}
|
}
|
errmsg = "调用推送学生进出学校消息接口URL为空!";
|
log4netService.Error(errmsg);
|
return null;
|
}
|
if (string.IsNullOrEmpty(corpId))
|
{
|
if (list != null && list.Count > 0)
|
{
|
foreach (AccessControlRecord acr in list)
|
{
|
//int id = int.Parse(acr.card_id.ToString());
|
int id = int.Parse(acr.id.ToString());
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 0);
|
}
|
}
|
errmsg = "调用推送学生进出学校消息接口URL为空!";
|
log4netService.Error(errmsg);
|
return null;
|
}
|
|
|
string httpUrl = sendMsgByAccessControlRecordUrl;
|
string requestUrl = string.Format("{0}", httpUrl);
|
//log4netService.Debug("requestUrl:" + requestUrl);
|
|
StreamReader reader;
|
Uri requestUri = new Uri(requestUrl);
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
|
request.Method = "POST";
|
request.ContentType = "application/x-www-form-urlencoded";
|
request.KeepAlive = true;
|
/**
|
* 接口调用的超时时间,默认为3000,单位毫秒
|
*/
|
request.Timeout = 30000;
|
|
StringBuilder data = new StringBuilder();
|
data.Append(string.Format("?t={0}", DateTime.Now.ToString()));
|
|
if (!string.IsNullOrEmpty(corpId))
|
{
|
data.Append("&corpId=" + corpId);
|
}
|
if (list != null && list.Count >= 0)
|
{
|
string listStr = Newtonsoft.Json.JsonConvert.SerializeObject(list);
|
data.Append("&data=" + listStr);
|
}
|
|
//log4netService.Debug("请求data:" + data.ToString() );
|
|
byte[] byteData = UTF8Encoding.GetEncoding("utf-8").GetBytes(data.ToString());
|
request.ContentLength = byteData.Length;
|
// 开始请求
|
using (Stream postStream = request.GetRequestStream())
|
{
|
postStream.Write(byteData, 0, byteData.Length);
|
}
|
|
|
WebResponse response = (HttpWebResponse)request.GetResponse();
|
reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
|
|
StringBuilder builder = new StringBuilder();
|
char[] buffer = new char[0x400];
|
int charCount = 0;
|
while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0)
|
{
|
builder.Append(buffer, 0, charCount);
|
}
|
reader.Close();
|
string result = builder.ToString();
|
if (string.IsNullOrEmpty(result))
|
{
|
if (list != null && list.Count > 0)
|
{
|
foreach (AccessControlRecord acr in list)
|
{
|
//int id = int.Parse(acr.card_id.ToString());
|
int id = int.Parse(acr.id.ToString());
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 0);
|
}
|
}
|
errmsg = "未知异常";
|
log4netService.Error(errmsg);
|
return null;
|
}
|
|
//反序列化Json数据
|
ResponseMsgMo rpm = JsonConvert.DeserializeObject<ResponseMsgMo>(result);//result为上面的Json数据
|
string errCode = rpm.errCode;
|
string leaveInfoStr = rpm.data;
|
|
//log4netService.Debug("响应errCode:" + errCode + " 上传记录leaveInfoStr:" + leaveInfoStr);
|
|
if (errCode == "0000")
|
{
|
if (leaveInfoStr == null || leaveInfoStr.Length <= 0)
|
{
|
|
}
|
else
|
{
|
//errmsg = "调用推送学生进出学校消息接口成功!";
|
//errmsg = "推送签到消息成功!";
|
errmsg = "门禁考勤数据上传成功!\r\n";
|
log4netService.Info(errmsg + leaveInfoStr);
|
Console.WriteLine(errmsg + leaveInfoStr);
|
|
|
if (list != null && list.Count > 0)
|
{
|
foreach (AccessControlRecord acr in list)
|
{
|
//int id = int.Parse(acr.card_id.ToString());
|
int id = int.Parse(acr.id.ToString());
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 2);
|
}
|
}
|
|
}
|
return rpm;
|
}
|
else
|
{
|
if (list != null && list.Count > 0)
|
{
|
foreach (AccessControlRecord acr in list)
|
{
|
//int id = int.Parse(acr.card_id.ToString());
|
int id = int.Parse(acr.id.ToString());
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 2);
|
}
|
}
|
errmsg = rpm.errMsg;
|
log4netService.Error("调用推送学生进出学校消息接口失败,错误码[" + errCode + "]错误信息[" + errmsg + "]");
|
return rpm;
|
}
|
|
}
|
catch (Exception ex)
|
{
|
if (list != null && list.Count > 0)
|
{
|
foreach (AccessControlRecord acr in list)
|
{
|
//int id = int.Parse(acr.card_id.ToString());
|
int id = int.Parse(acr.id.ToString());
|
|
//更新门禁记录为数据同步标志DataSycFlag为:0-未同步;1-同步中;2-已同步
|
UpdateDataSycFlag(id, 0);
|
}
|
}
|
log4netService.Error("调用推送学生进出学校消息接口异常:" + ex.Message);
|
}
|
return null;
|
|
}
|
|
//根据刷卡或刷脸时间、进出门标志检查是否需要推送消息。
|
public Boolean doCheck(DateTime passTime, int inOutFlag, string status_id)
|
{
|
try
|
{
|
|
//需要推送消息的时间段
|
string times = "";
|
//根据日期时间获取星期几,0-星期日
|
int week = CaculateWeekDay(passTime);
|
//log4netService.Debug("刷卡日期:" + passTime + " 是星期:" + week + passTime);
|
|
//截取时间
|
string time = passTime.ToLongTimeString().ToString();
|
|
//学生类型:55 住宿;66 走读;77-全日能行;其他(0,77,null)
|
//if (status_id == "55")
|
if (status_id == boarder_student )
|
{
|
|
//进出门标志inOutFlag:1-出门;2-进门
|
if (inOutFlag == 1)
|
{
|
if (week == 1)
|
{
|
times = out_boarder_monday;
|
}
|
else if (week == 2)
|
{
|
times = out_boarder_tuesday;
|
}
|
else if (week == 3)
|
{
|
times = out_boarder_wednesday;
|
}
|
else if (week == 4)
|
{
|
times = out_boarder_thursday;
|
}
|
else if (week == 5)
|
{
|
times = out_boarder_friday;
|
}
|
else if (week == 6)
|
{
|
times = out_boarder_saturday;
|
}
|
else if (week == 0)
|
{
|
times = out_boarder_sunday;
|
}
|
else
|
{
|
return true;
|
}
|
|
}
|
//进出门标志inOutFlag:1-出门;2-进门
|
else if (inOutFlag == 2)
|
{
|
if (week == 1)
|
{
|
times = in_boarder_monday;
|
}
|
else if (week == 2)
|
{
|
times = in_boarder_tuesday;
|
}
|
else if (week == 3)
|
{
|
times = in_boarder_wednesday;
|
}
|
else if (week == 4)
|
{
|
times = in_boarder_thursday;
|
}
|
else if (week == 5)
|
{
|
times = in_boarder_friday;
|
}
|
else if (week == 6)
|
{
|
times = in_boarder_saturday;
|
}
|
else if (week == 0)
|
{
|
times = in_boarder_sunday;
|
}
|
else
|
{
|
return true;
|
}
|
|
}
|
}
|
//走读生
|
//else if (status_id == "66")
|
else if (status_id == extern_student)
|
{
|
//进出门标志inOutFlag:1-出门;2-进门
|
if (inOutFlag == 1)
|
{
|
if (week == 1)
|
{
|
times = out_extern_monday;
|
}
|
else if (week == 2)
|
{
|
times = out_extern_tuesday;
|
}
|
else if (week == 3)
|
{
|
times = out_extern_wednesday;
|
}
|
else if (week == 4)
|
{
|
times = out_extern_thursday;
|
}
|
else if (week == 5)
|
{
|
times = out_extern_friday;
|
}
|
else if (week == 6)
|
{
|
times = out_extern_saturday;
|
}
|
else if (week == 0)
|
{
|
times = out_extern_sunday;
|
}
|
else
|
{
|
return true;
|
}
|
|
}
|
//进出门标志inOutFlag:1-出门;2-进门
|
else if (inOutFlag == 2)
|
{
|
if (week == 1)
|
{
|
times = in_extern_monday;
|
}
|
else if (week == 2)
|
{
|
times = in_extern_tuesday;
|
}
|
else if (week == 3)
|
{
|
times = in_extern_wednesday;
|
}
|
else if (week == 4)
|
{
|
times = in_extern_thursday;
|
}
|
else if (week == 5)
|
{
|
times = in_extern_friday;
|
}
|
else if (week == 6)
|
{
|
times = in_extern_saturday;
|
}
|
else if (week == 0)
|
{
|
times = in_extern_sunday;
|
}
|
else
|
{
|
return true;
|
}
|
|
}
|
}
|
else
|
{
|
return true;
|
}
|
|
//可推送时间段为空时
|
if (times == null || times == "")
|
{
|
return true;
|
}
|
|
|
//判断门禁时间是否在可推送消息的时间段内
|
Boolean bl = doTimeCheck(time, times);
|
|
//log4netService.Debug("判断门禁时间是否在可推送消息的时间段内:检验结果[" + bl + "]、times[" + times + "]、time[" + time + "]");
|
|
|
return bl;
|
}
|
catch (Exception e)
|
{
|
log4netService.Error("根据刷卡或刷脸时间、进出门标志检查处理异常:" + e.Message);
|
return false;
|
}
|
|
}
|
|
/// <summary>
|
/// 计算指定日期的星期值。星期日为0,其他为对应的星期值
|
/// 默认返回:int类型的星期值
|
/// </summary>
|
/// <param name="rq"> A System.DateTime</param>
|
public int CaculateWeekDay(DateTime rq)
|
{
|
int y = rq.Year;
|
int m = rq.Month;
|
int d = rq.Day;
|
if (m == 1 || m == 2)
|
{
|
m += 12;
|
y--;
|
}
|
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
|
return week;
|
}
|
|
/// <summary>
|
/// 检查刷卡时间是否在设置的时间段内(多个)时
|
/// 默认返回:int类型的星期值
|
/// </summary>
|
/// <param name="rq"> A System.DateTime</param>
|
public Boolean doTimeCheck(string time, string times)
|
{
|
try{
|
Boolean bl = false;
|
string[] timesArray = times.Split(';');
|
//时间段为空或不存在时,默认要推送。
|
if (timesArray == null || timesArray.Length <= 0)
|
{
|
return true;
|
}
|
|
string startTime = "";
|
string endTime = "";
|
string newTime = "";
|
for (int i = 0; i < timesArray.Length; i++)
|
{
|
newTime = timesArray[i];
|
//log4netService.Debug("时间段:" + newTime);
|
//Console.WriteLine("时间段:", newTime);
|
string[] newTimeArray = newTime.Split('~');
|
if (newTimeArray.Length != 2)
|
{
|
log4netService.Error("时间格式错误:" + newTime);
|
return false;
|
}
|
|
startTime = newTimeArray[0].Trim();
|
if (startTime.Length == 5)
|
{
|
startTime += ":00";
|
}
|
//时间格式检验
|
bl = IsTime(startTime);
|
if (!bl)
|
{
|
log4netService.Error("开始时间格式错误:" + newTimeArray[0].Trim() );
|
return false;
|
}
|
|
endTime = newTimeArray[1].Trim();
|
if (endTime.Length == 5)
|
{
|
endTime += ":59";
|
}
|
//时间格式检验
|
bl = IsTime(endTime);
|
if (!bl)
|
{
|
log4netService.Error("截止时间格式错误:" + newTimeArray[1].Trim());
|
return false;
|
}
|
|
|
DateTime dateTime = Convert.ToDateTime(time);
|
DateTime dateStartTime = Convert.ToDateTime(startTime);
|
DateTime dateEndTime = Convert.ToDateTime(endTime);
|
//log4netService.Debug("dateTime:" + dateTime);
|
//log4netService.Debug("dateStartTime:" + dateStartTime);
|
//log4netService.Debug("dateEndTime:" + dateEndTime);
|
|
//时间比较,参数1>参数2=1;参数1<参数2=-1;参数1=参数2=0;
|
if (DateTime.Compare(dateStartTime, dateEndTime) >= 0)
|
{
|
continue;
|
}
|
//刷卡时间在时间段之外
|
if (DateTime.Compare(dateTime, dateStartTime) < 0 || DateTime.Compare(dateTime, dateEndTime) > 0)
|
{
|
continue;
|
}
|
//在时间段内时,要推送。
|
else
|
{
|
return true;
|
}
|
|
|
}
|
|
return false;
|
|
}
|
catch (Exception e)
|
{
|
log4netService.Error("检查刷卡时间是否在设置的时间段内处理异常:" + e.Message);
|
return false;
|
}
|
|
}
|
|
//检查两次门禁考勤记录间同方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
//检查两次门禁考勤记录间反方向在60秒(1分钟)内,不推送微信;为0时都推送。
|
public Boolean doTwoRecordTimesCheck(DateTime passTime, int checkInOutFlag, int twoRecordSameTimes, int twoRecordContraryTimes, string emp_id, int id)
|
{
|
try
|
{
|
/**
|
string sql = "select DISTINCT a.id, a.card_id, a.emp_id, a.door_id, a.passTime, a.inOutFlag, a.evenFlag "
|
+ " from eastRiver.dbo.PassTime a, eastRiver.dbo.PassPicRecords b "
|
+ " where a.card_id = b.Card_No and a.passTime = b.PassTime "
|
+ " and a.EvenFlag in (0, 1, 2, 3, 4, 5, 6, 7, 211) "
|
+ " and a.inOutFlag = " + checkInOutFlag
|
+ " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) > 0 "
|
+ " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) <= " + twoRecordTimes
|
+ " and a.emp_id = '" + emp_id + "'";
|
**/
|
/**
|
string sql = "select DISTINCT a.id, a.card_id, a.emp_id, a.door_id, a.passTime, a.inOutFlag, a.evenFlag "
|
+ " from eastRiver.dbo.PassTime a "
|
+ " where 1!=1 "
|
//+ " where a.EvenFlag in (0, 1, 2, 3, 4, 5, 6, 7, 211) "
|
+ " and a.emp_id = '" + emp_id + "'";
|
|
string sql1 = "";
|
//只检查相同方向
|
if (twoRecordSameTimes > 0)
|
{
|
sql1 = " union all select DISTINCT a.id, a.card_id, a.emp_id, a.door_id, a.passTime, a.inOutFlag, a.evenFlag "
|
+ " from eastRiver.dbo.PassTime a "
|
+ " where 1=1 "
|
+ " and a.emp_id = '" + emp_id + "'"
|
+ " and a.inOutFlag = " + checkInOutFlag
|
+ " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) > 0 "
|
+ " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) <= " + twoRecordSameTimes;
|
|
}
|
string sql2 = "";
|
//只检查相反方向
|
if (twoRecordContraryTimes > 0)
|
{
|
sql2 = " union all select DISTINCT a.id, a.card_id, a.emp_id, a.door_id, a.passTime, a.inOutFlag, a.evenFlag "
|
+ " from eastRiver.dbo.PassTime a "
|
+ " where 1=1 "
|
+ " and a.emp_id = '" + emp_id + "'"
|
+ " and a.inOutFlag != " + checkInOutFlag
|
+ " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) > 0 "
|
+ " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) <= " + twoRecordContraryTimes;
|
|
}
|
|
sql += sql1 + sql2;
|
**/
|
|
string sql = "";
|
if (sql_WXCheck_def_1.Length>0)
|
{
|
sql += sql_WXCheck_def_1;
|
}
|
if (sql_WXCheck_def_2.Length > 0)
|
{
|
sql += sql_WXCheck_def_2;
|
}
|
if (sql_WXCheck_def_3.Length > 0)
|
{
|
sql += sql_WXCheck_def_3;
|
}
|
if (sql_WXCheck_def_4.Length > 0)
|
{
|
sql += sql_WXCheck_def_4 + emp_id + "'";
|
}
|
if (sql_WXCheck_def_5.Length > 0)
|
{
|
sql += sql_WXCheck_def_5;
|
}
|
|
|
//只检查相同方向间隔控制
|
string sql_WXCheck_judge1 = "";
|
if (twoRecordSameTimes > 0)
|
{
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_1;
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_2;
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_3;
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_4 + emp_id + "'";
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_5 + checkInOutFlag ;
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_6 + id + " ) ) > 0 ";
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_7 + id + " ) ) <= " + twoRecordSameTimes;
|
sql_WXCheck_judge1 += sql_WXCheck_judge1_8;
|
|
}
|
if (sql_WXCheck_judge1.Length > 0)
|
{
|
sql += sql_WXCheck_judge1;
|
}
|
|
//只检查相反方向间隔控制
|
string sql_WXCheck_judge2 = "";
|
if (twoRecordContraryTimes > 0)
|
{
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_1;
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_2;
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_3;
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_4 + emp_id + "'";
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_5 + checkInOutFlag;
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_6 + id + " ) ) > 0 ";
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_7 + id + " ) ) <= " + twoRecordContraryTimes;
|
sql_WXCheck_judge2 += sql_WXCheck_judge2_8;
|
}
|
|
sql += sql_WXCheck_judge1 + sql_WXCheck_judge2;
|
|
//log4netService.Debug("企业微信发送间隔控制:" + sql);
|
|
DataSet ds = da.ExecuteDataSet(System.Data.CommandType.Text, sql);
|
DataTable dt = ds.Tables[0];
|
List<AccessControlRecord> list = new List<AccessControlRecord>();
|
AccessControlRecord accessControlRecord = new AccessControlRecord();
|
if (dt.Rows.Count > 0)
|
{
|
//在twoRecordTimes时间内有记录时,本次第一步检验需不推送
|
return false;
|
}
|
else
|
{
|
//在twoRecordTimes时间内无记录时,本次第一步检验需推送
|
return true;
|
}
|
|
}
|
catch (Exception e)
|
{
|
log4netService.Error("检查在这twoRecordTimes时间之内是否有门禁考勤记录异常:" + e.Message);
|
return false;
|
}
|
|
}
|
|
|
//检查两次门禁考勤记录间在60秒(1分钟)内,不推送短信;为0时都推送。
|
public Boolean doTwoRecordTimesSMSCheck(DateTime passTime, int twoRecordSMSTimes, string emp_id, int id)
|
{
|
try
|
{
|
/**
|
string sql = "select DISTINCT a.id, a.card_id, a.emp_id, a.door_id, a.passTime, a.inOutFlag, a.evenFlag "
|
+ " from eastRiver.dbo.PassTime a "
|
+ " where 1=1 "
|
//+ " where a.EvenFlag in (0, 1, 2, 3, 4, 5, 6, 7, 211) "
|
+ " and a.emp_id = '" + emp_id + "'";
|
//相同方向和相反方向
|
if (twoRecordSMSTimes > 0)
|
{
|
sql += " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) > 0 ";
|
sql += " and DATEDIFF(ss, a.passTime, (select passTime from eastRiver.dbo.PassTime where id = " + id + " ) ) <= " + twoRecordSMSTimes;
|
|
}
|
**/
|
|
string sql = "";
|
if (sql_SMSCheck_def_1.Length>0)
|
{
|
sql += sql_SMSCheck_def_1;
|
}
|
if (sql_SMSCheck_def_2.Length > 0)
|
{
|
sql += sql_SMSCheck_def_2;
|
}
|
if (sql_SMSCheck_def_3.Length > 0)
|
{
|
sql += sql_SMSCheck_def_3;
|
}
|
if (sql_SMSCheck_def_4.Length > 0)
|
{
|
sql += sql_SMSCheck_def_4 + emp_id + "'";
|
}
|
if (sql_SMSCheck_def_5.Length > 0)
|
{
|
sql += sql_SMSCheck_def_5;
|
}
|
|
//只检查相同方向间隔控制
|
string sql_SMSCheck_judge1 = "";
|
//相同方向和相反方向
|
if (twoRecordSMSTimes > 0)
|
{
|
sql_SMSCheck_judge1 += sql_SMSCheck_judge1_1 + id + " ) ) > 0 ";
|
sql_SMSCheck_judge1 += sql_SMSCheck_judge1_2 + id + " ) ) <= " + twoRecordSMSTimes;
|
|
}
|
if (sql_SMSCheck_judge1.Length > 0)
|
{
|
sql += sql_SMSCheck_judge1;
|
}
|
|
//log4netService.Debug("短信发送间隔控制:" + sql);
|
|
DataSet ds = da.ExecuteDataSet(System.Data.CommandType.Text, sql);
|
DataTable dt = ds.Tables[0];
|
List<AccessControlRecord> list = new List<AccessControlRecord>();
|
AccessControlRecord accessControlRecord = new AccessControlRecord();
|
if (dt.Rows.Count > 0)
|
{
|
/**
|
foreach (DataRow dr in dt.Rows)
|
{
|
int new_id = int.Parse(dr["id"].ToString());
|
string new_card_id = dr["card_id"].ToString(); //人事资料卡号
|
string new_emp_id = dr["emp_id"].ToString();
|
int door_id = int.Parse(dr["door_id"].ToString());
|
DateTime new_passTime = Convert.ToDateTime(dr["passTime"].ToString());
|
int new_inOutFlag = int.Parse(dr["inOutFlag"].ToString());
|
|
log4netService.Debug("new_id..." + new_id);
|
log4netService.Debug("new_emp_id..." + new_emp_id);
|
log4netService.Debug("new_passTime..." + new_passTime);
|
log4netService.Debug("new_inOutFlag..." + new_inOutFlag);
|
|
}**/
|
|
//在twoRecordTimes时间内有记录时,本次第一步检验需不推送
|
return false;
|
}
|
else
|
{
|
|
//在twoRecordTimes时间内无记录时,本次第一步检验需推送
|
return true;
|
}
|
|
}
|
catch (Exception e)
|
{
|
log4netService.Error("检查在这twoRecordTimes时间之内是否有门禁考勤记录异常:" + e.Message);
|
return false;
|
}
|
|
}
|
|
/// <summary>
|
/// 是否为时间型字符串
|
/// </summary>
|
/// <param name="source">时间字符串(15:00:00)</param>
|
/// <returns></returns>
|
public Boolean IsTime(string StrSource)
|
{
|
return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
|
}
|
|
|
|
/// <summary>
|
/// 上传图片文件前检查服务端文件是否存在
|
/// </summary>
|
/// <returns></returns>
|
public Boolean checkDoorImage(string dateFolder, string fileName)
|
{
|
|
//log4netService.Debug("checkDoorImage...记录日期:" + dateFolder + " 文件名:" + fileName);
|
|
try
|
{
|
string errmsg = "";
|
|
string httpUrl = checkDoorImageUrl;
|
string requestUrl = string.Format("{0}", httpUrl);
|
|
StreamReader reader;
|
Uri requestUri = new Uri(requestUrl);
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
|
request.Method = "POST";
|
request.ContentType = "application/x-www-form-urlencoded";
|
request.KeepAlive = true;
|
/**
|
* 接口调用的超时时间,默认为3000,单位毫秒
|
*/
|
request.Timeout = 30000;
|
|
StringBuilder data = new StringBuilder();
|
data.Append(string.Format("?t={0}", DateTime.Now.ToString()));
|
|
if (!string.IsNullOrEmpty(dateFolder))
|
{
|
data.Append("&dateFolder=" + dateFolder);
|
}
|
if (!string.IsNullOrEmpty(fileName))
|
{
|
data.Append("&fileName=" + fileName);
|
}
|
if (!string.IsNullOrEmpty(corpId))
|
{
|
data.Append("&corpId=" + corpId);
|
}
|
|
//log4netService.Debug("请求data:" + data.ToString() );
|
|
byte[] byteData = UTF8Encoding.GetEncoding("utf-8").GetBytes(data.ToString());
|
request.ContentLength = byteData.Length;
|
// 开始请求
|
using (Stream postStream = request.GetRequestStream())
|
{
|
postStream.Write(byteData, 0, byteData.Length);
|
}
|
|
|
WebResponse response = (HttpWebResponse)request.GetResponse();
|
reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
|
|
StringBuilder builder = new StringBuilder();
|
char[] buffer = new char[0x400];
|
int charCount = 0;
|
while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0)
|
{
|
builder.Append(buffer, 0, charCount);
|
}
|
reader.Close();
|
string result = builder.ToString();
|
if (string.IsNullOrEmpty(result))
|
{
|
errmsg = "未知异常";
|
log4netService.Error(errmsg);
|
return true; //异常时,跳过上传图片
|
}
|
|
//反序列化Json数据
|
ResponseMsgMo rpm = JsonConvert.DeserializeObject<ResponseMsgMo>(result);//result为上面的Json数据
|
string errCode = rpm.errCode;
|
string errMsg = rpm.errMsg;
|
|
//log4netService.Debug("服务端门禁人脸识别图片检查:响应码:" + errCode + " 响应信息:" + errMsg);
|
|
if (errCode == "0000")
|
{
|
return true;
|
}
|
else
|
{
|
if (errmsg != "" && errmsg != "File already exists")
|
{
|
log4netService.Error("错误码[" + errCode + "]错误信息[" + errmsg + "]");
|
}
|
return false;
|
}
|
|
}
|
catch (Exception ex)
|
{
|
log4netService.Error("服务端门禁人脸识别图片检查异常:" + ex.Message);
|
return false;
|
}
|
|
}
|
|
/// <summary>
|
/// C# 中发送POST请求
|
/// filePath:文件全路径名称
|
/// </summary>
|
/// <param name="url"></param>
|
/// <param name="filePath"></param>
|
private void UploadRequest(string dateFolder, string filePath)
|
{
|
//Console.WriteLine("UploadRequest...");
|
//byte[] bytes = image;//Encoding.UTF8.GetBytes(image);//这里需要指定提交的编码
|
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
byte[] bytes = new byte[fs.Length];
|
try
|
{
|
fs.Read(bytes, 0, (int)fs.Length);
|
|
}
|
catch (Exception ex)
|
{
|
|
log4netService.Error("文件处理异常:" + ex.Message);
|
return ;
|
}
|
finally
|
{
|
if (fs != null)
|
{
|
//关闭资源
|
fs.Close();
|
}
|
}
|
|
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uploadDoorImageUrl);
|
webRequest.Method = "POST";
|
webRequest.ContentType = "application/x-www-form-urlencoded";
|
webRequest.Accept = "application/x-www-form-urlencoded";
|
|
string dataFormat = "\r\nContent-Disposition: form-data; name=\"{0}\";filename=\"{1}\"; dateFolder=\"{2}\"; corpId=\"{3}\"\r\nContent-Type:application/x-www-form-urlencoded\r\n\r\n";
|
string header = string.Format(dataFormat, "file", Path.GetFileName(filePath), dateFolder, corpId);
|
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(header);
|
|
long length = bytes.Length + postHeaderBytes.Length;
|
|
//webRequest.ContentLength = bytes.Length;
|
webRequest.ContentLength = length;//请求内容长度
|
|
Stream dataStream = webRequest.GetRequestStream();
|
|
//头信息
|
dataStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
|
//图片信息
|
dataStream.Write(bytes, 0, bytes.Length);
|
dataStream.Close();
|
|
//接收响应
|
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
|
MemoryStream ms = null;
|
using (var stream = webResponse.GetResponseStream())
|
{
|
Byte[] buffer = new Byte[webResponse.ContentLength];
|
int offset = 0, actuallyRead = 0;
|
do
|
{
|
actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
|
offset += actuallyRead;
|
}
|
while (actuallyRead > 0);
|
ms = new MemoryStream(buffer);
|
}
|
webResponse.Close();
|
}
|
|
/// <summary>
|
/// double比较大小
|
/// </summary>
|
/// <param name="value1"></param>
|
/// <param name="value2"></param>
|
/// <returns></returns>
|
public static bool DoubleEquals(double value1, double value2)
|
{
|
//双精度误差
|
var DOUBLE_DELTA = 1E-6;
|
return value1 == value2 || Math.Abs(value1 - value2) < DOUBLE_DELTA;
|
}
|
|
|
/// <summary>
|
/// 更新数据同步标志
|
/// </summary>
|
/// <param name="Id"></param>
|
/// <param name="sendstatus"></param>
|
public void UpdateDataSycFlag(int Id, int dataSycFlag)
|
{
|
//string sql = "update eastRiver.dbo.PassTime set dataSycFlag=@dataSycFlag where id=@id";
|
string sql = sql_update_SycFlag;
|
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
|
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "@id", Value = Id });
|
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "@dataSycFlag", Value = dataSycFlag });
|
cmd.CommandText = sql;
|
cmd.CommandType = CommandType.Text;
|
cmd.CommandTimeout = 10000000;
|
da.ExecuteNonQuery(cmd);
|
}
|
|
|
|
}
|
}
|