package com.ruoyi.web.controller.system;

import cn.hutool.json.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysSecretProject;
import com.ruoyi.system.domain.dto.SysSecretProjectDTO;
import com.ruoyi.system.mapper.SysSecretProjectMapper;
import com.ruoyi.system.service.ISysSecretProjectService;
import com.ruoyi.web.utils.ClipboardUtil;
import com.ruoyi.web.utils.JWTExample;
import com.ruoyi.web.utils.RestUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 项目授权管理Controller
 *
 * @author ruoyi
 * @date 2023-11-21
 */
@RestController
@RequestMapping("/system/secret")
public class SysSecretProjectController extends BaseController {
    @Autowired
    private ISysSecretProjectService sysSecretProjectService;
    @Autowired
    private SysSecretProjectMapper sysSecretProjectMapper;


    /**
     * 查询项目授权管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysSecretProjectDTO sysSecretProjectDTO) {
        startPage();
        List<SysSecretProjectDTO> list = sysSecretProjectService.selectSysSecretProjectList(sysSecretProjectDTO);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前日期前七天的日期
        LocalDate previousDate = currentDate.minusDays(7);
        for (SysSecretProjectDTO var : list) {
            int comparisonResult = var.getOverdueDate().compareTo(currentDate);
            if (comparisonResult > 0) {
                var.setStatus(1L);
                sysSecretProjectMapper.updateSysSecretProject(var);
            } else if (comparisonResult < 0) {
                var.setStatus(2L);
                sysSecretProjectMapper.updateSysSecretProject(var);
            } else if (var.getOverdueDate().isAfter(previousDate) && var.getOverdueDate().isBefore(currentDate)) {
                var.setStatus(3L);
                sysSecretProjectMapper.updateSysSecretProject(var);
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取项目授权管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysSecretProjectService.selectSysSecretProjectById(id));
    }

    /**
     * 新增项目授权管理
     */
    @Log(title = "新增项目授权管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSecretProjectDTO sysSecretProjectDTO) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前日期前七天的日期
        LocalDate previousDate = currentDate.minusDays(7);
        long milliseconds = Duration.between(LocalDateTime.now(), sysSecretProjectDTO.getOverdueDate().atStartOfDay()).toMillis();
        JSONObject json = new JSONObject();
        json.set("projectId", sysSecretProjectDTO.getProjectId());
        json.set("projectName", sysSecretProjectDTO.getProjectName());
        json.set("overdueDate", sysSecretProjectDTO.getOverdueDate());
        String jwt = JWTExample.createJWT(json.toString(), milliseconds);
        sysSecretProjectDTO.setSecretKey(jwt);
        int comparisonResult = sysSecretProjectDTO.getOverdueDate().compareTo(currentDate);
        if (comparisonResult > 0) {
            sysSecretProjectDTO.setStatus(1L);
//            sysSecretProjectDTO.setIsSwitch(1L);
        } else if (comparisonResult < 0) {
            sysSecretProjectDTO.setStatus(2L);
        } else if (sysSecretProjectDTO.getOverdueDate().isAfter(previousDate) && sysSecretProjectDTO.getOverdueDate().isBefore(currentDate)) {
//            sysSecretProjectDTO.setIsSwitch(1L);
            sysSecretProjectDTO.setStatus(3L);
        }
        return toAjax(sysSecretProjectService.insertSysSecretProject(sysSecretProjectDTO));
    }

    /**
     * 修改项目授权管理
     */
    @Log(title = "修改项目授权管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSecretProjectDTO sysSecretProjectDTO) {
        if (sysSecretProjectDTO.getAuthorityType().equals("2")) {
            long milliseconds = Duration.between(LocalDateTime.now(), sysSecretProjectDTO.getOverdueDate().atStartOfDay()).toMillis();
            JSONObject json = new JSONObject();
            json.set("projectId", sysSecretProjectDTO.getProjectId());
            json.set("projectName", sysSecretProjectDTO.getProjectName());
            json.set("overdueDate", sysSecretProjectDTO.getOverdueDate());
            String jwt = JWTExample.createJWT(json.toString(), milliseconds);
            sysSecretProjectDTO.setSecretKey(jwt);
        }

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前日期前七天的日期
        LocalDate previousDate = currentDate.minusDays(7);
        int comparisonResult = sysSecretProjectDTO.getOverdueDate().compareTo(currentDate);
        if (comparisonResult > 0) {
            sysSecretProjectDTO.setStatus(1L);
//            sysSecretProjectDTO.setIsSwitch(1L);
        } else if (comparisonResult < 0) {
            sysSecretProjectDTO.setStatus(2L);
        } else if (sysSecretProjectDTO.getOverdueDate().isAfter(previousDate) && sysSecretProjectDTO.getOverdueDate().isBefore(currentDate)) {
//            sysSecretProjectDTO.setIsSwitch(1L);
            sysSecretProjectDTO.setStatus(3L);
        }
        return toAjax(sysSecretProjectService.updateSysSecretProject(sysSecretProjectDTO));
    }

    /**
     * 关闭项目授权管理
     */
    @Log(title = "关闭项目授权管理", businessType = BusinessType.UPDATE)
    @PutMapping("/closeAuthority")
    public AjaxResult closeAuthority(@RequestBody SysSecretProjectDTO sysSecretProjectDTO) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前日期前七天的日期
        LocalDate previousDate = currentDate.minusDays(7);
        sysSecretProjectDTO.setOverdueDate(previousDate);
        sysSecretProjectDTO.setStatus(2L);
//        sysSecretProjectDTO.setIsSwitch(1L);
        RestUtil.get(sysSecretProjectService.selectSysSecretProjectAddress(sysSecretProjectDTO));
        return toAjax(sysSecretProjectService.updateSysSecretProject(sysSecretProjectDTO));
    }

    /**
     * 删除项目授权管理
     */
    @Log(title = "删除项目授权管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysSecretProjectService.deleteSysSecretProjectByIds(ids));
    }

    /**
     * 复制项目授权管理
     */
    @PostMapping("/copy")
    public AjaxResult copySecretKey(@RequestBody SysSecretProject sysSecretProject) {
        String secretKey = sysSecretProject.getSecretKey();
        ClipboardUtil.copyToClipboard(secretKey);
        return AjaxResult.success("信息已复制");
    }


    /**
     * 修改是否校验状态
     */
    @Log(title = "修改是否校验状态", businessType = BusinessType.UPDATE)
    @PutMapping("/updateIsSwitch")
    public AjaxResult updateIsSwitch(@RequestBody SysSecretProjectDTO sysSecretProjectDTO) {
        return toAjax(sysSecretProjectService.updateIsSwitch(sysSecretProjectDTO));
    }

}
