package com.keyware.shandan.system.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.common.util.StringUtils;
import com.keyware.shandan.system.entity.SysTheadConfig;
import com.keyware.shandan.system.service.SysTheadConfigService;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 前端数据表格表头配置前端控制器
 *
 * @author GuoXin
 * @date 2023/8/30
 */
@RestController
@RequestMapping("/sys/thead/config")
public class SysTheadConfigController {
    private final SysTheadConfigService theadConfigService;

    public SysTheadConfigController(SysTheadConfigService theadConfigService) {
        this.theadConfigService = theadConfigService;
    }

    @PostMapping(value = "/api/save")
    public Result<Object> save(String params, Authentication auth) {
        JSONArray jsonArray = JSONUtil.parseArray(params);
        List<SysTheadConfig> list = jsonArray.toList(SysTheadConfig.class);
        list.forEach(item->{
            if(!StringUtils.hasText(item.getId())){
                item.genId(auth.getName());
            }
        });
        return Result.of(theadConfigService.saveOrUpdateBatch(list));
    }
}
