package com.sw.mes.fb.turnstore.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.fb.turnstore.dto.RecheckInspectReceiveDetailDto;
import com.sw.mes.fb.turnstore.entity.RecheckInspectReceiveDetail;
import com.sw.mes.fb.turnstore.service.RecheckInspectReceiveDetailService;
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("/RecheckInspectReceiveDetail")
@Slf4j
@Api(description = "方棒退库复检接收接口")
public class RecheckInspectReceiveDetailController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private RecheckInspectReceiveDetailService recheckInspectReceiveDetailService;

	@ApiOperation(value = "分页查询方棒退库复检接收列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
	})
	@GetMapping("/listByPage")
	public Result<ResponsePage<RecheckInspectReceiveDetailDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
		Specification<RecheckInspectReceiveDetail> specification = DynamicSpecifications.bySearchFilter(request, RecheckInspectReceiveDetail.class);
    	Page<RecheckInspectReceiveDetail> page = recheckInspectReceiveDetailService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<RecheckInspectReceiveDetailDto> dtos = page.getContent().stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
		ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
	}

	@ApiOperation(value = "获取所有方棒退库复检接收")
	@GetMapping("/listAll")
	public Result<List<RecheckInspectReceiveDetailDto>> listAll(ServletRequest request) {
		Specification<RecheckInspectReceiveDetail> specification = DynamicSpecifications.bySearchFilter(request, RecheckInspectReceiveDetail.class);
		List<RecheckInspectReceiveDetail> recheckInspectReceiveDetails = recheckInspectReceiveDetailService.findByExampleSort(specification, null);
		List<RecheckInspectReceiveDetailDto> recheckInspectReceiveDetailDtos = recheckInspectReceiveDetails.stream()
					.map(this::toLazyDto)
					.collect(Collectors.toList());
		return ResultGenerator.success(recheckInspectReceiveDetailDtos);
	}

	@ApiOperation(value = "方棒退库复检接收详细信息")
	@GetMapping("/{id}")
	public Result<RecheckInspectReceiveDetailDto> get(@PathVariable @ApiParam(value="方棒退库复检接收id") Integer id) {
		RecheckInspectReceiveDetail recheckInspectReceiveDetail = recheckInspectReceiveDetailService.get(id);
		if (null == recheckInspectReceiveDetail)
			return ResultGenerator.fail("该方棒退库复检接收不存在");
		return ResultGenerator.success(toEagerDto(recheckInspectReceiveDetail));
	}

	@ApiOperation(value = "创建方棒退库复检接收")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "方棒退库复检接收对象") RecheckInspectReceiveDetailDto recheckInspectReceiveDetailDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		RecheckInspectReceiveDetail recheckInspectReceiveDetail = fromDto(recheckInspectReceiveDetailDto);
		recheckInspectReceiveDetailService.saveOrUpdate(recheckInspectReceiveDetail);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "更新方棒退库复检接收")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "方棒退库复检接收") RecheckInspectReceiveDetailDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        if (dto.getId() == null)
            return ResultGenerator.fail("id不能为空");
            RecheckInspectReceiveDetail entity = recheckInspectReceiveDetailService.get(dto.getId());
        if (entity == null)
            return ResultGenerator.fail("原信息不存在");
        try {
            RecheckInspectReceiveDetail entityFromFto = fromDto(dto);
			BeanUtils.copyProperties(entityFromFto, entity, BeanUtil.getIgnoreProperties(entityFromFto, null, null));
			recheckInspectReceiveDetailService.saveOrUpdate(entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

	@ApiOperation(value = "删除方棒退库复检接收")
	@PostMapping("/delete")
	public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Integer[] ids) {
		for (Integer id : ids) {
				recheckInspectReceiveDetailService.delete(id);
		}
		return ResultGenerator.success();
	}

	public RecheckInspectReceiveDetailDto toLazyDto(RecheckInspectReceiveDetail entity) {
		return modelMapper.map(entity, RecheckInspectReceiveDetailDto.class);
	}

	public RecheckInspectReceiveDetailDto toEagerDto(RecheckInspectReceiveDetail entity) {
		return modelMapper.map(entity, RecheckInspectReceiveDetailDto.class);
	}

	public RecheckInspectReceiveDetail fromDto(RecheckInspectReceiveDetailDto dto) {
		return modelMapper.map(dto, RecheckInspectReceiveDetail.class);
	}


}
