package com.tofly.qualitycontrol.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.qualitycontrol.entity.ScanFrequencyConfig;
import com.tofly.qualitycontrol.service.IScanFrequencyConfigService;
import com.tofly.qualitycontrol.util.QualityControlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

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

/**
 * 扫描频率配置接口控制器
 *
 * @author B
 */
@RestController
@RequestMapping("/scanFrequencyConfig")
@Api(value = "扫描频率配置接口", tags = "扫描频率配置接口")
@RequiredArgsConstructor
@Slf4j
public class ScanFrequencyConfigController {

	private final IScanFrequencyConfigService service;

	@ApiOperation(value = "扫描频率配置列表", notes = "", httpMethod = "GET")
	@GetMapping("/list")
	public ResultRespone<List<ScanFrequencyConfig>> list() {
		return ResultRespone.success(service.list(Wrappers.<ScanFrequencyConfig>lambdaQuery().orderByAsc(ScanFrequencyConfig::getId)),SUCCESS);
	}

	@ApiOperation(value = "配置扫描频率", notes = "", httpMethod = "POST")
	@PostMapping("/config")
	public ResultRespone config(@RequestBody ScanFrequencyConfig scanFrequencyConfig,BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultRespone.failed(QualityControlUtil.getErrorStrings(bindingResult),FAILED);
		}
		Boolean flag = service.updateById(scanFrequencyConfig);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

	@ApiOperation(value = "新增扫描频率", notes = "", httpMethod = "POST")
	@PostMapping("/newConfig")
	public ResultRespone newConfig(@Valid @RequestBody ScanFrequencyConfig scanFrequencyConfig,BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultRespone.failed(QualityControlUtil.getErrorStrings(bindingResult),FAILED);
		}
		Boolean flag = service.save(scanFrequencyConfig);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

	@ApiImplicitParams({
							   @ApiImplicitParam(value = "主键id", name = "id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class)
					   })
	@ApiOperation(value = "删除扫描频率", notes = "", httpMethod = "GET")
	@GetMapping("/deleteConfig")
	public ResultRespone deleteConfig(@RequestParam Long id) {
		Boolean flag = service.removeById(id);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

	@ApiImplicitParams({
							   @ApiImplicitParam(value = "主键id", name = "id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class)
					   })
	@ApiOperation(value = "传入对应id,设置扫描频率", notes = "", httpMethod = "GET")
	@GetMapping("/configScanFrequency")
	public ResultRespone configScanFrequency(@RequestParam Long id) {
		Boolean flag = service.configScanFrequency(id);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

}
