package com.sa.ibiz.demo.ibizutil.helper;

import com.sa.ibiz.demo.ibizutil.domain.IBZFILE;
import com.sa.ibiz.demo.ibizutil.security.AuthenticationUser;
import com.sa.ibiz.demo.ibizutil.service.IBZFILEService;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 表格数据导出工具类
 */
public class DEDataExportHelper {

    /**
     * 生成xls文件
     * @param strFile
     * @param colnums
     * @param datas
     * @throws IOException
     * @throws WriteException
     */
    public static File output(String strFile, List<Map<String,String>> colnums, List<Map<String,Object>> datas) throws IOException, WriteException {
        File outputFile=new File(strFile);

        WritableWorkbook workbook = Workbook.createWorkbook(outputFile);
        WritableSheet s1 = workbook.createSheet("数据", 0);
        int nRowIndex = 0;
        int nColumnIndex = 0;
        //绘制excel列头
        for(Map<String,String> col:colnums){
            for (Map.Entry<String,String> entry : col.entrySet()) {
                String colLogicName=entry.getValue(); //列名-逻辑名称
                Label l = new Label(nColumnIndex, nRowIndex, colLogicName);
                s1.addCell(l);
                s1.setColumnView(nColumnIndex, 30);
                nColumnIndex++;
            }
        }
        nRowIndex++;
        nColumnIndex = 0;
        // 输出行数据
        for(Map<String,Object> rowdata:datas){
            for(Map<String,String> col:colnums){//按照列头顺序输出行数据
                for (Map.Entry<String,String> entry : col.entrySet()) {
                    String strObj="";
                    Object rowobj =rowdata.get(entry.getKey());
                    if(rowobj!=null)
                        strObj=rowobj.toString();
                    Label l = new Label(nColumnIndex, nRowIndex, strObj);
                    s1.addCell(l);
                }
                nColumnIndex++;
            }
            nRowIndex++;
            nColumnIndex = 0;
        }
        workbook.write();
        workbook.close();
        return outputFile;
    }

    /**
     * 保存file表记录
     * @param
     * @param strLocalPath
     * @param fileid
     */
    public static  void saveFileData(File outputFile,String strLocalPath,String fileid,IBZFILEService ibzfileService){
        IBZFILE ibzfile = new IBZFILE();
        ibzfile.setFileid(fileid);
        ibzfile.setFilename(outputFile.getName());
        ibzfile.setFilepath(strLocalPath);
        ibzfile.setFileid(fileid);
        Integer nFileSize = 0;
        nFileSize = Integer.parseInt(outputFile.length() + "");
        ibzfile.setCreateman(AuthenticationUser.getAuthenticationUser().getUserid());
        ibzfile.setUpdateman(AuthenticationUser.getAuthenticationUser().getUserid());
        ibzfile.setCreatedate(new Timestamp(new Date().getTime()));
        ibzfile.setUpdatedate(new Timestamp(new Date().getTime()));
        ibzfileService.save(ibzfile);
    }

}
