package com.xdap.self_development.controller;

import com.definesys.mpaas.common.http.Response;
import com.xdap.self_development.common.MyPageResult;
import com.xdap.self_development.controller.form.ParameterPageForm;
import com.xdap.self_development.controller.form.UpdateParameterForm;
import com.xdap.self_development.domain.pojo.ParameterPojo;
import com.xdap.self_development.domain.view.TemplateInfoView;
import com.xdap.self_development.exception.CommonException;
import com.xdap.self_development.service.ParameterService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 模板参数控制器
 * 提供模板参数相关的功能，包括参数模糊搜索、维护参数等
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/parameter")
public class ParameterController {
    @Resource
    private ParameterService parameterService;

    /**
     * 参数的模糊搜索
     *
     * @param form          参数页面表单
     * @return 按条件筛选的参数分页结果
     */
    @GetMapping("/getParameterByCondition")
    public Response getParameterByCondition(
			@Valid ParameterPageForm form,
			BindingResult bindingResult
	) {
		if (bindingResult.hasErrors()) {
			bindingResult.getFieldErrors().forEach(error -> {
				throw new CommonException(error.getDefaultMessage());
			});
		}
		MyPageResult<ParameterPojo> list = parameterService.getParameterByCondition(form);
        return Response.ok().data(list);
    }


	/**
     * 维护参数
     *
     * @param form          更新参数表单
     * @return 操作结果响应
     */
    @PostMapping("/maintenanceParameters")
    public Response maintenanceParameters(
            @Valid @RequestBody UpdateParameterForm form,
			BindingResult bindingResult
    ) {
		if (bindingResult.hasErrors()) {
			bindingResult.getFieldErrors().forEach(error -> {
				throw new CommonException(error.getDefaultMessage());
			});
		}
		TemplateInfoView templateRowIdForm = parameterService.maintenanceParameters(form);
		return Response.ok().setMessage("维护成功").data(templateRowIdForm);
    }
}
