package com.zt.plat.module.contractorder.api;

import cn.hutool.extra.spring.SpringUtil;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.contractorder.api.dto.order.OrdDtlDTO;
import com.zt.plat.module.contractorder.api.dto.order.OrderDTO;
import com.zt.plat.module.contractorder.api.dto.order.PurchaseOrderWithDetailsDTO;
import com.zt.plat.module.contractorder.api.dto.order.SalesOrdDtlDTO;
import com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PrchOrdDtlDO;
import com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PurchaseOrderDO;
import com.zt.plat.module.contractorder.dal.dataobject.salesorder.SalesOrderDO;
import com.zt.plat.module.contractorder.dal.dataobject.salesorder.SalesOrderDetailDO;
import com.zt.plat.module.contractorder.dal.mysql.purchaseorder.PrchOrdDtlMapper;
import com.zt.plat.module.contractorder.dal.mysql.purchaseorder.PurchaseOrderMapper;
import com.zt.plat.module.contractorder.dal.mysql.salesorder.SalesOrderDetailMapper;
import com.zt.plat.module.contractorder.dal.mysql.salesorder.SalesOrderMapper;
import com.zt.plat.module.contractorder.service.purchaseorder.PurchaseOrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

@RestController
@Validated
@Slf4j
public class OrderApiImpl implements OrderApi {
    @Resource
    private PurchaseOrderService purchaseOrderService;

    @Override
    public CommonResult<List<OrderDTO>> getOrderByOrderIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new RuntimeException("订单id为空");
        }
        List<PurchaseOrderDO> ordersByIds = purchaseOrderService.getOrdersByIds(ids.stream().map(String::valueOf).toList());
        List<Long> purchaseOrderIds = new ArrayList<>();
        List<Long> salesOrderIds = new ArrayList<>();
        List<OrderDTO> order = new ArrayList<>();
        if (ordersByIds.isEmpty()) {
            return CommonResult.success(new ArrayList<>());
        }
        ordersByIds.forEach(o -> {
            if ("SALE".equals(o.getSplyBsnTp())) {
                // 销售订单
                salesOrderIds.add(o.getId());
            } else {
                // 非销售订单（采购订单）
                purchaseOrderIds.add(o.getId());
            }
        });
        if (!purchaseOrderIds.isEmpty()) {
            order.addAll(getPoOrdByIds(purchaseOrderIds));
        }
        if (!salesOrderIds.isEmpty()) {
            order.addAll(getSalesOrdByIds(salesOrderIds));
        }
        return  success(order);
    }

    @Override
    public CommonResult<List<OrderDTO>> getOrderByOrderNos(List<String> orderNoS) {
        List<PurchaseOrderDO> orderByNos = purchaseOrderService.getOrderByNos(orderNoS);
        if (orderByNos.isEmpty()) {
            return success(new ArrayList<>());
        }
        List<OrderDTO> order = new ArrayList<>();
        List<Long> purchaseOrderNoS = new ArrayList<>();
        List<Long> salesOrdNoS = new ArrayList<>();
        orderByNos.forEach(f -> {
            if ("SALE".equals(f.getSplyBsnTp())) {
                // 销售订单
                salesOrdNoS.add(f.getId());
            } else {
                // 非销售订单（采购订单）
                purchaseOrderNoS.add(f.getId());
            }
        });
        if (!purchaseOrderNoS.isEmpty()) {
            order.addAll(getPoOrdByIds(purchaseOrderNoS));
        }
        if (!salesOrdNoS.isEmpty()) {
            order.addAll(getSalesOrdByIds(salesOrdNoS));
        }

        return success(order);
    }

    private List<SalesOrderDO> getOrderByIds(List<Long> ids) {
        return SpringUtil.getBean(SalesOrderMapper.class).selectByIds(ids); // 采购订单与销售订单的
    }

    private List<OrderDTO> getPoOrdByIds(List<Long> ids) {
        List<SalesOrderDO> purchaseOrderDOS = getOrderByIds(ids);
        List<OrderDTO> orderDTOS = BeanUtils.toBean(purchaseOrderDOS, OrderDTO.class);
        if (orderDTOS == null) {
            return new ArrayList<>();
        }
        PrchOrdDtlMapper prchOrdDtlMapper = SpringUtil.getBean(PrchOrdDtlMapper.class);
        List<Long> orderIds = orderDTOS.stream().map(OrderDTO::getId).toList();
        List<PrchOrdDtlDO> prchOrdDtlDOS = prchOrdDtlMapper.selectList(PrchOrdDtlDO::getOrdId, orderIds);
        orderDTOS.forEach(o -> {
            List<OrdDtlDTO> ordDtlDTOS = new ArrayList<>();
            prchOrdDtlDOS.forEach(p -> {

                if (Objects.equals(o.getId(), p.getOrdId())) {
                    OrdDtlDTO ordDtlDTO = setOrderDtlDTO(o, p);
                    ordDtlDTOS.add(ordDtlDTO);
                }
            });
            o.setOrdDtlDTOS(ordDtlDTOS);
        });
        return orderDTOS;
    }

    private List<OrderDTO> getSalesOrdByIds(List<Long> ids) {
        List<SalesOrderDO> salesOrderDOS = getOrderByIds(ids);
        List<OrderDTO> orderDTOS = BeanUtils.toBean(salesOrderDOS, OrderDTO.class);
        if (orderDTOS == null) {
            return new ArrayList<>();
        }
        SalesOrderDetailMapper salesOrderDetailMapper = SpringUtil.getBean(SalesOrderDetailMapper.class);
        List<Long> orderIds = orderDTOS.stream().map(OrderDTO::getId).toList();
        List<SalesOrderDetailDO> salesOrderDetailDOS = salesOrderDetailMapper.selectList(SalesOrderDetailDO::getOrderId, orderIds);
        orderDTOS.forEach(o -> {
            List<OrdDtlDTO> salesOrdDtlDTOS = new ArrayList<>();
            salesOrderDetailDOS.forEach(s -> {
                if (Objects.equals(o.getId(), s.getOrderId())) {
                    OrdDtlDTO ordDtlDTO = setOrderDtlDTO(o, s);
                    salesOrdDtlDTOS.add(ordDtlDTO);
                }
            });
            o.setOrdDtlDTOS(salesOrdDtlDTOS);
        });
        return orderDTOS;
    }

    private <T> OrdDtlDTO setOrderDtlDTO(OrderDTO orderDTO, T t) {
        OrdDtlDTO ordDtlDTO = new OrdDtlDTO();
        if (t instanceof PrchOrdDtlDO p) {
            ordDtlDTO.setId(p.getId());
            ordDtlDTO.setOrderId(p.getOrdId());
            ordDtlDTO.setLineNum(p.getLineNum());
            ordDtlDTO.setMtrlName(p.getMtrlName());
            ordDtlDTO.setMtrlNum(p.getMtrlNum());
            ordDtlDTO.setFactoryName(p.getRcvFactNum());
            ordDtlDTO.setFactoryNum(p.getRcvFactNum());
            ordDtlDTO.setWarehouseName(p.getRcvWrhNum());
            ordDtlDTO.setWarehouseNum(p.getRcvWrhNum());
            ordDtlDTO.setUnit(p.getUnt());
            ordDtlDTO.setQuantity(p.getQty());
            ordDtlDTO.setProjectCategory(p.getPrjCtgr());
            ordDtlDTO.setAgreementNumber(p.getAgrNum());
            ordDtlDTO.setElementAbbreviation(p.getElemAbbr());
            ordDtlDTO.setElementName(p.getElemName());
            ordDtlDTO.setElementCode(p.getElemCdg());
            ordDtlDTO.setIsEnable(p.getIsEnb());
            ordDtlDTO.setTaxNum(p.getTaxNum());
            ordDtlDTO.setTaxRte(p.getTaxRte());
            //==============================
            ordDtlDTO.setMtrlCpntNum(p.getMtrlCpntNum());
            ordDtlDTO.setMtrlCpntDsp(p.getMtrlCpntDsp());
            ordDtlDTO.setBgnDt(p.getBgnDt());
            ordDtlDTO.setDdlDt(p.getDdlDt());
            ordDtlDTO.setTrfFactName(p.getTrfFactName());
            ordDtlDTO.setTrfFactNum(p.getTrfFactNum());
            ordDtlDTO.setTrfWrhName(p.getTrfWrhName());
            ordDtlDTO.setTrfWrhNum(p.getTrfWrhNum());
            ordDtlDTO.setRmk(p.getRmk());


        } else if (
                t instanceof SalesOrdDtlDTO s
        ) {
            ordDtlDTO.setId(s.getId());
            ordDtlDTO.setOrderId(s.getOrderId());
            ordDtlDTO.setLineNum(s.getLineNumber());
            ordDtlDTO.setMtrlName(s.getMaterialName());
            ordDtlDTO.setMtrlNum(s.getMaterialNumber());
            ordDtlDTO.setFactoryName(s.getFactoryName());
            ordDtlDTO.setFactoryNum(s.getFactoryNumber());
            ordDtlDTO.setWarehouseName(s.getWarehouseName());
            ordDtlDTO.setWarehouseNum(s.getWarehouseNumber());
            ordDtlDTO.setUnit(s.getUnit());
            ordDtlDTO.setQuantity(s.getQuantity());
            ordDtlDTO.setProjectCategory(s.getProjectCategory());
            ordDtlDTO.setAgreementNumber(s.getAgreementNumber());
            ordDtlDTO.setElementAbbreviation(s.getElementAbbreviation());
            ordDtlDTO.setElementName(s.getElementName());
            ordDtlDTO.setElementCode(s.getElementNumber());
            ordDtlDTO.setIsEnable(s.getIsEnable());
            ordDtlDTO.setTaxNum(s.getTaxAcctasscat());
            //==============================

        }
        return ordDtlDTO;
    }
}
