package com.huimv.admin.common.utils;

import com.huimv.admin.entity.BillPersonnelAdmission;
import com.huimv.admin.entity.dto.PrintAdmissionDto;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Print {
    /**
     * 打印方法  不插入图片
     *
     * @return
     * @throws Exception
     */
    public static void printAdmission(List<BillPersonnelAdmission> admissionList) throws Exception {

        //准备数据
        String path = "G://admission.xls";
        //获取模板文件，你自己当前模板的位置  我这里为当前项目下
        InputStream is = new FileInputStream("G://admission.xlsx");
        //根据模板生成的文件保存路径  我这里保存在本地D盘
        OutputStream os = new FileOutputStream(path);
        //绑定数据
        Context context = new Context();
        List<PrintAdmissionDto> list = new ArrayList<>();
        for (BillPersonnelAdmission admission : admissionList) {
            PrintAdmissionDto dto = new PrintAdmissionDto();
            dto.setUserName(admission.getAdmissionUserName());
            dto.setCarNum(admission.getCarNum());
            dto.setPhone(admission.getPhone());
            dto.setDestName(admission.getDestName());
            LocalDateTime date = admission.getVistitDate();
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            dto.setVistitDate(dtf.format(date));
            dto.setVistitReason(admission.getVistitReson());
            if (admission.getBillStatus() == 0) {
                dto.setBillStatus("待审核");
            } else if (admission.getBillStatus() == 1) {
                dto.setBillStatus("通过");
            } else if (admission.getBillStatus() == 2) {
                dto.setBillStatus("拒绝");
            }

            if (admission.getVistitType() == 0) {
                dto.setVistitType("人员");
            } else if (admission.getVistitType() == 1) {
                dto.setVistitType("环保车");
            } else if (admission.getVistitType() == 2) {
                dto.setVistitType("拉猪车");
            } else if (admission.getVistitType() == 3) {
                dto.setVistitType("饲料车");
            } else if (admission.getVistitType() == 4) {
                dto.setVistitType("送猪车");
            } else if (admission.getVistitType() == 5) {
                dto.setVistitType("物资车");
            }
            list.add(dto);
        }
        context.putVar("list", list);
        //生成
        JxlsHelper.getInstance().processTemplate(is, os, context);
    }


}