package com.yuchen.saas.park.service.controller.approve;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.approve.SaasGenericConfig;
import com.yuchen.saas.park.api.entity.approve.SaasGenericConfigAddReq;
import com.yuchen.saas.park.api.entity.approve.SaasGenericConfigUpdateReq;
import com.yuchen.saas.park.service.service.approve.ISaasGenericConfigService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: saas通用配置信息
 * @author: 洪铭铭
 * @time: 2024/5/27 16:36
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/saasGenericConfig")
public class SaasGenericConfigController {

    @Autowired
    private final ISaasGenericConfigService saasGenericConfigService;
    /**
     * @description: 获取全部配置
     * @param
     * @return: org.nest.springwrap.core.tool.api.R<java.util.List<com.yuchen.saas.park.api.entity.approve.SaasGenericConfig>>
     * @author: 洪铭铭
     * @time: 2024/5/27 17:17
     */
    @PostMapping("/getAll")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取全部配置", notes = "获取全部配置")
    public R<List<SaasGenericConfig>> getAll() {
        return R.data(saasGenericConfigService.getAll());
    }
    /**
     * @description: 新增配置
     * @param list
     * @return: org.nest.springwrap.core.tool.api.R
     * @author: 洪铭铭
     * @time: 2024/5/30 15:39
     */
    @PostMapping("/add")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增配置", notes = "新增配置")
    public R add(@Valid @RequestBody List<SaasGenericConfigAddReq> list) {
        List<SaasGenericConfig> configs = new ArrayList<>();
        for (SaasGenericConfigAddReq req : list) {
            SaasGenericConfig config = new SaasGenericConfig();
            BeanUtils.copyProperties(req, config);
            configs.add(config);
        }

        return R.status(saasGenericConfigService.saveBatch(configs));
    }

    /**
     * @description: 更新配置
     * @param list
     * @return: org.nest.springwrap.core.tool.api.R
     * @author: 洪铭铭
     * @time: 2024/5/30 15:39
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "更新配置", notes = "更新配置")
    public R update(@Valid @RequestBody List<SaasGenericConfigUpdateReq> list) {
        List<SaasGenericConfig> configs = new ArrayList<>();
        for (SaasGenericConfigUpdateReq req : list) {
            SaasGenericConfig config = new SaasGenericConfig();
            BeanUtils.copyProperties(req, config);
            configs.add(config);
        }
        return R.status(saasGenericConfigService.saveOrUpdateBatch(configs));
    }

    @GetMapping("/getConfigValue")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "获取配置", notes = "获取配置")
    public R<SaasGenericConfig> getConfigValue(@RequestParam("param") String param) {
        return R.data(saasGenericConfigService.getConfigValue(param));
    }


}
