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.WarehouseInputBillDetailDto;
import com.sw.mes.store.entity.WarehouseInputBillDetail;
import com.sw.mes.store.service.WarehouseInputBillDetailService;
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 java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wupeng
 * @Description 入库单明细接口-入库记录
 * @Date 08:18 2023/7/28
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/warehouse/inputBillDetail")
@Slf4j
@Api(tags = "线边仓-入库单明细记录")
public class WarehouseInputBillDetailController {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private WarehouseInputBillDetailService warehouseInputBillDetailService;

    @ApiOperation(value = "分页查询入库记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_inputBillNo", 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_EQ_status", value = "订单状态", dataType = "string", paramType = "query", defaultValue = "已入库"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<WarehouseInputBillDetailDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        QueryWrapper<WarehouseInputBillDetail> queryWrapper = DynmaicQueryWrapper.bySearchFilter(request, WarehouseInputBillDetail.class);
        Page<WarehouseInputBillDetail> 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<WarehouseInputBillDetail> listPage = warehouseInputBillDetailService.page(page, queryWrapper);
        List<WarehouseInputBillDetailDto> dtos = listPage.getRecords().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
        ResponsePage<WarehouseInputBillDetailDto> responsePage = new ResponsePage(dtos, listPage.getTotal());
        return ResultGenerator.success(responsePage);
    }


    public WarehouseInputBillDetailDto toLazyDto(WarehouseInputBillDetail entity) {
        return modelMapper.map(entity, WarehouseInputBillDetailDto.class);
    }

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

    public WarehouseInputBillDetail fromDto(WarehouseInputBillDetailDto dto) {
        return modelMapper.map(dto, WarehouseInputBillDetail.class);
    }


}