package com.tiesheng.core.controller.config;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiesheng.annotation.token.TokenIgnore;
import com.tiesheng.core.pojos.dao.CoreConfigSystem;
import com.tiesheng.core.pojos.dto.PageDTO;
import com.tiesheng.core.pojos.dto.config.ConfigSystemDTO;
import com.tiesheng.core.service.CoreConfigService;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hao
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    CoreConfigService coreConfigService;


    /**
     * 系统配置列表
     *
     * @return
     */
    @GetMapping("/system/page")
    @TokenIgnore
    public ApiResp<List<CoreConfigSystem>> systemPage(PageDTO dto) {

        QueryWrapper<CoreConfigSystem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        dto.likeColumns(queryWrapper, "config_key", "remark");

        Page<CoreConfigSystem> page = dto.pageObj();
        coreConfigService.page(page, queryWrapper);

        return ApiResp.respOK(page.getRecords(), page.getTotal());
    }


    /**
     * 编辑配置
     *
     * @return
     */
    @PostMapping("/system/update")
    public ApiResp<String> systemUpdate(@RequestBody ConfigSystemDTO dto) {

        CoreConfigSystem configKey = coreConfigService.getOneByColumn("config_key", dto.getConfigKey());
        if (configKey == null) {
            throw new ApiException("该配置不存在，请检查");
        }
        configKey.setConfigVal(dto.getConfigVal());
        configKey.setRemark(dto.getRemark());
        configKey.setExtra(dto.getExtra());
        coreConfigService.updateById(configKey);

        return ApiResp.respOK("");
    }


}
