package com.donglan.ahxf.ahxfmeetregisterwindow.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shj
 * @date 2021/1/25 10:44 上午
 */
@Controller
@RequestMapping("/meet/export")
public class MeetExportController {

    @RequestMapping(value = "/meetInterviewDetail",method = RequestMethod.POST)
    @CrossOrigin
    public void exportMeetInterviewDetail(@RequestBody Map<String,Object> map, HttpServletResponse response) throws IOException {
        ClassPathResource classPathResource =new ClassPathResource("static/excel/meetInterviewDetail.xls");
        InputStream is = classPathResource.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(is);
        ExcelWriter writer = reader.getWriter();
        writer.disableDefaultStyle();
        Map<String,Object> activity = (HashMap<String, Object>) map.get("activity");
        Map<String,Object> person = (HashMap<String, Object>) activity.get("person");
        Map<String,Object> handle = (HashMap<String, Object>) map.get("handle");
        writer.writeCellValue(6,0, "接谈日期:"+StrUtil.nullToEmpty((String)handle.get("startTalkingTime")).split(" ")[0]);
        writer.writeCellValue(6,1, "接谈室:" + StrUtil.nullToEmpty((String)handle.get("roomName")));
        writer.writeCellValue(6,2, "等待排序号:"+
                StrUtil.nullToEmpty((String)handle.get("roomCode"))+
                StrUtil.nullToEmpty((String)handle.get("roomCodeNum")));

        writer.writeCellValue(3,5, StrUtil.nullToEmpty((String)map.get("matterNum")));
        writer.writeCellValue(5,5,StrUtil.nullToEmpty((String)person.get("name")));
        writer.writeCellValue(7,5,StrUtil.nullToEmpty((String)person.get("identity")));
        writer.writeCellValue(3,6,StrUtil.nullToEmpty((String)person.get("IDType")));
        writer.writeCellValue(5,6,StrUtil.nullToEmpty((String)person.get("IDNumber")));
        writer.writeCellValue(3,7,StrUtil.nullToEmpty((String)person.get("idCardRegion")));
        writer.writeCellValue(5,7,StrUtil.nullToEmpty((String)person.get("idCardAddress")));
        writer.writeCellValue(3,8,StrUtil.nullToEmpty((String)person.get("addressRegion")));
        writer.writeCellValue(5,8,StrUtil.nullToEmpty((String)person.get("address")));
        writer.writeCellValue(3,9,StrUtil.nullToEmpty((String)person.get("domicile")));
        writer.writeCellValue(5,9,activity.get("xfNumber") == null ? 0 : activity.get("xfNumber"));
        writer.writeCellValue(7,9,StrUtil.nullToEmpty((String)activity.get("xfSource")));
        writer.writeCellValue(3,10,StrUtil.nullToEmpty((String)person.get("mobilePhone")));
        writer.writeCellValue(5,10,StrUtil.nullToEmpty((String)map.get("happenRegion")));
        writer.writeCellValue(7,10,StrUtil.nullToEmpty((String)map.get("belongs")));
        writer.writeCellValue(3,11,StrUtil.nullToEmpty((String)map.get("lfReason")));
        writer.writeCellValue(5,11,StrUtil.nullToEmpty((String)map.get("contentClassify")));
        writer.writeCellValue(3,12,StrUtil.nullToEmpty((String)map.get("abstract")));
        writer.writeCellValue(3,18,StrUtil.nullToEmpty((String)handle.get("roomOrgName")));
        writer.writeCellValue(7,18,StrUtil.nullToEmpty((String)handle.get("talkingUserName")));
        writer.writeCellValue(3,19,StrUtil.nullToEmpty((String)handle.get("startTalkingTime"))+
                "至"+
                StrUtil.nullToEmpty((String)handle.get("endTalkingTime")));
        writer.writeCellValue(3,20,StrUtil.nullToEmpty((String)handle.get("talkContent")));
        writer.writeCellValue(3,21,StrUtil.nullToEmpty((String)handle.get("talkingHandle")));
        writer.writeCellValue(3,22,"");

        writer.writeCellValue(3,23,StrUtil.nullToEmpty((String)handle.get("handleModel")));
        writer.writeCellValue(6,23,StrUtil.nullToEmpty((String)handle.get("finalDisposal")));
        writer.writeCellValue(3,24,StrUtil.nullToEmpty((String)handle.get("handleTime")));
        writer.writeCellValue(6,24,StrUtil.nullToEmpty((String)handle.get("handleUserName")));
        writer.writeCellValue(3,25,StrUtil.nullToEmpty((String)handle.get("examineContent")));
        if(handle.get("examine")!=null)
            writer.writeCellValue(3,26,(Integer) handle.get("examine")==0?"同意":"退回");
        else
            writer.writeCellValue(3,26, "");
        writer.writeCellValue(5,26,StrUtil.nullToEmpty((String)handle.get("examineUserName")));
        writer.writeCellValue(7,26,StrUtil.nullToEmpty((String)handle.get("examineTime")));
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("来访接谈处理单","utf-8") +".xls");
        writer.flush(os,true);
        writer.close();;
        IoUtil.close(os);
        IoUtil.close(is);
        writer.close();
        reader.close();
    }

    @RequestMapping(value = "/meetRoom/interviewList",method = RequestMethod.POST)
    @CrossOrigin
    public void exportMeetRoomInterviewList (@RequestBody List<Map<String,Object>> list, HttpServletResponse response) throws IOException {
        InputStream is = ResourceUtil.getStream("static/excel/meetRoomInterviewList.xlsx");
        ExcelReader reader = ExcelUtil.getReader(is);
        ExcelWriter writer = reader.getWriter();
        List<List<String>> rows = CollUtil.newLinkedList();

        list.stream().forEach(item->{
            List<String> row = CollUtil.newLinkedList();
            Map<String,Object> record = (Map<String, Object>) item.get("record");
            Map<String,Object> activity = (Map<String, Object>) item.get("activity");
            Map<String,Object> person = (Map<String, Object>) activity.get("person");
            row.add("");
            row.add(StrUtil.nullToEmpty((String)record.get("callingTime")).split(" ")[0]);
            row.add(StrUtil.nullToEmpty((String)record.get("roomName")));
            row.add(StrUtil.nullToEmpty((String)record.get("roomNum")));
            row.add(StrUtil.nullToEmpty((String)person.get("name")));
            row.add(StrUtil.nullToEmpty((String)person.get("domicile")));
            row.add(StrUtil.nullToEmpty((String)person.get("IDType")));
            row.add(StrUtil.nullToEmpty((String)person.get("IDNumber")));
            row.add(StrUtil.nullToEmpty((String)person.get("addressRegion")));
            row.add(StrUtil.nullToEmpty((String)person.get("address")));
            row.add(StrUtil.nullToEmpty((String)person.get("mobilePhone")));
            row.add(StrUtil.nullToEmpty((String)item.get("matterNum")));
            row.add(StrUtil.nullToEmpty((String)item.get("contentClassify")));
            row.add(StrUtil.nullToEmpty((String)record.get("callingTime")));
            row.add(StrUtil.nullToEmpty((String)record.get("beforeMood")));
            row.add(StrUtil.nullToEmpty((String)record.get("talkContent")));
            row.add(StrUtil.nullToEmpty((String)record.get("afterMood")));
            row.add(StrUtil.nullToEmpty((String)record.get("createTime")));
            row.add(StrUtil.nullToEmpty((String)record.get("talkingHandle")));
            row.add(StrUtil.nullToEmpty((String)record.get("orgName")));
            row.add(StrUtil.nullToEmpty((String)record.get("userName")));
            row.add(StrUtil.nullToEmpty((String)item.get("handleModel")));
            row.add(StrUtil.nullToEmpty((String)item.get("finalDisposal")));
            rows.add(row);
        });
//        writer.disableDefaultStyle();
        writer.passRows(4);
        writer.write(rows);

        for(int i=0;i<rows.size();i++){
            CellStyle cellStyle = writer.createCellStyle(0, 4 + i);
            cellStyle.setBorderBottom(BorderStyle.NONE);
            cellStyle.setBorderLeft(BorderStyle.NONE);
            cellStyle.setBorderRight(BorderStyle.NONE);
            cellStyle.setBorderTop(BorderStyle.NONE);
//            writer.setStyle(,0,4+i)
//            writer.writeCellValue(0,4+i,"1");
        }

        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("省直机关接谈情况表","utf-8") +".xlsx");
        writer.flush(os,true);
        writer.close();;
        IoUtil.close(os);
        IoUtil.close(is);
        writer.close();
        reader.close();
    }

    @RequestMapping(value = "/matter/persons",method = RequestMethod.POST)
    @CrossOrigin
    public void exportMatterPersonsList(@RequestBody Map<String,Object> map, HttpServletResponse response) throws IOException {
        InputStream is = ResourceUtil.getStream("static/excel/matterPersonsList.xlsx");
        ExcelReader reader = ExcelUtil.getReader(is);
        ExcelWriter writer = reader.getWriter();
        Map<String,Object> mainPerson = (Map<String, Object>) (((List<Object>)map.get("personList")).get(0));
        StyleSet defaultStyleSet = writer.getStyleSet();
        writer.disableDefaultStyle();
        writer.writeCellValue(2,2,StrUtil.nullToEmpty((String)map.get("printOrg")));
        writer.writeCellValue(9,2,"打印日期："+StrUtil.nullToEmpty((String)map.get("printTime")));
        writer.writeCellValue(2,3,StrUtil.nullToEmpty((String)map.get("name")));
        writer.writeCellValue(6,3,StrUtil.nullToEmpty((String)map.get("createTime")));
        writer.writeCellValue(9,3,StrUtil.nullToEmpty((String)map.get("registerPlace")));
        writer.writeCellValue(2,4,StrUtil.nullToEmpty((String)map.get("xfNature")));
        writer.writeCellValue(6,4,StrUtil.nullToEmpty((String)map.get("xfName")));
        writer.writeCellValue(2,5,StrUtil.nullToEmpty((String)map.get("objectName")));
        writer.writeCellValue(6,5,map.get("xfNumber"));
        writer.writeCellValue(9,5,StrUtil.nullToEmpty((String)map.get("contentClassifyAll")));
        List<Map<String, Object>> persons = (List<Map<String, Object>>) map.get("personList");
        List<List<String>> rows = CollUtil.newLinkedList();
        writer.setCurrentRow(7);
        writer.setStyleSet(defaultStyleSet);
        for(int i =1;i<persons.size()+1;i++){
            Map<String,Object> person = persons.get(i-1);
            writer.writeCellValue(0,6+i,i+"");
            writer.writeCellValue(1,6+i,StrUtil.nullToEmpty((String)person.get("name")));
            writer.writeCellValue(2,6+i,StrUtil.nullToEmpty((String)person.get("IDType")));
            writer.merge(6+i,6+i,3,5,
                    StrUtil.nullToEmpty((String)person.get("IDNumber")),false);
            writer.merge(6+i,6+i,6,7,
                    StrUtil.nullToEmpty((String)person.get("mobilePhone")),false);
            writer.writeCellValue(8,6+i,StrUtil.nullToEmpty((String)person.get("addressRegion")));
            writer.merge(6+i,6+i,9,10,
                    StrUtil.nullToEmpty((String)person.get("address")),false);
            if(person.get("isMain")!=null)
                writer.writeCellValue(11,6+i,(Integer) person.get("isMain")==1?"是":"否");
            else
                writer.writeCellValue(11,6+i,"");
        }
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("同件信访事项信访人名单","utf-8") +".xlsx");
        writer.flush(os,true);
        writer.close();;
        IoUtil.close(os);
        IoUtil.close(is);
        writer.close();
        reader.close();
    }

    @RequestMapping(value = "/meet/persons",method = RequestMethod.POST)
    @CrossOrigin
    public void exportMeetPersons(@RequestBody List<Map<String,Object>> list, HttpServletResponse response) throws IOException {
        InputStream is =ResourceUtil.getStream("static/excel/meetPersons.xlsx");
        ExcelReader reader = ExcelUtil.getReader(is);
        ExcelWriter writer = reader.getWriter();
        List<List<String>> rows = CollUtil.newLinkedList();
        list.stream().forEach((item)->{
            List<String> row = CollUtil.newLinkedList();
            row.add("");
            row.add(StrUtil.nullToEmpty((String)item.get("createTime")));
            row.add(StrUtil.nullToEmpty((String)item.get("registerPlace")));
            row.add(StrUtil.nullToEmpty((String)item.get("activityNum")));
            row.add(StrUtil.nullToEmpty((String)item.get("mainPersonName")));
            row.add(StrUtil.nullToEmpty((String)item.get("name")));
            row.add(StrUtil.nullToEmpty((String)item.get("domicileProvince")));
            row.add(StrUtil.nullToEmpty((String)item.get("domicileCity")));
            row.add(StrUtil.nullToEmpty((String)item.get("domicileCounty")));
            row.add(StrUtil.nullToEmpty((String)item.get("IDType")));
            row.add(StrUtil.nullToEmpty((String)item.get("IDNumber")));
            row.add(StrUtil.nullToEmpty((String)item.get("addressRegion")));
            row.add(StrUtil.nullToEmpty((String)item.get("address")));
            row.add(StrUtil.nullToEmpty((String)item.get("mobilePhone")));
            row.add(StrUtil.nullToEmpty((String)item.get("matterNum")));
            row.add(StrUtil.nullToEmpty((String)item.get("happenRegionProvince")));
            row.add(StrUtil.nullToEmpty((String)item.get("happenRegionCity")));
            row.add(StrUtil.nullToEmpty((String)item.get("happenRegionCounty")));
            rows.add(row);
        });
        writer.setCurrentRow(5);
        writer.write(rows);
        for(int i=0;i<rows.size();i++){
            CellStyle cellStyle = writer.createCellStyle(0, 5 + i);
            cellStyle.setBorderBottom(BorderStyle.NONE);
            cellStyle.setBorderLeft(BorderStyle.NONE);
            cellStyle.setBorderRight(BorderStyle.NONE);
            cellStyle.setBorderTop(BorderStyle.NONE);
        }
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("信访人统计表","utf-8") +".xlsx");
        writer.flush(os,true);
        writer.close();;
        IoUtil.close(os);
        IoUtil.close(is);
        writer.close();
        reader.close();
    }

    @RequestMapping(value = "/meet/activitys",method = RequestMethod.POST)
    @CrossOrigin
    public void exportMeetActivitys(@RequestBody List<Map<String,Object>> list,HttpServletResponse response) throws IOException {
        InputStream is = ResourceUtil.getStream("static/excel/meetActivitys.xlsx");
        ExcelReader reader = ExcelUtil.getReader(is);
        ExcelWriter writer = reader.getWriter();
        writer.setCurrentRow(5);
        Integer i=0;
        for(int j=0;j<list.size();j++){
            Map<String,Object> activity = list.get(j);
            List<Map<String,Object>> matters = (List<Map<String, Object>>) activity.get("matterList");
            //需要合并的行数
            Integer size = matters.size() > 1 ? matters.size()-1 : 0;
            if(size>0) {
                writer.merge(5 + i, 5 + i + size, 2, 2,
                        StrUtil.nullToEmpty((String) activity.get("djTime")), false);
                writer.merge(5 + i, 5 + i + size, 3, 3,
                        StrUtil.nullToEmpty((String) activity.get("registerPlace")), false);
                writer.merge(5 + i, 5 + i + size, 4, 4,
                        StrUtil.nullToEmpty((String) activity.get("activityNum")), false);
                writer.merge(5 + i, 5 + i + size, 5, 5,
                        activity.get("xfNumber") == null ? 0 : activity.get("xfNumber"), false);
                writer.merge(5 + i, 5 + i + size, 6, 6,
                        StrUtil.nullToEmpty((String) activity.get("isAbnormal")), false);
                writer.merge(5 + i, 5 + i + size, 7, 7,
                        StrUtil.nullToEmpty((String) activity.get("abnormalPlace")), false);
                writer.merge(5 + i, 5 + i + size, 8, 8,
                        StrUtil.nullToEmpty((String) activity.get("abnormalAction")), false);
                writer.merge(5 + i, 5 + i + size, 9, 9,
                        StrUtil.nullToEmpty((String) activity.get("mainPerson")), false);
                writer.merge(5 + i, 5 + i + size, 10, 10,
                        StrUtil.nullToEmpty((String) activity.get("domicileProvince")), false);
                writer.merge(5 + i, 5 + i + size, 11, 11,
                        StrUtil.nullToEmpty((String) activity.get("domicileCity")), false);
                writer.merge(5 + i, 5 + i + size, 12, 12,
                        StrUtil.nullToEmpty((String) activity.get("domicileCounty")), false);
                writer.merge(5 + i, 5 + i + size, 13, 13,
                        StrUtil.nullToEmpty((String) activity.get("IDType")), false);
                writer.merge(5 + i, 5 + i + size, 14, 14,
                        StrUtil.nullToEmpty((String) activity.get("IDNumber")), false);
                writer.merge(5 + i, 5 + i + size, 15, 15,
                        StrUtil.nullToEmpty((String) activity.get("addressRegion")), false);
                writer.merge(5 + i, 5 + i + size, 16, 16,
                        StrUtil.nullToEmpty((String) activity.get("address")), false);
                writer.merge(5 + i, 5 + i + size, 17, 17,
                        StrUtil.nullToEmpty((String) activity.get("mobilePhone")), false);
            }else{
                writer.writeCellValue(2,5+i, StrUtil.nullToEmpty((String) activity.get("djTime")));
                writer.writeCellValue(3,5+i, StrUtil.nullToEmpty((String) activity.get("registerPlace")));
                writer.writeCellValue(4,5+i, StrUtil.nullToEmpty((String) activity.get("activityNum")));
                writer.writeCellValue(5,5+i, activity.get("xfNumber") == null ? 0 : activity.get("xfNumber"));
                writer.writeCellValue(6,5+i, StrUtil.nullToEmpty((String) activity.get("isAbnormal")));
                writer.writeCellValue(7,5+i, StrUtil.nullToEmpty((String) activity.get("abnormalPlace")));
                writer.writeCellValue(8,5+i, StrUtil.nullToEmpty((String) activity.get("abnormalAction")));
                writer.writeCellValue(9,5+i, StrUtil.nullToEmpty((String) activity.get("mainPerson")));
                writer.writeCellValue(10,5+i, StrUtil.nullToEmpty((String) activity.get("domicileProvince")));
                writer.writeCellValue(11,5+i, StrUtil.nullToEmpty((String) activity.get("domicileCity")));
                writer.writeCellValue(12,5+i, StrUtil.nullToEmpty((String) activity.get("domicileCounty")));
                writer.writeCellValue(13,5+i, StrUtil.nullToEmpty((String) activity.get("IDType")));
                writer.writeCellValue(14,5+i, StrUtil.nullToEmpty((String) activity.get("IDNumber")));
                writer.writeCellValue(15,5+i, StrUtil.nullToEmpty((String) activity.get("addressRegion")));
                writer.writeCellValue(16,5+i, StrUtil.nullToEmpty((String) activity.get("address")));
                writer.writeCellValue(17,5+i, StrUtil.nullToEmpty((String) activity.get("mobilePhone")));
            }
            for (int n=0;n<matters.size();n++){
                Map<String,Object> matter = matters.get(n);
                writer.writeCellValue(18,5+i+n,StrUtil.nullToEmpty((String)matter.get("matterNum")));
                writer.writeCellValue(19,5+i+n,StrUtil.nullToEmpty((String)matter.get("happenRegionProvince")));
                writer.writeCellValue(20,5+i+n,StrUtil.nullToEmpty((String)matter.get("happenRegionCity")));
                writer.writeCellValue(21,5+i+n,StrUtil.nullToEmpty((String)matter.get("happenRegionCounty")));
                writer.writeCellValue(22,5+i+n,StrUtil.nullToEmpty((String)matter.get("belongs")));
                writer.writeCellValue(23,5+i+n,StrUtil.nullToEmpty((String)matter.get("isSKSFL")));
                writer.writeCellValue(24,5+i+n,StrUtil.nullToEmpty((String)matter.get("SKSFL")));
                writer.writeCellValue(25,5+i+n,StrUtil.nullToEmpty((String)matter.get("contentClassify1")));
                writer.writeCellValue(26,5+i+n,StrUtil.nullToEmpty((String)matter.get("contentClassify2")));
                writer.writeCellValue(27,5+i+n,StrUtil.nullToEmpty((String)matter.get("contentClassify3")));
                writer.writeCellValue(28,5+i+n,StrUtil.nullToEmpty((String)matter.get("contentClassify4")));
                writer.writeCellValue(29,5+i+n,StrUtil.nullToEmpty((String)matter.get("contentClassify5")));
                writer.writeCellValue(30,5+i+n,StrUtil.nullToEmpty((String)matter.get("abstract")));
                writer.writeCellValue(31,5+i+n,StrUtil.nullToEmpty((String)matter.get("appealContent")));
                writer.writeCellValue(32,5+i+n,StrUtil.nullToEmpty((String)matter.get("identityType")));
                writer.writeCellValue(33,5+i+n,StrUtil.nullToEmpty((String)matter.get("postType")));
                writer.writeCellValue(34,5+i+n,StrUtil.nullToEmpty((String)matter.get("talkingOrg")));
                writer.writeCellValue(35,5+i+n,StrUtil.nullToEmpty((String)matter.get("handleModel")));
                writer.writeCellValue(36,5+i+n,StrUtil.nullToEmpty((String)matter.get("finalDisposal")));
                writer.writeCellValue(37,5+i+n,StrUtil.nullToEmpty((String)matter.get("visit")));
                writer.writeCellValue(38,5+i+n,StrUtil.nullToEmpty((String)matter.get("createPerson")));
            }
            i=i+size+1;
        }
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("信访人统计表","utf-8") +".xlsx");
        writer.flush(os,true);
        writer.close();;
        IoUtil.close(os);
        IoUtil.close(is);
        writer.close();
        reader.close();
    }
}
