package com.fs.company.controller.newAdv;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.fs.common.result.Result;
import com.fs.newAdv.constant.AdvConfigConstant;
import com.fs.newAdv.dto.res.AdvConfigRes;
import com.fs.system.domain.SysConfig;
import com.fs.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/adv/config")
public class AdvConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 删除站点
     */
    @GetMapping("/detail")
    public Result<AdvConfigRes> detail() {
        return Result.success(JSONUtil.toBean(sysConfigService.selectConfigByKey(AdvConfigConstant.ADV_CONFIG), AdvConfigRes.class));
    }

    /**
     * 新增更新配置
     */
    @PostMapping("/addOrUpdate")
    public Result<Integer> addOrUpdate(@RequestBody AdvConfigRes advConfigRes) {
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey(AdvConfigConstant.ADV_CONFIG);
        int flag = 0;
        if (ObjectUtil.isEmpty(sysConfig)) {
            sysConfig = new SysConfig();
            sysConfig.setConfigKey(AdvConfigConstant.ADV_CONFIG);
            sysConfig.setConfigValue(JSONUtil.toJsonStr(advConfigRes));
            flag = sysConfigService.insertConfig(sysConfig);
        } else {
            sysConfig.setConfigValue(JSONUtil.toJsonStr(advConfigRes));
            flag = sysConfigService.updateConfig(sysConfig);
        }
        return Result.success(flag);
    }
}
