package com.bonus.system.holiday.controller;

import com.bonus.common.core.utils.DateTimeHelper;
import com.bonus.common.core.utils.StringHelper;
import com.bonus.common.core.utils.poi.MultiSheetExcelExporter;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.Log;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.system.holiday.entity.HolidayBean;
import com.bonus.system.holiday.entity.WorkReportBean;
import com.bonus.system.holiday.service.RequestReportService;
import com.bonus.system.holiday.service.WorkReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 工作安排统计报表
 * @author hay
 */
@RestController
@RequestMapping("/requestReport")
@Slf4j
public class RequestReportController extends BaseController {

    @Resource(name = "RequestReportervice")
    private RequestReportService service;

    /**
     * 获取列表
     */
    @RequiresPermissions("flow:requestReport:list")
    @GetMapping("/list")
    @Log(title = "流程管理->工作安排统计报表->列表查询", businessType = BusinessType.QUERY)
    public TableDataInfo list(WorkReportBean bean) {
        try{
            startPage();
            return getDataTable(service.getWorkReportList(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    @PostMapping("/getDetailsList")
    @Log(title = "流程管理->工作安排统计报表->详情查询", businessType = BusinessType.QUERY)
    public TableDataInfo getDetailsList(WorkReportBean bean) {
        try{
            startPage();
            return getDataTable(service.getDetailsList(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    @PostMapping("/setCheck")
    @Log(title = "流程管理->工作安排统计报表->详情查询", businessType = BusinessType.QUERY)
    public AjaxResult setCheck(WorkReportBean bean) {
        try{
            int res = service.setCheck(bean);
            return toAjax(res);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 新增假期
     */
//    @RequiresPermissions("flow:holiday:add")
    @PostMapping
    @Log(title = "流程管理->假期管理->新增假期", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody HolidayBean o) {
        try{
            int res = service.insertHoliday(o);
            return toAjax(res);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }


    @GetMapping("/export")
    @SysLog(title = "工作安排统计报表", businessType = OperaType.EXPORT,module = "工作安排统计报表")
    public void export(HttpServletResponse response, WorkReportBean bean) {
        try {
            MultiSheetExcelExporter exporter = new MultiSheetExcelExporter();
            if (StringHelper.isNotEmpty(bean.getStartDate())&& StringHelper.isNotEmpty(bean.getEndDate())){
                String[] dateRange = DateTimeHelper.generateDateRange(bean.getStartDate(), bean.getEndDate());
                for (String s : dateRange){
                    bean.setStartDate(s);
                    bean.setEndDate(s);
                    List<WorkReportBean> list = service.getWorkReportList(bean);
                    if (list.size()>0){
                        Sheet departmentSheet1 = exporter.createSheet(s);
                        List<String> departmentHeaders1 = Arrays.asList("序号", "姓名", "职务", "类型", "外出时间", "拟返回时间", "地点", "事由", "是否请示领导同意", "代理主持工作人员", "备注","审批状态");
                        exporter.addHeaderRow(departmentSheet1, departmentHeaders1);
                        List<Map<String, Object>> departmentData1 = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++){
                            Map<String, Object> map = new LinkedHashMap<>();
                            map.put("序号", i + 1);
                            map.put("姓名", list.get(i).getUserName());
                            map.put("职务", list.get(i).getPostName());
                            map.put("类型", list.get(i).getLeaveType());
                            map.put("外出时间",list.get(i).getStartDate());
                            map.put("拟返回时间",list.get(i).getEndDate());
                            map.put("地点",list.get(i).getLocation());
                            map.put("事由",list.get(i).getLeaveReason());
                            map.put("是否请示领导同意",list.get(i).getIsAgree());
                            map.put("代理主持工作人员",list.get(i).getHostUserId());
                            map.put("备注",list.get(i).getRemark());
                            if (StringHelper.isNotEmpty(list.get(i).getExamineStatus())){
                                if ("1".equals(list.get(i).getExamineStatus())){
                                    map.put("审批状态","已通过");
                                } else if ("0".equals(list.get(i).getExamineStatus())){
                                    map.put("审批状态","待审核");
                                }
                            } else {
                                map.put("审批状态","");
                            }
                            departmentData1.add(map);
                        }
                        exporter.addDataRows(departmentSheet1, departmentData1,departmentHeaders1);
                    }
                }
                // 导出Excel文件
                exporter.exportToResponse(response, "工作安排统计报表");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
