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

namespace Common
{
    public class MeterWorkCarActualFirstService
    {

        /// <summary>
        /// 直接新增一次计量数据（无业务）
        /// </summary>
        /// <param name="info">MeterWorkCarActualFirst</param>
        /// <returns></returns>
        public RESTfulResult<string> doAdd(PbSomeModel<MeterWorkCarActualFirst, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote> info)
        {
            DbHelper db = new DbHelper();

            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "/add", new object[] { info }, 1);
            RESTfulResult<string> result = new RESTfulResult<string>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            result.Data = rm.Data;
            return rm;
        }

        /// <summary>
        /// 根据预报编号查数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActualFirst>> doQueryWf(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            RESTfulResult<PageList<List<MeterWorkCarActualFirst>>> rm = db.doOption<PageList<List<MeterWorkCarActualFirst>>>("meterworkcaractualfirsts", "/", new object[] { info }, 0);

            List<MeterWorkCarActualFirst> lt = new List<MeterWorkCarActualFirst>();
            //如果一次数据在当前过期时间外，则将数据设置为失效状态
            if (rm.Succeed)
            {
                DateTime dtNow = DateTime.Now;
                if (rm.Data.list.Count > 0)
                {
                    foreach (MeterWorkCarActualFirst meter in rm.Data.list)
                    {
                        if (meter.createTime.Value.AddHours(PbCache.isEndHour).CompareTo(dtNow) < 0)
                        {
                            doUpdateWf(new MeterWorkCarActualFirst { actualFirstNo = meter.actualFirstNo, valueFlag = "0", updateTime = dtNow, updateManName = "超期未使用" });
                        }
                        else if (meter.baseSpotNo != "00018") //由于车辆可能在计量了吊钩秤后，几天才去回皮，还得让这个车进行其它作业任务
                        {
                            lt.Add(meter);
                        }
                    }
                }
            }

            RESTfulResult<List<MeterWorkCarActualFirst>> result = new RESTfulResult<List<MeterWorkCarActualFirst>>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            if (lt != null && lt.Count > 0)
            {
                result.Data = rm.Data.list;
            }

            return result;
        }
        public RESTfulResult<List<MeterWorkCarActualFirst>> doQueryListLike(MeterBaseMatterInfo info)
        {
            DbHelper db = new DbHelper();
            RESTfulResult<PageList<List<MeterWorkCarActualFirst>>> rm = db.doOption<PageList<List<MeterWorkCarActualFirst>>>("meterworkcaractualfirsts", "/like/", new object[] { info }, 1);

            RESTfulResult<List<MeterWorkCarActualFirst>> result = new RESTfulResult<List<MeterWorkCarActualFirst>>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            if (rm.Data != null && rm.Data.list != null && rm.Data.list.Count > 0)
            {
                result.Data = rm.Data.list;
            }
            return result;
        }

        /// <summary>
        /// 按车号查询最近的一条计量实绩数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActualFirst>> doQueryByCarNo(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            info.pageNum = 1;
            info.pageSize = 3;
            RESTfulResult<PageList<List<MeterWorkCarActualFirst>>> rm = db.doOption<PageList<List<MeterWorkCarActualFirst>>>("meterworkcaractualfirsts", "/", new object[] { info }, 0);
            RESTfulResult<List<MeterWorkCarActualFirst>> result = new RESTfulResult<List<MeterWorkCarActualFirst>>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            if (rm != null && rm.Data != null & rm.Data.list != null && rm.Data.list.Count > 0)
            {
                result.Data = rm.Data.list;
            }
            return result;
        }

        /// <summary>
        /// 按车号查询最近的一条计量实绩数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActualFirst>> doQueryOrderByTime(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            info.pageNum = 1;
            info.pageSize = 3;
            RESTfulResult<PageList<List<MeterWorkCarActualFirst>>> rm = db.doOption<PageList<List<MeterWorkCarActualFirst>>>("meterworkcaractualfirsts", "/queryOrderByTime", new object[] { info }, 0);
            RESTfulResult<List<MeterWorkCarActualFirst>> result = new RESTfulResult<List<MeterWorkCarActualFirst>>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            if (rm != null && rm.Data != null & rm.Data.list != null && rm.Data.list.Count > 0)
            {
                result.Data = rm.Data.list;
            }
            return result;
        }

        /// <summary>
        /// 按车号查询最近的一条计量实绩数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActualFirst>> doLikeQuery(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            info.pageNum = 1;
            info.pageSize = 3;
            RESTfulResult<PageList<List<MeterWorkCarActualFirst>>> rm = db.doOption<PageList<List<MeterWorkCarActualFirst>>>("meterworkcaractualfirsts", "/queryOrderByTime", new object[] { info }, 0);
            RESTfulResult<List<MeterWorkCarActualFirst>> result = new RESTfulResult<List<MeterWorkCarActualFirst>>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            if (rm != null && rm.Data != null & rm.Data.list != null && rm.Data.list.Count > 0)
            {
                result.Data = rm.Data.list;
            }
            return result;
        }

        /// <summary>
        /// 按车号查询查询计量时间及期限皮重时间：按照时间进行排序
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActualFirst>> queryForTimeInterval(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            info.pageNum = 1;
            info.pageSize = 3;
            RESTfulResult<PageList<List<MeterWorkCarActualFirst>>> rm = db.doOption<PageList<List<MeterWorkCarActualFirst>>>("meterworkcaractualfirsts", "/queryForTimeInterval", new object[] { info }, 0);
            RESTfulResult<List<MeterWorkCarActualFirst>> result = new RESTfulResult<List<MeterWorkCarActualFirst>>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            if (rm != null && rm.Data != null & rm.Data.list != null && rm.Data.list.Count > 0)
            {
                result.Data = rm.Data.list;
            }
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<string> doUpdateWf(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doUpdateWf", new object[] { info }, 1);
            return rm;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="info">MeterWorkCarActualFirst</param>
        /// <param name="link">MeterWorkPreLink</param>
        /// <param name="isLock">只有一车的互锁需要传值 "1"  isLock 1互锁   其它为非互锁  </param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doAddWf(MeterWorkCarActualFirst info, MeterWorkPreLink link, MeterMonitorNote monitorNote, string isLock, string saveTare)
        {
            if (info.weightType == "0" && PbCache.unloadTimes != null)
            {
                MeterBaseUnloadTime mbut = PbCache.unloadTimes.Where(s => s.loadPointNo == info.loadPointNo && s.baseSpotNo == info.baseSpotNo).FirstOrDefault();
                if (mbut != null)
                {
                    info.loadEndTime = info.createTime.Value.AddMinutes(mbut.loadTime.Value);
                }
            }

            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkPreLink, MeterWorkCarActual, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkPreLink, MeterWorkCarActual, MeterMonitorNote>();
            pbSomeModel.one = info;
            pbSomeModel.two = link;
            pbSomeModel.sOne = isLock;
            pbSomeModel.sTwo = saveTare;
            pbSomeModel.four = monitorNote;

            if (info != null && string.IsNullOrEmpty(info.createManNo))
            {
                info.createManNo = info.baseSpotNo;
                info.createManName = info.baseSpotName;
            }

            //RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddWf", new object[] { info, link, isLock, saveTare }, 1);
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        /// <summary>
        /// 结净
        /// </summary>
        /// <param name="secondDb">MeterWorkCarActualFirst</param>
        /// <param name="netDb">MeterWorkCarActual</param>
        /// <param name="link">MeterWorkPreLink</param>
        /// <param name="isLock">只有一车的互锁需要传值 "1"  isLock 1互锁   其它为非互锁  </param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doAddNetWf(MeterWorkCarActualFirst secondDb, MeterWorkCarActual netDb, MeterWorkPreLink link, MeterMonitorNote monitorNote, string isLock, string saveTare)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkPreLink, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkPreLink, MeterMonitorNote>();
            pbSomeModel.one = secondDb;
            pbSomeModel.two = netDb;
            pbSomeModel.three = link;
            pbSomeModel.sOne = isLock;
            pbSomeModel.sTwo = saveTare;
            pbSomeModel.four = monitorNote;

            if (secondDb != null && string.IsNullOrEmpty(secondDb.createManNo))
            {
                secondDb.createManNo = secondDb.baseSpotNo;
                secondDb.createManName = secondDb.baseSpotName;
            }

            //RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddNetWf", new object[] { secondDb, netDb, link, isLock, saveTare }, 1);
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddNetWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="info">MeterWorkCarActualFirst</param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doAddInnerWf(MeterWorkCarActualFirst info, MeterMonitorNote monitorNote, string saveTare)
        {
            if (info.weightType == "0" && PbCache.unloadTimes != null)
            {
                MeterBaseUnloadTime mbut = PbCache.unloadTimes.Where(s => s.loadPointNo == info.loadPointNo && s.baseSpotNo == info.baseSpotNo).FirstOrDefault();
                if (mbut != null)
                {
                    info.loadEndTime = info.createTime.Value.AddMinutes(mbut.loadTime.Value);
                }
            }

            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkPreLink, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkPreLink, MeterMonitorNote>();
            pbSomeModel.one = info;
            pbSomeModel.four = monitorNote;
            pbSomeModel.sOne = saveTare;

            if (info != null && string.IsNullOrEmpty(info.createManNo))
            {
                info.createManNo = info.baseSpotNo;
                info.createManName = info.baseSpotName;
            }

            //RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddInnerWf", new object[] { info, saveTare }, 1);
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddInnerWf", new object[] { pbSomeModel }, 1);

            return rm;
        }


        /// <summary>
        /// 结净
        /// </summary>
        /// <param name="secondDb">MeterWorkCarActualFirst</param>
        /// <param name="netDb">MeterWorkCarActual</param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doAddNetInnerWf(MeterWorkCarActualFirst secondDb, MeterWorkCarActual netDb, MeterMonitorNote monitorNote, string saveTare)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkPreLink, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkPreLink, MeterMonitorNote>();
            pbSomeModel.one = secondDb;
            pbSomeModel.two = netDb;
            pbSomeModel.sOne = saveTare;
            pbSomeModel.four = monitorNote;

            if (secondDb != null && string.IsNullOrEmpty(secondDb.createManNo))
            {
                secondDb.createManNo = secondDb.baseSpotNo;
                secondDb.createManName = secondDb.baseSpotName;
            }

            //RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddNetInnerWf", new object[] { secondDb, netDb, saveTare }, 1);
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddNetInnerWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        

        /// <summary>
        /// 根据预报编号查数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<string> doQRCodeWf(string strCode)
        {
            DbHelper db = new DbHelper();
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "encryptionForWeb", new object[] { new QRModel { str = strCode } }, 1);
            return rm;
        }

        public RESTfulResult<string> doAddNetByTermWf(MeterWorkCarActualFirst first, MeterWorkCarActual netDb, MeterMonitorNote monitorNote)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterMonitorNote, MeterWorkPreLink> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterMonitorNote, MeterWorkPreLink>();
            pbSomeModel.one = first;
            pbSomeModel.two = netDb;
            pbSomeModel.three = monitorNote;

            if (first != null && string.IsNullOrEmpty(first.createManNo))
            {
                first.createManNo = first.baseSpotNo;
                first.createManName = first.baseSpotName;
            }

            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddNetByTermWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        /// <summary>
        /// 联达保存毛重
        /// </summary>
        /// <param name="mwaf"></param>
        /// <param name="monitorNote"></param>
        /// <returns></returns>
        public RESTfulResult<string> doAddLianDaWf(MeterWorkCarActualFirst mwaf, MeterMonitorNote monitorNote)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote>();
            pbSomeModel.one = mwaf;
            pbSomeModel.two = monitorNote;

            if (mwaf != null && string.IsNullOrEmpty(mwaf.createManNo))
            {
                mwaf.createManNo = mwaf.baseSpotNo;
                mwaf.createManName = mwaf.baseSpotName;
            }
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddLianDaWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        /// <summary>
        /// 联达保存皮重
        /// </summary>
        /// <param name="secondDb"></param>
        /// <param name="netDb"></param>
        /// <param name="monitorNote"></param>
        /// <returns></returns>
        public RESTfulResult<string> doAddNetLianDaWf(MeterWorkCarActualFirst secondDb, MeterWorkCarActual netDb, MeterMonitorNote monitorNote)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterMonitorNote, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterMonitorNote, MeterMonitorNote>();
            pbSomeModel.one = secondDb;
            pbSomeModel.two = netDb;
            pbSomeModel.three = monitorNote;

            if (secondDb != null && string.IsNullOrEmpty(secondDb.createManNo))
            {
                secondDb.createManNo = secondDb.baseSpotNo;
                secondDb.createManName = secondDb.baseSpotName;
            }
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddNetLianDaWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        public RESTfulResult<string> doCraneDbWf(MeterWorkCarActualFirst first, MeterMonitorNote monitorNote)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote>();
            pbSomeModel.one = first;
            pbSomeModel.two = monitorNote;
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doCraneDbWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        public RESTfulResult<string> doCraneNetDbWf(MeterWorkCarActualFirst first, MeterWorkCarActual netDb, MeterMonitorNote monitorNote)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterMonitorNote, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterMonitorNote, MeterMonitorNote>();
            pbSomeModel.one = first;
            pbSomeModel.two = netDb;
            pbSomeModel.three = monitorNote;
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doCraneNetDbWf", new object[] { pbSomeModel }, 1);
            return rm;
        }

        /// <summary>
        /// 根据预报编号查数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActualFirst>> doQueryDefault(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            RESTfulResult<PageList<List<MeterWorkCarActualFirst>>> rm = db.doOption<PageList<List<MeterWorkCarActualFirst>>>("meterworkcaractualfirsts", "/", new object[] { info }, 0);

            List<MeterWorkCarActualFirst> lt = new List<MeterWorkCarActualFirst>();
            RESTfulResult<List<MeterWorkCarActualFirst>> result = new RESTfulResult<List<MeterWorkCarActualFirst>>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            if (lt != null && lt.Count > 0)
            {
                result.Data = rm.Data.list;
            }

            return result;
        }
        //多条件模糊查询 自带自定义状态
        public RESTfulResult<List<MeterWorkCarActualFirst>> doQueryAndMultiple(CommonPage<MeterWorkCarActualFirst> info)
        {
            DbHelper db = new DbHelper();
            RESTfulResult<List<MeterWorkCarActualFirst>> rm = db.doOption<List<MeterWorkCarActualFirst>>("meterworkcaractualfirsts", "doQueryAndMultiple", new object[] { info }, 1);
            return rm;
        }

        public RESTfulResult<string> addEmergency(List<MeterWorkCarActualFirst> meterWorkCarActualFirsts)
        {
            DbHelper db = new DbHelper();

            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "/addEmergency", new object[] { meterWorkCarActualFirsts }, 1);
            RESTfulResult<string> result = new RESTfulResult<string>();
            result.Succeed = rm.Succeed;
            result.Status = rm.Status;
            result.Message = rm.Message;
            result.ResultMessage = rm.ResultMessage;
            result.Code = rm.Code;
            result.Data = rm.Data;
            return rm;
        }

        #region JISCO特殊接口 By BourneCao 20220122
        /// <summary>
        /// 按车号查询状态为未使用或使用中的计量实绩数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActualFirst>> doQueryByFlag(MeterWorkCarActualFirst info)
        {
            DbHelper db = new DbHelper();
            info.pageNum = 1;
            info.pageSize = 3;
            RESTfulResult<List<MeterWorkCarActualFirst>> result = db.doOption<List<MeterWorkCarActualFirst>>("meterworkcaractualfirsts", "/queryByFlag", new object[] { info }, 1);
            return result;
        }
        #endregion
    }

    public class QRModel
    {
        public string str { get; set; }
    }

    
}