using Microsoft.Extensions.Logging;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service.@base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlnMesnac.Repository.service.Impl
{
    public class ProdOrderInfoServiceImpl : BaseServiceImpl<ProdOrderInfo>, ProdOrderInfoService
    {
        private ILogger<ProdPlanInfoServiceImpl> _logger;
        public ProdOrderInfoServiceImpl(Repository<ProdOrderInfo> rep, ILogger<ProdPlanInfoServiceImpl> logger) : base(rep)
        {
            _logger = logger;
        }

        /// <summary>
        /// 根据订单号查询订单信息
        /// </summary>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ProdOrderInfo GetProdOrderInfoByOrderCode(string orderCode)
        {
            ProdOrderInfo prodOrderInfo = new ProdOrderInfo();
            try
            {
                prodOrderInfo = _rep.GetFirst(x => x.OrderCode == orderCode);
            }
            catch (Exception ex)
            {
                _logger.LogError($"根据订单号查询订单信息异常{ex.Message}");
            }
            return prodOrderInfo;
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="orderCode"></param>
        /// <param name="status"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateOrderInfoStatus(string orderCode, string status)
        {
            try
            {
                var prodOrderInfo = _rep.GetFirst(x => x.OrderCode == orderCode);
                if (prodOrderInfo != null)
                {
                    prodOrderInfo.OrderStatus = status; // 更新订单状态
                    _rep.Update(prodOrderInfo); // 假设有一个Update方法来保存更改
                }
                else
                {
                    _logger.LogWarning($"未找到订单号为 {orderCode} 的订单信息");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"更新订单状态异常：{ex.Message}");
            }
        }

    }
}
