package com.jfinal.club._admin.downloadrecord;

import com.jfinal.club.common.model.DownloadRecord;
import com.jfinal.club.util.Cons;
import com.jfinal.club.util.ExcelUtils;
import com.jfinal.kit.PathKit;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @Auther: LiuChao
 * @Date: 2021/3/2 20:37
 * @Description:
 */
public class AdminDownloadRecordService {

    /**
     * 生成excel  文件
     */
    public void createExcel(List<DownloadRecord> list) {

        //excel标题
        String[] title = {"姓名","公司","手机","邮箱","文件名","文件分类","下载时间"};
        //sheet名
        String sheetName = "会员信息";

        if(list == null && list.size() <= 0){
            return;
        }

        String [][] content = new String[list.size()][title.length];

        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            DownloadRecord obj = list.get(i);
            content[i][0] = obj.get("name","");
            content[i][1] = obj.get("company","");
            content[i][2] = obj.get("mobile","");
            content[i][3] = obj.get("email","");
            content[i][4] = obj.get("filename","");
            content[i][5] = obj.getStr("type").equals("1") ? "明星产品" : "配方创意";
            content[i][6] = DateFormatUtils.format(obj.getCreateTime(),"yyyy-MM-dd hh:mm");
        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            FileOutputStream out = new FileOutputStream(new File(PathKit.getWebRootPath(),"upload"+Cons.EXPORT_DOWNLOADRECORD));
            wb.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
