package cn.yw.nhyw.nhywsbipgl.action;

import cn.base.aspect.OperateType;
import cn.base.bean.BaseAction;
import cn.base.excel.ExcelCol;
import cn.base.excel.ExportEngine;
import cn.base.util.*;
import cn.yw.nhyw.nhywsbipgl.service.NhywsbipglService;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;


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

    @Autowired
    private NhywsbipglService service;

    /**
     * 分页
     */
    @OperateType(value = LogConstants.LOG_QUERY, info = "listNhywsbipglByPage")
    public void listNhywsbipglByPage() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            int page = Integer.parseInt(Struts2Utils.getParameter("page"));
            int rows = Integer.parseInt(Struts2Utils.getParameter("rows"));
            int start = ((page == 0 ? 1 : page) - 1) * rows;
            map.put("start", start);
            map.put("limit", rows);
            Struts2Utils.renderJson(this.service.listNhywsbipglByPage(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("NhywsbipglAction-listNhywsbipglByPage-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 查询总数
     */
    @OperateType(value = LogConstants.LOG_QUERY, info = "count_Nhywsbipgl")
    public void count_Nhywsbipgl() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Struts2Utils.renderJson(this.service.count_Nhywsbipgl(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("NhywsbipglAction-count_Nhywsbipgl-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 删除
     */
    @OperateType(value = LogConstants.LOG_DELETE, info = "delete_Nhywsbipgl")
    public void delete_Nhywsbipgl() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            if (!CommUtils.isEmpty(map)) {
                int result = service.delete_Nhywsbipgl(map);
                Struts2Utils.renderJson(Constants.SUCCESS);
            } else {
                throw new Exception("请正确的选择删除记录!!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("NhywsbipglAction-delete_Nhywsbipgl-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 插入
     */
    @OperateType(value = LogConstants.LOG_ADD, info = "insert_Nhywsbipgl")
    public void insert_Nhywsbipgl() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            if (StringUtil.checkEmpty(map.get("id"))) {
                map.put("id", StringUtil.getUUID());
            }
            service.insert_Nhywsbipgl(map);
            Struts2Utils.renderJson(Constants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("NhywsbipglAction-insert_Nhywsbipgl()-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 修改
     */
    @OperateType(value = LogConstants.LOG_UPDATE, info = "update_Nhywsbipgl")
    public void update_Nhywsbipgl() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            int result = service.update_Nhywsbipgl(map);
            Struts2Utils.renderJson(Constants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("NhywsbipglAction-update_Nhywsbipgl-error: " + e);
            Struts2Utils.renderJson(null);
        }
    }


    /**
     * 导出Excel
     */

    @OperateType(value = LogConstants.LOG_EXPORT, info = "exp_Nhywsbipgl")
    public void exp_Nhywsbipgl() {
        try {
            Map map = Struts2Utils.getRequestParameterMap();
            map.put("start", 0);
            map.put("limit", 5000);
            List<Map> sList = service.query_Nhywsbipgl(map);

            String[] zdbm = {"qs","zj","lhybh","jczhptbh","sjkkbh","zdkkbh","zpdybm","kkmc","zmptmc","sbh","fx","ip","ym","wg","vlan","lkdm","lddm","dlms","jd","wd","dlmc","fxlx","xzqh","ldlx","kkwzlx","kkyylx","jpljtj","zpfxlx","sfbjkk","ljdy","cdbh","dcxs","xcxs","zdxs","cdzs","cj","dkh","yhm","yhmm","sblx"};
            String[] zdmc = {"期数","镇街","六合一编号","集成指挥平台编号","卡口编号——市局","卡口编号_支队","抓拍单元编码","卡口名称","中盟平台名称","设备号","方向","IP","新掩码","新网关","vlan编号","路口代码","路段代码","道路米数","经度","纬度","道路名称","方向类型","行政区划","路段类型","卡口位置类型","卡口应用类型","具备拦截条件","抓拍方向类型","是否边界卡口","邻界地域","车道编号","打车限速","小车限速","最低限速","车道总数","厂家","端口号","用户名","用户密码","设备类型"};
            int excelNum = zdbm.length;
            ExcelCol[] cells = new ExcelCol[excelNum];
            for(int i=0;i<excelNum;i++){
                cells[i] = new ExcelCol();
                cells[i].setColKey(zdbm[i]);
                cells[i].setColName(zdmc[i]);
            }
            SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd");
            String dateTime = formart.format(new Date());
            Map params = new HashMap();
            params.put("fileName", "设备IP管理-" + dateTime);
            params.put("sheetName", "设备IP管理");
            params.put("header", "设备IP管理");
            new ExportEngine().exportCommData(params, cells, (ArrayList) sList, Struts2Utils.getResponse());
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("NhywsbipglAction-exp_Nhywsbipgl-error:" + e);

        }
    }

    /**
     * 导入Excel
     */
    @OperateType(value = LogConstants.LOG_IMPORT, info = "imp_Nhywsbipgl")
    public void imp_Nhywsbipgl() {
        try {
            MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
            String result = "0";
            List<Map> list = new ArrayList<>();
            List<List<Object>> listob = null;
            wrapper.getParameter("filename");
            File file = wrapper.getFiles("file")[0];
            if (null == file) {
                Struts2Utils.renderJson("文件不存在");
                iLog.error("NhywsbipglAction-imp_Nhywsbipgl-error:文件不存在");
            }
            String directory = "/upload";
            String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
            //生成上传的文件对象
            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();
            if (listob.size() > 0) {
                List<Object> columns = listob.get(0);
                if (columns.size() >= 13) { //判断模板列数是否正确
                    for (int i = 0; i < listob.size(); i++) {
                        Map vo = new HashMap();
                        vo.put("id", StringUtil.getUUID());
                        vo.put("qs", String.valueOf(listob.get(i).get(0)));
                        vo.put("zj", String.valueOf(listob.get(i).get(1)));
                        vo.put("lhybh", String.valueOf(listob.get(i).get(2)));
                        vo.put("jczhptbh", String.valueOf(listob.get(i).get(3)));
                        vo.put("sjkkbh", String.valueOf(listob.get(i).get(4)));
                        vo.put("zdkkbh", String.valueOf(listob.get(i).get(5)));
                        vo.put("zpdybm", String.valueOf(listob.get(i).get(6)));
                        vo.put("kkmc", String.valueOf(listob.get(i).get(7)));
                        vo.put("zmptmc", String.valueOf(listob.get(i).get(8)));
                        vo.put("sbh", String.valueOf(listob.get(i).get(9)));
                        vo.put("fx", String.valueOf(listob.get(i).get(10)));
                        vo.put("ip", String.valueOf(listob.get(i).get(11)));
                        vo.put("ym", String.valueOf(listob.get(i).get(12)));
                        vo.put("wg", String.valueOf(listob.get(i).get(13)));
                        vo.put("vlan", String.valueOf(listob.get(i).get(14)));
                        vo.put("lkdm", String.valueOf(listob.get(i).get(15)));
                        vo.put("lddm", String.valueOf(listob.get(i).get(16)));
                        vo.put("dlms", String.valueOf(listob.get(i).get(17)));
                        vo.put("jd", String.valueOf(listob.get(i).get(18)));
                        vo.put("wd", String.valueOf(listob.get(i).get(19)));
                        vo.put("dlmc", String.valueOf(listob.get(i).get(20)));
                        vo.put("fxlx", String.valueOf(listob.get(i).get(21)));
                        vo.put("xzqh", String.valueOf(listob.get(i).get(22)));
                        vo.put("ldlx", String.valueOf(listob.get(i).get(23)));
                        vo.put("kkwzlx", String.valueOf(listob.get(i).get(24)));
                        vo.put("kkyylx", String.valueOf(listob.get(i).get(25)));
                        vo.put("jpljtj", String.valueOf(listob.get(i).get(26)));
                        vo.put("zpfxlx", String.valueOf(listob.get(i).get(27)));
                        vo.put("sfbjkk", String.valueOf(listob.get(i).get(28)));
                        vo.put("ljdy", String.valueOf(listob.get(i).get(29)));
                        vo.put("cdbh", String.valueOf(listob.get(i).get(30)));
                        vo.put("dcxs", String.valueOf(listob.get(i).get(31)));
                        vo.put("xcxs", String.valueOf(listob.get(i).get(32)));
                        vo.put("zdxs", String.valueOf(listob.get(i).get(33)));
                        vo.put("cdzs", String.valueOf(listob.get(i).get(34)));
                        vo.put("cj", String.valueOf(listob.get(i).get(35)));
                        vo.put("dkh", String.valueOf(listob.get(i).get(36)));
                        vo.put("yhm", String.valueOf(listob.get(i).get(37)));
                        vo.put("yhmm", String.valueOf(listob.get(i).get(38)));
                        vo.put("sblx", String.valueOf(listob.get(i).get(39)));
                        list.add(vo);
                    }
                    result = this.service.batch_insertNhywsbipglinfo(list);
                }
                if (result.equals("1")) {
                    Struts2Utils.renderJson("导入成功" + list.size() + "条数据");
                } else {
                    Struts2Utils.renderJson("导入失败");
                }
            } else {
                Struts2Utils.renderJson("模板错误");
            }
        } catch (Exception e) {
            iLog.error("NhywsbipglAction-imp_Nhywsbipgl-error:" + e);
            Struts2Utils.renderJson("导入失败");
        }

    }
}
