using com.hnshituo.core.webapp.vo;
using Common;
using System;
using System.Collections.Generic;

namespace MeterConditionLibrary
{
    /// <summary>
    /// 验证车号是否需要进行复磅操作
    /// </summary>
    public class validRecoverInfo
    {
        public void ValidMethod(String strCarNo)
        {
            Log lg = Log.GetInstance();
            try
            {
                PbCache.isAllowRecover = true;
                //第一次仅这个方法，判断是否需要复磅
                if (PbCache.carRecover == null)
                {
                    MeterWorkCarRecoverService recoverService = new MeterWorkCarRecoverService();
                    RESTfulResult<List<MeterWorkCarRecover>> recoverResulist = recoverService.doQueryList(new MeterWorkCarRecover { carNo = strCarNo, valueFlag = "0" });
                    if (recoverResulist.Succeed == true && recoverResulist != null && recoverResulist.Data != null && recoverResulist.Data.Count > 0)
                    {
                        if (recoverResulist.Data.Count > 1)
                        {
                            PbCache.ResultMessage = string.Format("复磅的车号[{0}]存在[{1}]条待复磅的数据，请点击[语音求助]！", strCarNo, recoverResulist.Data.Count);
                            lg.WriteLog(17, string.Format("复磅的车号[{0}]存在[{1}]条待复磅的数据，请点击[语音求助]！", strCarNo, recoverResulist.Data.Count));
                            PbCache.isAllowRecover = false;
                        }
                        else if (recoverResulist.Data.Count == 1)
                        {
                            PbCache.carRecover = recoverResulist.Data[0];
                            PbCache.isAllowRecover = true;
                        }
                        else
                        {
                            PbCache.isAllowRecover = false;
                        }
                    }
                    else
                    {
                        PbCache.isAllowRecover = false;
                    }
                }
                //存在复磅记录，且点击了复磅按钮，验证是否允许复磅
                if (PbCache.carRecover != null && PbCache.isLockFrm)
                {
                    if (!PbCache.carRecover.carNo.Equals(strCarNo))
                    {
                        PbCache.ResultMessage = string.Format("程序异常，需要复磅的车号[{0}]与当前车号[{1}]不一致，请点击[语音求助]！", PbCache.carRecover.carNo, strCarNo);
                        lg.WriteLog(17, string.Format("程序异常，需要复磅的车号[{0}]与当前车号[{1}]不一致，请点击[语音求助]！", PbCache.carRecover.carNo, strCarNo));
                        PbCache.isAllowRecover = false;
                    }
                    if (!string.IsNullOrEmpty(PbCache.carRecover.recBaseSpotNo) && !PbCache.sportInfo.baseSpotNo.Equals(PbCache.carRecover.recBaseSpotNo))
                    {
                        PbCache.ResultMessage = string.Format("需要复磅的计量点[{0}]与当前计量点[{1}]不一致，请点击[语音求助]！", PbCache.carRecover.recBaseSpotName, PbCache.sportInfo.baseSpotName);
                        lg.WriteLog(17, string.Format("需要复磅的计量点[{0}]与当前计量点[{1}]不一致，请点击[语音求助]！", PbCache.carRecover.recBaseSpotName, PbCache.sportInfo.baseSpotName));
                        PbCache.isAllowRecover = false;
                    }
                }
            }
            catch (Exception ex)
            {
                lg.WriteLog(17, "validUnloadAutomatic的ValidMethod方法" + ex);
                PbCache.isAllowRecover = false;
            }
        }

        /// <summary>
        /// 是否允许点击保存按钮：若为true,则不允许继续保存。
        /// </summary>
        /// <param name="strCarNo"></param>
        public void ValidAllowSave(String strCarNo)
        {
            Log lg = Log.GetInstance();
            try
            {
                PbCache.isAllowRecover = false;
                if (PbCache.carRecover != null && PbCache.isLockFrm)
                {
                    if (PbCache.carRecover.carNo.Equals(strCarNo))
                    {
                        PbCache.ResultMessage = string.Format("当前车号[{0}]需要复磅，复磅计量点为[{1}]，请到指定的秤房点击[复磅]！", strCarNo, PbCache.carRecover.recBaseSpotName);
                        lg.WriteLog(17, string.Format("当前车号[{0}]需要复磅，复磅计量点为[{1}]，请到指定的秤房点击[复磅]！", strCarNo, PbCache.carRecover.recBaseSpotName));
                        PbCache.isAllowRecover = true;
                    }
                }

                //放在此处，可以让计量员干预后，再进行二次验证
                if (PbCache.carRecover == null)
                {
                    MeterWorkCarRecoverService recoverService = new MeterWorkCarRecoverService();
                    RESTfulResult<List<MeterWorkCarRecover>> recoverResulist = recoverService.doQueryList(new MeterWorkCarRecover { carNo = strCarNo, valueFlag = "0" });
                    if (recoverResulist.Succeed == true && recoverResulist != null && recoverResulist.Data != null && recoverResulist.Data.Count > 0)
                    {
                        if (recoverResulist.Data.Count > 1)
                        {
                            PbCache.ResultMessage = string.Format("复磅的车号[{0}]存在[{1}]条待复磅的数据，请点击[语音求助]！", strCarNo, recoverResulist.Data.Count);
                            lg.WriteLog(17, string.Format("复磅的车号[{0}]存在[{1}]条待复磅的数据，请点击[语音求助]！", strCarNo, recoverResulist.Data.Count));
                            PbCache.isAllowRecover = true;
                        }
                        else
                        {
                            PbCache.carRecover = recoverResulist.Data[0];
                            PbCache.ResultMessage = string.Format("当前车号[{0}]需要复磅，复磅计量点为[{1}]，请到指定的秤房点击[复磅]！", strCarNo, PbCache.carRecover.recBaseSpotName);
                            lg.WriteLog(17, string.Format("当前车号[{0}]需要复磅，复磅计量点为[{1}]，请到指定的秤房点击[复磅]！", strCarNo, PbCache.carRecover.recBaseSpotName));
                            PbCache.isAllowRecover = true;
                        }
                    }
                    else
                    {
                        PbCache.carRecover = null;
                    }
                }
            }
            catch (Exception ex)
            {
                lg.WriteLog(17, "validUnloadAutomatic的ValidAllowSave方法" + ex);
            }
        }
    }
}