package com.sw.mes.yb.turnstore.controller;

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.DynamicSpecifications;
import com.sw.mes.core.utils.PageUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.yb.turnstore.dto.WarehousingStatisticsYbDto;
import com.sw.mes.yb.turnstore.entity.WarehousingStatisticsYb;
import com.sw.mes.yb.turnstore.service.WarehousingStatisticsYbService;
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.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
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;

@RestController
@RequestMapping("/WarehousingStatisticsYb")
@Slf4j
@Api(description = "圆棒入库统计")
public class WarehousingStatisticsYbController {

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    WarehousingStatisticsYbService warehousingStatisticsService;


    @ApiOperation(value = "分页查询圆棒入库统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_IN_sectionNo", value = "晶段编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_trayNo", value = "托号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_IN_state", value = "入库状态", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_hasGone", value = "产品去向", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_operationTime", value = "操作时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LTE_operationTime", value = "操作时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_confirmTime", value = "确认时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LTE_confirmTime", value = "确认时间", dataType = "string", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<WarehousingStatisticsYbDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        Specification<WarehousingStatisticsYb> specification = DynamicSpecifications.bySearchFilter(request, WarehousingStatisticsYb.class);
        Page<WarehousingStatisticsYb> page = warehousingStatisticsService.findByExample(specification, PageUtil.createPageable(requestPage));
        List<WarehousingStatisticsYbDto> dtos = page.getContent().stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
    }


    public WarehousingStatisticsYbDto toLazyDto(WarehousingStatisticsYb entity) {
        return modelMapper.map(entity, WarehousingStatisticsYbDto.class);
    }

    public WarehousingStatisticsYbDto toEagerDto(WarehousingStatisticsYb entity) {
        return modelMapper.map(entity, WarehousingStatisticsYbDto.class);
    }

    public WarehousingStatisticsYb fromDto(WarehousingStatisticsYbDto dto) {
        return modelMapper.map(dto, WarehousingStatisticsYb.class);
    }
}
