package cn.yw.nhyw.nhywjtxcrzgl.action;

import cn.base.aspect.OperateType;
import cn.base.bean.BaseAction;
import cn.base.util.ImportExcelUtil;
import cn.base.util.LogConstants;
import cn.base.util.Struts2Utils;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component("uploadFileAction")
public class UploadFileAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    private static Logger iLog = Logger.getLogger(UploadFileAction.class);

    @OperateType(value = LogConstants.LOG_IMPORT, info = "uploadFile")
    public void uploadFile() {
        try {
            MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
            String result = "0";
            List<Map> list = new ArrayList<>();
            List<List<Object>> listob = null;
            wrapper.getParameter("filename");
            File[] files = wrapper.getFiles("file");
            if (null == files || files.length==0) {
                iLog.error("NhywjjzcxxAction-imp_Nhywjjzcxx-error:文件不存在");
            }
            String directory = "/upload";
            String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
            for(File file:files)
            {
                //生成上传的文件对象
                File target = new File(targetDirectory, wrapper.getParameter("filename"));
                //如果文件已经存在，则删除原有文件
                if (target.exists()) {
                    target.delete();
                }
                FileUtils.copyFile(file, target);           //复制file对象，实现上传
                FileInputStream in = new FileInputStream(target);
                listob = new ImportExcelUtil().getBankListByExcel(in, target.getName());
                in.close();
            }
            Struts2Utils.renderJson("导入失败");
        } catch (Exception e) {
            iLog.error("NhywjjzcxxAction-imp_Nhywjjzcxx-error:" + e);
            Struts2Utils.renderJson("导入失败");
        }

    }

    @OperateType(value = LogConstants.LOG_IMPORT, info = "uploadFile")
    public void insert_kshPicture() {
        try {
            Map map = Struts2Utils.getRequestParameterMap();
            Map queryMap = JSONObject.fromObject(map.get("fileParam"));

        } catch (Exception e) {
            iLog.error("NhywjjzcxxAction-imp_Nhywjjzcxx-error:" + e);
            Struts2Utils.renderJson("导入失败");
        }

    }
}
