package com.jwipc.order.controller.pc;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.exception.DaoException;
import com.jwipc.order.constant.LabelBoxType;
import com.jwipc.order.constant.PrintTemplateCode;
import com.jwipc.order.dto.LabelBarBaseDTO;
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.security.LoginUtil;
import com.jwipc.sys.client.PrintTemplateClient;
import com.jwipc.sys.client.dto.PrintTemplateCDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

import static com.jwipc.order.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> 标签打印相关页面</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequiredArgsConstructor
@RequestMapping(MODULE_ROOT + "/barcodePrinter/page")
@Slf4j
public class BarcodePrinterPageController {
    private final LabelPrintSettingLogic labelPrintSettingLogic;
    private final PrintTemplateClient printTemplateClient;
    private final IPurchaseOrderDetailService purchaseOrderDetailService;

    @GetMapping("index")
    public String index(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/barcodePrinter/index";
    }

    @GetMapping("dpi200")
    public String dpi200(Model model) {
        return MODULE_ROOT + "/barcodePrinter/print_200";
    }

    @GetMapping("dpi300")
    public String dpi300(Model model) {
        return MODULE_ROOT + "/barcodePrinter/print_300";
    }

    @GetMapping("detail")
    public String detail(@RequestParam(required = false, defaultValue = "0") Integer type, Model model) {
        model.addAttribute("type", type);
        return MODULE_ROOT + "/barcodePrinter/detail";
    }

    @GetMapping("lookup")
    public String lookup(Long id, @RequestParam(required = false, defaultValue = "0") Integer isPrint, Model model) {
        List<LabelBarBaseDTO> baseDTOS = labelPrintSettingLogic.getLabelBars(id, isPrint);
        final PrintTemplateCDTO printTemplate = printTemplateClient.getPrintTemplate(
                PrintTemplateCode.LABEL.getCode());
        for (LabelBarBaseDTO baseDTO : baseDTOS) {
            final String boxType = baseDTO.getBoxType();
            final LabelBoxType byCode = LabelBoxType.getByCode(boxType);
            baseDTO.setBoxType(byCode.getTitle());
        }
        model.addAttribute("tmpJson", printTemplate.getTemplateJson());
        model.addAttribute("dataJsonStr", JSON.toJSONString(baseDTOS));
        return MODULE_ROOT + "/barcodePrinter/lookup";
    }

    @GetMapping("lookup1")
    public String lookup1(String ids, @RequestParam(required = false, defaultValue = "0") Integer isPrint,
                          Model model)
    {
        List<LabelBarBaseDTO> baseDTOS = labelPrintSettingLogic.getLabelBars(ids, isPrint);
        final PrintTemplateCDTO printTemplate = printTemplateClient.getPrintTemplate(
                PrintTemplateCode.LABEL.getCode());
        for (LabelBarBaseDTO baseDTO : baseDTOS) {
            final String boxType = baseDTO.getBoxType();
            final LabelBoxType byCode = LabelBoxType.getByCode(boxType);
            baseDTO.setBoxType(byCode.getTitle());
        }
        model.addAttribute("tmpJson", printTemplate.getTemplateJson());
        model.addAttribute("dataJsonStr", JSON.toJSONString(baseDTOS));
        return MODULE_ROOT + "/barcodePrinter/lookup";
    }

    @PostMapping("print")
    public String print(@RequestBody LabelPrintSettingBaseDTO printSettingBaseDTO, Model model) {
        log.info("界面打印参数:{}", printSettingBaseDTO);
        final String supplierCode = printSettingBaseDTO.getSupplierCode();
        final String materialCode = printSettingBaseDTO.getMaterialCode();
        final String po = printSettingBaseDTO.getPurchaseOrderCode();
        final PurchaseOrderDetail orderDetail = purchaseOrderDetailService.getOne(
                new LambdaQueryWrapper<PurchaseOrderDetail>()
                        .eq(PurchaseOrderDetail::getOrderCode, po)
                        .eq(PurchaseOrderDetail::getSupplierErpCode,supplierCode)
                        .eq(PurchaseOrderDetail::getMaterialCode, materialCode), false);
        if (Objects.isNull(orderDetail)) {
            throw new DaoException("订单信息不存在");
        }
        List<LabelBarBaseDTO> baseDTOS = labelPrintSettingLogic.print(printSettingBaseDTO, LoginUtil.getUserId());
        final PrintTemplateCDTO printTemplate = printTemplateClient.getPrintTemplate(
                PrintTemplateCode.LABEL.getCode());
        for (LabelBarBaseDTO baseDTO : baseDTOS) {
            final String boxType = baseDTO.getBoxType();
            final LabelBoxType byCode = LabelBoxType.getByCode(boxType);
            baseDTO.setBoxType(byCode.getTitle());
        }
        model.addAttribute("tmpJson",printTemplate.getTemplateJson());
        model.addAttribute("dataJsonStr", JSON.toJSONString(baseDTOS));
        return MODULE_ROOT + "/barcodePrinter/print";
    }

    @GetMapping("labelIndex")
    public String labelIndex(Model model,String ids) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("ids", ids);
        return MODULE_ROOT + "/barcodePrinter/lable_index";
    }

    @GetMapping("pdfPrint")
    public String pdfPrint(String ids,Model model){
        List<LabelBarBaseDTO> baseDTOS = labelPrintSettingLogic.getLabelBarsBySettingIds(ids);
        if(baseDTOS.size()>300){
            model.addAttribute("count", baseDTOS.size());
            return MODULE_ROOT + "/barcodePrinter/error";
        }
        final PrintTemplateCDTO printTemplate = printTemplateClient.getPrintTemplate(
                PrintTemplateCode.LABEL.getCode());
        for (LabelBarBaseDTO baseDTO : baseDTOS) {
            final String boxType = baseDTO.getBoxType();
            final LabelBoxType byCode = LabelBoxType.getByCode(boxType);
            baseDTO.setBoxType(byCode.getTitle());
        }
        model.addAttribute("tmpJson", printTemplate.getTemplateJson());
        model.addAttribute("dataJsonStr", JSON.toJSONString(baseDTOS));
        return MODULE_ROOT + "/barcodePrinter/pdf_print";
    }

    @GetMapping("pdfPrints")
    public String pdfPrints(String ids,Model model){
        List<LabelBarBaseDTO> baseDTOS = labelPrintSettingLogic.getLabelBars(ids, 0);
        if(baseDTOS.size()>300){
            model.addAttribute("count", baseDTOS.size());
            return MODULE_ROOT + "/barcodePrinter/error";
        }
        final PrintTemplateCDTO printTemplate = printTemplateClient.getPrintTemplate(
                PrintTemplateCode.LABEL.getCode());
        for (LabelBarBaseDTO baseDTO : baseDTOS) {
            final String boxType = baseDTO.getBoxType();
            final LabelBoxType byCode = LabelBoxType.getByCode(boxType);
            baseDTO.setBoxType(byCode.getTitle());
        }
        model.addAttribute("tmpJson", printTemplate.getTemplateJson());
        model.addAttribute("dataJsonStr", JSON.toJSONString(baseDTOS));
        return MODULE_ROOT + "/barcodePrinter/pdf_print";
    }

}
