<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.poi.ss.usermodel.Workbook" %>
<%@ page import="org.apache.poi.xssf.usermodel.XSSFWorkbook" %>
<%@ page import="org.apache.poi.ss.usermodel.Sheet" %>
<%@ page import="org.apache.poi.ss.usermodel.Row" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="com.engine.custom.archives.entity.Dept" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.BaseBean" %>


<%
    List<Dept> depts = readExcelFile("/opt/weaver/archivesFile/template/zzxx.xlsx");
    // 输出或处理dept列表
    out.println(depts.size());
    out.println(JSONObject.toJSONString(depts));
    BaseBean bb = new BaseBean();
    bb.writeLog(JSONObject.toJSONString(depts));
    RecordSet recordSet = new RecordSet();
    String comSql = "update hrmsubcompanydefined set DAPYBH = ? where SUBCOMID = (select id from hrmsubcompany where SUBCOMPANYNAME = ? )";
    // String deptByDeptSql = "update hrmdepartmentdefined set DAPYBH = ? where SUBCOMPANYNAME = ? and SUPDEPID = (select * from hrmdepartmentdefined where DAPYBH = ? )";
    // String deptByComSql = "update hrmdepartmentdefined set DAPYBH = ? where SUBCOMPANYNAME = ? and SUPDEPID = (select * from SUBCOMPANYID1 where DAPYBH = ? )";
    String deptSql = "update hrmdepartmentdefined set DAPYBH = ? where deptid in (select id from hrmdepartment where DEPARTMENTNAME = ? ) and deptid in  (select id from hrmdepartment where SUBCOMPANYID1 = (select SUBCOMID from hrmsubcompanydefined where DAPYBH = ? ) or SUPDEPID = (select DEPTID from hrmdepartmentdefined where DAPYBH = ? ) )";

    for (Dept dept : depts) {
        bb.writeLog(JSONObject.toJSONString(dept));
        if ("zgs".equals(dept.getType())) {
            continue;
        }
        if ("zonghang".equals(dept.getType())) {

            recordSet.executeUpdate(comSql, dept.getId(), dept.getName());
            bb.writeLog(comSql+ "_"+dept.getId()+"_"+ dept.getName());
        }
        if ("zhih".equals(dept.getType()) || "fh".equals(dept.getType())) {
            if ("10".equals(dept.getParentId())) {
                recordSet.executeUpdate(comSql, dept.getId(), dept.getName());
                bb.writeLog(comSql+ "_"+dept.getId()+"_"+ dept.getName());
            } else {
                recordSet.executeUpdate(deptSql, dept.getId(), dept.getName(), dept.getParentId(), dept.getParentId());
                bb.writeLog(deptSql+ "_"+dept.getId()+"_"+ dept.getName()+"_"+dept.getParentId()+"_"+dept.getParentId());
            }
        }
        if ("ld".equals(dept.getType()) || "bm".equals(dept.getType())) {
            recordSet.executeUpdate(deptSql, dept.getId(), dept.getName(), dept.getParentId(), dept.getParentId());
            bb.writeLog(deptSql +"_"+ dept.getId()+"_"+ dept.getName()+"_"+ dept.getParentId()+"_"+ dept.getParentId());
        }
    }


%>

<%!
    public static List<Dept> readExcelFile(String filePath) {
        List<Dept> dept = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(new File(filePath));
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();

            if (rowIterator.hasNext()) {
                rowIterator.next(); // 跳过标题行
            }

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Dept Dept = new Dept();
                Dept.setId(row.getCell(0).getStringCellValue());
                Dept.setName(row.getCell(1).getStringCellValue());
                Dept.setType(row.getCell(3).getStringCellValue());
                Dept.setParentId(row.getCell(4).getStringCellValue());
                Dept.setAllName(row.getCell(2).getStringCellValue());
                // 假设第一列是姓名，第二列是年龄
                dept.add(Dept);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dept;
    }

%>


