using Edge.Core.Domain.FccOilInfo.Input;
using Edge.Core.Domain.FccOilInfo.Ouput;
using FccLite.Web.Repositories.FccOilInfo;
using System.Text.Json;

namespace FccLite.Web.Services.FccOilInfo
{
    public class OilInfoServiceImpl : IOilInfoService
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        private readonly IOilInfoReposity _oilInfoReposity;
        public OilInfoServiceImpl(IOilInfoReposity oilInfoReposity) 
        {
            _oilInfoReposity = oilInfoReposity;
        }

        /// <summary>
        /// 通过 id 删除油品信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public async Task<OilInfoSetOutput> DeleteById(long id)
        {
            bool result = await _oilInfoReposity.DeleteByID(id);
            Logger.Info($"delete oil info by id result:{result}");
            if (result)
            {
                return new OilInfoSetOutput()
                {
                    Result = true,
                    Message = "删除成功"
                };
            }

            return new OilInfoSetOutput()
            {
                Result = false,
                Message = "删除失败"
            };
        }

        /// <summary>
        /// 分页获取油品信息
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="size">页数</param>
        /// <param name="id">过滤条件——id</param>
        /// <param name="name">过滤条件——油品名</param>
        /// <param name="code">过滤条件——油品码</param>
        /// <returns></returns>
        public async Task<OilInfoOutput> GetPage(int page, int size, long? id, string? name, int? code)
        {
            IEnumerable<Edge.Core.Domain.FccOilInfo.FccOilInfo> enumerable = await _oilInfoReposity.GetPage(page, size, id, name, code);

            List<OilInfoOutput.OilInfo> oilInfos = enumerable.Select(info =>
                new OilInfoOutput.OilInfo()
                {
                    Id = info.Id,
                    Name = info.Name,
                    Code = info.Code,
                    Price = info.Price
                }
            ).ToList();

            Logger.Info($"get page of oil info,count is {oilInfos.Count}");
            return new OilInfoOutput()
            {
                Total = oilInfos.Count,
                OilInfoList = oilInfos
            };
        }

        /// <summary>
        /// 新增、修改油品信息
        /// </summary>
        /// <param name="oilInfoInput">油品信息</param>
        /// <returns></returns>
        public async Task<OilInfoSetOutput> Save(OilInfoInput oilInfoInput)
        {
            Edge.Core.Domain.FccOilInfo.FccOilInfo fccOilInfo = await _oilInfoReposity.UploadOilInfo(oilInfoInput);
            Logger.Info($"add or update oil info,{JsonSerializer.Serialize(fccOilInfo)}");
            if (fccOilInfo == null)
            {
                return new OilInfoSetOutput()
                {
                    Result = false,
                    Message = "保存失败"
                };
            }

            return new OilInfoSetOutput()
            {
                Result = true,
                Message = "保存成功"
            };
        }
    }
}
