package com.bootdo.data.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.data.domain.CaInfo;
import com.bootdo.data.entity.TfInfoEnterprise;
import com.bootdo.data.service.CaInfoService;
import com.bootdo.data.service.TfInfoEnterpriseService;
import com.bootdo.data.util.ApplyCertUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * ca证书 控制层
 *
 * @author Mr.xiBel
 * @since 2022-05-20 15:51:40
 */
@Controller
@RequestMapping("/data/caInfo")
public class CaInfoController {
    /**
     * 服务对象
     */
    @Resource
    private TfInfoEnterpriseService tfInfoEnterpriseService;

    /**
     * 证书信息 服务对象
     */
    @Resource
    private CaInfoService caInfoService;

    @Log("访问证书信息")
    @GetMapping()
    @RequiresPermissions("caInfo:info:list")
    String dataInfo() {
        return "data/caInfo/dataInfo";
    }

    /**
     * 分页获取数据
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("caInfo:info:list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<CaInfo> dataInfoList = caInfoService.queryPage(query);
        int total = caInfoService.queryPageCount(query);
        return new PageUtils(dataInfoList, total);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id
     * @param model
     * @return
     */
    @Log("访问证书信息-详情")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("caInfo:info:update")
    String edit(@PathVariable("id") Integer id, Model model) {
        CaInfo data = this.caInfoService.queryById(id);
        model.addAttribute("data", data);
        return "data/caInfo/edit";
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/edit")
    @RequiresPermissions("caInfo:info:update")
    public CaInfo selectOne(Integer id) {
        return this.caInfoService.queryById(id);
    }


    @Log("删除证书信息")
    @PostMapping("/remove")
    @RequiresPermissions("caInfo:info:remove")
    @ResponseBody
    public R disable(Integer id) throws Exception {
        caInfoService.caStatusUpdate(id, 0);
        return R.ok();
    }


    /**
     * 证书申请 审核
     * 这里 审核通过时, 需要生成证书
     * 具体逻辑 参见实现
     *
     * @param id
     * @param caStatus
     * @return
     */

    @Log("更新证书信息状态")
    @PostMapping("/application")
    @RequiresPermissions("caInfo:info:status")
    @ResponseBody
    public R application(Integer id, Integer caStatus) throws Exception {
        caInfoService.caStatusUpdate(id, caStatus);
        return R.ok();
    }
}