package com.usky.iot.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.iot.domain.PmWorkContent;
import com.usky.iot.domain.PmWorkReport;
import com.usky.iot.service.PmWorkContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
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;

    @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 = "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.projectQuery(startDate, endDate, projectName, projectAscription, reportId));
    }



    @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));
    }

    @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));
    }
}

