using Microsoft.AspNetCore.Mvc;
using WCS.BLL;
using WCS.BLL.HardWare;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Tool;
using WCS.Model;
using WCS.Model.ApiModel.InStore;

namespace WebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class InstoreController : ControllerBase
    {

        private readonly ILogger<InstoreController> _logger;
        private readonly IInstoreService _instoreService;

        public InstoreController(ILogger<InstoreController> logger, IInstoreService instoreService)
        {
            _logger = logger;
            _instoreService = instoreService;
        }

        /// <summary>
        /// 进入入库模式
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("shelfGoInInStore")]
        [HttpPost(Name = "shelfGoInInStore")]
        public async Task<ResponseBase> shelfGoInInStore(ShelfGoInInstoreRequest request)
        {
            string content = string.Empty;
            try
            {
                Logs.Write($"[request.IpAdress]{request.IpAdress}", LogsType.Instore);
                //获取调用设备的IP地址
                if (string.IsNullOrEmpty(request.IpAdress))
                {
                    var IPAdress = HttpContext?.Connection?.RemoteIpAddress?.ToString();
                    if (string.IsNullOrEmpty(IPAdress))
                    {

                    }
                    else
                    {
                        //获取到调用设备的Ip地址 缓存进request
                        request.IpAdress = IPAdress;
                    }
                }
                Logs.Write($"[request.IpAdress]{request.IpAdress}", LogsType.Instore);
                return _instoreService.shelfGoInInStore(request);

            }
            catch (Exception ex)
            {
                return new ShelfGoInInstoreResponse()
                {
                    Code = 300,
                    Message = "货架进入入库模式失败：" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 退出入库模式
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("shelfGoOutInStore")]
        [HttpPost(Name = "shelfGoOutInStore")]
        public async Task<ResponseBase> shelfGoOutInStore(ShelfGoOutInStoreRequest request)
        {
            try
            {
                return _instoreService.shelfGoOutInStore(request);
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 300,
                    Message = $"货架退出入库模式失败:{ex.Message}",
                };
            }
        }

        /// <summary>
        /// 扫码绑定物料 获取物料明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("queryByMatSn")]
        [HttpPost(Name = "queryByMatSn")]
        public async Task<ResponseBase> queryByMatSn(QueryByMatSnRequest request)
        {
            return await _instoreService.queryByMatSn(request);
        }

        /// <summary>
        /// 扫码绑定物料 获取物料明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("queryByMatSnOut")]
        [HttpPost(Name = "queryByMatSnOut")]
        public async Task<ResponseBase> queryByMatSnOut(QueryByMatSnRequest request)
        {
            return await _instoreService.queryByMatSnOut(request);
        }

        /// <summary>
        /// 查询入库状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("queryInstoreStatus")]
        [HttpPost(Name = "queryInstoreStatus")]
        public async Task<ResponseBase> queryInstoreStatus(QueryByMatSnRequest request)
        {
            try
            {
                return await _instoreService.queryInstoreStatus(request);
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }
    }
}
