using Fuel.Core;
using Fuel.Core.Models;
using Fuel.Core.Nozzle.Dto;
using FuelServer.Core.Entity;
using Org.BouncyCastle.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using static FreeSql.Internal.GlobalFilter;

namespace Fuel.Application.Service
{
    public class NozzleService : INozzleService
    {
        private readonly EntityHelper _entityHelper;
        public readonly IFreeSql _fsql;
        public NozzleService(EntityHelper entityHelper, IFreeSql fsql)
        {
            _entityHelper = entityHelper;
            _fsql = fsql;
        }
        #region 油品
        /// <summary>
        /// 上传油品
        /// </summary>
        /// <param name="uploadProduct"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> UploadProduct(UploadProduct uploadProduct)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
            if (_product != null)
            {
                return ServiceResponse.Error("油品已存在，请勿重复上传");
            }
            product product = new product();
            product.Buid = guid;
            product.ProductId = uploadProduct.ProductId;
            product.ProductCode = uploadProduct.ProductCode;
            product.ProductName = uploadProduct.ProductName;
            product.ProductPrice = uploadProduct.ProductPrice;

            int affectedRows = _fsql.Insert<product>().AppendData(product).ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油品信息插入失败");
            }
            return ServiceResponse.Ok(product);
        }

        /// <summary>
        /// 更新油品信息
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> UpdateProduct(UploadProduct uploadProduct)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
            if (_product == null)
            {
                return ServiceResponse.Error("未找到油枪");
            }
            _product.ProductCode = uploadProduct.ProductCode;
            _product.ProductName = uploadProduct.ProductName;
            _product.ProductPrice = uploadProduct.ProductPrice;
            int affectedRows = _fsql.Update<product>().SetSource(_product).ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油品信息更新失败");
            }
            return ServiceResponse.Ok(_product);
        }

        /// <summary>
        /// 删除油品信息
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> DeleteProduct(UploadProduct uploadProduct)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
            if (_product == null)
            {
                return ServiceResponse.Error("未找到油品");
            }
            int affectedRows = _fsql.Delete<product>()
     .Where(p => p.ProductId == uploadProduct.ProductId)
     .ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油品信息删除失败");
            }
            return ServiceResponse.Ok();
        }

        #endregion


        #region 油罐

        /// <summary>
        /// 上传油罐
        /// </summary>
        /// <param name="uploadProduct"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> UploadTanks(UploadTanks uploadTanks)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First();
            if (_tanks != null)
            {
                return ServiceResponse.Error("油灌已存在，请勿重复上传");
            }
            tanks tanks = new tanks();
            tanks.Buid = guid;
            tanks.ProductId = uploadTanks.ProductId;
            tanks.TankCapacity = uploadTanks.TankCapacity;
            tanks.ProductName = uploadTanks.ProductName;
            tanks.TankNumber = uploadTanks.TankNumber;

            int affectedRows = _fsql.Insert<tanks>().AppendData(tanks).ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油灌信息插入失败");
            }
            return ServiceResponse.Ok(tanks);
        }

        /// <summary>
        /// 更新油罐
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> UpdateTanks(UploadTanks uploadTanks)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First();
            if (_tanks != null)
            {
                return ServiceResponse.Error("油灌已存在，请勿重复上传");
            }
            _tanks.ProductId = uploadTanks.ProductId;
            _tanks.TankCapacity = uploadTanks.TankCapacity;
            _tanks.ProductName = uploadTanks.ProductName;
            _tanks.TankNumber = uploadTanks.TankNumber;
            int affectedRows = _fsql.Update<tanks>().SetSource(_tanks).ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油灌信息更新失败");
            }
            return ServiceResponse.Ok(_tanks);
        }

        /// <summary>
        /// 删除油罐
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> DeleteTanks(UploadTanks uploadTanks)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First();
            if (_tanks != null)
            {
                return ServiceResponse.Error("油灌已存在，请勿重复上传");
            }
            int affectedRows = _fsql.Delete<tanks>()
     .Where(p => p.TankID == uploadTanks.TankID)
     .ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油灌信息删除失败");
            }
            return ServiceResponse.Ok();
        }

        #endregion

        #region 油枪

        /// <summary>
        /// 上传油枪
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> uploadNozzle(UploadNozzle uploadNozzle)
        {
            //RedisHelper.HSetAsync("Transaction", "11:22:33:44", "3232");
            //RedisHelper.SetAsync("33:22:33:44", "qweqweqwe", 3600);
            // var fsdds = RedisHelper.GetAsync("33:22:33:44");
            //var das = RedisHelper.ExpireAsync("33:22:33:44", 10);
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductName == uploadNozzle.ProductName).First();
            var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankNumber == uploadNozzle.TankNumber).First();
            var isproduct = _fsql.Select<nozzle>().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First();
            if (isproduct != null)
            {
                return ServiceResponse.Error("油枪已存在");
            }
            if (_product != null || _tanks != null)
            {
                return ServiceResponse.Error("油品或油罐信息为空");
            }
            nozzle _nozzle = new nozzle();
            _nozzle.Buid = guid;
            _nozzle.PumpId = uploadNozzle.PumpID;
            _nozzle.TankId = _tanks.Id;
            _nozzle.InternalGunNumber = uploadNozzle.InternalGunNumber;
            _nozzle.ExternalGunNumber = uploadNozzle.ExternalGunNumber;
            _nozzle.ProductID = _product.Id;
            int affectedRows = _fsql.Insert<nozzle>().AppendData(_nozzle).ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油枪信息插入失败");
            }
            return ServiceResponse.Ok(_nozzle);
        }
        /// <summary>
        /// 更新油枪信息
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> UpdateNozzle(UploadNozzle uploadNozzle)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _nozzle = _fsql.Select<nozzle>().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First();
            if (_nozzle == null)
            {
                return ServiceResponse.Error("未找到油枪");
            }
            _nozzle.PumpId = uploadNozzle.PumpID;
            _nozzle.InternalGunNumber = uploadNozzle.InternalGunNumber;
            _nozzle.ExternalGunNumber = uploadNozzle.ExternalGunNumber;
            int affectedRows = _fsql.Update<nozzle>().SetSource(_nozzle).ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油枪信息更新失败");
            }
            return ServiceResponse.Ok(_nozzle);
        }
        /// <summary>
        /// 删除油枪信息
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<ServiceResponse> DeleteNozzle(UploadNozzle uploadNozzle)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            var _nozzle = _fsql.Select<nozzle>().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First();
            if (_nozzle == null)
            {
                return ServiceResponse.Error("未找到油枪");
            }
            int affectedRows = _fsql.Delete<nozzle>()
    .Where(p => p.NozzleId == uploadNozzle.NozzleId) 
    .ExecuteAffrows();
            if (affectedRows <= 0)
            {
                return ServiceResponse.Error("油枪信息删除失败");
            }
            return ServiceResponse.Ok();
        }

        public async Task<List<NozzleInfo>> GetNozzleInfo(int Nozzleid)
        {
            Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
            return _entityHelper._fsql.Select<nozzle, tanks, product>()
                    .LeftJoin((a, b, c) => a.TankId == b.Id)
                    .LeftJoin((a, b, c) => a.ProductID == c.Id)
                    .Where((a, b, c) => a.Buid == guid && a.ExternalGunNumber == Nozzleid)
                    .ToList((a, b, c) => new NozzleInfo()
                    {
                        Nozzleid = a.ExternalGunNumber,
                        ProductName = c.ProductName,
                        ProductPrice = c.ProductPrice,
                        Status = a.Status,
                        TankNumber = b.TankNumber
                    });
        }
        #endregion
    }
}
