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.ExcelUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.store.dto.WarehouseOutputBillDetailDto;
import com.sw.mes.store.dto.WarehouseOutputRecordBillDetailDto;
import com.sw.mes.store.entity.WarehouseOutputBill;
import com.sw.mes.store.entity.WarehouseOutputBillDetail;
import com.sw.mes.store.entity.WarehouseOutputRecordBillDetail;
import com.sw.mes.store.service.WarehouseOutputBillDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wupeng
 * @Description 出库单明细
 * @Date 18:46 2023/7/31
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/warehouse/outputBillDetail")
@Slf4j
@Api(tags = "线边仓-出库单明细")
public class WarehouseOutputBillDetailController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private WarehouseOutputBillDetailService warehouseOutputBillDetailService;

    @ApiOperation(value = "分页查询出库单明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_outputBillNo", value = "出库单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_materialModelCode", value = "物料料号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_materialModelName", value = "物料名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_uniqueCode", value = "物料条码(唯一码)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_batchNo", value = "批次号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_receiveTime", value = "审核开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LTE_receiveTime", value = "审核结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_status", value = "订单状态", dataType = "string", paramType = "query", defaultValue = "已入库"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<WarehouseOutputBillDetailDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        QueryWrapper<WarehouseOutputBillDetail> queryWrapper = DynmaicQueryWrapper.bySearchFilter(request, WarehouseOutputBillDetail.class);
        Page<WarehouseOutputBillDetail> page = new Page<>(requestPage.getPage(), requestPage.getRows());
        String orderDirection = requestPage.getOrderDirection();
        String orderField = requestPage.getOrderField();
        page.addOrder(RequestPage.ORDER_DIRECTION_DESC.equals(orderDirection) ? OrderItem.desc(orderField) : OrderItem.asc(orderField));
        Page<WarehouseOutputBillDetail> listPage = warehouseOutputBillDetailService.page(page, queryWrapper);
        List<WarehouseOutputBillDetailDto> dtos = listPage.getRecords().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
        ResponsePage<WarehouseOutputBillDetailDto> responsePage = new ResponsePage(dtos, listPage.getTotal());
        return ResultGenerator.success(responsePage);
    }
    public WarehouseOutputBillDetailDto toLazyDto(WarehouseOutputBillDetail entity) {
        return modelMapper.map(entity, WarehouseOutputBillDetailDto.class);
    }

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

    public WarehouseOutputBillDetail fromDto(WarehouseOutputBillDetailDto dto) {
        return modelMapper.map(dto, WarehouseOutputBillDetail.class);
    }


}