package com.codvision.base.controller.manager;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.base.entity.SysConfig;
import com.codvision.base.service.ISysConfigService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统配置 管理模块
 *
 * @author lingee
 * @date 2023-11-09
 */
@RestController
@RequestMapping("/v1/manager/sysConfig")
@Api(tags = "平台管理端-系统配置模块")
public class ManagerSysConfigController {

    @Autowired
    private ISysConfigService configService;

    @ApiOperation(value = "获取系统配置所有列表", notes = "获取系统配置所有列表接口")
    @PostMapping("/findAll")
    public ResponseEntity<Map<String, List<SysConfig>>> findAll(
            @ApiParam(value = "大类") @RequestParam(value = "largeCategory", required = false) String largeCategory) {
        Map<String, List<SysConfig>> map = configService.getListByLargeCategory(largeCategory);
        return new ResponseEntity<Map<String, List<SysConfig>>>().ok().data(map);
    }

    @ApiOperation(value = "获取系统配置详情", notes = "获取系统配置详情接口")
    @PostMapping("/detail")
    public ResponseEntity<SysConfig> getByKey(@ApiParam(value = "关键字k", required = true) @RequestParam String k) {
        SysConfig sysConfig = configService.getByKey(k);
        return new ResponseEntity<SysConfig>().ok().data(sysConfig);
    }

    @ApiOperation(value = "新增系统配置", notes = "新增系统配置接口")
    @PostMapping("/add")
    @RequiresPermissions("sys_config_add")
    public ResponseEntity<String> addConfig(@Valid @RequestBody SysConfig sysConfig) {
        configService.save(sysConfig);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "删除系统配置", notes = "删除系统配置接口")
    @PostMapping("/delete")
    @RequiresPermissions("sys_config_del")
    public ResponseEntity<String> deleteById(@ApiParam(value = "系统配置ID", required = true) @RequestParam List<Long> ids) {
        configService.removeBatchByIds(ids);
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "更新系统配置", notes = "编辑接系统配置接口")
    @PostMapping("/update")
    @RequiresPermissions("sys_config_edit")
    public ResponseEntity<String> updateConfig(@ApiParam(value = "系统配置列表", required = true) @RequestBody List<SysConfig> sysConfigs) {
        configService.updateAll(sysConfigs);
        return new ResponseEntity<String>().ok().message("编辑成功");
    }

    @PostMapping("/getListByKeys")
    @ApiOperation(value = "根据k值集合获取配置列表", notes = "根据k值集合获取配置列表接口")
    public ResponseEntity<List<SysConfig>> getListByKeys(@ApiParam(value = "签名", required = true) @RequestParam(value = "sign") String sign,
                                                         @ApiParam(value = "时间戳") @RequestParam(value = "timestamp") Long timestamp,
                                                         @RequestParam String keys) {
        ResponseEntity<List<SysConfig>> responseEntity = new ResponseEntity<>();

        String nSign = SecureUtil.md5("FJ99A0Nf3vg4dJ3dwS65eis6KYzQ4KeL" + timestamp);
        if (!sign.equals(nSign)) {
            throw new BusinessException(10000, "签名sign校验失败");
        }

        return responseEntity.data(configService.getListByKeys(Arrays.asList(keys.split(","))));
    }

    @ApiOperation(value = "获取所有系统配置的大类集合", notes = "获取所有系统配置的大类集合接口")
    @PostMapping("/getLargeCategory")
    public ResponseEntity<Set<String>> getLargeCategory() {
        ResponseEntity<Set<String>> responseEntity = new ResponseEntity<>();
        Set<String> set = configService.list()
                .stream().map(SysConfig::getLargeCategory).filter(StrUtil::isNotBlank).collect(Collectors.toSet());
        return responseEntity.data(set);
    }
}
