package cn.wisenergy.web.admin.controller.app;

import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.common.utils.exception.Result;
import cn.wisenergy.model.app.WorkDept;
import cn.wisenergy.model.dto.GetMonthlyCollectParam;
import cn.wisenergy.model.dto.ProjectStatisticsByMonth;
import cn.wisenergy.model.dto.UserWorkTimeStatisticsByProject;
import cn.wisenergy.model.dto.WorkTimeAndCostCollect;
import cn.wisenergy.service.StatisticsService;
import cn.wisenergy.service.WorkDeptService;
import cn.wisenergy.web.admin.controller.common.BaseController;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: nh
 * @create: 2021-01-19 17:13
 **/
@RestController
@RequestMapping(value = "/statistics")
@Slf4j
@Api(tags = "统计报表")
public class StatisticsController extends BaseController {
    @Autowired
    StatisticsService statisticsService;

    @Autowired
    WorkDeptService workDeptService;

    @ApiOperation(value = "获取部门员工一个月每天的工时", notes = "获取部门员工一个月每天的工时")
    @GetMapping("/getMonthlyCollect")
    public PageInfo getMonthlyCollect(GetMonthlyCollectParam param) {
        log.info("StatisticsController[]getMonthlyCollect[]input.param" + param);
        PageInfo monthlyCollect = statisticsService.getDayWorkTimeOfMonth(param);
        return monthlyCollect;
    }

    @ApiOperation(value = "获取全部部门信息或单条信息", notes = "获取全部部门信息或单条信息")
    @ApiImplicitParam(name = "deptId", value = "部门主键")
    @GetMapping("/getDeptInfo")
    public List<WorkDept> getDeptInfo(Integer deptId) {
        log.info("StatisticsController[]getDeptInfo[]input.param" + deptId);
        List<WorkDept> workDepts = workDeptService.getDeptInfo(deptId);
        return workDepts;
    }

    @ApiOperation(value = "获取负责项目工时汇总", notes = "获取负责项目工时汇总")
    @GetMapping("/getCurrentMonthWorkTimeCollect")
    public Result<List<WorkTimeAndCostCollect>> getWorkTimeCollect(Integer userId, String startTime) {
        log.info("WorkUserController[]getCurrentMonthWorkTimeCollect[]input.param" + userId, startTime);
        if (userId == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<WorkTimeAndCostCollect> currentMonthWorkTimeCollect = statisticsService.getCurrentMonthWorkTimeCollect(userId, startTime);
        return getResult(currentMonthWorkTimeCollect);
    }

    @ApiOperation(value = "获取本月工时统计和审核统计", notes = "获取本月", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户主键", dataType = "int", required = true)
    @GetMapping("/getMonthlyWorkingHours")
    public Result<Map<String, Object>> getMonthlyWorkingHours(Integer userId) {
        log.info("WorkOrderController[]getMonthlyWorkingHours[]input.param" + userId);
        if (userId == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        Map<String, Object> monthlyStatistics = statisticsService.getMonthlyStatistics(userId);
        return getResult(monthlyStatistics);
    }

    @ApiOperation(value = "导出部门员工一个月每天的工时", notes = "导出部门员工一个月每天的工时")
    @GetMapping("/exportWorkTime")
    public void export(GetMonthlyCollectParam param, HttpServletResponse response) {
        log.info("StatisticsController[]getDeptInfo[]input.param" + param);
        // 获取excel
        HSSFWorkbook sheets = statisticsService.exportWorkTimeExcel(param);

        // 获取excel中的第2行第2列中的部门名称
        String deptName = sheets.getSheet("Sheet1").getRow(1).getCell(1).getStringCellValue();
        // 文件名
        String fileName = deptName + "-" + param.getYear() + "年" + param.getMonth() + "月工时汇总";
        //响应类型为application/octet- stream情况下使用了这个头信息的话，那就意味着不想直接显示内容
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        //attachment为以附件方式下载
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(
                    fileName + ".xls",
                    "utf-8"));

            response.setHeader("Cache-Control", "No-cache");
            response.flushBuffer();
            sheets.write(response.getOutputStream());
            sheets.close();
        } catch (Exception e) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.SERVER_ERROR);
        }

    }

    @ApiOperation(value = "获取人员工时统计报表", notes = "获取人员工时统计报表")
    @GetMapping("/getUserWorkTimeStatisticsReport")
    public Result<List<UserWorkTimeStatisticsByProject>> getUserWorkTimeStatisticsReport(Integer userId, Integer deptId, String startDate, String endDate, Integer isConclusion) {
        log.info("StatisticsController[]getUserWorkTimeStatisticsReport[]input.param{}" + userId, deptId, startDate, endDate, isConclusion);
        List<UserWorkTimeStatisticsByProject> userWorkTimeStatisticsByProjects = statisticsService.getUserWorkTimeStatisticsReport(userId,deptId, startDate, endDate, isConclusion);
        return getResult(userWorkTimeStatisticsByProjects);
    }

    @ApiOperation(value = "获取项目统计报表", notes = "获取项目统计报表")
    @GetMapping("/getProjectStatistics")
    public Result<List<ProjectStatisticsByMonth>> getProjectStatistics(Integer userId, Integer deptId, Integer year, Integer projectId) {
        log.info("StatisticsController[]getProjectStatistics[]input.param" + userId + deptId + year, projectId);
        List<ProjectStatisticsByMonth> projectStatisticsByMonths = statisticsService.getProjectStatistics(userId, deptId, year, projectId);
        return getResult(projectStatisticsByMonths);
    }

}