package com.jpsoft.shinestar.modules.business.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.config.OSSConfig;
import com.jpsoft.shinestar.modules.base.dto.MeetingPersonDTO;
import com.jpsoft.shinestar.modules.base.entity.MeetingInfo;
import com.jpsoft.shinestar.modules.base.entity.MeetingPerson;
import com.jpsoft.shinestar.modules.base.entity.MeetingTime;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.MeetingInfoService;
import com.jpsoft.shinestar.modules.base.service.MeetingPersonService;
import com.jpsoft.shinestar.modules.base.service.MeetingTimeService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.business.dto.MeetingPersonRecodeDTO;
import com.jpsoft.shinestar.modules.business.entity.MeetingPersonRecode;
import com.jpsoft.shinestar.modules.business.service.MeetingPersonRecodeService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.OSSUtil;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.sys.service.DataDictionaryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/business/meetingPersonRecode")
@Slf4j
public class MeetingPersonRecodeController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MeetingPersonRecodeService meetingPersonRecodeService;

    @Autowired
    private MeetingPersonService meetingPersonService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private OSSConfig ossConfig;

    @Autowired
    private DataDictionaryService dataDictionaryService;

    @Autowired
    private MeetingTimeService meetingTimeService;

    @Autowired
    private MeetingInfoService meetingInfoService;

    @ApiOperation(value = "应到列表")
    @RequestMapping(value = "shouldArriveList", method = RequestMethod.POST)
    public MessageResult<Map> shouldArriveList(
            String meetingId, String name, String jobNumber,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            HttpServletRequest request) {
        String subject = (String) request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String, Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("a.create_time", "desc"));

        if (StringUtils.isNotEmpty(meetingId)) {
            searchParams.put("meetingId", meetingId);
        }

        if (StringUtils.isNotEmpty(name)) {
            searchParams.put("name", "%" + name + "%");
        }

        if (StringUtils.isNotEmpty(jobNumber)) {
            searchParams.put("jobNumber", jobNumber + "%");
        }

        Page<MeetingPerson> page = meetingPersonService.pageSearch(searchParams, pageIndex, pageSize, true, sortList);

        Page<MeetingPersonRecodeDTO> pageDto = new Page<>();

        for (MeetingPerson meetingPerson : page) {
            MeetingPersonRecodeDTO dto = new MeetingPersonRecodeDTO();
            PersonInfo personInfo = personInfoService.get(meetingPerson.getPersonId());
            if (personInfo != null) {
                dto.setName(personInfo.getName());
                dto.setJobNumber(personInfo.getJobNumber());
            }

            pageDto.add(dto);
        }

        pageDto.setPageSize(page.getPageSize());
        pageDto.setTotal(page.getTotal());
        pageDto.setPageNum(page.getPageNum());

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(pageDto));

        return msgResult;
    }


    //应到map
    private List<Map> getShouldArriveMap(String meetingId, int pageIndex, int pageSize) {
        List<Map> mapList = new ArrayList<>();

        Map<String, Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time", "desc"));

        if (StringUtils.isNotEmpty(meetingId)) {
            searchParams.put("meetingId", meetingId);
        }

        Page<MeetingPerson> page = meetingPersonService.pageSearch(searchParams, pageIndex, pageSize, true, sortList);

        for (MeetingPerson meetingPerson : page) {
            Map<String, Object> map = new HashMap<>();

            Long personId = meetingPerson.getPersonId();

            PersonInfo personInfo = personInfoService.get(personId);
            String name = "";
            String jobNumber = "";
            if (personInfo != null) {
                name = personInfo.getName();
                jobNumber = personInfo.getJobNumber();
            }

            map.put("name", name);
            map.put("jobNumber", jobNumber);

            mapList.add(map);
        }

        return mapList;
    }

    @ApiOperation(value = "实到列表")
    @RequestMapping(value = "realToList", method = RequestMethod.POST)
    public MessageResult<Map> realToList(
            String meetingId, String type, String name, String jobNumber,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            HttpServletRequest request) {
        String subject = (String) request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        try {

            Map<String, Object> searchParams = new HashMap<>();

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("a.sign_time", "desc"));

            if (StringUtils.isNotEmpty(meetingId)) {
                searchParams.put("meetingId", meetingId);

                List<MeetingTime> meetingTimeList = meetingTimeService.findByMeetingId(meetingId);
                if (meetingTimeList.size() > 0) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    MeetingTime meetingTime = meetingTimeList.get(0);
                    if (meetingTime.getMeetingDate() != null) {
                        String date = sdf.format(meetingTime.getMeetingDate());

                        if (StringUtils.isNotEmpty(meetingTime.getEndSignInTime())) {
                            JSONObject jsonObject = JSONObject.fromObject(meetingTime.getEndSignInTime());
                            String start = jsonObject.get("start").toString();
                            String end = jsonObject.get("end").toString();

                            String startTime = date +" "+ start;
                            String endTime = date +" "+ end;

                            searchParams.put("startTime", startTime);
                            searchParams.put("endTime", endTime);
                        }
                    }

                }

            }

            if (StringUtils.isNotEmpty(name)) {
                searchParams.put("name", "%" + name + "%");
            }

            if (StringUtils.isNotEmpty(jobNumber)) {
                searchParams.put("jobNumber", jobNumber + "%");
            }

            Page<Map> page;

            if (type.equals("2")) {
                //实到
                page = meetingPersonService.realToSearch(searchParams, pageIndex, pageSize, true, sortList);
            } else {
                //if(type.equals("5")) 迟到
                page = meetingPersonService.lateSearch(searchParams, pageIndex, pageSize, true, sortList);
            }
//            else{
            //早退
//                page = meetingPersonRecodeService.leaveEarlySearch(searchParams, pageIndex, pageSize, true, sortList);
//            }

            Page<MeetingPersonRecodeDTO> pageDto = new Page<>();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            for (Map map : page) {
                MeetingPersonRecodeDTO dto = new MeetingPersonRecodeDTO();
                if (map.get("person_id") != null) {
                    Long personId = Long.parseLong(map.get("person_id").toString());
                    PersonInfo personInfo = personInfoService.get(personId);
                    if (personInfo != null) {
                        dto.setName(personInfo.getName());
                        dto.setJobNumber(personInfo.getJobNumber());
                    }
                }

                if (map.get("sign_time") != null) {
                    Date startSignTime = sdf.parse(map.get("sign_time").toString());
                    dto.setStartSignTime(startSignTime);
                }
                //map.put("endTime","2024-01-04 00:00:00");

                if (map.get("endTime") != null) {
                    Date endSignTime = sdf.parse(map.get("endTime").toString());
                    dto.setEndSignTime(endSignTime);
                }

                pageDto.add(dto);
            }

            pageDto.setPageSize(page.getPageSize());
            pageDto.setTotal(page.getTotal());
            pageDto.setPageNum(page.getPageNum());

            msgResult.setResult(true);
            msgResult.setData(PojoUtils.pageWrapper(pageDto));
        } catch (Exception e) {
            log.error(e.getMessage(), e);

            msgResult.setResult(false);
            msgResult.setMessage(e.getMessage());
        }

        return msgResult;
    }


    //实到，迟到，早退map
    private List<Map> getRealToMap(String meetingId, String type, int pageIndex, int pageSize) {
        List<Map> mapList = new ArrayList<>();

        Map<String, Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("a.sign_time", "desc"));

        if (StringUtils.isNotEmpty(meetingId)) {
            searchParams.put("meetingId", meetingId);

            List<MeetingTime> meetingTimeList = meetingTimeService.findByMeetingId(meetingId);
            if (meetingTimeList.size() > 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                MeetingTime meetingTime = meetingTimeList.get(0);
                if (meetingTime.getMeetingDate() != null) {
                    String date = sdf.format(meetingTime.getMeetingDate());

                    if (StringUtils.isNotEmpty(meetingTime.getEndSignInTime())) {
                        JSONObject jsonObject = JSONObject.fromObject(meetingTime.getEndSignInTime());
                        String start = jsonObject.get("start").toString();
                        String end = jsonObject.get("end").toString();

                        String startTime = date +" "+ start;
                        String endTime = date +" "+ end;

                        searchParams.put("startTime", startTime);
                        searchParams.put("endTime", endTime);
                    }
                }

            }
        }

        Page<Map> page;

        if (type.equals("2")) {
            //实到
            page = meetingPersonService.realToSearch(searchParams, pageIndex, pageSize, true, sortList);
        } else {
            //if(type.equals("5")) 迟到
            page = meetingPersonService.lateSearch(searchParams, pageIndex, pageSize, true, sortList);
        }
//        else{
//            //早退
//            page = meetingPersonRecodeService.leaveEarlySearch(searchParams, pageIndex, pageSize, true, sortList);
//        }

        for (Map map : page) {
            String name = "";
            String jobNumber = "";

            if (map.get("person_id") != null) {
                Long personId = Long.parseLong(map.get("person_id").toString());
                PersonInfo personInfo = personInfoService.get(personId);
                if (personInfo != null) {
                    name = personInfo.getName();
                    jobNumber = personInfo.getJobNumber();
                }
            }

            map.put("name", name);
            map.put("jobNumber", jobNumber);

            mapList.add(map);
        }

        return mapList;
    }


    @ApiOperation(value = "缺席列表")
    @RequestMapping(value = "absentList", method = RequestMethod.POST)
    public MessageResult<Map> absentList(
            String meetingId, String name, String jobNumber,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            HttpServletRequest request) {
        String subject = (String) request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        try {

            Map<String, Object> searchParams = new HashMap<>();

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("a.sign_time", "desc"));

            if (StringUtils.isNotEmpty(meetingId)) {
                searchParams.put("meetingId", meetingId);
            }

            if (StringUtils.isNotEmpty(name)) {
                searchParams.put("name", "%" + name + "%");
            }

            if (StringUtils.isNotEmpty(jobNumber)) {
                searchParams.put("jobNumber", jobNumber + "%");
            }

            MeetingInfo meetingInfo = meetingInfoService.get(meetingId);

            Page<MeetingPersonRecodeDTO> pageDto = new Page<>();

            //如果会议已结束才统计确实
            if ("3".equals(meetingInfo.getStatus())) {
                //缺席
                Page<MeetingPerson> page = meetingPersonService.absentSearch(searchParams, pageIndex, pageSize, true, sortList);

                for (MeetingPerson meetingPerson : page) {
                    MeetingPersonRecodeDTO dto = new MeetingPersonRecodeDTO();

                    Long personId = meetingPerson.getPersonId();
                    PersonInfo personInfo = personInfoService.get(personId);
                    if (personInfo != null) {
                        dto.setName(personInfo.getName());
                        dto.setJobNumber(personInfo.getJobNumber());
                    }

                    pageDto.add(dto);
                }
                pageDto.setPageSize(page.getPageSize());
                pageDto.setTotal(page.getTotal());
                pageDto.setPageNum(page.getPageNum());
            } else {
                pageDto.setPageSize(0);
                pageDto.setTotal(0);
                pageDto.setPageNum(0);
            }

            msgResult.setResult(true);
            msgResult.setData(PojoUtils.pageWrapper(pageDto));
        } catch (Exception e) {
            log.error(e.getMessage(), e);

            msgResult.setResult(false);
            msgResult.setMessage(e.getMessage());
        }

        return msgResult;
    }


    //缺席map
    private List<Map> getAbsentMap(String meetingId, int pageIndex, int pageSize) {
        List<Map> mapList = new ArrayList<>();

        Map<String, Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("sign_time", "desc"));

        if (StringUtils.isNotEmpty(meetingId)) {
            searchParams.put("meetingId", meetingId);
        }

        //缺席
        Page<MeetingPerson> page = meetingPersonService.absentSearch(searchParams, pageIndex, pageSize, true, sortList);

        for (MeetingPerson meetingPerson : page) {
            Map<String, Object> map = new HashMap<>();

            Long personId = meetingPerson.getPersonId();

            PersonInfo personInfo = personInfoService.get(personId);
            String name = "";
            String jobNumber = "";
            if (personInfo != null) {
                name = personInfo.getName();
                jobNumber = personInfo.getJobNumber();
            }

            map.put("name", name);
            map.put("jobNumber", jobNumber);

            mapList.add(map);
        }

        return mapList;
    }


    @ApiOperation(value = "会议统计表")
    @RequestMapping(value = "meetingRecordPageList", method = RequestMethod.POST)
    public MessageResult<Object> meetingRecordPageList(
            String meetingId, String recordTimeRange,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "exportFlag", defaultValue = "false") Boolean exportFlag,
            HttpServletRequest request) {
        String subject = (String) request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Object> msgResult = new MessageResult<>();

        try {

            Map<String, Object> searchParams = new HashMap<>();

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("a.create_time", "desc"));

            if (StringUtils.isNotEmpty(meetingId)) {
                searchParams.put("meetingId", meetingId);
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            if (StringUtils.isNotEmpty(recordTimeRange)) {
                String[] timeRangeArray = recordTimeRange.split(",");
                String beginTime = "";
                String endTime = "";
                if (timeRangeArray.length == 1) {
                    beginTime = timeRangeArray[0];
                    beginTime += " 00:00:00";
                } else if (timeRangeArray.length == 2) {
                    beginTime = timeRangeArray[0];
                    endTime = timeRangeArray[1];
                    beginTime += " 00:00:00";
                    endTime += " 23:59:59";
                }

                searchParams.put("beginTime", beginTime);
                searchParams.put("endTime", endTime);
            }

            Page<Map> page = meetingPersonRecodeService.meetingRecordPageList(searchParams, pageIndex, pageSize, true, sortList);

            List<Map> mapList = new ArrayList<>();

            for (Map map : page) {
                String id = map.get("id").toString();
                searchParams.put("meetingId", id);

                //缺席
//                Page<Map> pageTemp = meetingPersonRecodeService.noAbsentSearch(searchParams, pageIndex, 10000, true, sortList1);
//                List<Long> personIdList = new ArrayList<>();
//                for (Map tempMap:pageTemp) {
//                    if(tempMap.get("person_id")!=null){
//                        personIdList.add(Long.parseLong(tempMap.get("person_id").toString()));
//                    }
//                }
//                if(personIdList.size()>0){
//                    searchParams.put("personIdList", personIdList);
//                }

                MeetingInfo meetingInfo = meetingInfoService.get(id);

                Page<MeetingPerson> page0;

                //会议已结束才统计缺席人数
                if ("3".equals(meetingInfo.getStatus())) {
                    //缺席人数
                    page0 = meetingPersonService.absentSearch(searchParams, pageIndex, 10000, true, sortList);
                } else {
                    page0 = new Page<>();
                }

                //实到
                Page<Map> page1 = meetingPersonService.realToSearch(searchParams, pageIndex, 10000, true, sortList);

                //迟到
                Page<Map> page2 = meetingPersonService.lateSearch(searchParams, pageIndex, 10000, true, sortList);

                //早退
//                Page<Map>  page3 = meetingPersonRecodeService.leaveEarlySearch(searchParams, pageIndex, 10000, true, sortList);

                map.put("absentNum", page0.getTotal());
                map.put("realNum", page1.getTotal());
                map.put("lateNum", page2.getTotal());
//                map.put("leaveEarlyNum",page3.getTotal());

                String statusN = "";

                if (map.get("status") != null) {
                    statusN = dataDictionaryService.findNameByCatalogNameAndValue("会议状态", map.get("status").toString());
                }

                map.put("statusN", statusN);

                List<MeetingTime> meetingTimeList = meetingTimeService.findByMeetingId(id);

                String meetingDateStrArr = "";

                for (MeetingTime meetingTime : meetingTimeList) {
                    String meetingDateStr = sdf.format(meetingTime.getMeetingDate());
                    meetingTime.setMeetingDateStr(meetingDateStr);
                    meetingDateStrArr += meetingDateStr + ",";
                }

                if (StringUtils.isNotEmpty(meetingDateStrArr)) {
                    meetingDateStrArr = meetingDateStrArr.substring(0, meetingDateStrArr.lastIndexOf(","));
                }

                map.put("meetingTimeList", meetingTimeList);
                map.put("meetingDateStrArr", meetingDateStrArr);

                mapList.add(map);
            }

            if (exportFlag) {
                String filePath = exportXls(mapList);
                msgResult.setData(filePath);
            } else {
                Map<String, Object> dataMap = PojoUtils.pageWrapper(page);
                dataMap.put("data", mapList);
                msgResult.setData(dataMap);
            }

            msgResult.setResult(true);

        } catch (Exception e) {
            log.error(e.getMessage(), e);

            msgResult.setResult(false);
            msgResult.setMessage(e.getMessage());
        }

        return msgResult;

    }


    private String exportXls(List<Map> mapList) throws IOException {
        String downloadUrl = "";

        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        //表头
        Row rowTitle = sheet.createRow(0);

        String[] titles = new String[]{"序号", "会议名称", "应到人数", "实到人数", "缺席人数", "迟到人数", "会议状态", "会议日期"};

        for (int i = 0; i < titles.length; i++) {
            Cell cell = rowTitle.createCell(i);
            cell.setCellValue(titles[i]);
        }

        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = mapList.get(i);

            Row row = sheet.createRow(i + 1);

            int colIndex = 0;
            row.createCell(colIndex++).setCellValue(i + 1);
            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("name"), ""));
            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("needNum"), ""));
            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("realNum"), ""));
            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("absentNum"), ""));
            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("lateNum"), ""));
//            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("leaveEarlyNum"),""));
            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("statusN"), ""));
            row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("meetingDateStrArr"), ""));
        }

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        try {
            workbook.write(output);

            byte[] buffer = output.toByteArray();
            ByteArrayInputStream input = new ByteArrayInputStream(buffer);

            downloadUrl = OSSUtil.upload(ossConfig, "meetingRecordPageList", "会议统计表.xls", input);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

        return downloadUrl;
    }


    @ApiOperation(value = "导出会议签到详情")
    @PostMapping(value = "exportXlsMeetingRecord")
    public MessageResult<Object> exportXlsMeetingRecord(
            String meetingId,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {

        MessageResult<Object> messageResult = new MessageResult<>();

        List<List<Map>> totalMapList = new ArrayList<>();

        MeetingInfo meetingInfo = meetingInfoService.get(meetingId);

        List<Map> mapList = getShouldArriveMap(meetingId, pageIndex, pageSize);
        List<Map> mapList2 = getRealToMap(meetingId, "2", pageIndex, pageSize);
        List<Map> mapList3 = getAbsentMap(meetingId, pageIndex, pageSize);
        List<Map> mapList4 = getRealToMap(meetingId, "5", pageIndex, pageSize);
        //List<Map> mapList5 = getRealToMap(meetingId,"6", pageIndex, pageSize);

        totalMapList.add(mapList);
        totalMapList.add(mapList2);
        totalMapList.add(mapList3);
        totalMapList.add(mapList4);
        //totalMapList.add(mapList5);

        String downloadUrl = "";

        Workbook workbook = new HSSFWorkbook();

        for (int j = 0; j < totalMapList.size(); j++) {

            Sheet sheet;
            if (j == 0) {
                sheet = workbook.createSheet("应到" + String.valueOf(mapList.size()));
            } else if (j == 1) {
                sheet = workbook.createSheet("实到" + String.valueOf(mapList2.size()));
            } else if (j == 2) {
                sheet = workbook.createSheet("缺席" + String.valueOf(mapList3.size()));
            } else {
                sheet = workbook.createSheet("迟到" + String.valueOf(mapList4.size()));
            }
//            else{
//                sheet = workbook.createSheet("早退"+ String.valueOf(mapList5.size()));
//            }

            //表头
            Row rowTitle = sheet.createRow(0);

            String[] titles;

            if (j == 0 || j == 2) {
                titles = new String[]{"序号", "姓名", "工号"};
            } else {
                titles = new String[]{"序号", "姓名", "工号", "签到时间", "会后签到时间"};
            }

            for (int i = 0; i < titles.length; i++) {
                Cell cell = rowTitle.createCell(i);
                cell.setCellValue(titles[i]);
            }


            if (j == 0 || j == 2) {
                for (int i = 0; i < totalMapList.get(j).size(); i++) {
                    Map<String, Object> map = totalMapList.get(j).get(i);

                    Row row = sheet.createRow(i + 1);

                    int colIndex = 0;
                    row.createCell(colIndex++).setCellValue(i + 1);
                    row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("name"), ""));
                    row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("jobNumber"), ""));
                }
            } else {
                for (int i = 0; i < totalMapList.get(j).size(); i++) {
                    Map<String, Object> map = totalMapList.get(j).get(i);

                    Row row = sheet.createRow(i + 1);

                    int colIndex = 0;
                    row.createCell(colIndex++).setCellValue(i + 1);
                    row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("name"), ""));
                    row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("jobNumber"), ""));
                    row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("sign_time"), ""));
                    row.createCell(colIndex++).setCellValue(com.jpsoft.shinestar.modules.common.utils.StringUtils.strValue(map.get("endTime"), ""));
                }
            }
        }

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        try {

            workbook.write(output);

            byte[] buffer = output.toByteArray();

            ByteArrayInputStream input = new ByteArrayInputStream(buffer);

            String meetingName = "";

            if (meetingInfo != null) {
                meetingName = meetingInfo.getName();
            }

            downloadUrl = OSSUtil.upload(ossConfig, "meetingRecord", meetingName + "--会议签到详情.xls", input);

            messageResult.setData(downloadUrl);
            messageResult.setResult(true);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            messageResult.setResult(false);
        }

        return messageResult;

    }

}
