package com.schisandra.system.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;

import com.schisandra.system.application.convert.SchisandraSysConfigDTOConverter;
import com.schisandra.system.application.dto.SchisandraSysConfigDTO;
import com.schisandra.system.common.entity.Result;
import com.schisandra.system.domain.bo.SchisandraSysConfigBO;
import com.schisandra.system.domain.service.SchisandraSysConfigDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * controller
 *
 * @author landaiqing
 * @since 2024-05-11 22:45:55
 */
@RestController
@RequestMapping("/system/")
@Slf4j
public class SchisandraSysConfigController {

    @Resource
    private SchisandraSysConfigDomainService schisandraSysConfigDomainService;

    /**
     * 新增
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraSysConfigDTO schisandraSysConfigDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraSysConfigController.add.dto:{}", JSON.toJSONString(schisandraSysConfigDTO));
            }
            Preconditions.checkNotNull(schisandraSysConfigDTO.getId(), "id不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigName(), "名称不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigKey(), "参数键不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigValue(), "参数值不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getCreateBy(), "创建者不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigType(), "系统内置（0是 1否）不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getCreateDate(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getUpdateBy(), "更新者不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getUpdateDate(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getRemarks(), "备注信息不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getIsDeleted(), "是否删除 0未删除 1已删除不能为空");
            SchisandraSysConfigBO SchisandraSysConfigBO = SchisandraSysConfigDTOConverter.INSTANCE.convertDTOToBO(schisandraSysConfigDTO);
            return Result.ok(schisandraSysConfigDomainService.add(SchisandraSysConfigBO));
        } catch (Exception e) {
            log.error("SchisandraSysConfigController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增失败");
        }

    }

    /**
     * 修改
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraSysConfigDTO schisandraSysConfigDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraSysConfigController.update.dto:{}", JSON.toJSONString(schisandraSysConfigDTO));
            }
            Preconditions.checkNotNull(schisandraSysConfigDTO.getId(), "id不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigName(), "名称不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigKey(), "参数键不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigValue(), "参数值不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getCreateBy(), "创建者不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigType(), "系统内置（0是 1否）不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getCreateDate(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getUpdateBy(), "更新者不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getUpdateDate(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getRemarks(), "备注信息不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getIsDeleted(), "是否删除 0未删除 1已删除不能为空");
            SchisandraSysConfigBO schisandraSysConfigBO = SchisandraSysConfigDTOConverter.INSTANCE.convertDTOToBO(schisandraSysConfigDTO);
            return Result.ok(schisandraSysConfigDomainService.update(schisandraSysConfigBO));
        } catch (Exception e) {
            log.error("SchisandraSysConfigController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新信息失败");
        }

    }

    /**
     * 删除
     */
    @DeleteMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraSysConfigDTO schisandraSysConfigDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraSysConfigController.delete.dto:{}", JSON.toJSONString(schisandraSysConfigDTO));
            }
            Preconditions.checkNotNull(schisandraSysConfigDTO.getId(), "id不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigName(), "名称不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigKey(), "参数键不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigValue(), "参数值不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getCreateBy(), "创建者不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getConfigType(), "系统内置（0是 1否）不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getCreateDate(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getUpdateBy(), "更新者不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getUpdateDate(), "更新时间不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getRemarks(), "备注信息不能为空");
            Preconditions.checkNotNull(schisandraSysConfigDTO.getIsDeleted(), "是否删除 0未删除 1已删除不能为空");
            SchisandraSysConfigBO schisandraSysConfigBO = SchisandraSysConfigDTOConverter.INSTANCE.convertDTOToBO(schisandraSysConfigDTO);
            return Result.ok(schisandraSysConfigDomainService.delete(schisandraSysConfigBO));
        } catch (Exception e) {
            log.error("SchisandraSysConfigController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除信息失败");
        }

    }

    @PostMapping(value = "getConfigByKey")
    public Result<SchisandraSysConfigDTO> getConfigByKey(@RequestParam(value = "key") String key) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraSysConfigController.getConfigByKey.key:{}", key);
            }
            return Result.ok(schisandraSysConfigDomainService.getConfigByKey(key));
        } catch (Exception e) {
            return Result.fail("配置信息获取失败");
        }
    }

}
