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.dto.MaterialUnpackingDetailDto;
import com.sw.mes.itemclean.clean.domain.MaterialUnpackingDetail;
import com.sw.mes.itemclean.clean.mapper.MaterialUnpackingDetailMapper;
import com.sw.mes.itemclean.clean.service.MaterialUnpackingDetailService;
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("/clean/material-unpacking-detail")
@Slf4j
@Api(tags = "分选拆箱明细行")
public class MaterialUnpackingDetailController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private MaterialUnpackingDetailService materialUnpackingDetailService;

    @Autowired
    private MaterialUnpackingDetailMapper materialUnpackingDetailMapper;

    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<MaterialUnpackingDetailDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        //分页查询
        QueryWrapper<MaterialUnpackingDetail> queryWrapper =  DynmaicQueryWrapper.searchFilter(request, MaterialUnpackingDetail.class);
        Page<MaterialUnpackingDetail> listPage = materialUnpackingDetailService.page(new Page<>(requestPage.getPage(), requestPage.getRows()), queryWrapper);
        List<MaterialUnpackingDetailDto> dtos = listPage.getRecords().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
        ResponsePage responsePage = new ResponsePage(dtos, listPage.getTotal());
        return ResultGenerator.success(responsePage);
    }

    @ApiOperation(value = "获取所有列表")
    @GetMapping("/listAll")
    public Result<List<MaterialUnpackingDetailDto>> listAll(ServletRequest request) {
        QueryWrapper<MaterialUnpackingDetail> queryWrapper =  DynmaicQueryWrapper.searchFilter(request, MaterialUnpackingDetail.class);
        List<MaterialUnpackingDetail> materialUnpackingDetails = materialUnpackingDetailService.list(queryWrapper);
        List<MaterialUnpackingDetailDto> materialUnpackingDetailDtos = materialUnpackingDetails.stream()
                .map(materialUnpackingDetail -> toLazyDto(materialUnpackingDetail))
                .collect(Collectors.toList());
        return ResultGenerator.success(materialUnpackingDetailDtos);
    }

    @ApiOperation(value = "详细信息")
    @GetMapping("/{id}")
    public Result<MaterialUnpackingDetailDto> get(@PathVariable @ApiParam(value="设备类型id") Integer id) {
        MaterialUnpackingDetail materialUnpackingDetail = materialUnpackingDetailService.getById(id);
        if (null == materialUnpackingDetail)
            return ResultGenerator.fail("该对象不存在");
        return ResultGenerator.success(toEagerDto(materialUnpackingDetail));
    }

    @ApiOperation(value = "创建")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "Dto对象") MaterialUnpackingDetailDto materialUnpackingDetailDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        MaterialUnpackingDetail materialUnpackingDetail = fromDto(materialUnpackingDetailDto);
        materialUnpackingDetailService.saveOrUpdate(materialUnpackingDetail);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "Dto对象") MaterialUnpackingDetailDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        if (dto.getId() == null)
            return ResultGenerator.fail("id不能为空");
        MaterialUnpackingDetail entity = materialUnpackingDetailService.getById(dto.getId());
        if (entity == null)
            return ResultGenerator.fail("原信息不存在");
        try {
            MaterialUnpackingDetail entityfromFto = fromDto(dto);
            BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
            materialUnpackingDetailService.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) {
            materialUnpackingDetailMapper.deleteById(id);
        }
        return ResultGenerator.success();
    }

    public MaterialUnpackingDetailDto toLazyDto(MaterialUnpackingDetail entity) {
        return modelMapper.map(entity, MaterialUnpackingDetailDto.class);
    }

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

    public MaterialUnpackingDetail fromDto(MaterialUnpackingDetailDto dto) {
        return modelMapper.map(dto, MaterialUnpackingDetail.class);
    }
}
