using AutoMapper;
using WIDESEAWCS_Common.LocationEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.Utilities;
using WIDESEAWCS_DTO.BasicInfo;
using WIDESEAWCS_IWMSPart;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_WMSPart
{
    /// 
    /// 货位信息业务接口实现层
    /// 
    public class LocationInfoService : ServiceBase>, ILocationInfoService
    {
        public IRepository Repository => BaseDal;
        private readonly IRepository _dt_storagemode;
        private readonly IMapper _mapper;
        private readonly IRepository _stockInfoRepository;
        private readonly IRepository _batchInfoRepository;
        //private readonly Idt_ErrormsginfoService _ErrormsginfoService;
        public LocationInfoService(IRepository BaseDal, IRepository storagemode, IRepository stockInfoRepository, IRepository batchInfoRepository, IMapper mapper) : base(BaseDal)
        {
            _dt_storagemode = storagemode;
            _mapper = mapper;
            _stockInfoRepository = stockInfoRepository;
            _batchInfoRepository = batchInfoRepository;
        }
        public override PageGridData GetPageData(PageDataOptions options)
        {
            return base.GetPageData(options);
        }
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            int id = saveModel.MainData["id"].ObjToInt();
            int locationStatus = saveModel.MainData["locationStatus"].ObjToInt();
            int enableStatus = saveModel.MainData["enableStatus"].ObjToInt();
            Dt_LocationInfo locationInfo = BaseDal.QueryFirst(x => x.Id == id);
            locationInfo.LocationStatus = locationStatus;
            locationInfo.EnableStatus = enableStatus;
            //有货生成库存信息
            if (locationStatus == 2)
            {
                //判断是否存在库存信息
                var w = _stockInfoRepository.QueryFirst(x => x.LocationCode == locationInfo.LocationCode);
                if(w == null)
                {
                        Dt_StockInfo stockInfo = new Dt_StockInfo();
                        //查询当前需要出库的批次号,以及出库物料类型
                        var batch = _batchInfoRepository.QueryFirst(v => v.Id == 1);
                        stockInfo.BatchNo = batch.OutBatch;
                        stockInfo.Weight = 4;
                        stockInfo.LocationCode = locationInfo.LocationCode;
                        stockInfo.WarehouseId = 0;
                        stockInfo.StockStatus = 0;
                        stockInfo.Remark = 1;
                        _stockInfoRepository.AddData(stockInfo);
                }
            }
            //无货删除库存信息
            else if(locationStatus == 0)
            {
                //判断是否存在库存信息
                var w = _stockInfoRepository.QueryFirst(x => x.LocationCode == locationInfo.LocationCode);
                if (w != null)
                {
                    _stockInfoRepository.DeleteData(w);
                }
            }
            return UpdateData(locationInfo);
            //return base.UpdateData(saveModel);
        }
        public WebResponseContent GetLocationLayer()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List