package com.sw.mes.sap.controller;

import com.sw.mes.core.response.ResponsePage;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.core.utils.DynamicSpecifications;
import com.sw.mes.core.utils.PageUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.sap.dto.ReportErpSquareOutputRecordDto;
import com.sw.mes.sap.entity.ReportErpSquareOutputRecord;
import com.sw.mes.sap.service.ReportErpSquareOutputRecordService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@RestController
@RequestMapping("/ReportErpSquareOutputRecord")
@Slf4j
@Api(description = "拉取长晶棒产出信息【已切断】上报erp记录主表接口")
public class ReportErpSquareOutputRecordController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private ReportErpSquareOutputRecordService reportErpSquareOutputRecordService;

	@ApiOperation(value = "分页查询拉取长晶棒产出信息【已切断】上报erp记录主表列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
	})
	@GetMapping("/listByPage")
	public Result<ResponsePage<ReportErpSquareOutputRecordDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
		Specification<ReportErpSquareOutputRecord> specification = DynamicSpecifications.bySearchFilter(request, ReportErpSquareOutputRecord.class);
    	Page<ReportErpSquareOutputRecord> page = reportErpSquareOutputRecordService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<ReportErpSquareOutputRecordDto> dtos = page.getContent().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
		ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
	}

	@ApiOperation(value = "获取所有拉取长晶棒产出信息【已切断】上报erp记录主表")
	@GetMapping("/listAll")
	public Result<List<ReportErpSquareOutputRecordDto>> listAll(ServletRequest request) {
		Specification<ReportErpSquareOutputRecord> specification = DynamicSpecifications.bySearchFilter(request, ReportErpSquareOutputRecord.class);
		List<ReportErpSquareOutputRecord> reportErpSquareOutputRecords = reportErpSquareOutputRecordService.findByExampleSort(specification, null);
		List<ReportErpSquareOutputRecordDto> reportErpSquareOutputRecordDtos = reportErpSquareOutputRecords.stream()
					.map(reportErpSquareOutputRecord -> toLazyDto(reportErpSquareOutputRecord))
					.collect(Collectors.toList());
		return ResultGenerator.success(reportErpSquareOutputRecordDtos);
	}

	@ApiOperation(value = "拉取长晶棒产出信息【已切断】上报erp记录主表详细信息")
	@GetMapping("/{id}")
	public Result<ReportErpSquareOutputRecordDto> get(@PathVariable @ApiParam(value="拉取长晶棒产出信息【已切断】上报erp记录主表id") Long id) {
		ReportErpSquareOutputRecord reportErpSquareOutputRecord = reportErpSquareOutputRecordService.get(id);
		if (null == reportErpSquareOutputRecord)
			return ResultGenerator.fail("该拉取长晶棒产出信息【已切断】上报erp记录主表不存在");
		return ResultGenerator.success(toEagerDto(reportErpSquareOutputRecord));
	}

	@ApiOperation(value = "创建拉取长晶棒产出信息【已切断】上报erp记录主表")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "拉取长晶棒产出信息【已切断】上报erp记录主表对象") ReportErpSquareOutputRecordDto reportErpSquareOutputRecordDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		ReportErpSquareOutputRecord reportErpSquareOutputRecord = fromDto(reportErpSquareOutputRecordDto);
		reportErpSquareOutputRecordService.saveOrUpdate(reportErpSquareOutputRecord);
		return ResultGenerator.success();
	}

	@ApiOperation(value = "长晶棒产出信息上报erp【发起报工】")
	@PostMapping("/reportForWork")
	public Result reportForWork(@RequestBody @Valid @ApiParam(value = "长晶棒产出信息上报erp【发起报工】") ReportErpSquareOutputRecordDto reportErpSquareOutputRecordDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		ReportErpSquareOutputRecord reportErpSquareOutputRecord = fromDto(reportErpSquareOutputRecordDto);
		Result result = reportErpSquareOutputRecordService.reportForWork(reportErpSquareOutputRecord);
		return result;
	}

    @ApiOperation(value = "更新拉取长晶棒产出信息【已切断】上报erp记录主表")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "拉取长晶棒产出信息【已切断】上报erp记录主表") ReportErpSquareOutputRecordDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        if (dto.getId() == null)
            return ResultGenerator.fail("id不能为空");
            ReportErpSquareOutputRecord entity = reportErpSquareOutputRecordService.get(dto.getId());
        if (entity == null)
            return ResultGenerator.fail("原信息不存在");
        try {
            ReportErpSquareOutputRecord entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			reportErpSquareOutputRecordService.saveOrUpdate(entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

	@ApiOperation(value = "删除拉取长晶棒产出信息【已切断】上报erp记录主表")
	@PostMapping("/delete")
	public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Integer[] ids) {
		for (Integer id : ids) {
				reportErpSquareOutputRecordService.delete(id);
		}
		return ResultGenerator.success();
	}

	public ReportErpSquareOutputRecordDto toLazyDto(ReportErpSquareOutputRecord entity) {
		return modelMapper.map(entity, ReportErpSquareOutputRecordDto.class);
	}

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

	public ReportErpSquareOutputRecord fromDto(ReportErpSquareOutputRecordDto dto) {
		return modelMapper.map(dto, ReportErpSquareOutputRecord.class);
	}


}
