package com.xdap.self_development.controller;

import com.definesys.mpaas.common.http.Response;
import com.xdap.self_development.controller.form.*;
import com.xdap.self_development.domain.dto.ChartCommonParameterDTO;
import com.xdap.self_development.exception.CommonException;
import com.xdap.self_development.service.CommonParameterService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 常用参数控制器
 * 提供常用参数相关的功能，包括子系统和参数查询、机型参数常用设置、数据分析常用设置等
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/common")
public class CommonParameterController {
    @Resource
    private CommonParameterService commonParameterService;

    /**
     * 获取全部子系统和参数
     *
     * @return 子系统和参数项列表
     */
    @GetMapping("/getSystemAndPara")
    public Response getSystemAndPara() {
        List<ModelCommonItem> list = commonParameterService.getSystemAndPara();
        return Response.ok().data(list);
    }

    /**
     * 查询机型参数常用
     *
     * @param userId 用户ID
     * @return 机型常用参数列表
     */
    @GetMapping("/getModelCommon")
    public Response getModelCommonParameter(
            @NotBlank @RequestParam String userId
    ) {
        List<ModelCommon> list = commonParameterService.getModelCommonParameter(userId);
        return Response.ok().data(list);
    }
    /**
     * 新增机型参数常用
     *
     * @param form          机型参数插入表单
     * @return 操作结果响应
     */
    @PostMapping("/insertModelCommon")
    public Response insertModelCommonParameter(
            @Valid @RequestBody ModelCommonParameterInsertForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        commonParameterService.insertModelCommonParameter(form);
        return Response.ok().setMessage("新增成功");
    }
    /**
     * 修改机型参数常用
     *
     * @param form          机型参数更新表单
     * @return 操作结果响应
     */
    @PostMapping("/updateModelCommon")
    public Response updateModelCommonParameter(
            @Valid @RequestBody ModelCommonParameterUpdateForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        commonParameterService.updateModelCommonParameter(form);
        return Response.ok().setMessage("修改成功");
    }

/*-----------------------------------------------------------------*/

    /**
     * 查询数据分析常用
     *
     * @param userId 用户ID
     * @return 分析数据常用参数列表
     */
    @GetMapping("/getAnalysisCommonParameter")
    public Response getAnalysisCommonParameter(
            @NotBlank @RequestParam String userId
    ) {
        List<AnalysisCommonParameterInsertForm> list = commonParameterService.getAnalysisCommonParameter(userId);
        return Response.ok().data(list);
    }

    /**
     * 新增数据分析常用
     *
     * @param form          分析参数插入表单
     * @return 操作结果响应
     */
    @PostMapping("/insertAnalysisCommon")
    public Response insertAnalysisCommonParameter(
            @Valid @RequestBody AnalysisCommonParameterInsertForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        commonParameterService.insertAnalysisCommonParameter(form);
        return Response.ok().setMessage("新增成功");
    }

    /**
     * 修改数据分析常用
     *
     * @param form          分析参数更新表单
     * @return 操作结果响应
     */
    @PostMapping("/updateAnalysisCommon")
    public Response updateAnalysisCommonParameter(
            @Valid @RequestBody AnalysisCommonParameterUpdateForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        commonParameterService.updateAnalysisCommonParameter(form);
        return Response.ok().setMessage("修改成功");
    }

    //---------------------------------------------------

    /**
     * 获取图表常用参数
     *
     * @param userId 用户ID
     * @return 图表常用参数列表
     */
    @GetMapping("/getChartCommonParameter")
    public Response getChartCommonParameter(
            @NotBlank @RequestParam String userId
    ) {
        List<ChartCommonParameterDTO> list = commonParameterService.getChartCommonParameter(userId);
        return Response.ok().data(list);
    }

    /**
     * 新增图表常用参数
     *
     * @param form 图表参数插入表单
     * @return 操作结果响应
     */
    @PostMapping("/insertChartCommon")
    public Response insertChartCommonParameter(
            @Valid @RequestBody ChartCommonParameterInsertForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        commonParameterService.insertChartCommonParameter(form);
        return Response.ok().setMessage("新增成功");
    }

    /**
     * 修改图表常用参数
     *
     * @param form 图表参数更新表单
     * @return 操作结果响应
     */
    @PostMapping("/updateChartCommon")
    public Response updateChartCommonParameter(
            @Valid @RequestBody ChartCommonParameterUpdateForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        commonParameterService.updateChartCommonParameter(form);
        return Response.ok().setMessage("修改成功");
    }
}
