package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.dto.ApiGroupDTO;
import com.sunyard.chsm.dto.CryptoServiceDTO;
import com.sunyard.chsm.enums.ApiFunEnum;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.service.CryptoServiceService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 密码服务管理接口
 *
 * @author liulu
 * @since 2024/10/15
 */
@Slf4j
@RestController
@RequestMapping("/crypto/service")
public class CryptoServiceController {

    @Resource
    private CryptoServiceService cryptoServiceService;

    /**
     * 查询密码服务接口分组列表
     *
     * @return
     */
    @GetMapping("/apiGroupList")
    public R<List<ApiGroupDTO>> getApiGroupList() {

        List<ApiGroupDTO> res = Arrays.stream(ApiFunEnum.values())
                .collect(Collectors.groupingBy(ApiFunEnum::getGroup))
                .entrySet().stream()
                .map(it -> {
                    ApiGroupDTO group = new ApiGroupDTO();
                    group.setGroupCode(it.getKey().getCode());
                    group.setGroupName(it.getKey().getName());
                    group.setApiList(it.getValue().stream().map(it2 -> {
                                ApiGroupDTO.Api api = new ApiGroupDTO.Api();
                                api.setCode(it2.getCode());
                                api.setName(it2.getName());
                                return api;
                            })
                            .collect(Collectors.toList()));
                    return group;
                })
                .collect(Collectors.toList());

        return R.data(res);
    }

    /**
     * 分页查询密码服务列表
     *
     * @param query 查询条件
     * @return 密码服务列表
     */
    @GetMapping("/pageList")
    public R<Page<CryptoServiceDTO.CSView>> servicePageList(CryptoServiceDTO.Query query) {

        Page<CryptoServiceDTO.CSView> page = cryptoServiceService.selectPageList(query);

        return R.data(page);
    }

    /**
     * 创建密码服务
     *
     * @param save 参数
     * @return 密码服务id
     */
    @PostMapping
    @AuditControllerLog(description = "创建密码服务", operateType = AuditLogConst.ADD)
    public R<String> saveService(@Valid @RequestBody CryptoServiceDTO.CSSave save) {
        Long id = cryptoServiceService.save(save);
        return R.data(String.valueOf(id));
    }

    /**
     * 修改密码服务
     *
     * @param update 参数
     * @return 密码服务id
     */
    @PutMapping
    @AuditControllerLog(description = "修改密码服务", operateType = AuditLogConst.UPDATE)
    public R<Void> updateService(@Valid @RequestBody CryptoServiceDTO.CSSave update) {
        Assert.notNull(update.getId(), "密码服务id不能为空");
        cryptoServiceService.update(update);
        return R.ok();
    }

    /**
     * 启用密码服务
     *
     * @param id id
     * @return
     */
    @AuditControllerLog(description = "启用密码服务", operateType = AuditLogConst.UPDATE)
    @PostMapping("/enable")
    public R<Void> enable(Long id) {
        Assert.notNull(id, "应用id不能为空");
        cryptoServiceService.enable(id);
        return R.ok();
    }

    /**
     * 停用密码服务
     *
     * @param id id
     * @return
     */
    @AuditControllerLog(description = "停用密码服务", operateType = AuditLogConst.UPDATE)
    @PostMapping("/disable")
    public R<Void> disable(Long id) {
        Assert.notNull(id, "应用id不能为空");
        cryptoServiceService.disable(id);
        return R.ok();
    }

    /**
     * 删除密码服务
     *
     * @param id 密码服务id
     * @return
     */
    @DeleteMapping
    @AuditControllerLog(description = "删除密码服务", operateType = AuditLogConst.DELETE)
    public R<Void> deleteService(Long id) {
        Assert.notNull(id, "密码服务id不能为空");
        cryptoServiceService.delete(id);
        return R.ok();
    }

}
