package com.dxhy.admin.sys.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.dxhy.admin.sys.entity.SysConfigEntity;
import com.dxhy.admin.sys.service.SysConfigService;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.R;
import com.dxhy.common.utils.ValidatorUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 系统配置信息
 *
 * @author jiaohongyang
 */
@RestController
@RequestMapping("/sys/config")
@Slf4j
public class SysConfigController extends AbstractController {
    @Resource
    private SysConfigService sysConfigService;

    /**
     * 所有配置列表
     */
    @RequestMapping("/list")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        return ResponseEntity.ok(R.ok().put("page", sysConfigService.queryPage(params)));
    }

    /**
     * 配置信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public ResponseEntity<?> info(@PathVariable("id") Long id) {
        SysConfigEntity config = sysConfigService.queryConfigById(id);

        return ResponseEntity.ok(R.ok().put("config", config));
    }

    /**
     * 保存配置
     */
    @RequestMapping("/save")
    public ResponseEntity<?> save(@RequestBody SysConfigEntity config) {
        try {

            ValidatorUtils.validateEntity(config);
            SysConfigEntity queryCode = sysConfigService.queryCode(config.getParamKey());
            if (queryCode != null) {
                return ResponseEntity.ok(R.error("保存失败，"));
            }
            sysConfigService.saveConfig(config);

            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    /**
     * 修改配置
     */
    @RequestMapping("/update")
    public ResponseEntity<?> update(@RequestBody SysConfigEntity config) {
        try {
            ValidatorUtils.validateEntity(config);
            SysConfigEntity queryCode = sysConfigService.queryCode(config.getParamKey());
            if (queryCode != null) {
                return ResponseEntity.ok(R.error("保存失败，"));
            }
            sysConfigService.update(config);
            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    /**
     * 删除配置
     */
    @RequestMapping("/delete")
    public ResponseEntity<?> delete(Long id) {
        try {
            sysConfigService.delete(id);

            return ResponseEntity.ok(R.ok("删除成功"));
        } catch (Exception e) {
            return ResponseEntity.ok(R.error("删除失败"));
        }
    }

    /**
     * 根据key 查询参数
     */
    @RequestMapping("/queryCode")
    public ResponseEntity<?> queryCode(String code) {
        SysConfigEntity queryCode = sysConfigService.queryCode(code);
        return ResponseEntity.ok(R.ok().put("config", queryCode));
    }

}
