package com.keyware.shandan.system.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.system.entity.SysFormConfig;
import com.keyware.shandan.system.service.SysFormConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 系统表单配置模块前端控制类
 *
 * @author GuoXin
 * @date 2023/8/23
 */
@Slf4j
@RestController
@RequestMapping("/sys/form/config")
public class SysFormConfigController {
    private final SysFormConfigService formConfigService;

    public SysFormConfigController(SysFormConfigService formConfigService) {
        this.formConfigService = formConfigService;
    }


    @GetMapping("/page/index")
    public ModelAndView indexPage(ModelAndView mav) {
        mav.setViewName("sys/form/formConfig");
        return mav;
    }


    @GetMapping("/api/get/{id}")
    public Result<SysFormConfig> getById(@PathVariable String id) {
        return Result.of(formConfigService.getById(id));
    }

    @GetMapping("/api/list/{formType}")
    public Result<List<SysFormConfig>> list(@PathVariable String formType) {
        QueryWrapper<SysFormConfig> query = new QueryWrapper<>();
        query.eq("FORM_TYPE", formType);
        return Result.of(formConfigService.list(query));
    }

    @PostMapping(value = "/api/save")
    public Result<Object> save(String params) {
        JSONArray jsonArray = JSONUtil.parseArray(params);
        List<SysFormConfig> list = jsonArray.toList(SysFormConfig.class);
        return Result.of(formConfigService.saveOrUpdateBatch(list));
    }
}
