package com.sw.mes.yb.report.controller;

import com.sw.mes.core.annotation.TokenNotValidation;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.yb.report.dto.*;
import com.sw.mes.yb.report.entity.BrokenBractBase;
import com.sw.mes.yb.report.service.NumberOfOperationsService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@RestController
@RequestMapping("/board")
@Slf4j
@Api(description = "大屏看板")
public class NumberOfOperationsController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private NumberOfOperationsService numberOfOperationsService;


	@ApiOperation(value = "工序次数统计表")
	@GetMapping("/numberOfOperations")
	public Result getNumerOfOperations(String area) {

		List<NumberOfOperationsDto> numerOfOperations = numberOfOperationsService.getNumerOfOperations(area);
		return ResultGenerator.success(numerOfOperations);
	}

	@ApiOperation(value = "中间")
	@TokenNotValidation
	@GetMapping("/constitutionalDiagram")
	public Result getConstitutionalDiagram(String area) {

		NumberOfErrorDto numberOfErrorDto = numberOfOperationsService.getConstitutionalDiagram(area);
		return ResultGenerator.success(numberOfErrorDto);
	}

	@ApiOperation(value = "中下1")
	@GetMapping("/constitutionalDiagram1")
	public Result getConstitutionalDiagram1(String area) throws Exception {

		WorkHourDto workHourDto = numberOfOperationsService.getConstitutionalDiagram1(area);
		return ResultGenerator.success(workHourDto);
	}

	@ApiOperation(value = "中下2")
	@GetMapping("/constitutionalDiagram2")
	public Result getConstitutionalDiagram2(String area) {

		PerProductDto numerOfOperations = numberOfOperationsService.getConstitutionalDiagram2(area);
		return ResultGenerator.success(numerOfOperations);
	}

	@ApiOperation(value = "中下3")
	@GetMapping("/constitutionalDiagram3")
	public Result getConstitutionalDiagram3(String area) {

		PerProductDto dto = numberOfOperationsService.getConstitutionalDiagram3(area);
		return ResultGenerator.success(dto);
	}

	@ApiOperation(value = "左上")
	@GetMapping("/yield")
	public Result getYield(String area, String type) {

		BigDecimal dto = numberOfOperationsService.getYield(area, type);
		return ResultGenerator.success(dto);
	}

	@ApiOperation(value = "左中")
	@GetMapping("/yieldChart")
	public Result getYieldChart(String area) {

		PerProductDto dto = numberOfOperationsService.getYieldChart(area);
		return ResultGenerator.success(dto);
	}

	@ApiOperation(value = "左下")
	@GetMapping("/hourList")
	public Result getHourList(String area) throws IllegalAccessException {

		List<HourListDto> dto = numberOfOperationsService.getHourList(area);
		return ResultGenerator.success(dto);
	}

	@ApiOperation(value = "右上")
	@GetMapping("/erroeDevice")
	public Result getErroeDevice(String area) {

		List<ErrorDeviceDto> dto = numberOfOperationsService.getErrorDevice(area);
		Collections.sort(dto);
		return ResultGenerator.success(dto);
	}

	@ApiOperation(value = "右下")
	@GetMapping("/errorList")
	public Result getErrorList(String area) {

		List<ErrorListDto> dto = numberOfOperationsService.getErrorList(area);
		return ResultGenerator.success(dto);
	}

	public BrokenBractBaseDto toLazyDto(BrokenBractBase entity) {
		return modelMapper.map(entity, BrokenBractBaseDto.class);
	}

	public BrokenBractBaseDto toEagerDto(BrokenBractBase entity) {
		BrokenBractBaseDto dto = modelMapper.map(entity, BrokenBractBaseDto.class);
		return dto;
	}

	public BrokenBractBase fromDto(BrokenBractBaseDto dto) {
		return modelMapper.map(dto, BrokenBractBase.class);
	}


}
