using FangYar.BLL; using FangYar.BLL.TBL; using FangYar.Model.TBL; using RoadFlow.Data.Model.WorkFlowExecute; using RoadFlow.Platform; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Web; namespace HardWareInteface.Handle { /// /// HMCarRec 的摘要说明 //拉萨特勤一站 车辆道闸 /// public class LSTQYZCarRec : IHttpHandler { const string ORG_ID = "021933cc9b11441c920a431eeffe60c2"; const string PICPATH = "xzlstqyz"; const string LOGPATH = "xzlstqyz"; FangYar.BLL.TBL_CARACCESS bll = new FangYar.BLL.TBL_CARACCESS(); FangYar.BLL.TBL_SYS_CAR car_bll = new FangYar.BLL.TBL_SYS_CAR(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string returnStr = ""; returnStr = ReceiveAlarmInfoPlate(context); context.Response.Write(returnStr); } private string ReceiveAlarmInfoPlate(HttpContext context) { WriteLog("进入!"); HttpRequest request = context.Request; Stream stream = request.InputStream; string data = string.Empty; string response = string.Empty; bool isadd = false; if (stream.Length != 0) { try { StreamReader streamReader = new StreamReader(stream); data = streamReader.ReadToEnd(); WriteLog(data.ToString()); string[] sArray = data.Split('&'); FangYar.Model.TBL_CARACCESS model = new FangYar.Model.TBL_CARACCESS(); for (int i = 0; i < sArray.Length; i++) { string[] keyValue = sArray[i].Split('='); PropertyInfo[] propertys = model.GetType().GetProperties(); Hashtable ht = new Hashtable(); if (keyValue[0].ToUpper() == "TYPE" && keyValue[1] != "online") { isadd = false; break; } else if (keyValue[0].ToUpper() == "TYPE") { model.TYPE = keyValue[1]; } else if (keyValue[0].ToUpper() == "CAR_PLATE" || keyValue[0].ToUpper() == "PLATE_NUM") { model.CAR_PLATE = keyValue[1]; } else if (keyValue[0].ToUpper() == "COLOR" || keyValue[0].ToUpper() == "PLATE_COLOR") { model.COLOR = keyValue[1]; } else if (keyValue[0].ToUpper() == "VEHICLETYPE" || keyValue[0].ToUpper() == "VEHICLE_TYPE") { model.VEHICLETYPE = keyValue[1]; } else if (keyValue[0].ToUpper() == "CAR_COLOR") { model.CAR_COLOR = keyValue[1]; } else if (keyValue[0].ToUpper() == "CAR_LOGO") { model.CAR_LOGO = keyValue[1]; } else if (keyValue[0].ToUpper() == "PARK_ID") { model.PARK_ID = keyValue[1]; } else if (keyValue[0].ToUpper() == "CAMERA_ID" || keyValue[0].ToUpper() == "CAM_ID") { model.CAMERA_ID = keyValue[1]; if (keyValue[1] == "1803001008a6")//进 { model.INOROUT = "0"; } else { model.INOROUT = "1"; } } else if (keyValue[0].ToUpper() == "START_TIME") { string str = keyValue[1]; long unixTimeStamp = long.Parse(str); System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 DateTime dt = startTime.AddSeconds(unixTimeStamp); model.START_TIME = dt; } else if (keyValue[0].ToUpper() == "PICTURE") { string str = keyValue[1].Replace('-', '+').Replace('_', '/').Replace('.', '='); string dummyData = str.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (dummyData.Length % 4 > 0) { dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '='); } byte[] byteArray = Convert.FromBase64String(dummyData); //model.PICTURE = byteArray; model.EXTEND2 = GetFilePath(byteArray, "0"); } else if (keyValue[0].ToUpper() == "CLOSEUP_PIC") { string str = keyValue[1].Replace('-', '+').Replace('_', '/').Replace('.', '='); string dummyData = str.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (dummyData.Length % 4 > 0) { dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '='); } byte[] byteArray = Convert.FromBase64String(dummyData); //model.CLOSEUP_PIC = byteArray; model.EXTEND3 = GetFilePath(byteArray, "1"); } isadd = true; } if (isadd) { model.ID = Guid.NewGuid().ToString("N"); model.ORG_ID = "021933cc9b11441c920a431eeffe60c2"; FangYar.Model.TBL_SYS_CAR car_model = car_bll.CarNumGetModel(model.ORG_ID, model.CAR_PLATE); if (car_model != null) { model.CLASS = car_model.CLASS; } else { model.CLASS = "2"; } if (model.CLASS == "0" && model.INOROUT == "1") { System.Data.DataTable dt = bll.getProccar_access_alarm(model.CAR_PLATE, model.ORG_ID, model.EXTEND2); model.APPLY_ID = dt.Rows[0]["applyId"].ToString(); model.USERMAN = dt.Rows[0]["userman"].ToString(); } isadd = bll.Add(model); WriteLog("是否入库成功:" + isadd); } //try //{ // ReutrnResult result = new ReutrnResult(); // //车辆自动还车节点补齐 // if (model.CLASS == "0")//公务车自动还车 // { // result = CarApplyAutoReturn(model.CAR_PLATE, model.ORG_ID, model.START_TIME.ToString()); // } // if (result.result) // { // return result.result + ":" + result.msg; // } // else // { // return result.result + ":" + result.msg; // } //} //catch (Exception e) //{ // WriteLog("自动还车报错:" + e.InnerException.ToString()); //} } catch (Exception e) { WriteLog(e.Message); } } else { WriteLog("输入流的长度为0!"); } if (isadd == true) { return "true"; } else { return "false"; } } #region 自动还车 /// /// 车辆通过道闸识别车牌后自动还车 /// /// 车牌号 /// 具体操作信息 public ReutrnResult CarApplyAutoReturn(string license, string orgId, string returnDate) { TBL_SYS_CARAPPLY carApplyBLL = new TBL_SYS_CARAPPLY(); ReutrnResult result = new ReutrnResult(); if (!String.IsNullOrWhiteSpace(license)) { string where = string.Format(@" 1=1 and CARNUM='{0}' and state=1 and (extendcode2 =1 or extendcode2 is null) and ( date_format('{1}', '%Y-%m-%d %H:%i:%s') between carapply.STARTTIME and carapply.FINISHTIME) order by carapply.finishtime desc,task.sort desc", license, returnDate); var carAppyTb = carApplyBLL.GetCarApplyAndTaskListByLicense(where).Tables[0]; Hashtable hashtable = new Hashtable(); for (int i = 0; i < carAppyTb.Rows.Count; i++) { string instanceId = carAppyTb.Rows[i]["taskinstanceid"].ToString(); if (!hashtable.Contains(instanceId)) { hashtable.Add(instanceId, i); } } if (hashtable.Count > 0) { SysUsersBLL sysUsersBLL = new SysUsersBLL(); string userId = System.Configuration.ConfigurationManager.AppSettings["AutoReturnCarAccount"]; TBL_SYS_USERS_Model userInfoModel = sysUsersBLL.GetModelByUID(userId); var usersList = new List(); usersList.Add(userInfoModel); TBL_SYS_USERS_Model senderUserModel = new TBL_SYS_USERS_Model() { USERS_UID = userInfoModel.USERS_UID, USERS_PWD = userInfoModel.USERS_PWD, USERS_WEEK = userInfoModel.USERS_WEEK, USERS_TIME = userInfoModel.USERS_TIME, USERS_STATE = userInfoModel.USERS_STATE, USERS_EMAIL = userInfoModel.USERS_EMAIL, USERS_MOBILE = userInfoModel.USERS_MOBILE, ORG_ID = orgId, EMP_ID = userInfoModel.EMP_ID, IS_THIS = userInfoModel.IS_THIS, IS_SUPER = userInfoModel.IS_SUPER }; foreach (DictionaryEntry item in hashtable) { var index = int.Parse(item.Value.ToString()); senderUserModel.USERS_NAME = carAppyTb.Rows[index]["taskreceivename"].ToString(); result = ExecuteFlowStep(carAppyTb, usersList, senderUserModel, index, returnDate, license); } } else { result.result = false; result.msg = "查询不到该车的申请记录"; } } else { result.result = false; result.msg = "车牌号不能为空"; } return result; } /// /// 自动补齐后续还车节点 /// /// 流程节点数据 /// 节点处理人信息 /// 自动处理信息 /// 数据索引 /// public ReutrnResult ExecuteFlowStep(DataTable dataTable, List usersList, TBL_SYS_USERS_Model users, int dataIndex, string returnDate, string license) { ReutrnResult result = new ReutrnResult(); Execute executeModel = new Execute(); WorkFlowTask workFlowTask = new WorkFlowTask(); WorkFlow flow = new WorkFlow(); try { var nextStep = flow.GetNextSteps(dataTable.Rows[dataIndex]["taskflowid"].ToString().ToGuid(), dataTable.Rows[dataIndex]["taskstepid"].ToString().ToGuid()); foreach (var step in nextStep) { executeModel.Steps.Add(step.ID, usersList); } } catch (Exception e) { result.result = false; result.msg = "查询下一处理节点时报错"; WriteLog("查询下一处理节点时报错"); } executeModel.ExecuteType = EnumType.ExecuteType.Submit; executeModel.Comment = "自动还车"; executeModel.FlowID = dataTable.Rows[dataIndex]["taskflowid"].ToString().ToGuid(); executeModel.GroupID = dataTable.Rows[dataIndex]["taskgroupid"].ToString().ToGuid(); executeModel.InstanceID = dataTable.Rows[dataIndex]["taskinstanceid"].ToString(); executeModel.IsSign = false; executeModel.Note = "车辆通过道闸进入营区,已完成自动还车"; executeModel.Sender = users; executeModel.StepID = dataTable.Rows[dataIndex]["taskstepid"].ToString().ToGuid(); executeModel.TaskID = dataTable.Rows[dataIndex]["TaskId"].ToString().ToGuid(); try { var flowExecuteResult = workFlowTask.Execute(executeModel); var nextTask = flowExecuteResult.NextTasks; if (nextTask.Count() > 0) { //ExecuteFlowStep(dataTable, usersList, users, dataIndex); result = CarApplyAutoReturn(license, users.ORG_ID, returnDate); } else { TBL_SYS_CARAPPLY carApply = new TBL_SYS_CARAPPLY(); try { var boolResult = carApply.EditReturnState(dataTable.Rows[dataIndex]["ID"].ToString(), "2"); } catch (Exception e) { WriteLog(String.Format("自动还车后修改申请表状态失败:ID:{0},报错信息:{1}", dataTable.Rows[dataIndex]["ID"].ToString(), e.InnerException.ToString())); } result.result = true; result.msg = "已完成自动还车"; } } catch (Exception e) { result.result = false; result.msg = "执行自动还车节点时报错"; WriteLog("执行自动还车节点时报错"); } return result; } #endregion #region 自定义返回结果 public class ReutrnResult { public bool result { get; set; } public string msg { get; set; } } #endregion #region base64转图片保存到服务器 /// /// base转图片保存到服务器 /// /// private string GetFilePath(byte[] bytes, string strType) { string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();//年月 string ImageFilePath = "/Attachment" + "/Car/lstqyz/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString(); if (System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(ImageFilePath)) == false)//如果不存在就创建文件夹 { System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(ImageFilePath)); } string ImagePath, DbImagePath; //string str = System.DateTime.Now.ToString("yyyyHHddHHmmss"); string str = System.DateTime.Now.ToString("yyyyHHddHHmmssfff") + Guid.NewGuid().ToString(); if (strType == "0")//全景图 { DbImagePath = ImageFilePath + "/" + str;//定义图片名称 ImagePath = HttpContext.Current.Server.MapPath(ImageFilePath) + "/" + str;//定义图片名称 } else { DbImagePath = ImageFilePath + "/Plate" + str;//定义图片名称 ImagePath = HttpContext.Current.Server.MapPath(ImageFilePath) + "/Plate" + str;//定义图片名称 } File.WriteAllBytes(ImagePath + ".png", bytes); //保存图片到服务器,然后获取路径 return DbImagePath + ".png";//获取保存后的路径 } #endregion #region 记录日志 private void WriteLog(string msg) { FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = msg, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "caraccess_lstqyz" }); //string path = AppDomain.CurrentDomain.BaseDirectory + "log\\caraccess_lstqyz\\"; //string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt"; //if (Directory.Exists(path) == false)//如果不存在就创建文件夹 //{ // Directory.CreateDirectory(path); //} //FileInfo file = new FileInfo(path + filename); //if (!file.Exists) //{ // FileStream fs; // fs = File.Create(path + filename); // fs.Close(); //} //using (FileStream fs = new FileStream(path + filename, FileMode.Append, FileAccess.Write)) //{ // using (StreamWriter sw = new StreamWriter(fs)) // { // sw.WriteLine("时间:" + DateTime.Now.ToString() + " 信息:" + msg); // } //} } #endregion public bool IsReusable { get { return false; } } } }