package com.hys.app.controller.erp.use;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dto.WarehouseOutItemQueryParams;
import com.hys.app.model.erp.vo.WarehouseOutItemV1;
import com.hys.app.service.erp.WarehouseOutItemManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出库单明细相关API
 *
 * @author 张崧
 * @since 2023-12-07 17:06:32
 */
@RestController
@RequestMapping("/admin/erp/warehouseOutItem")
@Api(tags = "出库单商品明细相关API")
@Validated
public class WarehouseOutItemController {

    @Autowired
    private WarehouseOutItemManager warehouseOutItemManager;

    @ApiOperation(value = "分页列表")
    @PostMapping("/query")
    public WebPage<WarehouseOutItemV1> list( WarehouseOutItemQueryParams request) {
        Long count=warehouseOutItemManager.selectCount(request);
        List<WarehouseOutItemV1> list=warehouseOutItemManager.selectList(request);
        WebPage<WarehouseOutItemV1> webPage=new WebPage<>();
        webPage.setData(list);
        webPage.setPageNo(request.getPageNo());
        webPage.setPageSize(request.getPageSize());
        webPage.setDataTotal(count);
        return webPage;
    }


    public static void generateCombinedQRCode(String weChatLink, String alipayLink, int width, int height, String filePath) {
        try {
            String combinedLinks = "WeChat Payment Link: " + weChatLink + "\nAlipay Payment Link: " + alipayLink;
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(combinedLinks, BarcodeFormat.QR_CODE, width, height, hints);

            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                }
            }

            File qrFile = new File(filePath);
            ImageIO.write(image, "png", qrFile);

        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String weChatLink = "https://github.com/search?q=%E8%81%9A%E5%90%88%E6%94%B6%E6%AC%BE%E7%A0%81&type=repositories";
        String alipayLink = "https://v3studio.raycos.net/#/sales/order-add";
        int width = 400;
        int height = 400;
        String filePath = "D:";
        generateCombinedQRCode(weChatLink, alipayLink, width, height, filePath);
    }





}

