package com.usky.pm.controller.web;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.pm.domain.PmWorkContent;
import com.usky.pm.domain.PmWorkReport;
import com.usky.pm.service.PmWorkContentService;
import com.usky.pm.service.vo.PmWorkHourStatisticRequestVO;
import com.usky.pm.service.vo.PmWorkReportSlideVO;
import com.usky.pm.service.vo.WorkTimeExportTwoVO;
import com.usky.pm.service.vo.WorkTimeExportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * <p>
 * 工作内容表 前端控制器
 * </p>
 *
 * @author fu
 * @since 2024-05-20
 */
@RestController
@RequestMapping("/pmWorkContent")
public class PmWorkContentController {
    @Autowired
    private PmWorkContentService pmWorkContentService;

    /**
     * 项目名
     *
     * @param startDate
     * @param endDate
     * @param projectName
     * @return
     */
    @GetMapping("/projectName")
    public ApiResult<List<PmWorkContent>> pageList(@RequestParam(value = "startDate", required = false) String startDate,
                                                   @RequestParam(value = "endDate", required = false) String endDate,
                                                   @RequestParam(value = "projectName", required = false) String projectName,
                                                   @RequestParam(value = "projectAscription", required = false, defaultValue = "1") Integer projectAscription,
                                                   @RequestParam(value = "submitterId", required = false) Long submitterId) {
        return ApiResult.success(pmWorkContentService.projectQuery(startDate, endDate, projectName, projectAscription, submitterId));
    }

    /**
     * 报告记录
     *
     * @param startDate
     * @param endDate
     * @param projectName
     * @param reportId
     * @param projectAscription
     * @return
     */
    @GetMapping("/reportRecord")
    public ApiResult<List<PmWorkReport>> reportRecord(@RequestParam(value = "startDate", required = false) String startDate,
                                                      @RequestParam(value = "endDate", required = false) String endDate,
                                                      @RequestParam(value = "projectName", required = false) String projectName,
                                                      @RequestParam(value = "reportId", required = false) Integer reportId,
                                                      @RequestParam(value = "projectAscription", required = false, defaultValue = "1") Integer projectAscription) {
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
            startDate = LocalDate.now().toString();
            endDate = LocalDate.now().toString();
        }
        return ApiResult.success(pmWorkContentService.workReportQuery(startDate, endDate, projectName, projectAscription, reportId));
    }

    /**
     * 报告详情分页列表
     *
     * @param projectAscription 1:我负责；2：我收到；3：我发出
     * @param pageNum           页数
     * @param pageSize          页大小
     * @return
     */
    @GetMapping("/page")
    public ApiResult<CommonPage<PmWorkReport>> page(@RequestParam(value = "projectAscription", required = false, defaultValue = "-1") Integer projectAscription,
                                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "reportId", required = false) Integer reportId,
                                                    @RequestParam(value = "startDate", required = false) String startDate,
                                                    @RequestParam(value = "endDate", required = false) String endDate,
                                                    @RequestParam(value = "projectId", required = false) Integer projectId,
                                                    @RequestParam(value = "submitterId", required = false) Long submitterId,
                                                    @RequestParam(value = "upOrDown", required = false) Integer upOrDown,
                                                    @RequestParam(value = "slideSum", required = false) Integer slideSum,
                                                    @RequestParam(value = "submitDate", required = false) String submitDate) {
        return ApiResult.success(pmWorkContentService.reportPage(projectAscription, pageNum, pageSize, reportId, startDate, endDate, projectId, submitterId, upOrDown, slideSum, submitDate));
    }

    /**
     * 上下滑动
     * @param upOrDown 0:上滑；1：下滑
     * @param slideSum 滑动数量
     * @param submitDate 提交时间
     * @return
     */
    @GetMapping("/slide")
    public ApiResult<PmWorkReportSlideVO> slide(@RequestParam(value = "upOrDown", required = false) Integer upOrDown,
                                                @RequestParam(value = "slideSum", required = false) Integer slideSum,
                                                @RequestParam(value = "submitDate") LocalDateTime submitDate){
        return ApiResult.success(pmWorkContentService.slide(upOrDown, slideSum, submitDate));
    }

    /**
     * 工时统计-新
     * @return
     */
    @PostMapping("/workHourStatisticNew")
    public ApiResult<List<Object>> workHourStatistic(@RequestBody PmWorkHourStatisticRequestVO requestVO) {
        return ApiResult.success(pmWorkContentService.workHourStatisticNew(requestVO));
    }

    //回退代码
    @GetMapping("/workHourStatistic")
    public ApiResult<List<Object>> workHourStatistic(@RequestParam(value = "userId", required = false) Long userId,
                                                     @RequestParam(value = "projectId", required = false) Integer projectId,
                                                     @RequestParam(value = "filter", required = false, defaultValue = "1") Integer filter,
                                                     @RequestParam(value = "startDate", required = false) String startDate,
                                                     @RequestParam(value = "endDate", required = false) String endDate,
                                                     @RequestParam(value = "workerOrProject", required = false, defaultValue = "1") Integer workerOrProject) {
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
            startDate = LocalDate.now().with(TemporalAdjusters.firstDayOfYear()).toString();
            endDate = LocalDate.now().with(TemporalAdjusters.lastDayOfYear()).toString();
        }
        return ApiResult.success(pmWorkContentService.workHourStatistic(userId, projectId, filter, startDate, endDate, workerOrProject));
    }

/**
 * 工时明细导出
 *
 * @param response
 */
    @Log(title = "导出工时明细", businessType = BusinessType.EXPORT)
    @PostMapping("/workHourStatisticExport")
    public void export(HttpServletResponse response, @RequestBody PmWorkHourStatisticRequestVO requestVO){
        List<WorkTimeExportVO> list = pmWorkContentService.workHourStatisticExport(requestVO);
        ExcelUtil<WorkTimeExportVO> util = new ExcelUtil<WorkTimeExportVO>(WorkTimeExportVO.class);
        util.exportExcel(response, list, "工时明细", "工时明细");
    }

    /**
     * 工时导出
     *
     * @param response
     */
    @Log(title = "导出工时统计", businessType = BusinessType.EXPORT)
    @PostMapping("/workHourExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "userId", required = false) Long userId,
                       @RequestParam(value = "projectId", required = false) Integer projectId,
                       @RequestParam(value = "filter", required = false, defaultValue = "1") Integer filter,
                       @RequestParam(value = "startDate", required = false) String startDate,
                       @RequestParam(value = "endDate", required = false) String endDate,
                       @RequestParam(value = "workerOrProject", required = false, defaultValue = "1") Integer workerOrProject) throws IOException {
        List<WorkTimeExportTwoVO> list = pmWorkContentService.workHourExport(userId, projectId, filter, startDate, endDate, workerOrProject);
        ExcelUtil<WorkTimeExportTwoVO> util = new ExcelUtil<WorkTimeExportTwoVO>(WorkTimeExportTwoVO.class);
        util.exportExcel(response, list, "工时统计", "工时统计");
    }

}

