package com.ruoyi.ptxlib.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.asset.domain.TbAssetInformation;
import com.ruoyi.asset.domain.TbLocation;
import com.ruoyi.asset.service.ITbAssetInformationService;
import com.ruoyi.asset.service.ITbLocationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.MyUtils;
import com.ruoyi.label.domain.TbLabelModelInfo;
import com.ruoyi.label.service.ITbLabelModelInfoService;
import com.ruoyi.ptxlib.domain.PTXLabelCode;
import com.ruoyi.ptxlib.domain.PTXLabelInfo;
import com.ruoyi.ptxlib.domain.PTXLabelText;
import com.ruoyi.ptxlib.service.impl.PTXService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/print")
public class PTXController extends BaseController {
    @Resource
    private PTXService ptxService;

    @Resource
    private ITbAssetInformationService assetInformationService;

    @Resource
    private ITbLocationService locationService;

    @Resource
    private ISysDeptService sysDeptService;

    @PostMapping
    public AjaxResult print(@RequestBody PTXLabelInfo ptxLabelInfo) {
        ptxService.printLabel(ptxLabelInfo);
        return success();
    }

    @Resource
    private ITbLabelModelInfoService labelModelInfoService;

    @PostMapping("/test")
    public AjaxResult test(@RequestBody PTXLabelInfo ptxLabelInfo) {
//        TbAssetInformation tbAssetInformation = assetInformationService.selectTbAssetInformationById(8L);
//
//        ptxLabelInfo = ptxService.filterInfo(ptxLabelInfo, tbAssetInformation);
        TbLabelModelInfo tbLabelModelInfo = new TbLabelModelInfo();
        tbLabelModelInfo.setName("标签 80mm*25mm");
        tbLabelModelInfo.setLabelCode("BQ001");
        tbLabelModelInfo.setLabelValue(JSONUtil.toJsonStr(ptxLabelInfo));
        tbLabelModelInfo.setCreateBy("admin");
        labelModelInfoService.insertTbLabelModelInfo(tbLabelModelInfo);

        return AjaxResult.success();
    }

    /**
     * 打印标签内容过滤
     *
     * @param ptxLabelInfo 打印标签信息
     * @param filterData 过滤数据
     * @return 打印标签数据
     */
    public PTXLabelInfo filterInfo(PTXLabelInfo ptxLabelInfo, Object filterData) {
        String epc = ptxLabelInfo.getEpc();
        String[] symbolContent = MyUtils.getSymbolContent(epc);
        epc = MyUtils.replaceBrackets(filterData, epc, symbolContent);
        ptxLabelInfo.setEpc(epc);

        List<PTXLabelText> ptxLabelTexts = ptxLabelInfo.getPtxLabelTexts();
        if (!ptxLabelTexts.isEmpty()) {
            for (PTXLabelText ptxLabelText : ptxLabelTexts) {
                // 拥有占位符的打印字符串
                String printData = ptxLabelText.getPrintData();
                // 获取占位符中的字符串集
                String[] symbolContents = MyUtils.getSymbolContent(printData);

                printData = MyUtils.replaceBrackets(filterData, printData, symbolContents);
                ptxLabelText.setPrintData(printData);
            }
        }

        List<PTXLabelCode> ptxLabelCodes = ptxLabelInfo.getPtxLabelCodes();
        if (!ptxLabelCodes.isEmpty()) {
            for (PTXLabelCode ptxLabelCode : ptxLabelCodes) {
                String data = ptxLabelCode.getData();
                // 获取占位符中的字符串集
                String[] symbolContents = MyUtils.getSymbolContent(data);
                data = MyUtils.replaceBrackets(filterData, data, symbolContents);
                ptxLabelCode.setData(data);
            }
        }

        return ptxLabelInfo;
    }

    @GetMapping
    public AjaxResult print2() {
        PTXLabelInfo labelInfo = new PTXLabelInfo();
        // 连接打印机
        labelInfo.setConnectType(0);
        labelInfo.setPrinterName("USB");
        // 字体加粗
        labelInfo.setIsBold(true);

        TbAssetInformation tbAssetInformation = assetInformationService.selectTbAssetInformationById(4L);
        String assetNumber = tbAssetInformation.getNumber();
        String locationNumber = tbAssetInformation.getLocationNumber();
        TbLocation tbLocation = locationService.selectTbLocationByNumber(locationNumber);
        String locationName = tbLocation.getName();
        String assetName = tbAssetInformation.getName();
        String department = tbAssetInformation.getDepartment();
        long deptId = Long.parseLong(department);
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        String deptName = sysDept.getDeptName();
        String assetModel = tbAssetInformation.getSpecificationsModel();
        if (StrUtil.isBlank(assetModel)) {
            assetModel = "无";
        }

        // 写入epc
        labelInfo.setEpc(assetNumber);

        // 定义标题
        PTXLabelText title = new PTXLabelText(1, 80, 140, 60, 15, 15, "固定资产标识卡");
        // 定义资产编号
        PTXLabelText number = new PTXLabelText(1, 80, 10, 145, 10, 10, "资产编号：" + assetNumber);
        // 定义资产名称
        PTXLabelText name = new PTXLabelText(1, 80, 10, 205, 10, 10, "资产名称：" + assetName);
        // 定义存放地点
        PTXLabelText location = new PTXLabelText(1, 80, 450, 205, 10, 10, "存放地点：" + locationName);
        // 定义使用部门
        PTXLabelText dept = new PTXLabelText(1, 80, 10, 265, 10, 10, "使用部门：" + deptName);
        // 定义规格型号
        PTXLabelText model = new PTXLabelText(1, 80, 450, 265, 10, 10, "规格型号：" + assetModel);
        // 定义存放地点二维码
        PTXLabelCode code = new PTXLabelCode(2, 80, 140, 355, 10, locationNumber);

        List<PTXLabelText> ptxLabelTexts = new ArrayList<>();
        ptxLabelTexts.add(title);
        ptxLabelTexts.add(number);
        ptxLabelTexts.add(name);
        ptxLabelTexts.add(location);
        ptxLabelTexts.add(dept);
        ptxLabelTexts.add(model);

        List<PTXLabelCode> ptxLabelCodes = new ArrayList<>();
        ptxLabelCodes.add(code);

        labelInfo.setPtxLabelTexts(ptxLabelTexts);
        labelInfo.setPtxLabelCodes(ptxLabelCodes);

        ptxService.printLabel(labelInfo);

        return success();
    }
}
