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

namespace Common
{
    public class MeterWorkCarActualService
    {
        private Log lg = Log.GetInstance(); //写日志

        /// <summary>
        /// 查询一次该车最近的3条计量历史数据
        /// </summary>
        /// <returns></returns>
        public RESTfulResult<DataTable> doQueryWf(CommonPage<MeterWorkCarActual> cpc)
        {
            try
            {
                DbHelper db = new DbHelper();
                cpc.pageNum = 1;
                if (cpc.pageSize == 0)
                    cpc.pageSize = 10;
                RESTfulResult<PageList<List<MeterWorkCarActual>>> rm = db.doOption<PageList<List<MeterWorkCarActual>>>("meterworkcaractuals", "doQueryWf", new object[] { cpc }, 1);
                RESTfulResult<DataTable> result = new RESTfulResult<DataTable>();
                result.Succeed = rm.Succeed;
                result.Status = rm.Status;
                result.Message = rm.Message;
                result.ResultMessage = rm.ResultMessage;
                result.Code = rm.Code;

                if (rm.Succeed && rm.Data != null && rm.Data.list != null && rm.Data.list.Count > 0)
                {
                    result.Data = rm.Data.list.ListToDataTable();
                }
                else
                {
                    result.Data = new DataTable();
                }
                return result;
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, "查询计量实绩信息异常MeterWorkCarActualService/doQueryWf，车号【" + cpc.param.carNo + "】！异常信息：" + ex);
                return null;
            }
        }

        /// <summary>
        /// 查询当前预报最近的1条数据
        /// </summary>
        /// <param name="cpc"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActual>> doQueryOneWf(CommonPage<MeterWorkCarActual> cpc)
        {
            try
            {
                DbHelper db = new DbHelper();
                cpc.pageNum = 1;
                if (cpc.pageSize == 0)
                    cpc.pageSize = 1;
                RESTfulResult<PageList<List<MeterWorkCarActual>>> rm = db.doOption<PageList<List<MeterWorkCarActual>>>("meterworkcaractuals", "doQueryWf", new object[] { cpc }, 1);
                RESTfulResult<List<MeterWorkCarActual>> result = new RESTfulResult<List<MeterWorkCarActual>>();
                result.Succeed = rm.Succeed;
                result.Status = rm.Status;
                result.Message = rm.Message;
                result.ResultMessage = rm.ResultMessage;
                result.Code = rm.Code;

                result.Data = rm.Data?.list;
                return result;
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, "查询计量实绩信息异常MeterWorkCarActualService/doQueryOneWf，车号【" + cpc.param.carNo + "】！异常信息：" + ex);
                return null;
            }
        }

        /// <summary>
        /// 查询当前预报最近的1条数据
        /// </summary>
        /// <param name="cpc"></param>
        /// <returns></returns>
        public RESTfulResult<List<MeterWorkCarActual>> doQueryActaulWf(MeterWorkCarActual cpc)
        {
            try
            {
                DbHelper db = new DbHelper();
                RESTfulResult<List<MeterWorkCarActual>> rm = db.doOption<List<MeterWorkCarActual>>("meterworkcaractuals", "selectByMap", new object[] { cpc }, 1);
                RESTfulResult<List<MeterWorkCarActual>> result = new RESTfulResult<List<MeterWorkCarActual>>();
                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 result;
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, "查询计量实绩信息异常MeterWorkCarActualService/doQueryOneWf，车号【" + cpc.carNo + "】！异常信息：" + ex);
                return null;
            }
        }
        //多条件模糊查询 自带自定义状态
        public RESTfulResult<List<MeterWorkCarActual>> doQueryAndMultiple(CommonPage<MeterWorkCarActual> info)
        {
            DbHelper db = new DbHelper();
            RESTfulResult<List<MeterWorkCarActual>> rm = db.doOption<List<MeterWorkCarActual>>("meterworkcaractuals", "doQueryAndMultiple", new object[] { info }, 1);
            return rm;
        }

        #region JISCO接口
        /// <summary>
        /// 结净
        /// </summary>
        /// <param name="secondDb">MeterWorkCarActualFirst</param>
        /// <param name="netDb">MeterWorkCarActual</param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doAddNet(MeterWorkCarActualFirst onceDb, 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="secondDb">MeterWorkCarActualFirst</param>
        /// <param name="netDb">MeterWorkCarActual</param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doTermTareNet(PreTrackScale scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst1)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActualFirst, PreTrackScale, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActualFirst, PreTrackScale, MeterMonitorNote>();
            pbSomeModel.one = actualFirst1;
            pbSomeModel.two = actualFirst;
            pbSomeModel.three = scale;

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

            //RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doAddNetInnerWf", new object[] { secondDb, netDb, saveTare }, 1);
            RESTfulResult<string> rm = db.doOption<string>("meterworkcaractualfirsts", "doNetByFirst", 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> doAddNetError(MeterWorkCarActualFirst onceDb, 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", "doAddNetInnerWfError", new object[] { pbSomeModel }, 1);
            return rm;
        }

        /// <summary>
        /// 混装混卸逻辑
        /// </summary>
        /// <param name="secondDb">MeterWorkCarActualFirst</param>
        /// <param name="secondDb2">混装混卸中，自动新增的一次计量数据</param>
        /// <param name="netDb">MeterWorkCarActual</param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doAddNetHZHX(MeterWorkCarActualFirst secondDb, MeterWorkCarActualFirst secondDb2, MeterWorkCarActual netDb, MeterMonitorNote monitorNote, string saveTare)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkCarActualFirst, MeterMonitorNote > pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, MeterWorkCarActualFirst, MeterMonitorNote>();
            pbSomeModel.one = secondDb;
            pbSomeModel.two = netDb;
            pbSomeModel.three = secondDb2;
            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", "doAddNetHZHX", new object[] { pbSomeModel }, 1);
            return rm;
        }

        /// <summary>
        /// 双委托逻辑
        /// </summary>
        /// <param name="scale">List<PreTrackScale></param>
        /// <param name="actualFirst">双委托中，自动新增的一次计量数据</param>
        /// <param name="netDb">MeterWorkCarActual</param>
        /// <returns></returns>
        public RESTfulResult<string> doNetAddTwo(List<PreTrackScale> scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActual netDb)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, PreTrackScale, PreTrackScale> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, PreTrackScale, PreTrackScale>();
            pbSomeModel.one = actualFirst;
            pbSomeModel.two = netDb;
            pbSomeModel.three = scale[0];
            pbSomeModel.four = scale[1];

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

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

        /// <summary>
        /// 双委托逻辑
        /// </summary>
        /// <param name="scale">List<PreTrackScale></param>
        /// <param name="actualFirst">一次计量数据</param>
        /// <param name="netDb">MeterWorkCarActual</param>
        /// <param name="saveTare">0 不保存历史皮，1保存历史皮</param>
        /// <returns></returns>
        public RESTfulResult<string> doNetTermTare(MeterWorkCarActualFirst actualFirst, MeterWorkCarActual netDb,string saveTare)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, PreTrackScale, PreTrackScale> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, PreTrackScale, PreTrackScale>();
            pbSomeModel.one = actualFirst;
            pbSomeModel.two = netDb;
            pbSomeModel.sOne = saveTare;

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

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

        /// <summary>
        /// 多毛一皮逻辑
        /// </summary>
        /// <param name="actualFirst">一次计量数据</param>
        /// <returns></returns>
        public RESTfulResult<string> doNetAddMuch(MeterWorkCarActualFirst actualFirst)
        {
            DbHelper db = new DbHelper();
            PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, PreTrackScale, PreTrackScale> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterWorkCarActual, PreTrackScale, PreTrackScale>();
            pbSomeModel.one = actualFirst;

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

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