using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_IOutboundRepository;
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
namespace WIDESEA_IOutboundService
{
    public partial class OutStockLockInfoService
    {
        /// 
        /// 分拣
        /// 
        /// 托盘号
        /// 
        public WebResponseContent MaterialPick(string palletCode)
        {
            try
            {
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(palletCode);
                if (stockInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该托盘库存信息");
                }
                if (stockInfo.StockStatus != StockStatusEmun.出库完成.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"该托盘还未出库完成");
                }
                List outStockLockInfos = Repository.QueryData(x => x.PalletCode == palletCode && x.Status == OutLockStockStatusEnum.出库完成.ObjToInt());
                if (outStockLockInfos == null || outStockLockInfos.Count == 0)
                {
                    return WebResponseContent.Instance.Error("该托盘已全部分拣完成");
                }
                for (int i = 0; i < outStockLockInfos.Count; i++)
                {
                    outStockLockInfos[i].Status = OutLockStockStatusEnum.拣选完成.ObjToInt();
                }
                List beforeDetaile = stockInfo.Details.GroupBy(x => x.MaterielCode).Select(x => new Dt_StockInfoDetail { MaterielCode = x.Key, StockQuantity = x.Sum(v => v.StockQuantity) }).ToList();
                List updateDetailList = new List();
                List deleteDetailList = new List();
                List residueDetailList = new List();
                for (int i = 0; i < stockInfo.Details.Count; i++)
                {
                    if (stockInfo.Details[i].OutboundQuantity > 0)
                    {
                        if (stockInfo.Details[i].StockQuantity > stockInfo.Details[i].OutboundQuantity)
                        {
                            stockInfo.Details[i].StockQuantity -= stockInfo.Details[i].OutboundQuantity;
                            updateDetailList.Add(stockInfo.Details[i]);
                        }
                        else
                        {
                            deleteDetailList.Add(stockInfo.Details[i]);
                        }
                    }
                    else
                    {
                        residueDetailList.Add(stockInfo.Details[i]);
                    }
                }
                stockInfo.StockStatus = StockStatusEmun.拣选完成.ObjToInt();
                List mCodes = stockInfo.Details.GroupBy(x => x.MaterielCode).Select(x => x.Key).ToList();
                _unitOfWorkManage.BeginTran();
                foreach (string code in mCodes)
                {
                    _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, residueDetailList, updateDetailList, deleteDetailList, StockChangeTypeEnum.Outbound);
                }
                updateDetailList.ForEach(x =>
                {
                    x.OutboundQuantity = 0;
                });
                if (stockInfo.Details.Count > deleteDetailList.Count)
                    _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                else
                    _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, OperateTypeEnum.自动完成);
                _stockService.StockInfoDetailService.Repository.DeleteAndMoveIntoHty(deleteDetailList, OperateTypeEnum.自动完成);
                _stockService.StockInfoDetailService.Repository.UpdateData(updateDetailList);
                //_stockService.StockInfoDetailService.Repository.DeleteData(deleteDetailList);
                BaseDal.UpdateData(outStockLockInfos);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}