package com.ruoyi.web.controller.authority;


import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.SysSecretProject;
import com.ruoyi.system.mapper.SysSecretProjectMapper;
import com.ruoyi.web.controller.sm4.SM4Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/project/limits")
public class ProjectAuthorityController {

    @Autowired
    private SysSecretProjectMapper sysSecretProjectMapper;

    /**
     * 项目授权认证
     *
     * @param json
     * @return map
     * @throws ParseException
     */
    @PostMapping("/authority")
    public R authority(@RequestBody JSONObject json) throws ParseException {
        SM4Utils sm4 = new SM4Utils();
        Map map = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String secretKey = sm4.decryptData_CBC(json.get("secretKey").toString());
        SysSecretProject sysSecretProject = sysSecretProjectMapper.selectBaseBySecretKey(secretKey);
        Date now = new Date();
        Date date = dateFormat.parse(sysSecretProject.getOverdueDate().toString());
        long seconds = (date.getTime() - now.getTime()) / 1000;
        if (seconds > 0) {
            map.put("isOverdue", true);
        } else {
            map.put("isOverdue", false);
        }
        map.put("overdueDate", seconds);
        map.put("isSwitch", sysSecretProject.getIsSwitch().equals(true) ? 1 : 0);
        return R.ok(map);
    }

}
