package com.zcloud.fire.check.domain.model;

import cn.hutool.json.JSONObject;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.annotation.TableField;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.jjb.saas.framework.domain.model.BaseE;
import com.zcloud.gbscommon.excelEntity.FirePointExcelExportEntity;
import com.zcloud.gbscommon.excelEntity.ListManagerStatisticExcelExportEntity;
import com.zcloud.gbscommon.utils.ExcelUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang.StringUtils;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * web-domain
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:12:54
 */
@Data
public class FirePointE extends BaseE {
    //主键
    private Long id;
    //业务主键id
    private String firePointId;
    //企业id
    private Long corpinfoId;
    //消防区域id
    private String fireRegionId;
    //消防点位名称
    private String firePointName;
    //消防点位编码
    private String firePointCode;
    //部门id
    private Long departmentId;
    //负责人
    private Long userId;
    //经度
    private String longitude;
    //纬度
    private String latitude;
    private Integer nfcFlag;
    //乐观锁
    private Integer version;
    //创建人
    private Long createId;
    //创建人姓名
    private String createName;
    //创建时间
    private LocalDateTime createTime;
    //更新人
    private Long updateId;
    //修改人名称
    private String updateName;
    //更新时间
    private LocalDateTime updateTime;
    //描述
    private String remarks;
    //是否删除
    private String deleteEnum;
    //租户ID
    private Long tenantId;
    //机构ID
    private Long orgId;
    //环境
    private String env;
    //消防区域名称
    private String fireRegionName;
    //消防区域编码
    private String fireRegionCode;

    public static void checkFirePoint(String firePointName, long count) {
        if (count > 0) {
            throw new BizException(firePointName+"点位下已存在设备");
        }
    }

    public void exportFirePointTable(HttpServletResponse httpServletResponse, List<FirePointExcelExportEntity> listFirePointExcelExportEntityList,String pointUrlPrefix) {
        try {
            //获取当前年月日时分秒字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateTimeString = sdf.format(new Date());
            listFirePointExcelExportEntityList.forEach(listManagerStatisticsExcelExportEntity -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("firePointId", listManagerStatisticsExcelExportEntity.getFirePointId());
                listManagerStatisticsExcelExportEntity.setCheckQrcode(generateQrCodeImage(jsonObject.toString()));

                listManagerStatisticsExcelExportEntity.setViewQrcode(generateQrCodeImage(pointUrlPrefix+"/Container/QrCode/PartQrCode/List?firePointId="+listManagerStatisticsExcelExportEntity.getFirePointId()));
            });
            ExcelUtils.exportExcel(httpServletResponse, FirePointExcelExportEntity.class,dateTimeString, listFirePointExcelExportEntityList);
        }catch (Exception e) {
            throw new BizException("导出失败");
        }
    }
    // 生成二维码图片的方法
    public byte[] generateQrCodeImage(String url) {
        if (StringUtils.isNotBlank(url)) {
            try {
                return generateQrCodeBytes(url);
            } catch (Exception e) {
                return null;
            }
        }
       return null;
    }

    // 生成二维码字节数组的方法
    private byte[] generateQrCodeBytes(String content) throws Exception {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400);

        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        return pngOutputStream.toByteArray();
    }

}

