package com.tofly.qualitycontrol.controller;

import com.tofly.common.core.entity.ResultRespone;
import com.tofly.qualitycontrol.entity.TimeConfig;
import com.tofly.qualitycontrol.service.ITimeConfigService;
import com.tofly.qualitycontrol.util.QualityControlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.tofly.qualitycontrol.common.QualityControlConstant.FAILED;
import static com.tofly.qualitycontrol.common.QualityControlConstant.SUCCESS;

/**
 * 时限配置接口控制器
 *
 * @author B
 */
@RestController
@RequestMapping("/timeConfig")
@Api(value = "时限配置接口", tags = "时限配置接口")
@RequiredArgsConstructor
public class TimeConfigController {

	private final ITimeConfigService service;

	@ApiOperation(value = "时限配置列表", notes = "", httpMethod = "GET")
	@GetMapping("/list")
	public ResultRespone<List<TimeConfig>> list() {
		return ResultRespone.success(service.list(),SUCCESS);
	}

	@ApiOperation(value = "配置时限", notes = "只修改时限", httpMethod = "POST")
	@PostMapping("/config")
	public ResultRespone config(@Validated @RequestBody TimeConfig timeConfig,BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultRespone.failed(QualityControlUtil.getErrorStrings(bindingResult),FAILED);
		}
		Boolean flag = service.updateById(timeConfig);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

}
