package com.youchain.appupdate.service.impl;

import cn.hutool.core.date.DateUtil;
import com.youchain.appupdate.ReturnJson.ReturnTaskVo;
import com.youchain.appupdate.service.BydAppService;
import com.youchain.basicdata.domain.Area;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.service.ItemService;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.StockService;
import com.youchain.basicdata.vo.BarCodeVo;
import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.service.*;
import com.youchain.exception.BadRequestException;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class BydAppServiceImpl implements BydAppService {
    private final StockService stockService;
    private final ItemService itemService;
    private final PointService pointService;
    private final AsnDetailService asnDetailService;
    private final ItemKeyService itemKeyService;
    private final TaskService taskService;
    private final AgvTaskService agvTaskService;
    private final InventoryService inventoryService;
    private final KMReService kmReService;
    private final PickService pickService;
    private final StockTypeToAreaMap stockTypeToAreaMap;


    /**
     * 任务放行
     *
     * @param code 编号
     */
    @Override
    public void operationFeedback(String code) {

    }

    /**
     * 原材料入库
     *
     * @param stockCode  托盘
     * @param boxNumbers 箱号
     * @param pointCode  点位
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void materialIn(String stockCode, List<String> boxNumbers, String pointCode) {
        //验证容器
        Stock stock = stockService.validateStock(stockCode);

        //验证容器状态
        stockService.validateStockStatus(stock);

        //验证箱条码集合
        validateBoxNumbers(boxNumbers);

        //解析箱条码集合
        List<BarCodeVo> barCodeVos = parseBoxNumbers(boxNumbers);

        //验证箱条码集合是否为同一物料,箱号是否重复
        validateItemCodeAndOrderNumber(barCodeVos);

        //验证物料
        String firstItemCode = barCodeVos.iterator().next().getItemCode();
        Item item = itemService.validateItem(firstItemCode);

        //指定容器类型：托盘
        validateStockType(stock.getStockType());

        //验证起点点位
        Point srcPoint = pointService.validatePoint(pointCode);

        //验证起点点位状态
        pointService.validatePointStatus(srcPoint);

        //验证起点点位是否在可入库区域
        validateSrcPointArea(srcPoint);

        //验证终点点位
        String areaName = stockTypeToAreaMap.getValueByKey(item.getGoodType());
        List<Point> pointList = pointService.queryPoints(null, BaseStatus.FREE, BaseStatus.STORAGE, areaName);
        if (pointList.isEmpty()) {
            throw new BadRequestException(areaName + "没有空闲的点位");
        }
        Point endPoint = pointList.get(0);

        //创建下发任务
        AgvTask agvTask = agvTaskService.createAgvTask(BizStatus.ASN, stock, srcPoint.getCode(), endPoint.getCode(), "FORKLIFT_MOVE");

        //创建ASN明细和ItemKey和Task任务
        createAsnDetailsAndItemKeysAndTasks(barCodeVos, item, stock, srcPoint, endPoint, agvTask);

        //更新库存和点位状态
        updateStockAndPoints(stock, srcPoint, endPoint);

        //发送任务
        kmReService.sendAgvTask(agvTask, kmReService.sendAgvTaskCcJson(agvTask));

    }

    @Override
    public ReturnTaskVo scanStock(String stockCode) {
        List<Task> taskList = taskService.findTaskByContains(BizStatus.PICK, BizStatus.ARRIVED, stockCode);
        if (taskList.isEmpty()) {
            throw new BadRequestException("未找到" + stockCode + "托盘的翻包拣货任务!");
        }
        Task task = taskList.get(0);
        ReturnTaskVo returnTaskVo = new ReturnTaskVo();
        returnTaskVo.setTaskId(task.getId());
        returnTaskVo.setSrcOrderNumber(task.getItemKey().getOrderNumber());
        returnTaskVo.setItemCode(task.getItem().getCode());
        returnTaskVo.setItemName(task.getItem().getName());
        returnTaskVo.setSrcPointCode(task.getSrcPointCode());
        returnTaskVo.setDstPointCode(task.getDstPointCode());
        returnTaskVo.setPlanQty(task.getPlanQty());
        return returnTaskVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fbPicking(Long taskId, String orderNumber, String dstStockCode) {
        //验证任务
        Task task = validateTask(taskId, orderNumber);

        //工单
        Pick pick = task.getPickDetail().getPick();

        //验证目标容器
        Stock dstStock = validatedstStock(task, pick, dstStockCode);

        //库存移位
        taskService.moveInventory(task, dstStock, task.getPlanQty());

        //刷新出库单状态
        pickService.refreshPickStatus(task.getPickDetail().getPick());

        //成品原托盘返库；单品不回库
        if (BaseStatus.GD_TYPE_CT.equals(pick.getOrderType())) {
            this.returnFBAgvTask(task);
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unBindContainer(String stockCode) {
        Stock stock = stockService.validateStock(stockCode);
        stock.setStatus(BaseStatus.FREE);
        stockService.update(stock);
    }

    /**
     * 验证箱条码集合
     *
     * @param boxNumbers 箱条码集合
     */
    private void validateBoxNumbers(List<String> boxNumbers) {
        if (boxNumbers.isEmpty()) {
            throw new BadRequestException("请扫描箱号!");
        }
    }

    /**
     * 解析箱条码集合
     *
     * @param boxNumbers 箱条码集合
     */
    private List<BarCodeVo> parseBoxNumbers(List<String> boxNumbers) {
        List<BarCodeVo> barCodeVos = new ArrayList<>();
        for (String mo : boxNumbers) {
            Map<String, String> map = parseString(mo);
            BarCodeVo barCodeVo = new BarCodeVo();
            try {
                barCodeVo.setMo(mo);
                barCodeVo.setItemCode(getStringCode(map.get("M")));
                barCodeVo.setPropC1(map.get("Lot"));
                barCodeVo.setBoxNumber(map.get("S"));
                barCodeVo.setPropC3(map.get("P"));
                barCodeVo.setPropD1(map.get("D"));
                barCodeVo.setOrderQty(parseQuantityString(map.get("Q")));
                barCodeVos.add(barCodeVo);
            } catch (Exception e) {
                throw new BadRequestException("箱号格式错误!");
            }
        }
        if (barCodeVos.isEmpty()) {
            throw new BadRequestException("请扫描箱号!");
        }
        return barCodeVos;
    }

    /**
     * 解析条形码
     *
     * @param QRCode 条形码
     */
    private Map<String, String> parseString(String QRCode) {
        //P:工厂;V:客户编号;M:物料编号/PCS;B:BYD批次;Lot:供应商批次号;S:工单编号;PO:采购订单/箱号;Q:数量;D:生产日期;YX:有效天数;
        // 使用分号分隔字符串
        String[] pairs = QRCode.split(";");
        Map<String, String> resultMap = new HashMap<>();
        for (String pair : pairs) {
            // 使用冒号分隔键和值
            String[] keyValue = pair.split(":");
            if (keyValue.length == 2) {
                String key = keyValue[0].trim();
                String value = keyValue[1].trim();
                resultMap.put(key, value);
            }
        }
        return resultMap;
    }

    /**
     * 获取字符串中的前部分
     *
     * @param str 字符串
     */
    private String getStringCode(String str) {
        int index = str.indexOf('/');
        return str.substring(0, index);
    }

    /**
     * 获取数量
     *
     * @param str 字符串
     */
    public static int parseQuantityString(String str) {
        // 定义一个正则表达式来匹配第一个数字
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(str);

        // 查找第一个匹配的数字
        if (matcher.find()) {
            return Integer.parseInt(matcher.group());
        } else {
            throw new BadRequestException("数量格式错误!");
        }
    }

    /**
     * 验证箱条码集合是否为同一物料,箱号是否重复
     *
     * @param barCodeVos 箱条码集合
     */
    private void validateItemCodeAndOrderNumber(List<BarCodeVo> barCodeVos) {
        String firstItemCode = barCodeVos.iterator().next().getItemCode();
        boolean allSame = barCodeVos.stream().allMatch(vo -> vo.getItemCode().equals(firstItemCode));
        if (!allSame) {
            throw new BadRequestException("一个托盘只能放同一种物料!");
        }

        Set<String> barNumbers = barCodeVos.stream().map(BarCodeVo::getBoxNumber).collect(Collectors.toSet());
        List<AsnDetail> asnDetails = asnDetailService.existsByBoxNumber(barNumbers);
        if (!asnDetails.isEmpty()) {
            Set<String> newBarNumbers = asnDetails.stream().map(AsnDetail::getOrderNumber).collect(Collectors.toSet());
            throw new BadRequestException("箱号已入库,请勿重复入库:" + newBarNumbers);
        }

    }

    /**
     * 指定托盘类型
     *
     * @param stockType 容器类型
     */
    private void validateStockType(String stockType) {
        if (!"托盘".equals(stockType)) {
            throw new BadRequestException(stockType + "容器类型错误!");
        }
    }

    /**
     * 验证起点点位是否在可入库区域
     *
     * @param srcPoint 起点点位
     */
    private void validateSrcPointArea(Point srcPoint) {
        String srcPointAreaName = srcPoint.getArea().getName();
        if (!AreaNameDic.DXJRKQ.equals(srcPointAreaName)) {
            throw new BadRequestException(srcPointAreaName + "的" + srcPoint.getCode() + "点位不能入库");
        }
    }

    public void createAsnDetailsAndItemKeysAndTasks(List<BarCodeVo> barCodeVos, Item item, Stock stock, Point srcPoint, Point endPoint, AgvTask agvTask) {
        for (BarCodeVo barCodeVo : barCodeVos) {
            AsnDetail asnDetail = asnDetailService.createAsnDetail(item, stock, srcPoint, barCodeVo.getPropC1(), barCodeVo.getBoxNumber(), barCodeVo.getPropC3(), Timestamp.valueOf(DateUtil.formatDateTime(DateUtil.parse(barCodeVo.getPropD1()))), barCodeVo.getOrderQty(), barCodeVo.getMo());
            ItemKey itemKey = itemKeyService.createItemKey(item, barCodeVo.getPropC1(), asnDetail.getPropD1(), barCodeVo.getBoxNumber());
            Task task = taskService.createTask(item, asnDetail.getOrderQty(), itemKey, asnDetail, BizStatus.ASN, null, null, stock, srcPoint, endPoint, agvTask);
        }
    }

    private void updateStockAndPoints(Stock stock, Point srcPoint, Point endPoint) {
        //容器占用
        stockService.usedStock(stock, srcPoint, BaseStatus.USED);

        //起点占用
        pointService.usedPoint(srcPoint);

        //终点占用
        pointService.usedPoint(endPoint);

    }

    /**
     * 验证任务
     *
     * @param taskId      任务Id
     * @param orderNumber 箱号
     */
    private Task validateTask(Long taskId, String orderNumber) {
        Task task = taskService.findById(taskId);
        if (task == null) {
            throw new BadRequestException("未找到相应任务记录！");
        }
        if (orderNumber.equals(task.getItemKey().getOrderNumber())) {
            return task;
        } else {
            //重新分配
            return taskService.reassignTask(task, orderNumber);
        }
    }


    /**
     * 验证目标容器
     *
     * @param dstStockCode 容器
     */
    private Stock validatedstStock(Task task, Pick pick, String dstStockCode) {
        if (StringUtils.isEmpty(dstStockCode)) {
            throw new BadRequestException("请扫描目标托盘号!");
        }
        //验证容器
        Stock dstStock = stockService.findByCode(dstStockCode);

        if (dstStock.getPoint() == null) {
            throw new BadRequestException(dstStockCode + "目标托盘没有关联点位!");
        }

        //验证目标托盘是否在翻包存储区
        String itemType = task.getItemKey().getItem().getGoodType();
        String areaName = "大件".equals(itemType) ? AreaNameDic.DJFBCQ : AreaNameDic.XJFBCQ;
        if (!areaName.equals(dstStock.getPoint().getArea().getName())) {
            throw new BadRequestException(dstStockCode + "目标托盘没有在" + areaName);
        }

        if (pick.getStock() == null) {
            List<Inventory> inventoryList = inventoryService.queryInventory(dstStock.getId(), null, null, null, dstStock.getDept().getId());
            if (!inventoryList.isEmpty()) {
                throw new BadRequestException(dstStock + "目标托盘已绑定其他出库单,请更换其它的托盘!");
            }
            pick.setStock(dstStock);
            pickService.update(pick);
        } else if (!dstStockCode.equals(pick.getStock().getCode())) {
            throw new BadRequestException(pick.getGdCode() + "工单只能放入同一个货架! 请放入" + pick.getStock().getCode() + "货架!");
        }
        return dstStock;
    }

    /**
     * AGV中的任务都拣货完成托盘回库
     *
     * @param task 任务
     */
    private void returnFBAgvTask(Task task) {
        double planQty = 0;
        double moveQty = 0;
        List<Task> taskList = taskService.findTaskByAgvTask(task.getAgvTask().getId());
        for (Task task1 : taskList) {
            planQty += task1.getPlanQty();
            moveQty += task1.getMoveQty();
        }
        if (planQty == moveQty) {
            //成品当前托盘没有库存则不需要回库;
            List<Inventory> inventoryList = inventoryService.findByStockCode(task.getSrcStock().getCode());
            List<Inventory> smallAreaInventories = inventoryList.stream()
                    .filter(inv -> {
                        if (inv.getPoint() != null && inv.getPoint().getArea() != null) {
                            String areaName = inv.getPoint().getArea().getName();
                            return AreaNameDic.XJQ.equals(areaName) || AreaNameDic.XJFBQ.equals(areaName);
                        }
                        return false;
                    })
                    .collect(Collectors.toList());
            if (!smallAreaInventories.isEmpty()) {
                kmReService.returnFBAgvTask(task.getDstPoint(), task.getSrcStock(), task.getItem());
            }

        }
    }


}
