using MaterialTraceability.Common;
using MaterialTraceability.Entity.DAO;
using MaterialTraceability.Entity.DTO;
using MaterialTraceability.SqlSugar;
using MaterialTraceability.SqlSugar.ServiceImpl;
using MaterialTraceability.WebService;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace MaterialTraceability.Business
{
    /// <summary>
    /// 业务辅助类
    /// </summary>
    public class BusinessHelper
    {
        private static IBaseServices<ProUpRecord> upRecordServices = new BaseServices<ProUpRecord>();

        private static IBaseServices<ProDownRecord> downRecordServices = new BaseServices<ProDownRecord>();

        private static IBaseServices<ProBindInfo> bindInfoServices = new BaseServices<ProBindInfo>();
        
        /// <summary>
        /// Mes WebService接口
        /// </summary>
        private IMesWebServices MesWebServices = new MesWebServicesImpl();


        /// <summary>
        /// 放卷位RFID标签是否重复
        /// </summary>
        /// <param name="rfid"></param>
        /// <returns></returns>
        public static async Task<bool> UpRfidIsRecur(string rfid,int position)
        {
            try
            {
                AppConfigDto appConfig = AppConfigDto.Instance;
                LogHelper.Info("判断放卷位RFID:" + rfid + "是否和上一次读取相同");
                Expression<Func<ProUpRecord, bool>> exp = s1 => true;
                exp = exp.And(x => x.PositionId == position && x.MachineId == appConfig.machineId);
                Expression<Func<ProUpRecord, object>> order = (x) => x.RecordTime;
                ProUpRecord upRecord = await upRecordServices.QueryFirst(exp, order, false);
                if (upRecord != null)
                {
                    if (upRecord.Rfid == rfid)
                    {
                        return true;
                    }
                }
                return false;
            }catch(Exception ex)
            {
                LogHelper.Error("放卷位卷筒是否和上一次读取相同过滤异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 收卷位RFID标签是否重复
        /// </summary>
        /// <returns></returns>
        public static async Task<bool> DownRfidIsRecur(string rfid,int position)
        {
            try
            {
                AppConfigDto appConfig = AppConfigDto.Instance;
                LogHelper.Info("判断收卷位RFID:"+ rfid + "是否和上一次读取相同过滤异常");
                Expression<Func<ProDownRecord, bool>> exp = s1 => true;
                exp = exp.And(x => x.PositionId == position && x.MachineId == appConfig.machineId);
                Expression<Func<ProDownRecord, object>> order = (x) => x.RecordTime;
                ProDownRecord downRecord = await downRecordServices.QueryFirst(exp, order, false);
                if (downRecord != null)
                {
                    if (downRecord.Rfid == rfid)
                    {
                        return true;
                    }
                }
                return false;
            }catch(Exception ex)
            {
                LogHelper.Error("收卷位卷筒是否和上一次读取相同过滤异常",ex);
                return false;
            }
        }

        /// <summary>
        /// 绑定RFID与SFC信息
        /// </summary>
        /// <param name="rfid"></param>
        /// <param name="sfc"></param>
        /// <returns></returns>
        public static async Task<bool> BindRfidAndSfc(string rfidStr, string sfcStr)
        {
            Expression<Func<ProBindInfo, bool>> exp = s1 => true;
            exp = exp.And(x => x.rfid == rfidStr);
            Expression<Func<ProBindInfo, object>> order = (x) => x.bindTime;
            ProBindInfo bindInfo = await bindInfoServices.QueryFirst(exp, order, true);
            //如果有RFID更新绑定信息
            if (bindInfo != null)
            {
                bindInfo.sfc = sfcStr;
                bindInfo.bindTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                bool bindResult = await bindInfoServices.Update(bindInfo);
                if (bindResult)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else  //如果没有新加绑定信息
            {
                bindInfo = new ProBindInfo();
                bindInfo.id = System.Guid.NewGuid().ToString("N");
                bindInfo.rfid = rfidStr;
                bindInfo.sfc = sfcStr;
                bindInfo.bindTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                int addResult = await bindInfoServices.Add(bindInfo);
                if (addResult > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
    }
}