package com.sw.mes.itemclean.clean.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.DynmaicQueryWrapper;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.itemclean.clean.domain.ReportErpXlInputRecordDetail;
import com.sw.mes.itemclean.clean.dto.ReportErpXlInputRecordDetailDto;
import com.sw.mes.itemclean.clean.service.ReportErpXlInputRecordDetailService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
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;



@RestController
@RequestMapping("/ReportErpXlInputRecordDetail")
@Slf4j
@Api(tags = "拉取洗料发料上报erp记录子表接口")
public class ReportErpXlInputRecordDetailController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private ReportErpXlInputRecordDetailService reportErpXlInputRecordDetailService;


	@ApiOperation(value = "创建拉取洗料发料上报erp记录子表")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "拉取洗料发料上报erp记录子表对象") ReportErpXlInputRecordDetailDto reportErpXlInputRecordDetailDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		ReportErpXlInputRecordDetail reportErpXlInputRecordDetail = fromDto(reportErpXlInputRecordDetailDto);
		reportErpXlInputRecordDetailService.saveOrUpdate(reportErpXlInputRecordDetail);
		return ResultGenerator.success();
	}

	@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<ReportErpXlInputRecordDetailDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
		//分页查询
		QueryWrapper<ReportErpXlInputRecordDetail> queryWrapper =  DynmaicQueryWrapper.searchFilter(request, ReportErpXlInputRecordDetail.class);
		Page<ReportErpXlInputRecordDetail> listPage = reportErpXlInputRecordDetailService.page(new Page<>(requestPage.getPage(), requestPage.getRows()), queryWrapper);
		List<ReportErpXlInputRecordDetailDto> dtos = listPage.getRecords().stream()
				.map(entity -> toLazyDto(entity))
				.collect(Collectors.toList());
		ResponsePage responsePage = new ResponsePage(dtos, listPage.getTotal());
		return ResultGenerator.success(responsePage);
	}



	public ReportErpXlInputRecordDetailDto toLazyDto(ReportErpXlInputRecordDetail entity) {
		return modelMapper.map(entity, ReportErpXlInputRecordDetailDto.class);
	}

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

	public ReportErpXlInputRecordDetail fromDto(ReportErpXlInputRecordDetailDto dto) {
		return modelMapper.map(dto, ReportErpXlInputRecordDetail.class);
	}


}
