package com.jwipc.order.api.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.constant.FlagConst;
import com.jwipc.order.api.dto.BarcodePrinterRestDTO;
import com.jwipc.order.api.dto.SupplierPrintParamDTO;
import com.jwipc.order.api.logic.BarcodePrinterLogic;
import com.jwipc.order.constant.LabelBoxType;
import com.jwipc.order.constant.LabelPrintSettingType;
import com.jwipc.order.constant.PrinterType;
import com.jwipc.order.dto.LabelBarBaseDTO;
import com.jwipc.order.dto.LabelPrintOptionDTO;
import com.jwipc.order.dto.LabelPrintSettingBaseDTO;
import com.jwipc.order.entity.PurchaseOrderDetail;
import com.jwipc.order.logic.LabelPrintSettingLogic;
import com.jwipc.order.service.IPurchaseOrderDetailService;
import com.jwipc.supplier.clent.dto.SupplierAvlBaseDTO;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.ProductStockClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BarcodePrinterLogicImpl implements BarcodePrinterLogic {
    private final SupplierClient supplierClient;
    private final IPurchaseOrderDetailService purchaseOrderDetailService;
    private final LabelPrintSettingLogic labelPrintSettingLogic;
    private final ProductStockClient productStockClient;

    @Override
    public BarcodePrinterRestDTO print(SupplierPrintParamDTO supplierPrintParamDTO) {
        log.info("接口打印参数:{}", supplierPrintParamDTO);
        BarcodePrinterRestDTO barcodePrinterRestDTO = new BarcodePrinterRestDTO();
        barcodePrinterRestDTO.setResult(true);
        final String supplierCode = supplierPrintParamDTO.getVenDor();
        final String materialCode = supplierPrintParamDTO.getPN();
        final String mpn = supplierPrintParamDTO.getMPN();
        final String productName = supplierPrintParamDTO.getMAN();
        final SupplierAvlBaseDTO avlForPrint = supplierClient.getAvlForPrint(supplierCode, materialCode, productName,
                mpn);
        final String msl = avlForPrint.getMsl();
        if (StrUtil.isEmpty(msl)) {
            barcodePrinterRestDTO.setResult(false);
            barcodePrinterRestDTO.setMessage("avl中的msl不存在!");
            return barcodePrinterRestDTO;
        }
        final String po = supplierPrintParamDTO.getPO();
        final PurchaseOrderDetail orderDetail = purchaseOrderDetailService.getOne(
                new LambdaQueryWrapper<PurchaseOrderDetail>()
                        .eq(PurchaseOrderDetail::getOrderCode, po)
                        .eq(PurchaseOrderDetail::getMaterialCode, materialCode), false);
        if (Objects.isNull(orderDetail)) {
            barcodePrinterRestDTO.setResult(false);
            barcodePrinterRestDTO.setMessage("订单信息不存在!");
            return barcodePrinterRestDTO;
        }
        final String unitCode = orderDetail.getUnitCode();
        final String defaultStock = productStockClient.getStockLocation(materialCode);//从单独表获取库存地点
        final SupplierDTO supplier = supplierClient.getSupplierByCode(supplierCode);
        if (Objects.isNull(supplier)) {
            barcodePrinterRestDTO.setResult(false);
            barcodePrinterRestDTO.setMessage("供应商不存在!");
            return barcodePrinterRestDTO;
        }
        final Long supplierId = supplier.getId();
        LabelPrintOptionDTO labelPrintOptionDTO = new LabelPrintOptionDTO();
        labelPrintOptionDTO.setSupplierCode(supplierCode);
        labelPrintOptionDTO.setSupplierName(supplier.getSupplierName());
        labelPrintOptionDTO.setMinQuantity(supplierPrintParamDTO.getMinQty());//最小包装数数量
        labelPrintOptionDTO.setSheets(supplierPrintParamDTO.getPRINTQTY());//打印数量
        labelPrintOptionDTO.setMaterialCode(materialCode);
        labelPrintOptionDTO.setDc(supplierPrintParamDTO.getDC());
        labelPrintOptionDTO.setLot(supplierPrintParamDTO.getLOT());
        labelPrintOptionDTO.setMsl(msl);
        labelPrintOptionDTO.setUnit(unitCode);
        labelPrintOptionDTO.setMpn(mpn);
        labelPrintOptionDTO.setInnerFlag(FlagConst.N.getCode());
        labelPrintOptionDTO.setManufacturer(productName);
        labelPrintOptionDTO.setDefaultStock(defaultStock);
        labelPrintOptionDTO.setPurchaseOrderCode(po);
        labelPrintOptionDTO.setNum(supplierPrintParamDTO.getQTY());//数量
        labelPrintOptionDTO.setFullQuantity(supplierPrintParamDTO.getQTY());//数量
        labelPrintOptionDTO.setPrinterType(PrinterType.DEFAULT.getType());//打印机内容
        labelPrintOptionDTO.setOrigin(StringUtils.isEmpty(supplierPrintParamDTO.getORIGIN()) ? "" : supplierPrintParamDTO.getORIGIN());
        final String cartontype = supplierPrintParamDTO.getCARTONTYPE();
        final LabelBoxType byCode = LabelBoxType.getByCode(cartontype);
        labelPrintOptionDTO.setLabelBoxType(byCode);
        labelPrintOptionDTO.setType(LabelPrintSettingType.API.getType());
        labelPrintOptionDTO.setVersion(String.valueOf(0));
        LabelPrintSettingBaseDTO labelPrintSettingBaseDTO = new LabelPrintSettingBaseDTO();
        labelPrintSettingBaseDTO.setSupplierId(supplierId);
        labelPrintSettingBaseDTO.setMaterialCode(materialCode);
        labelPrintSettingBaseDTO.setPrinterType(PrinterType.DEFAULT.getType());
        labelPrintSettingBaseDTO.setType(LabelPrintSettingType.API.getType());
        labelPrintSettingBaseDTO.setPurchaseOrderCode(po);
        labelPrintSettingBaseDTO.setSupplierCode(supplierCode);
        labelPrintSettingBaseDTO.setSupplierName(supplier.getSupplierName());
        labelPrintSettingBaseDTO.setCreateId(1L);
        labelPrintSettingBaseDTO.setCreateTime(LocalDateTime.now());

        labelPrintOptionDTO.setFactoryCode(orderDetail.getFactoryCode());
        labelPrintSettingBaseDTO.setOptionJson(JSON.toJSONString(labelPrintOptionDTO));
        final List<LabelBarBaseDTO> print = labelPrintSettingLogic.print(labelPrintSettingBaseDTO, 1L);
        List<BarcodePrinterRestDTO.DataListItem> list = new ArrayList<>();
        for (LabelBarBaseDTO labelBarBaseDTO : print) {
            BarcodePrinterRestDTO.DataListItem dataListItem = new BarcodePrinterRestDTO.DataListItem();
            dataListItem.setDC(labelBarBaseDTO.getDc());
            dataListItem.setVenDor(labelBarBaseDTO.getSupplierCode());
            dataListItem.setVER(labelBarBaseDTO.getVersion());
            dataListItem.setLOT(labelBarBaseDTO.getLot());
            dataListItem.setMAN(labelBarBaseDTO.getManufacturer());
            dataListItem.setMPN(labelBarBaseDTO.getMpn());
            dataListItem.setMSL(labelBarBaseDTO.getMsl());
            dataListItem.setPN(labelBarBaseDTO.getMaterialCode());
            dataListItem.setPO(labelBarBaseDTO.getOrderNumber());
            dataListItem.setLISTNO(labelBarBaseDTO.getBarcodeContent());
            dataListItem.setLOC(defaultStock);
            dataListItem.setQTY(labelBarBaseDTO.getQuantity().stripTrailingZeros().toPlainString());
            dataListItem.setUNIT(unitCode);
            //
            final LabelBoxType byCode1 = LabelBoxType.getByCode(labelBarBaseDTO.getBoxType());
            if (byCode1.equals(LabelBoxType.OUT)) {
                dataListItem.setLableType("外");
            } else {
                dataListItem.setLableType(StrUtil.EMPTY);
            }
            list.add(dataListItem);
        }
        barcodePrinterRestDTO.setDataList(list);
        barcodePrinterRestDTO.setRecordsTotal(list.size());
        return barcodePrinterRestDTO;
    }
}