using Microsoft.AspNetCore.Mvc;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.Model.ApiModel.MatInventoryDetail;
using WCS.Model;
using WCS.Model.ApiModel.StoreInfo;
using WCS.BLL.DbModels;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.DAL.DbModels;
using WCS.DAL.Db;
using WCS.Model.ApiModel.Home;
using WCS.Model.ApiModel.LocationInfo;
using WCS.Model.ApiModel.MatDetailCurrentInfo;
using WCS.WebApi.Helper;

namespace WCS.WebApi.Controllers
{
    /// <summary>
    /// 位置管理的接口
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class LocationInfoController : ControllerBase
    {
        public ILocationInfoService _locationInfoService { get; set; }
        public LocationInfoController(ILocationInfoService locationInfoService)
        {
            _locationInfoService = locationInfoService;
        }

        #region 位置管理
        [Route("getLocationAreas")]
        [HttpPost(Name = "getLocationAreas")]
        public async Task<ResponseBase> getLocationAreas(RequestBase request)
        {
            try
            {
                //直接获取当前所有货架类型并返回
                var locationAreaInfos = DbHelp.db.Queryable<LocationAreaInfo>()
                                          .Select(t => new LocationAreaInfoModel()
                                          {
                                              Id = t.Id,
                                              LocationAreaName = t.LocationAreaName
                                          })
                                          .ToList();
                return new PageQueryResponse<LocationAreaInfoModel>()
                {
                    Code = 200,
                    Message = "success",
                    Data = new PageQueryResponseData<LocationAreaInfoModel>
                    {
                        Lists = locationAreaInfos,
                        Count = locationAreaInfos.Count
                    }
                };
            }
            catch (Exception ex)
            {
                return new PageQueryResponse<LocationAreaInfoModel>()
                {
                    Code = 300,
                    Message = $"查询失败:{ex.Message}",
                };
            }
        }

        [Route("getLocationInfos")]
        [HttpPost(Name = "getLocationInfos")]
        public async Task<ResponseBase> GetLocationInfos(GetLocationInfosRequest request)
        {
            return await _locationInfoService.GetLocationInfos(request);
        }

        [Route("exportLocationInfos")]
        [HttpPost(Name = "exportLocationInfos")]
        public async Task<IActionResult> ExportLocationInfos(GetLocationInfosRequest request)
        {
            var result = await _locationInfoService.ExportLocationInfos(request);
            var data = result.Data?.Lists;
            var columns = new[]
            {
                new ExportableColumn("序号","RowNumber"),
                new ExportableColumn("位置区域","LocationArea"),
                new ExportableColumn("位置编号","LocationCode"),
                new ExportableColumn("RCS库位编号","RcsStoreCode"),
                new ExportableColumn("可放置货架类型","AllowShelfTypesNameStr"),
                new ExportableColumn("启用状态","IsEnableStr"),
                new ExportableColumn("更新人", "ModifyUser"),
                new ExportableColumn("更新时间", "ModifyTime"),
            };
            if (data == null)
            {
                return NotFound();
            }
            else
                return ExportExcelHelper.Export("导出数据", columns, data);
        }

        [HttpPost("addOrUpdateLocationInfo")]
        public async Task<ResponseCommon<object>> addOrUpdateLocationInfo(AddLocaionInfoRequest<LocationInfo> request)
        {
            return await _locationInfoService.addOrUpdateLocationInfo(request);
        }

        [HttpPost("deleteLocationInfo")]
        public async Task<ResponseCommon<object>> deleteLocationInfo(DeleteInfosRequest request)
        {
            return await _locationInfoService.deleteLocationInfo(request);
        }
        #endregion
    }
}
