package com.usky.pm.controller.web;


import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.pm.domain.PmTimeConf;
import com.usky.pm.service.PmTimeConfService;
import com.usky.pm.service.vo.PmSubmitCountResponseVO;
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.LocalDateTime;
import java.time.LocalTime;

/**
 * <p>
 * 工作报告提交时间配置表 前端控制器
 * </p>
 *
 * @author fu
 * @since 2025-01-10
 */
@RestController
@RequestMapping("/pmTimeConf")
public class PmTimeConfController {

    @Autowired
    private PmTimeConfService pmTimeConfService;

    /**
     * 提交统计
     * @param submitDate 统计日期
     * @return 统计结果
     */
    @GetMapping("/submitCount")
    public PmSubmitCountResponseVO submitCount(@RequestParam(value = "submitDate", required = false) String submitDate) {
        if (submitDate == null) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime today1800 = LocalDateTime.of(now.toLocalDate(), LocalTime.of(18, 0));
            if (now.isBefore(today1800)) {
                submitDate = now.toLocalDate().minusDays(1).toString();
            } else {
                submitDate = now.toLocalDate().toString();
            }
        }
        return pmTimeConfService.submitCount(submitDate);
    }

    /**
     * 提交记录分页
     * @param queryType 查询类型（0：按时提交，1：迟交，2：未提交）
     * @param submitDate 提交日期
     * @param reportId 报告id
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 分页结果
     */
    @GetMapping("/submitPage")
    public CommonPage<Object> submitPage(@RequestParam(value = "queryType", required = false, defaultValue = "0") Integer queryType,
                                         @RequestParam(value = "submitDate", required = false) String submitDate,
                                         @RequestParam(value = "reportId", required = false) Integer reportId,
                                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (StringUtils.isBlank(submitDate)) {
            submitDate = LocalDate.now().toString();
        }
        return pmTimeConfService.submitPage(submitDate, queryType, reportId, pageNum, pageSize);
    }

    /**
     * 获取配置时间
     * @return 配置时间
     */
    @GetMapping("/timeQuery")
    public PmTimeConf timeQuery() {
        Integer tenantId = SecurityUtils.getTenantId();
        return pmTimeConfService.getTimeConf(tenantId);
    }

    /**
     * 添加配置时间
     * @param pmTimeConf 配置时间
     */
    @Log(title = "新增配置时间", businessType = BusinessType.INSERT)
    @PostMapping("/addTimeConf")
    public void addTimeConf(@RequestBody PmTimeConf pmTimeConf) {
        pmTimeConfService.addOrUpdateTimeConf(pmTimeConf);
    }

    /**
     * 修改配置时间
     * @param pmTimeConf 配置时间
     */
    @Log(title = "修改配置时间", businessType = BusinessType.UPDATE)
    @PutMapping("/updateTimeConf")
    public void updateTimeConf(@RequestBody PmTimeConf pmTimeConf) {
        pmTimeConfService.addOrUpdateTimeConf(pmTimeConf);
    }

    /**
     * 打开或关闭提醒
     * @param isOpen 是否开启 0：关闭 1：开启
     * @param id 配置id
     */
    @PutMapping("/isOpen")
    public void isOpen(@RequestParam(value = "isOpen") Integer isOpen,
                       @RequestParam(value = "id") Integer id) {
        pmTimeConfService.isOpen(isOpen, id);
    }

}

