package cn.pluss.platform.controller.encrypt;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.encrypt.EncryptKeyService;
import cn.pluss.platform.entity.EncryptKey;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 商务密钥存储表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2021-10-28
 */
@Controller
@RequestMapping("/encryptKey")
public class EncryptKeyController {

    @Autowired
    private EncryptKeyService encryptKeyService;

    @GetMapping("/index")
    public String index() {
        return "encrypt/index";
    }

    @GetMapping("/detail")
    public String detail() {
        return "encrypt/detail";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<Page<EncryptKey>> page(Page<EncryptKey> page, EncryptKey condition, OrderItem orderItem) {
        if (orderItem != null) {
            page.addOrder(orderItem);
        } else {
            page.addOrder(OrderItem.desc("version"));
            page.addOrder(OrderItem.desc("createTime"));
        }

        page = encryptKeyService.page(page, Wrappers.query(condition));

        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("/save")
    public Result<Object> save(EncryptKey encryptKey) {
        encryptKeyService.save(encryptKey);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/enable")
    public Result<Object> enable(String id) {
        encryptKeyService.enable(id);
        return ResultGenerator.genSuccessResult();
    }
}
