package com.dayou.controller;

import com.dayou.utils.EnumFactory;
import com.dayou.vo.BrokerageConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IGlobalConfigService;
import com.dayou.entity.GlobalConfig;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 全局配置
 *
 * @author wucl
 * @since 2023-01-13
 */
@RestController
@RequestMapping("globalConfig")
@Slf4j
public class GlobalConfigController extends BaseController {
    @Autowired
    private IGlobalConfigService globalConfigService;

    /**
    * 全局配置列表
    */
    @GetMapping("")
    public RestResponse<Page<GlobalConfig>> page(GlobalConfig globalConfig, Page page){
        Page<GlobalConfig> pages=globalConfigService.selectPage(page,globalConfig);
        return RestResponse.data(pages);
    }

    /**
     * 全局配置详情
     */
    @GetMapping("/{id}")
    public RestResponse<GlobalConfig> detail(@PathVariable Long id){
        GlobalConfig xGlobalConfig =globalConfigService.detail(id);
        return RestResponse.data(xGlobalConfig);
     }

    /**
     * 全局配置新增
     */
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody GlobalConfig globalConfig) {
        Boolean ret = globalConfigService.add(globalConfig);
        return RestResponse.data(ret);
    }

    /**
     * 全局配置更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody GlobalConfig globalConfig) {
        Boolean ret = globalConfigService.update(globalConfig);
        return RestResponse.data(ret);
    }

    /**
     * 全局配置删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = globalConfigService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 全局配置下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<GlobalConfig> lambdaQueryWrapper = new LambdaQueryWrapper<GlobalConfig>()
        .select(GlobalConfig::getId,GlobalConfig::getField);
        List<GlobalConfig> list= globalConfigService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 获取所有枚举
     *
     * @return
     */
    @GetMapping("/getAllEnum")
    public RestResponse<Map<String, List<String>>> getAllEnum() {
        Map<String, List<String>> map = EnumFactory.getAllEnum();
        return RestResponse.data(map);
    }

    /**
     * 根据key获取枚举列表
     *
     * @param key
     * @return
     */
    @GetMapping("/getEnumByKey")
    public RestResponse<List<String>> getEnumByKey(String key) {
        List<String> list = EnumFactory.getEnumByKey(key);
        return RestResponse.data(list);
    }
}

