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.BeanUtil;
import com.sw.mes.core.utils.DynmaicQueryWrapper;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.itemclean.clean.domain.ReportErpXlInputRecord;
import com.sw.mes.itemclean.clean.domain.ReportErpXlInputRecordDetail;
import com.sw.mes.itemclean.clean.dto.ReportErpXlInputRecordDto;
import com.sw.mes.itemclean.clean.mapper.ReportErpXlInputRecordMapper;
import com.sw.mes.itemclean.clean.service.ReportErpXlInputRecordDetailService;
import com.sw.mes.itemclean.clean.service.ReportErpXlInputRecordService;
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.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("/ReportErpXlInputRecord")
@Slf4j
@Api(tags = "拉取洗料发料上报erp记录主表接口")
public class ReportErpXlInputRecordController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private ReportErpXlInputRecordService reportErpXlInputRecordService;

	@Autowired
	private ReportErpXlInputRecordDetailService reportErpXlInputRecordDetailService;

	@Autowired
	private ReportErpXlInputRecordMapper reportErpXlInputRecordMapper;

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



	@ApiOperation(value = "拉取洗料发料上报erp记录主表详细信息")
	@GetMapping("/{id}")
	public Result<List<ReportErpXlInputRecordDetail>> get(@PathVariable @ApiParam(value="拉取洗料发料上报erp记录主表id") Long id) {
		List<ReportErpXlInputRecordDetail> erpXlInputRecordDetailDtos = reportErpXlInputRecordDetailService.findList(id);
		return ResultGenerator.success(erpXlInputRecordDetailDtos);
	}

	@ApiOperation(value = "创建拉取洗料发料上报erp记录主表")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "拉取洗料发料上报erp记录主表对象") ReportErpXlInputRecordDto reportErpXlInputRecordDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		ReportErpXlInputRecord reportErpXlInputRecord = fromDto(reportErpXlInputRecordDto);
		reportErpXlInputRecordService.saveOrUpdate(reportErpXlInputRecord);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "更新拉取洗料发料上报erp记录主表")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "拉取洗料发料上报erp记录主表") ReportErpXlInputRecordDto dto, BindingResult bindingResult) {

		if (bindingResult.hasErrors())
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		if (dto.getId() == null)
			return ResultGenerator.fail("id不能为空");
		ReportErpXlInputRecord entity = reportErpXlInputRecordService.getById(dto.getId());
		if (entity == null)
			return ResultGenerator.fail("原信息不存在");
		try {
			ReportErpXlInputRecord entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			reportErpXlInputRecordService.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) {
				reportErpXlInputRecordMapper.deleteById(id);
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "上报erp")
	@PostMapping("/pushToErp")
	public Result pushToErp(@RequestBody @ApiParam(value="拉取投料上报erp记录主表") ReportErpXlInputRecord dto) {
		try {
			reportErpXlInputRecordService.pushToErp(dto);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	public ReportErpXlInputRecordDto toLazyDto(ReportErpXlInputRecord entity) {
		return modelMapper.map(entity, ReportErpXlInputRecordDto.class);
	}

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

	public ReportErpXlInputRecord fromDto(ReportErpXlInputRecordDto dto) {
		return modelMapper.map(dto, ReportErpXlInputRecord.class);
	}


}
