package com.sunyard.chsm.controller;

import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.enums.KeyStatus;
import com.sunyard.chsm.model.Option;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.model.dto.KeyInfoDTO;
import com.sunyard.chsm.service.KeyInfoService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import com.sunyard.ssp.common.exception.SspwebException;
import org.springframework.web.bind.annotation.GetMapping;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 密钥管理公共接口
 *
 * @author liulu
 * @since 2024/10/23
 */
@RestController
@RequestMapping("/key/info")
public class KeyInfoController {

    @Resource
    private KeyInfoService keyInfoService;

    /**
     * 获取密钥状态选项
     */
    @GetMapping("/statusList")
    public R<List<Option>> getStatusList() {

        List<Option> options = Arrays.stream(KeyStatus.values())
                .map(it -> new Option(it.getCode(), it.getDesc()))
                .filter(it -> !KeyStatus.DESTROY.getCode().equals(it.getCode()))
                .collect(Collectors.toList());
        return R.data(options);
    }

    /**
     * 创建密钥
     *
     * @param save 参数
     * @return id
     */
    @PostMapping("/create")
    @AuditControllerLog(description = "创建密钥", operateType = AuditLogConst.ADD)
    public R<String> save(@Valid @RequestBody KeyInfoDTO.KeySave save) {
        Long id = keyInfoService.save(save);
        return R.data(String.valueOf(id));
    }

    /**
     * 更新密钥
     *
     * @param update 参数
     * @return id
     */
    @PostMapping("/update")
    @AuditControllerLog(description = "更新密钥", operateType = AuditLogConst.UPDATE)
    public R<Void> save(@Valid @RequestBody KeyInfoDTO.KeyUpdate update) {
        keyInfoService.update(update);
        return R.ok();
    }

    /**
     * 恢复密钥(文件上传)
     *
     * @param file 密钥备份文件
     * @return id
     */
    @PostMapping("/recovery")
    public R<String> recoveryKey(MultipartFile file) {
        try {
            String s = keyInfoService.recoveryKey(file.getInputStream());
            return R.ok(s);
        } catch (IOException e) {
            throw new SspwebException("获取文件内容异常");
        }
    }

    /**
     * 启用密钥
     *
     * @param param 密钥id
     * @return id
     */
    @PostMapping("/enable")
    @AuditControllerLog(description = "启用密钥", operateType = AuditLogConst.UPDATE)
    public R<Void> enableKey(@Valid @RequestBody KeyInfoDTO.IDs param) {
        keyInfoService.enableKey(param.getIds());
        return R.ok();
    }

    /**
     * 停用密钥
     *
     * @param param 密钥id
     * @return id
     */
    @PostMapping("/disable")
    @AuditControllerLog(description = "停用密钥", operateType = AuditLogConst.UPDATE)
    public R<Void> disableKey(@Valid @RequestBody KeyInfoDTO.IDs param) {
        keyInfoService.disableKey(param.getIds());
        return R.ok();
    }

    /**
     * 归档密钥
     *
     * @param param 密钥id
     * @return id
     */
    @PostMapping("/archive")
    @AuditControllerLog(description = "归档密钥", operateType = AuditLogConst.UPDATE)
    public R<Void> archiveKey(@Valid @RequestBody KeyInfoDTO.IDs param) {
        keyInfoService.archiveKey(param.getIds());
        return R.ok();
    }

    /**
     * 销毁密钥
     *
     * @param param 密钥id
     * @return id
     */
    @PostMapping("/destroy")
    @AuditControllerLog(description = "销毁密钥", operateType = AuditLogConst.DELETE)
    public R<Void> destroyKey(@Valid @RequestBody KeyInfoDTO.IDs param) {
        keyInfoService.destroyKey(param.getIds());
        return R.ok();
    }

}
