using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using NPOI.SS.UserModel;
using SqlSugar;
using System.Xml.Linq;
using WCS.BLL.DbModels;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.DAL.Db;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.WebApi.Helper;

namespace WCS.WebApi.Controllers
{
    /// <summary>
    /// 接口记录
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class MatBaseInfoController : ControllerBase
    {
        public IMatBaseInfoService _matBaseInfoService { get; set; }

        public IGenerateService _generateMatInfoService { get; set; }

        public MatBaseInfoController(IMatBaseInfoService matBaseInfoService, IGenerateService generateMatInfoService)
        {
            _matBaseInfoService = matBaseInfoService;
            _generateMatInfoService = generateMatInfoService;
        }

        [Route("getMatBaseInfo")]
        [HttpPost(Name = "getMatBaseInfo")]
        public async Task<ResponseBase> getMatBaseInfo(GetMatBaseInfoRequest request)
        {
            return await _matBaseInfoService.getMatBaseInfo(request);
        }

        [HttpPost("exportMatBaseInfo")]
        public async Task<IActionResult> exportMatBaseInfo(GetMatBaseInfoRequest request)
        {
            var result = await _matBaseInfoService.exportMatBaseInfo(request);
            var data = result.Data?.Lists;
            var columns = new[]
            {
                new ExportableColumn("序号","RowNumber"),
                new ExportableColumn("物料编码","MatCode"),
                new ExportableColumn("名称","MatName"),
                new ExportableColumn("规格","MatSpec"),
                new ExportableColumn("单位","MatUnit"),
                new ExportableColumn("客户","MatCustomer"),
                new ExportableColumn("状态","IsEnableStr"),
                new ExportableColumn("更新人","ModifyUser"),
                new ExportableColumn("更新时间", "ModifyTime"),
            };
            if (data == null)
            {
                return NotFound();
            }
            else
                return ExportExcelHelper.Export("导出数据", columns, data);
        }

        [HttpPost("importMatBaseInfo")]
        public async Task<ResponseBase> importMatBaseInfo([FromForm] IFormFile excelFile, [FromForm] string userName, [FromForm] string deviceType)
        {
            //文件校验
            if (excelFile == null || excelFile.Length == 0)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = "导入失败:文件无有效内容！"
                };
            }
            //
            using (var stream = new MemoryStream())
            {
                await excelFile.CopyToAsync(stream);
                stream.Position = 0;
                var list = MiniExcelLibs.MiniExcel.Query<MatBaseInfoImportModel>(stream, "物料管理", ExcelType.XLSX).ToList();
                //去除空白行
                list.RemoveAll(x => string.IsNullOrWhiteSpace(x.物料编码));

                return await _matBaseInfoService.importMatBaseInfo(list, userName, deviceType);
            }
        }

        [HttpPost("addOrUpdateMatBaseInfo")]
        public async Task<ResponseCommon<object>> addOrUpdateMatBaseInfo(AddMatBaseInfoRequest<MatBaseInfo> request)
        {
            return await _matBaseInfoService.addOrUpdateMatBaseInfo(request);
        }

        [HttpPost("deleteMatBaseInfo")]
        public async Task<ResponseCommon<object>> deleteMatBaseInfo(DeleteInfosRequest request)
        {
            return await _matBaseInfoService.deleteMatBaseInfo(request);
        }

        [HttpPost("getMatCodeList")]
        public async Task<ResponseCommon<List<string>>> getMatCodeList(GetMatCodeListRequest request)
        {
            return await _matBaseInfoService.getMatCodeList(request);
        }


        [HttpPost("generateMatInfo")]
        public async Task<ResponseCommon<List<MatInfo>>> generateMatInfo(GenerateMatInfoRequest request)
        {
            return await _generateMatInfoService.generateMatInfo(request);
        }

        /// <summary>
        /// 获取物料明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("getMatInfo")]
        [HttpPost(Name = "getMatInfo")]
        public async Task<ResponseBase> getMatInfo(GetMatInfoRequest request)
        {
            return await _matBaseInfoService.getMatInfo(request);
        }

        /// <summary>
        /// 打印物料条码后上传打印
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("printedMatInfo")]
        [HttpPost(Name = "printedMatInfo")]
        public async Task<ResponseBase> printedMatInfo(PrintedMatInfoRequest request)
        {
            return await _matBaseInfoService.printedMatInfo(request);
        }


        /// <summary>
        /// 金川项目-编码获取 没得的就生成
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("JinChuanGenerateMatInfo")]
        public async Task<ResponseBase> JinChuanGenerateMatInfo(JinChuanGenerateMatInfoRequest request)
        {
            return await _generateMatInfoService.JinChuanGenerateMatInfo(request);
        }

        /// <summary>
        /// 金川项目-扫描解析生成打印
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("JinChuanCommitMatInfo")]
        public async Task<ResponseBase> JinChuanCommitMatInfo(JinChuanCommitMatInfoRequest request)
        {
            return await _generateMatInfoService.JinChuanCommitMatInfo(request);
        }
    }
}
