using Masuit.Tools; 
 | 
using SqlSugar; 
 | 
using WIDESEA_Core.Const; 
 | 
using WIDESEA_DTO.MOM; 
 | 
using WIDESEA_DTO.WMS; 
 | 
  
 | 
//using WIDESEAWCS_Model.Models; 
 | 
  
 | 
namespace WIDESEA_StorageTaskServices; 
 | 
  
 | 
public partial class Dt_TaskService : ServiceBase<Dt_Task, IDt_TaskRepository>, IDt_TaskService 
 | 
{ 
 | 
    /// <summary> 
 | 
    /// 根据当前工序判断下一工序,送至对应工序 
 | 
    /// </summary> 
 | 
    /// <param name="boxing">组盘</param> 
 | 
    /// <param name="area">区域</param> 
 | 
    /// <param name="input">请求参数</param> 
 | 
    /// <param name="resultTray">托盘电芯</param> 
 | 
    /// <returns></returns> 
 | 
    private async Task<WebResponseContent> ProcessBasedOnProcessCode(DtBoxingInfo boxing, Dt_AreaInfo area, RequestTaskDto input, ResultTrayCellsStatus resultTray) 
 | 
    { 
 | 
        if (boxing.ProcessCode == "OCVB") 
 | 
        { 
 | 
            return await ProcessOCVB(boxing, area, input); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            return await ProcessOtherProcessCode(boxing, area, input, resultTray); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// OCVB特殊逻辑 
 | 
    /// </summary> 
 | 
    /// <param name="boxing">组盘信息</param> 
 | 
    /// <param name="area">区域</param> 
 | 
    /// <param name="input">请求信息</param> 
 | 
    /// <returns></returns> 
 | 
    /// <exception cref="InvalidOperationException"></exception> 
 | 
    private async Task<WebResponseContent> ProcessOCVB(DtBoxingInfo boxing, Dt_AreaInfo area, RequestTaskDto input) 
 | 
    { 
 | 
        WebResponseContent content = new WebResponseContent(); 
 | 
        try 
 | 
        { 
 | 
            // 判断需不需要去包装,不需要就去常温三 
 | 
            var stationManagers = _stationManagerRepository.QueryData(x => x.stationPLC == "1007" && x.stationArea == "Cache" && x.productLine == input.ProductionLine); 
 | 
  
 | 
            var STATION = _stationManagerRepository.QueryFirst(x => x.stationChildCode == input.Position); 
 | 
  
 | 
            //var station = stationManagers.Select(x => x.stationChildCode).ToList(); 
 | 
  
 | 
            //// 获取WCSip地址相关配置 
 | 
            var wcsIpAddrss = GetWCSIpAddress(); 
 | 
            //if (wcsIpAddrss == null) 
 | 
            //{ 
 | 
            //    throw new InvalidOperationException("WCS IP 未配置"); 
 | 
            //} 
 | 
  
 | 
            //var abc = HttpHelper.PostAsync(wcsIpAddrss, station.ToJsonString()).Result; 
 | 
            //content = JsonConvert.DeserializeObject<WebResponseContent>(abc); 
 | 
            //var num = content.Data.ObjToInt(); 
 | 
  
 | 
            //如果当前库内存在比当前时间的对应产线的料 则入库至常温3 
 | 
            DtStockInfo stockInfo = _stockInfoRepository.QueryFirst(x => x.AreaCode == "CWSC3" && x.IsFull == true && x.ProductionLine == STATION.productLine && x.OutboundTime < DateTime.Now); 
 | 
            if (stockInfo != null) 
 | 
            { 
 | 
                var config = _configService.GetByConfigKey("SYS_InStacker", "CW3InStacker"); 
 | 
                var strings = config.ConfigValue.Split(',').ToList(); 
 | 
                // 入库送至常温3 
 | 
                var resultContent = await CreateNewTask(input, input.ProductionLine, boxing.ProcessCode, strings); 
 | 
                if (resultContent.Status) 
 | 
                { 
 | 
                    await _boxingInfoRepository.AddDataNavAsync(boxing); 
 | 
                } 
 | 
                return resultContent; 
 | 
            } 
 | 
  
 | 
            // TODO 判断在途数量 
 | 
  
 | 
            var needBarcode = await SqlSugarHelper.DbWCS.Queryable<dt_needBarcode>().FirstAsync(x => x.productLine == STATION.productLine && x.fromArea == "CW"); 
 | 
            var needCount = BaseDal.QueryData(x => x.TargetAddress == needBarcode.toArea).Count(); 
 | 
            //var count = BaseDal.QueryData(x => x.TargetAddress == stationManagers[0].Roadway).Count; 
 | 
            if (needCount < needBarcode.cacheNum) 
 | 
            { 
 | 
                // 送至包装 
 | 
                List<string> strings = stationManagers.Where(x => x.stationType == 0 && x.productLine == input.ProductionLine).Select(x => x.Roadway).ToList(); 
 | 
  
 | 
                var x = await SqlSugarHelper.DbWCS.Updateable<dt_needBarcode>() 
 | 
                    .SetColumns(x => x.inLineNum == x.inLineNum + 1) 
 | 
                    .Where(x => x.id == needBarcode.id).ExecuteCommandHasChangeAsync(); 
 | 
  
 | 
                ConsoleHelper.WriteSuccessLine($"在途数量添加{(x ? "成功" : "失败")}数量+1,当前工序{boxing.ProcessCode},产线{input.ProductionLine}"); 
 | 
                LogFactory.GetLog("OCVB").InfoFormat(true, $"在途数量添加{(x ? "成功" : "失败")}数量+1,当前工序{boxing.ProcessCode},产线{input.ProductionLine}", "无参数"); 
 | 
                return await CreateNewTask(input, input.ProductionLine, boxing.ProcessCode, strings, 3); 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                var config = _configService.GetByConfigKey("SYS_InStacker", "CW3InStacker"); 
 | 
                var strings = config.ConfigValue.Split(',').ToList(); 
 | 
                // 入库送至常温3 
 | 
                var resultContent = await CreateNewTask(input, input.ProductionLine, boxing.ProcessCode, strings); 
 | 
                if (resultContent.Status) 
 | 
                { 
 | 
                    await _boxingInfoRepository.AddDataNavAsync(boxing); 
 | 
                } 
 | 
                return resultContent; 
 | 
            } 
 | 
        } 
 | 
        catch (Exception ex) 
 | 
        { 
 | 
            ConsoleHelper.WriteErrorLine(ex.Message); 
 | 
            LogFactory.GetLog("OCVB").InfoFormat(true, $",异常信息:{ex.Message},{ex.StackTrace}", "无参数"); 
 | 
            return content.Error(ex.Message); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 正常入库请求流程 
 | 
    /// </summary> 
 | 
    /// <param name="boxing">组盘信息</param> 
 | 
    /// <param name="area">区域</param> 
 | 
    /// <param name="input">请求信息</param> 
 | 
    /// <param name="result">整盘电芯数据</param> 
 | 
    /// <returns></returns> 
 | 
    /// <exception cref="Exception"></exception> 
 | 
    private async Task<WebResponseContent> ProcessOtherProcessCode(DtBoxingInfo boxing, Dt_AreaInfo area, RequestTaskDto input, ResultTrayCellsStatus result) 
 | 
    { 
 | 
        WebResponseContent content = new WebResponseContent(); 
 | 
        // 获取工艺路线 
 | 
        ProcessApplyDto process = await GetProcessApplyAsync(result); 
 | 
        if (process == null) return content; 
 | 
  
 | 
        // 赋值上位软件名称和设备编码 
 | 
        process.Software = area.Spare3; 
 | 
        process.EquipmentCode = area.Spare2; 
 | 
  
 | 
        content = await _processApplyService.GetProcessApplyAsync(process); 
 | 
        if (!content.Status) return content.Error("工艺申请失败"); 
 | 
  
 | 
        var resultProcessApply = JsonConvert.DeserializeObject<ResultProcessApply>(content.Data.ToString()); 
 | 
        if (!resultProcessApply.Success) 
 | 
        { 
 | 
            return await HandleErrorCells(input, area); 
 | 
            //return content.Error("工艺申请失败"); 
 | 
        } 
 | 
  
 | 
        var number = resultProcessApply.ProcessInfo.Where(x => x.ProcessCode.Contains(boxing.ProcessCode)).FirstOrDefault().Number.ToInt32(); 
 | 
        foreach (var item in resultProcessApply.ProcessInfo) 
 | 
        { 
 | 
            if (item.Number.ToInt32() == number + 1) 
 | 
            { 
 | 
                boxing.NextProcessCode = item.ProcessCode; 
 | 
                break; 
 | 
            } 
 | 
        } 
 | 
  
 | 
        var areaIn = GetAreaInByNextProcessCode(boxing.NextProcessCode); 
 | 
        var stationManagers = _stationManagerRepository.QueryData(x => x.stationType == 1 && x.stationChildCode == input.Position && x.stationArea.Contains(areaIn)).FirstOrDefault(); 
 | 
        if (stationManagers == null) 
 | 
        { 
 | 
            throw new Exception("未找到入库站台配置"); 
 | 
        } 
 | 
        //List<string> strings; 
 | 
        //if (areaIn == "GWSC1") 
 | 
        //{ 
 | 
        //    strings = new List<string> 
 | 
        //    { 
 | 
        //        (boxing.ProductionLine != null && boxing.ProductionLine == "ZJ-7") ? "GWSC1" : "GWSC2" 
 | 
        //    }; 
 | 
        //} 
 | 
        //else 
 | 
        //{ 
 | 
        //    strings = stationManagers.Roadway.Split(',').ToList(); 
 | 
        //} 
 | 
  
 | 
        List<string> strings = stationManagers.Roadway.Split(',').ToList(); 
 | 
  
 | 
        var resultContent = await CreateNewTask(input, result.ProductionLine, boxing.ProcessCode, strings); 
 | 
        if (resultContent.Status) 
 | 
        { 
 | 
            var isBox = await _boxingInfoRepository.AddDataNavAsync(boxing); 
 | 
        } 
 | 
        return resultContent; 
 | 
    } 
 | 
  
 | 
    private string GetAreaInByNextProcessCode(string nextProcessCode) 
 | 
    { 
 | 
        switch (nextProcessCode) 
 | 
        { 
 | 
            case "CH01": 
 | 
                return "CH001"; 
 | 
  
 | 
            case "JZ01": 
 | 
                return "JZ001"; 
 | 
  
 | 
            case "GW01": 
 | 
                return "GWSC1"; 
 | 
  
 | 
            case "CW01": 
 | 
                return "CWSC1"; 
 | 
  
 | 
            case "CW02": 
 | 
                return "CWSC2"; 
 | 
  
 | 
            default: 
 | 
                return string.Empty; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    private string GetWCSIpAddress() 
 | 
    { 
 | 
        var configz = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress); 
 | 
        var wcsBasez = configz.Where(x => x.ConfigKey == SysConfigConst.WCSIPAddress).FirstOrDefault()?.ConfigValue; 
 | 
        var address = configz.Where(x => x.ConfigKey == SysConfigConst.GetStation).FirstOrDefault()?.ConfigValue; 
 | 
        if (wcsBasez == null || address == null) 
 | 
        { 
 | 
            return null; 
 | 
        } 
 | 
        return wcsBasez + address; 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 电芯NG送至NG口 
 | 
    /// </summary> 
 | 
    private async Task<WebResponseContent> HandleErrorCells(RequestTaskDto input, Dt_AreaInfo area, List<SerialNoDto> serialNosError = null) 
 | 
    { 
 | 
        WebResponseContent content = new WebResponseContent(); 
 | 
        // TODO 创建任务送至NG排出口 
 | 
        var stationManagers = _stationManagerRepository.QueryData(x => x.stationType == 1 && x.stationChildCode == input.Position && x.stationArea == area.AreaCode).FirstOrDefault(); 
 | 
        if (stationManagers == null) 
 | 
        { 
 | 
            throw new Exception("未找到NG入库站台配置"); 
 | 
        } 
 | 
        //List<string> NGStation = stationManagers.Roadway.Split(',').ToList(); 
 | 
        //if (NGStation.Count <= 0) 
 | 
        //{ 
 | 
        //    NGStation = stationManagers.stationNGChildCode.Split(',').ToList(); 
 | 
        //} 
 | 
        List<string> NGStation = stationManagers.stationNGChildCode.Split(',').ToList(); 
 | 
        content = await CreateNewTask(input, input.ProductionLine, "", NGStation, 2); 
 | 
        return content.Error("存在异常电芯"); 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 创建并获取整盘电芯 
 | 
    /// </summary> 
 | 
    private TrayCellsStatusDto CreateTrayCellsStatusDto(Dt_AreaInfo area, string palletCode) 
 | 
    { 
 | 
        return new TrayCellsStatusDto() 
 | 
        { 
 | 
            Software = area.Spare3, 
 | 
            TrayBarcode = palletCode, 
 | 
            EquipmentCode = area.Spare2, 
 | 
            SceneType = area.Spare4 
 | 
        }; 
 | 
    } 
 | 
  
 | 
    private WebResponseContent CreateAndReturnWMSTaskDTO(Dt_Task task) 
 | 
    { 
 | 
        WMSTaskDTO taskDTO = new WMSTaskDTO() 
 | 
        { 
 | 
            TaskNum = task.TaskNum.Value, 
 | 
            Grade = 1, 
 | 
            PalletCode = task.PalletCode, 
 | 
            RoadWay = task.Roadway, 
 | 
            SourceAddress = task.SourceAddress, 
 | 
            TargetAddress = task.Roadway, 
 | 
            TaskState = task.TaskState.Value, 
 | 
            Id = 0, 
 | 
            TaskType = task.TaskType, 
 | 
            ProductionLine = task.ProductionLine, 
 | 
        }; 
 | 
        return new WebResponseContent().OK(data: taskDTO); 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 查询任务 
 | 
    /// </summary> 
 | 
    private async Task<Dt_Task> QueryTaskByPalletCode(string palletCode) 
 | 
    { 
 | 
        return await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode); 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 查询库存信息 
 | 
    /// </summary> 
 | 
    private async Task<DtStockInfo> QueryStockInfo(string palletCode) 
 | 
    { 
 | 
        return await _stockInfoRepository.QueryFirstNavAsync(x => x.PalletCode == palletCode && x.IsFull); 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 根据位置查询区域信息 
 | 
    /// </summary> 
 | 
    private async Task<Dt_AreaInfo> QueryAreaInfoByPosition(string position) 
 | 
    { 
 | 
        return await _areaInfoRepository.QueryFirstAsync(x => x.Spare1.Contains(position)); 
 | 
    } 
 | 
} 
 |