package com.sw.mes.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
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.store.dto.WarehouseAllotBillDto;
import com.sw.mes.store.dto.WarehouseModelDto;
import com.sw.mes.store.dto.WarehouseOutputAgvDto;
import com.sw.mes.store.entity.WarehouseAllotBill;
import com.sw.mes.store.entity.WarehouseModel;
import com.sw.mes.store.entity.WarehouseOutputAgv;
import com.sw.mes.store.service.WarehouseOutputAgvService;
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("/warehouse/agv")
@Slf4j
@Api(tags = "出库呼叫AGV")
public class WarehouseOutputAgvController {
    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private WarehouseOutputAgvService agvService;

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

    @ApiOperation(value = "获取所有呼叫AGV")
    @GetMapping("/listAll")
    public Result<List<WarehouseOutputAgvDto>> listAll(ServletRequest request) {
        QueryWrapper<WarehouseOutputAgv> queryWrapper = DynmaicQueryWrapper.bySearchFilter(request, WarehouseOutputAgv.class);
        List<WarehouseOutputAgv> entities = agvService.list(queryWrapper);
        List<WarehouseOutputAgvDto> dtos = entities.stream()
                .map(storeOutputBill -> toEagerDto(storeOutputBill))
                .collect(Collectors.toList());
        return ResultGenerator.success(dtos);
    }

    @ApiOperation(value = "调拨呼叫AGV信息")
    @GetMapping("/{id}")
    public Result<WarehouseOutputAgvDto> get(@PathVariable @ApiParam(value = "调拨单id") Integer id) {
        WarehouseOutputAgv warehouseOutputAgv = agvService.getById(id);
        if (null == warehouseOutputAgv) {
            return ResultGenerator.fail("该调拨单不存在");
        }
        return ResultGenerator.success(toEagerDto(warehouseOutputAgv));
    }

    @ApiOperation(value = "创建呼叫AGV")
    @PostMapping("/create")
    public Result<String> create(@RequestBody @Valid @ApiParam(value = "调拨单对象") WarehouseOutputAgvDto warehouseOutputAgvDto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        Boolean aBoolean = agvService.create(warehouseOutputAgvDto);
        if(aBoolean==false){
            return ResultGenerator.fail("创建失败");
        }
        return ResultGenerator.success();
    }
    @ApiOperation(value = "更新呼叫AGV")
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Valid @ApiParam(value = "调拨单对象") WarehouseOutputAgvDto warehouseOutputAgvDto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        Boolean aBoolean = agvService.create(warehouseOutputAgvDto);
        if(aBoolean==false){
            return ResultGenerator.fail("更新失败");
        }
        return ResultGenerator.success();
    }
    @ApiOperation(value = "删除呼叫AGV(支持批量)")
    @GetMapping("/delete")
    public Result<String> delete(@RequestParam List<Long> ids) {
        Boolean delete = agvService.delete(ids);
        if(delete==false){
            return ResultGenerator.fail("删除失败");
        }
       return ResultGenerator.success();
    }
    public WarehouseOutputAgvDto toEagerDto(WarehouseOutputAgv entity) {
        WarehouseOutputAgvDto dto = modelMapper.map(entity, WarehouseOutputAgvDto.class);
        return dto;
    }

    public WarehouseOutputAgv fromDto(WarehouseOutputAgvDto dto) {
        return modelMapper.map(dto, WarehouseOutputAgv.class);
    }
}
