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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sw.mes.core.response.ResponsePage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.MaterialBrokenDownDto;
import com.sw.mes.itemclean.clean.domain.MaterialBrokenDown;
import com.sw.mes.itemclean.clean.mapper.MaterialBrokenDownMapper;
import com.sw.mes.itemclean.clean.service.MaterialBrokenDownService;
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/materialBrokenDown")
@Slf4j
@Api(tags = "破碎下料")
public class MaterialBrokenDownController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private MaterialBrokenDownService materialBrokenDownService;

    @Autowired
    private MaterialBrokenDownMapper materialBrokenDownMapper;

    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_productBatchNo", value = "生产批次号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_materialBoxNo", value = "成箱箱号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_materialName", value = "物料名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_materialCode", value = "物料编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_downTime", value = "下料成箱开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LTE_downTime", value = "下料成箱结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_downUser", value = "成箱人", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_status", value = "状态: 0 破碎成箱未组托 1 破碎成箱组托", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<MaterialBrokenDownDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        //分页查询
        QueryWrapper<MaterialBrokenDown> queryWrapper =  DynmaicQueryWrapper.searchFilter(request, MaterialBrokenDown.class);
        Page<MaterialBrokenDown> listPage = materialBrokenDownService.page(new Page<>(requestPage.getPage(), requestPage.getRows()), queryWrapper);
        List<MaterialBrokenDownDto> 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<MaterialBrokenDownDto>> listAll(ServletRequest request) {
        QueryWrapper<MaterialBrokenDown> queryWrapper =  DynmaicQueryWrapper.searchFilter(request, MaterialBrokenDown.class);
        List<MaterialBrokenDown> materialBrokenDowns = materialBrokenDownService.list(queryWrapper);
        List<MaterialBrokenDownDto> materialBrokenDownDtos = materialBrokenDowns.stream()
                .map(materialBrokenDown -> toLazyDto(materialBrokenDown))
                .collect(Collectors.toList());
        return ResultGenerator.success(materialBrokenDownDtos);
    }

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

    @ApiOperation(value = "创建")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "Dto对象") MaterialBrokenDownDto materialBrokenDownDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        MaterialBrokenDown materialBrokenDown = fromDto(materialBrokenDownDto);
        materialBrokenDownService.saveOrUpdate(materialBrokenDown);
        return ResultGenerator.success();
    }

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

    public MaterialBrokenDownDto toLazyDto(MaterialBrokenDown entity) {
        return modelMapper.map(entity, MaterialBrokenDownDto.class);
    }

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

    public MaterialBrokenDown fromDto(MaterialBrokenDownDto dto) {
        return modelMapper.map(dto, MaterialBrokenDown.class);
    }
}
