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.ReportErpOutputRecordDto;
import com.sw.mes.sap.entity.ReportErpOutputRecord;
import com.sw.mes.sap.service.ReportErpOutputRecordService;
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("/ReportErpOutputRecord")
@Slf4j
@Api(description = "拉取长晶棒产出信息上报erp记录主表接口")
public class ReportErpOutputRecordController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private ReportErpOutputRecordService reportErpOutputRecordService;

	@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<ReportErpOutputRecordDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
		Specification<ReportErpOutputRecord> specification = DynamicSpecifications.bySearchFilter(request, ReportErpOutputRecord.class);
    	Page<ReportErpOutputRecord> page = reportErpOutputRecordService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<ReportErpOutputRecordDto> 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<ReportErpOutputRecordDto>> listAll(ServletRequest request) {
		Specification<ReportErpOutputRecord> specification = DynamicSpecifications.bySearchFilter(request, ReportErpOutputRecord.class);
		List<ReportErpOutputRecord> reportErpOutputRecords = reportErpOutputRecordService.findByExampleSort(specification, null);
		List<ReportErpOutputRecordDto> reportErpOutputRecordDtos = reportErpOutputRecords.stream()
					.map(reportErpOutputRecord -> toLazyDto(reportErpOutputRecord))
					.collect(Collectors.toList());
		return ResultGenerator.success(reportErpOutputRecordDtos);
	}

	@ApiOperation(value = "拉取长晶棒产出信息上报erp记录主表详细信息")
	@GetMapping("/{id}")
	public Result<ReportErpOutputRecordDto> get(@PathVariable @ApiParam(value="拉取长晶棒产出信息上报erp记录主表id") Long id) {
		ReportErpOutputRecord reportErpOutputRecord = reportErpOutputRecordService.get(id);
		if (null == reportErpOutputRecord)
			return ResultGenerator.fail("该拉取长晶棒产出信息上报erp记录主表不存在");
		return ResultGenerator.success(toEagerDto(reportErpOutputRecord));
	}

	@ApiOperation(value = "创建拉取长晶棒产出信息上报erp记录主表")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "拉取长晶棒产出信息上报erp记录主表对象") ReportErpOutputRecordDto reportErpOutputRecordDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		ReportErpOutputRecord reportErpOutputRecord = fromDto(reportErpOutputRecordDto);
		reportErpOutputRecordService.saveOrUpdate(reportErpOutputRecord);
		return ResultGenerator.success();
	}

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

    @ApiOperation(value = "更新拉取长晶棒产出信息上报erp记录主表")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "拉取长晶棒产出信息上报erp记录主表") ReportErpOutputRecordDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        if (dto.getId() == null)
            return ResultGenerator.fail("id不能为空");
            ReportErpOutputRecord entity = reportErpOutputRecordService.get(dto.getId());
        if (entity == null)
            return ResultGenerator.fail("原信息不存在");
        try {
            ReportErpOutputRecord entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			reportErpOutputRecordService.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) {
				reportErpOutputRecordService.delete(id);
		}
		return ResultGenerator.success();
	}

	public ReportErpOutputRecordDto toLazyDto(ReportErpOutputRecord entity) {
		return modelMapper.map(entity, ReportErpOutputRecordDto.class);
	}

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

	public ReportErpOutputRecord fromDto(ReportErpOutputRecordDto dto) {
		return modelMapper.map(dto, ReportErpOutputRecord.class);
	}


}
