using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model.ModelVm;
using Model.ModelVm.SysVm;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using Model.ModelVm.LogVm;
using WMS.IBLL.IBllAsnServer;
using WMS.IBLL.ILogServer;
using Model.ModelVm.BllAsnVm;
using Utility;
using Model.ModelDto.BllAsnDto;
using System.Threading.Tasks;
using SqlSugar;
using System.Diagnostics;
using Model.ModelDto.SysDto;
using static System.Reflection.Metadata.BlobBuilder;
using WMS.DAL;
using static System.Net.WebRequestMethods;
using WMS.IBLL;
namespace Wms.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class BllAsnController : ControllerBase
    {
        #region 依赖注入
        private readonly IArrivalNoticeServer _arrivalNoticeSvc;                    // 入库单据Svc 
        private readonly IPalletBindServer _PalletBindSvc;                          // 托盘绑定Svc 
        private readonly IBllBoxInfoServer _BoxInfoSvc;                             // 箱支关系Svc
        private readonly ITaskServer _taskSvc;                                      // 入库任务Svc 
        private readonly IOperationASNServer _logSvc;                               // 入库库操作日志Svc 
        private readonly IPalletUnbindServer _palletUnbind;                         // 托盘解绑绑定
        private readonly IAuditLogServer _auditLog;                                 // 审核记录
        private readonly IBllLabelBoxNoServer _labelBox;                            // 箱码标签
        private readonly IProcurePlanServer _procurePlanSvc;                            // 箱码标签
        private readonly IPalletUpShelfServer _palletUpShelfSvc;                            // 上架记录
        private readonly IHttpServer _http;
        #endregion
        #region 构造函数
        public BllAsnController(IArrivalNoticeServer arrivalNoticeSvc, IPalletBindServer palletBindSvc, IBllBoxInfoServer bllBoxInfoSvc, ITaskServer taskSvc, IOperationASNServer logSvc, IPalletUnbindServer palletUnbind, IAuditLogServer auditLog, IBllLabelBoxNoServer labelBox, IProcurePlanServer procurePlanSvc, IPalletUpShelfServer palletUpShelfSvc, IHttpServer http)
        {
            _arrivalNoticeSvc = arrivalNoticeSvc;
            _PalletBindSvc = palletBindSvc;
            _BoxInfoSvc = bllBoxInfoSvc;
            _taskSvc = taskSvc;
            _logSvc = logSvc;
            _palletUnbind = palletUnbind;
            _auditLog = auditLog;
            _labelBox = labelBox;
            _procurePlanSvc = procurePlanSvc;
            _palletUpShelfSvc = palletUpShelfSvc;
            _http = http;
        }
        #endregion
        #region 入库单据
        /// 
        /// 获取入库单信息
        /// 
        /// 查询条件
        /// 入库单信息
        [HttpPost]
        [ServiceFilter(typeof(ApiResponseActionFilter))]
        public async Task GetArrivalNoticeList(ArrivalNoticeVm model)
        {
            var models = Common.GetCarPath("141401", "040401", 1,"1");
            RefAsync count = new RefAsync();
            var bolls = await _arrivalNoticeSvc.GetArrivalNoticeList(model, count);
            return new SqlSugarPagedList() { Items = bolls, Total = count };
        }
        [HttpPost]
        [ServiceFilter(typeof(ApiResponseActionFilter))]
        public async Task GetArrivalNoticeDetailList(ArrivalNoticeDetailVm model)
        {
            RefAsync count = new RefAsync();
            var bolls = await _arrivalNoticeSvc.GetArrivalNoticeDetailList(model, count);
            return new SqlSugarPagedList() { Items = bolls, Total = count };
        }
        /// 
        /// 获取入库单明细剩余打印数量
        /// 
        /// 
        /// 
        [HttpGet]
        public IActionResult GetAsnDetailQtyList(int id)
        {
            try
            {
                var models = _arrivalNoticeSvc.GetAsnDetailQtyList(id);
                return Ok(new { code = 0, msg = "入库单明细信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 获取添加明细数据源
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetMaterialsList(GetMaterialsVm model)
        {
            try
            {
                var models = _arrivalNoticeSvc.GetMaterialsList(model);
                return Ok(new { code = 0, msg = "物料信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult AddArrivalNotice(ArrivalNoticeVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                model.CreateUser = int.Parse(UserId);
                string strMesage = _arrivalNoticeSvc.AddArrivalNotice(model);
                if (strMesage == "")
                {
                    return Ok(new { code = 0, msg = "添加成功" });
                }
                if (strMesage.Contains("-1"))
                {
                    return Ok(new { code = 1, msg = strMesage });
                }
                else
                {
                    return Ok(new { code = 1, msg = strMesage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 入库单据导入
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult ImportArrivalNotice(ArrivalNoticeVms models)
        {
            try
            {
                if (models.ListArrivalNotice.Count > 50)
                {
                    throw new Exception("单次导入最多50条数据");
                }
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                models.CreateUser = int.Parse(UserId);
                string strMessage = _arrivalNoticeSvc.ImportArrivalNotice(models);
                if (strMessage.Contains("-1"))
                {
                    return Ok(new { code = 1, msg = strMessage });
                }
                else
                {
                    return Ok(new { code = 0, msg = strMessage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult EditArrivalNotice(ArrivalNoticeVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                if (string.IsNullOrWhiteSpace(model.ASNNo)) 
                {
                    throw new Exception("单据号不可为null!");
                }
                model.CreateUser = int.Parse(UserId);
                string strMesage = _arrivalNoticeSvc.EditArrivalNotice(model);
                if (strMesage == "")
                {
                    return Ok(new { code = 0, msg = "编辑成功" });
                }
                if (strMesage.Contains("-1"))
                {
                    return Ok(new { code = 1, msg = "部分成功 " + strMesage });
                }
                else
                {
                    return Ok(new { code = 1, msg = strMesage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult DelArrivalNotice(ArrivalNoticeVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                model.CreateUser = int.Parse(UserId);
                string strMesage = _arrivalNoticeSvc.DelArrivalNotice(model);
                if (strMesage == "")
                {
                    return Ok(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Ok(new { code = 1, msg = strMesage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult DelArrivalNoticeDetail(ArrivalNoticeDetailVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                model.CreateUser = int.Parse(UserId);
                string strMesage = _arrivalNoticeSvc.DelArrivalNoticeDetail(model);
                if (strMesage == "")
                {
                    return Ok(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Ok(new { code = 1, msg = strMesage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 入库单撤销申请
        /// 
        /// 
        /// 
        /// 
        [HttpGet]
        public IActionResult CancelOrder(int id, string reason)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _arrivalNoticeSvc.CancelOrder(id, reason, int.Parse(userId));
                return Ok(new { code = 0, msg = "入库单撤销申请成功" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 维护入库单备注信息
        /// 
        /// 
        /// 
        /// 
        [HttpGet]
        public IActionResult EditNoticeDemo(int id, string demo)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _arrivalNoticeSvc.EditNoticeDemo(id, demo, int.Parse(userId));
                return Ok(new { code = 0, msg = "编辑备注成功" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 组托收货
        [HttpPost]
        public IActionResult GetPalletBindList(PalletBindVm model)
        {
            try
            {
                var models = _PalletBindSvc.GetPalletBindList(model, out int count);
                return Ok(new { code = 0, count, msg = "托盘绑定信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult GetBoxInfoList(BoxInfoVm model)
        {
            try
            {
                var models = _PalletBindSvc.GetBoxInfoList(model, out int count);
                return Ok(new { code = 0, count, msg = "箱支明细信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpGet]
        public IActionResult GetBoxInfoByBoxNo(string boxNo, string boxNo3, int bindNo = 0)
        {
            try
            {
                var models = _PalletBindSvc.GetBoxInfoByBoxNo(boxNo, boxNo3, bindNo);
                return Ok(new { code = 0, count = models.Count, msg = "箱支明细信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 删除托盘绑定信息
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult DelPalletBind(IdVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _PalletBindSvc.DelPalletBind(model.Id, int.Parse(userId));
                return Ok(new { code = 0, count = 0, msg = "删除成功", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 删除绑定的箱码信息
        /// 
        /// 箱号
        /// 
        [HttpGet]
        public IActionResult DelBindBoxInfo(string boxNo)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _PalletBindSvc.DelBindBoxInfo(boxNo, int.Parse(userId));
                return Ok(new { code = 0, count = 0, msg = "删除成功", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 托盘绑定撤销申请
        /// 
        /// 
        /// 申请原因
        /// 
        [HttpGet]
        public IActionResult CancelPalletBind(int id, string reason)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _PalletBindSvc.CancelPalletBind(id, reason, int.Parse(userId));
                return Ok(new { code = 0, count = 0, msg = "托盘绑定撤销申请成功", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 根据箱码获取未组盘的箱码信息
        /// 
        /// 
        /// 
        /// 
        /// 
        [HttpGet]
        public IActionResult GetBoxInfoListByBoxCode(string boxNo, string isContinue, string boxNo2)
        {
            try
            {
                var list = _BoxInfoSvc.GetBoxInfoList(boxNo, isContinue, boxNo2);
                var num = 0;
                if (list != null)
                {
                    num = list.Count;
                }
                return Ok(new { code = 0, count = num, msg = "箱码信息", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 指定储位
        /// 
        /// 指定储位数据源(正常的空储位)
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetLocateList(GetOkLocateVm model)
        {
            try
            {
                var models = _PalletBindSvc.GetLocateList("W01", model.RoadwayNo, model.Row, model.Column, model.Layer, model.LocateNo, 
                    model.StockDetailId, model.Page, model.Limit, out int count);
                return Ok(new { code = 0, count, msg = "获取指定储位信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 保存指定的储位
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult SaveAppointSlot(SaveLocateVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _PalletBindSvc.SaveAppointSlot(model.BindId, model.LocateId, int.Parse(userId));
                return Ok(new { code = 0, count = 0, msg = "指定储位成功", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 物料信息录入
        [HttpPost]
        public IActionResult GetBllBoxInfoList(BoxInfoVm model)
        {
            try
            {
                var models = _BoxInfoSvc.GetBoxInfoList(model, out int count);
                return Ok(new { code = 0, count, msg = "箱支信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult AddBllBoxInfo(BoxInfoVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                model.CreateUser = int.Parse(UserId);
                string strMesage = _BoxInfoSvc.AddBoxInfo(model);
                if (strMesage == "")
                {
                    return Ok(new { code = 0, msg = "添加成功" });
                }
                else
                {
                    return Ok(new { code = 1, msg = strMesage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = "添加失败:" + e.Message });
            }
        }
        [HttpPost]
        public IActionResult DelBllBoxInfo(BoxInfoVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                model.CreateUser = int.Parse(UserId);
                string strMesage = _BoxInfoSvc.DelBoxInfo(model);
                if (strMesage == "")
                {
                    return Ok(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Ok(new { code = 1, msg = strMesage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 物料信息录入 导入
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult ImportBllBoxInfo(BoxInfoVms models)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                models.CreateUser = int.Parse(UserId);
                string strMessage = _BoxInfoSvc.ImportBoxInfo(models);
                if (strMessage.Contains("-1"))
                {
                    return Ok(new { code = 1, msg = strMessage });
                }
                else
                {
                    return Ok(new { code = 0, msg = strMessage });
                }
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 入库日志
        /// 
        /// 获取入库任务信息
        /// 
        /// 
        /// 
        [HttpPost]
        public async Task GetArrivalTaskList(GetTaskVm model)
        {
            try
            {
                var type = new List() { "0" };
                RefAsync count = new RefAsync();
                var bolls = await _taskSvc.GetTaskList(type, model.Type, model.Status, model.TaskNo, model.IsSuccess, model.PalletNo, model.Msg, model.Page, model.Limit, count);
                return Ok(new { code = 0, count = count.Value, msg = "入库任务信息", data = bolls });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 取消入库任务
        /// 
        /// 
        /// 
        [HttpGet]
        public IActionResult CancelAsnTask(string taskNo)
        {
            try
            {
                //获取当前登录的用户ID
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                _PalletBindSvc.CancelAsnTask(taskNo, int.Parse(userId));
                return Ok(new { code = 0, msg = "取消任务已完成", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 完成入库任务
        /// 
        /// 
        /// 
        [HttpGet]
        public IActionResult FinishAsnTask(string taskNo)
        {
            try
            {
                //获取当前登录的用户ID
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                //_PalletBindSvc.ArrivalSuccessBack(taskNo, int.Parse(userId));
                _http.ArrivalSuccess2(taskNo, int.Parse(userId));
                return Ok(new { code = 0, msg = "成功完成任务", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 操作日志
        /// 
        /// 获取入库操作日志信息
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetLogOperationAsnList(GetOperationVm model)
        {
            try
            {
                var bolls = _logSvc.GetLogOperationAsnList(model.MenuName, model.Type, model.Msg, model.StartTime, model.EndTime, model.Page, model.Limit, out int count);
                return Ok(new { code = 0, count, msg = "入库操作日志信息", data = bolls });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 托盘解绑绑定
        [HttpPost]
        public IActionResult GetPalletUnbindList(PalletUnbindVm model)
        {
            try
            {
                int count = 0;
                var list = _palletUnbind.GetPalletUnbindList(model, out count);
                return Ok(new { code = 0, count = count, msg = "托盘解绑绑定列表", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult GetPalletUnbindList2(PalletUnbindVm model)
        {
            try
            {
                int count = 0;
                var list = _palletUnbind.GetPalletUnbindList2(model, out count);
                return Ok(new { code = 0, count = count, msg = "托盘解绑绑定箱码列表", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        #endregion
        #region 审核记录
        [HttpPost]
        public IActionResult GetAuditLogList(AuditLogVm model)
        {
            try
            {
                var list = _auditLog.GetAuditLogList(model, out int count);
                return Ok(new { code = 0, count, msg = "审核记录列表", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        /// 
        /// 编辑审核
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult EditAudit(EditAuditVm model)
        {
            try
            {
                //获取当前登录的用户ID
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                _auditLog.EditAudit(model.Id, model.Status, model.Opinion, int.Parse(userId));
                return Ok(new { code = 0, count = 0, msg = "审核记录列表", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        #endregion
        #region 箱码标签
        [HttpPost]
        public IActionResult GetLabelBoxList(LabelBoxInfoVm model)
        {
            try
            {
                var list = _BoxInfoSvc.GetLabelBoxList(model, out int count);
                return Ok(new { code = 0, count, msg = "箱码标签列表", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        [HttpGet]
        public IActionResult GetLabelBoxModel(int id)
        {
            try
            {
                var list = _BoxInfoSvc.GetLabelBoxModel(id);
                return Ok(new { code = 0, msg = "箱码模板信息", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult AddLabelBox(AddLabelBoxInfoVm model)
        {
            try
            {
                //获取当前登录的用户ID
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var list = _BoxInfoSvc.AddLabelBox(model.Id, model.IsReset, decimal.Parse(model.ArriveQty), model.ProductionTime, model.ExpirationTime, model.StoreTime, model.SupplierLot, int.Parse(userId));
                return Ok(new { code = 0, msg = "生成箱码标签成功", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        [HttpPost]
        public IActionResult GetBuDaLabelList(BuDaLabelBoxVm model)
        {
            try
            {
                //获取当前登录的用户ID
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var list = _BoxInfoSvc.GetBuDaLabelList(model.BoxNo, model.EndBoxNo, model.BoxNo2, model.EndBoxNo2, model.Type, int.Parse(userId));
                return Ok(new { code = 0, msg = "获取补打箱码标签信息", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        //根据入库单号过去单据下所有批次号
        [HttpGet]
        public IActionResult GetLotNoListByAsn(string asnNo)
        {
            try
            {
                var list = _BoxInfoSvc.GetLotNoListByAsn(asnNo);
                return Ok(new { code = 0, msg = "成功获取单据下所有批次号信息", data = list });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        //删除单据下单个或多个批次的标签
        [HttpPost]
        public IActionResult DelLabelByAsnNo(LabelBoxInfoVm model)
        {
            try
            {
                //获取当前登录的用户ID
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
                }
                _BoxInfoSvc.DelLabelByAsnNo(model.AsnNo, model.LotNo, int.Parse(userId));
                return Ok(new { code = 0, msg = "删除成功", data = "" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, count = 0, msg = e.Message });
            }
        }
        #endregion
        #region 数据归档
        /// 
        /// 获取入库单信息-归档数据
        /// 
        /// 查询条件
        /// 入库单信息
        [HttpPost]
        public IActionResult GetArchivingArrivalNoticeList(ArrivalNoticeVm model)
        {
            try
            {
                var models = _arrivalNoticeSvc.GetArchivingArrivalNoticeList(model, out int count);
                return Ok(new { code = 0, count, msg = "入库单信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 获取入库单明细信息-归档数据
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetArchivingArrivalNoticeDetailList(ArrivalNoticeDetailVm model)
        {
            try
            {
                var models = _arrivalNoticeSvc.GetArchivingArrivalNoticeDetailList(model, out int count);
                return Ok(new { code = 0, count, msg = "入库单明细信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 获取箱支关系信息-归档数据
        /// 
        /// 查询条件
        /// 箱支关系信息
        [HttpPost]
        public IActionResult GetArchivingBoxInfoList(BoxInfoVm model)
        {
            try
            {
                var models = _BoxInfoSvc.GetArchivingBoxInfoList(model, out int count);
                return Ok(new { code = 0, count, msg = "箱支信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 获取托盘绑定列表-归档数据
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetArchivingPalletBindList(PalletBindVm model)
        {
            try
            {
                var models = _PalletBindSvc.GetArchivingPalletBindList(model, out int count);
                return Ok(new { code = 0, count, msg = "托盘绑定信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        ///  获取托盘绑定箱码信息-归档数据
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetArchivingBoxInfoListGroup(BoxInfoVm model)
        {
            try
            {
                var models = _PalletBindSvc.GetArchivingBoxInfoList(model, out int count);
                return Ok(new { code = 0, count, msg = "箱支明细信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 通过箱码获取箱码信息-数据归档
        /// 
        /// 
        /// 
        /// 
        [HttpGet]
        public IActionResult GetArchivingBoxInfoByBoxNo(string boxNo, string boxNo3)
        {
            try
            {
                var models = _PalletBindSvc.GetArchivingBoxInfoByBoxNo(boxNo, boxNo3);
                return Ok(new { code = 0, count = models.Count, msg = "箱支明细信息", data = models });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 获取任务信息-数据归档
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetArchivingTaskList(GetTaskVm model)
        {
            try
            {
                var type = new List() { "0", "1", "2" };
                var bolls = _taskSvc.GetArchivingTaskList(type, model.Type, model.Status, model.TaskNo, model.IsSuccess, model.PalletNo, model.Msg, model.Page, model.Limit, out int count);
                return Ok(new { code = 0, count, msg = "任务信息", data = bolls });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 获取操作日志信息-数据归档
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult GetArchivingLogOperationList(GetOperationVm model)
        {
            try
            {
                var bolls = _logSvc.GetArchivingLogOperationList(model.ComeFrom, model.MenuName, model.Type, model.Msg, model.StartTime, model.EndTime, model.Page, model.Limit, out int count);
                return Ok(new { code = 0, count, msg = "入库操作日志信息", data = bolls });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 采购单据
        /// 
        /// 获取采购单信息
        /// 
        /// 查询条件
        /// 采购单信息
        [HttpPost]
        [ServiceFilter(typeof(ApiResponseActionFilter))]
        public async Task GetProcurePlanNoticeList(ProcurePlanNoticeVm model)
        {
            RefAsync count = new RefAsync();
            var bolls = await _procurePlanSvc.GetProcurePlanNoticeList(model, count);
            return new SqlSugarPagedList() { Items = bolls, Total = count };
        }
        /// 
        /// 获取采购单明细信息
        /// 
        /// 
        /// 
        [HttpPost]
        [ServiceFilter(typeof(ApiResponseActionFilter))]
        public async Task GetProcurePlanNoticeDetailList(ProcurePlanNoticeDetailVm model)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            RefAsync count = new RefAsync();
            var bolls = await _procurePlanSvc.GetProcurePlanNoticeDetailList(model, count);
            stopwatch.Stop();
            var time = stopwatch.ElapsedMilliseconds;
            return new SqlSugarPagedList() { Items = bolls, Total = count };
        }
        /// 
        /// 通过采购单生成入库单据
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult CreateAsnByProcurePlan(ProcurePlanNoticeDetailVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _procurePlanSvc.CreateAsnByProcurePlan(Convert.ToInt32(model.Id), int.Parse(UserId));
                return Ok(new { code = 0, msg = "添加成功" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        /// 
        /// 关闭采购单据
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult CloseProcurePlan(ProcurePlanNoticeDetailVm model)
        {
            try
            {
                var claimsIdentity = this.User.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    throw new Exception("未获取到用户信息");
                }
                string UserId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
                if (string.IsNullOrWhiteSpace(UserId))
                {
                    throw new Exception("未获取到用户信息");
                }
                _procurePlanSvc.CloseProcurePlan(Convert.ToInt32(model.Id), int.Parse(UserId));
                return Ok(new { code = 0, msg = "关单成功" });
            }
            catch (Exception e)
            {
                return Ok(new { code = 1, msg = e.Message });
            }
        }
        #endregion
        #region 上架任务记录
        /// 
        /// 获取上架任务记录信息分页集合
        /// 
        /// 
        /// 
        [HttpPost]
        [ServiceFilter(typeof(ApiResponseActionFilter))]
        public async Task GetPalletUpShelfList(PalletUpShelfVm model)
        {
            RefAsync count = new RefAsync();
            var data = await _palletUpShelfSvc.GetPalletUpShelfList(model.TraceNo, model.TaskNo,model.PalletNo,model.SkuNo,model.SkuName,model.LotNo,model.Status,model.Page,model.Limit, count);
            return new SqlSugarPagedList() { Items = data, Total = count };
        }
        #endregion
    }
}