using System;
using QMAPP.FJC.TRACING.DAInterface;
using QMAPP.FJC.Entity.QT;
using QMAPP.FJC.DAL.CheckTime;
using QMAPP.FJC.Entity.CheckTime;
namespace QMAPP.FJC.TRACING.DataValidators
{
///
/// 外购件时间验证
///
public class ProductTimeValidator : IDataValidator
{
public ValidateResult Validate(DAObject data,DAIValidation validOption)
{
if (!string.IsNullOrEmpty(data.DAI.DA_CODE) && !string.IsNullOrEmpty(data.WorkLocState.WORKLOC_CODE) && !string.IsNullOrEmpty(data.DAValue.ToString()))
{
CHECKTIMEDAL dal = new CHECKTIMEDAL();
var listCheck = dal.SelectCheck(data.WorkLocState.WORKLOC_CODE, data.DAI.DA_CODE);
if (listCheck.Count > 0)
{
var checktime = new CHECKTIME();
checktime = listCheck[0];
var time = (Convert.ToDecimal(checktime.Check_Value) / 3600).ToString("0.0");
if (string.IsNullOrEmpty(checktime.CodeTimeNumber) && string.IsNullOrEmpty(checktime.CodeTimeStart)) return new ValidateResult(true, "");
else
{
var pCheckColumnValue = data.DAValue.ToString().Substring(Convert.ToInt32(checktime.CodeTimeStart), Convert.ToInt32(checktime.CodeTimeNumber));
pCheckColumnValue = pCheckColumnValue.Replace("-", "").Replace(":", "");
pCheckColumnValue = datetime(pCheckColumnValue);
var gettime = string.Empty;
var IsCheck = TimeOutCompare(pCheckColumnValue, checktime.Operator, checktime.Check_Value, checktime.Check_ValueTo, checktime.Check_Type,ref gettime);
if (IsCheck) return new ValidateResult(true, "");
else
{
if (checktime.Check_Type == "1")
{
gettime = (Convert.ToDouble(gettime) / 3600).ToString("0.0");
return new ValidateResult(false, string.Format("未达到时间验证标准!,设置为{0}{1}小时,当前时间为{2:0.0}小时!", checktime.Operator, time, gettime));
}
else
return new ValidateResult(true, "");
}
//return new ValidateResult(false, $"未达到时间验证标准!未达到{time}小时,");
}
}
else
return new ValidateResult(true, "");
}
else
{
return new ValidateResult(true, "");
}
}
///
/// 比较时间是否满足条码
///
/// 校验字段配置的值
/// 操作符
/// 验证值
/// 验证值2
/// 状态
///
public static bool TimeOutCompare(string pCheckColumnValue, string pOperator, string pCheck_Value,string pCheck_ValueTo,string pCheck_Type, ref string gettime)
{
CHECKTIMEDAL dal = new CHECKTIMEDAL();
var preStatTime = DateTime.Now;
if (!DateTime.TryParse(pCheckColumnValue, out preStatTime))
{
throw new Exception("检测值CheckColumnValue必须是DateTime类型");
}
int len = 0;
if (!int.TryParse(pCheck_Value, out len))
{
throw new Exception("验证值Check_Value必须是int类型");
}
int len2 = 0;
if (!string.IsNullOrEmpty(pCheck_ValueTo))
{
if (!int.TryParse(pCheck_ValueTo, out len2))
{
throw new Exception("验证值pCheck_ValueTo必须是int类型");
}
}
DateTime serviceTime = dal.GetServiceDateTime();
TimeSpan ts = serviceTime.Subtract(preStatTime);
if (pCheck_Type == "1")
{
gettime = ts.TotalSeconds.ToString("0.0");
switch (pOperator)
{
case ">": return ts.TotalSeconds > len;
case "<": return ts.TotalSeconds < len;
case "=": return ts.TotalSeconds == len;
case ">=": return ts.TotalSeconds >= len;
case "<=": return ts.TotalSeconds <= len;
}
}
else
{
if (ts.TotalSeconds > len && ts.TotalSeconds < len2) return true;
else return false;
}
return true;
}
public string datetime(string CodeTimeNumber)
{
if (CodeTimeNumber.Length == 12)
CodeTimeNumber = "20" + CodeTimeNumber;
return CodeTimeNumber.Substring(0, 4) + "-" + CodeTimeNumber.Substring(4, 2) + "-" + CodeTimeNumber.Substring(6, 2) + " " + CodeTimeNumber.Substring(8, 2) + ":" + CodeTimeNumber.Substring(10, 2) + ":" + CodeTimeNumber.Substring(12, 2);
}
}
}