package com.wms_main.service.controller.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wms_main.constant.enums.wms.OrderStatusEnum;
import com.wms_main.dao.ITAppLocationService;
import com.wms_main.dao.ITAppOrderInService;
import com.wms_main.dao.ITAppOrderOutService;
import com.wms_main.dao.ITAppWcsTaskService;
import com.wms_main.model.dto.query.OrderInQuery;
import com.wms_main.model.dto.request.wms.OrderInRequest;
import com.wms_main.model.dto.request.wms.OrderOutRequest;
import com.wms_main.model.dto.response.wms.BaseWmsApiResponse;
import com.wms_main.model.dto.response.ycwms.YcwmsResponse;
import com.wms_main.model.po.TAppLocation;
import com.wms_main.model.po.TAppOrderIn;
import com.wms_main.model.po.TAppOrderOut;
import com.wms_main.model.po.TAppWcsTask;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.repository.utils.UUIDUtils;
import com.wms_main.service.controller.IOrderControllerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class OrderControllerServiceImpl implements IOrderControllerService {
    private final ITAppOrderInService appOrderInService;
    private final ITAppOrderOutService appOrderOutService;
    private final ITAppLocationService appLocationService;
    private final ITAppWcsTaskService appWcsTaskService;

    @Override
    public BaseWmsApiResponse addOrderIn(OrderInRequest request) {
        log.info("入库单请求参数{}", JSON.toJSON(request));
        if (request == null) return BaseWmsApiResponse.error("参数缺少");
        if (StringUtils.isEmpty(request.getVehicleNo()) || StringUtils.isEmpty(request.getVehicleSize())
                || StringUtils.isEmpty(request.getInStand()) || request.getGoodsDetail() == null || request.getGoodsDetail().isEmpty()) {
            return BaseWmsApiResponse.error("参数错误");
        }
        List<TAppOrderIn> orderInList = new ArrayList<>();
        for (var goodsDetail : request.getGoodsDetail()) {
            if (goodsDetail == null || goodsDetail.getQuantity() == null) {
                return BaseWmsApiResponse.error("参数错误");
            }
            TAppOrderIn orderIn = new TAppOrderIn();
            orderIn.setRecordId(UUIDUtils.getNewUUID());
            orderIn.setOrderId(UUIDUtils.getNewUUID());
            orderIn.setVehicleNo(request.getVehicleNo());
            orderIn.setVehicleSize(request.getVehicleSize());
            orderIn.setInStand(request.getInStand());
            orderIn.setGoodsId(goodsDetail.getGoodsId());
            orderIn.setGoodsName(goodsDetail.getGoodsName());
            orderIn.setBatch(goodsDetail.getBatch());
            orderIn.setGoodsType(goodsDetail.getGoodsType());
            orderIn.setSpecification(goodsDetail.getSpecification());
            orderIn.setQuantity(goodsDetail.getQuantity());
            orderIn.setGoodsDesc(goodsDetail.getGoodsDesc());
            orderIn.setOrderStatus(OrderStatusEnum.CREATE.getCode());
            orderIn.setCreateTime(LocalDateTime.now());
            orderIn.setUpdateTime(LocalDateTime.now());
            orderInList.add(orderIn);
        }
        if (!appOrderInService.saveBatch(orderInList)) {
            return BaseWmsApiResponse.error("入库单保存失败，请稍后再试");
        }
        return BaseWmsApiResponse.success();
    }

    @Override
    public BaseWmsApiResponse addOrderOut(OrderOutRequest request) {
        log.info("出库单请求参数{}", JSON.toJSON(request));
        if (request == null) return BaseWmsApiResponse.error("参数错误");
        if (request.getQuantity() == null
                || (StringUtils.isEmpty(request.getGoodsId()) && request.getBatch() == null && request.getSpecification() == null)) {
            return BaseWmsApiResponse.error("参数错误");
        }
        TAppOrderOut orderOut = new TAppOrderOut();
        orderOut.setRecordId(UUIDUtils.getNewUUID());
        orderOut.setOrderId(UUIDUtils.getNewUUID());
        orderOut.setGoodsId(request.getGoodsId());
        orderOut.setBatch(request.getBatch());
        orderOut.setSpecification(request.getSpecification());
        orderOut.setQuantity(request.getQuantity());
        orderOut.setOrderStatus(OrderStatusEnum.CREATE.getCode());
        orderOut.setCreateTime(LocalDateTime.now());
        orderOut.setUpdateTime(LocalDateTime.now());
        if (!appOrderOutService.save(orderOut)) {
            return BaseWmsApiResponse.error("出库单保存失败，请稍后再试");
        }
        return BaseWmsApiResponse.success();
    }

    @Override
    public List<TAppOrderIn> getOrderIns(OrderInQuery query) {
        if (query == null) return null;
        else if (StringUtils.isEmpty(query.getVehicleNo()) && StringUtils.isEmpty(query.getVehicleSize()) && StringUtils.isEmpty(query.getInStand()) && query.getOrderStatus() == null) {
            return appOrderInService.list();
        } else {
            return appOrderInService.list(new LambdaQueryWrapper<TAppOrderIn>().eq(TAppOrderIn::getVehicleNo, query.getVehicleNo())
                    .eq(TAppOrderIn::getVehicleSize, query.getVehicleSize())
                    .eq(TAppOrderIn::getInStand, query.getInStand())
                    .eq(TAppOrderIn::getOrderStatus, query.getOrderStatus()));
        }
    }

    @Override
    public BaseWmsApiResponse updateOrderIn(String recordId, String orderStatus) {
        if (StringUtils.isEmpty(recordId) || StringUtils.isEmpty(orderStatus)) {
            return BaseWmsApiResponse.error("参数缺失");
        } else {
            appOrderInService.update(
                    new LambdaUpdateWrapper<TAppOrderIn>()
                            .set(TAppOrderIn::getOrderStatus, orderStatus)
                            .eq(TAppOrderIn::getRecordId, recordId));
            return BaseWmsApiResponse.success();
        }
    }

    @Override
    public BaseWmsApiResponse deleteOrderIn(String recordId) {
        if (StringUtils.isEmpty(recordId)) {
            return BaseWmsApiResponse.error("参数缺失");
        } else {
            TAppOrderIn orderIn = appOrderInService.getById(recordId);
            if (orderIn == null) {
                return BaseWmsApiResponse.error("未查询到入库任务");
            }
            if (StringUtils.isNotEmpty(orderIn.getDestination())) {
                appLocationService.update(
                        new LambdaUpdateWrapper<TAppLocation>()
                                .eq(TAppLocation::getLocationId, orderIn.getDestination())
                                .set(TAppLocation::getIsOccupy, 0)
                );
                appWcsTaskService.remove(
                        new LambdaQueryWrapper<TAppWcsTask>()
                                .eq(TAppWcsTask::getDestination, orderIn.getDestination())
                );
            }
            appOrderInService.removeById(orderIn);
            return BaseWmsApiResponse.success();
        }
    }

}
