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

import com.sw.mes.core.entity.FbCheckOutData;
import com.sw.mes.core.enums.SectionIndexRangeEnum;
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.EasyExcelUtils;
import com.sw.mes.core.utils.PageUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.fb.checkout.service.FbCheckOutDataService;
import com.sw.mes.fb.turnstore.dto.WarehousingStatisticsDto;
import com.sw.mes.fb.turnstore.entity.WarehousingStatistics;
import com.sw.mes.fb.turnstore.service.WarehousingStatisticsService;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/warehousingStatistics")
@Slf4j
@Api(description = "方棒入库统计接口")
public class WarehousingStatisticsController {

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    WarehousingStatisticsService warehousingStatisticsService;

    @Autowired
    private FbCheckOutDataService fbCheckOutDataService;


    @ApiOperation(value = "分页查询方棒入库统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_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_EQ_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<WarehousingStatisticsDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        Specification<WarehousingStatistics> specification = DynamicSpecifications.bySearchFilter(request, WarehousingStatistics.class);
        Page<WarehousingStatistics> page = warehousingStatisticsService.findByExample(specification, PageUtil.createPageable(requestPage));
        List<WarehousingStatisticsDto> dtos = page.getContent().stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
    }

    @GetMapping("/export")
    public void export(ServletRequest request, HttpServletResponse response, @ApiIgnore RequestPage requestPage) {
        try {
            requestPage.setRows(10000);
            Specification<WarehousingStatistics> specification = DynamicSpecifications.bySearchFilter(request, WarehousingStatistics.class);
            Page<WarehousingStatistics> page = warehousingStatisticsService.findByExample(specification, PageUtil.createPageable(requestPage));
            List<WarehousingStatisticsDto> dtos = page.getContent().stream()
                    .map(this::toLazyDto)
                    .collect(Collectors.toList());
            EasyExcelUtils.write(response, WarehousingStatisticsDto.class, "方棒入库统计", "方棒入库统计导出", dtos);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("方棒入库统计导出，错误信息：" + e.getMessage());
        }
    }

    public WarehousingStatisticsDto toLazyDto(WarehousingStatistics entity) {
        WarehousingStatisticsDto dto = modelMapper.map(entity, WarehousingStatisticsDto.class);
        String fromMark = dto.getSectionNo().substring(SectionIndexRangeEnum.基地追溯产品类型.getBeginIndex(), SectionIndexRangeEnum.基地追溯产品类型.getEndIndex());
        if (fromMark.matches("[A-Z]+")) {
            dto.setCrystalNo(dto.getSectionNo().substring(SectionIndexRangeEnum.毛棒范围.getBeginIndex(), SectionIndexRangeEnum.毛棒范围.getEndIndex()));
        } else {
            dto.setCrystalNo(dto.getSectionNo().substring(0, 12));
        }
        FbCheckOutData data = fbCheckOutDataService.findDataByFbSectionNo(dto.getSectionNo());
        if(data != null){
            dto.setYbSectionNo(data.getYbSectionNo());
        }
        return dto;
    }

    public WarehousingStatisticsDto toEagerDto(WarehousingStatistics entity) {
        return modelMapper.map(entity, WarehousingStatisticsDto.class);
    }

    public WarehousingStatistics fromDto(WarehousingStatisticsDto dto) {
        return modelMapper.map(dto, WarehousingStatistics.class);
    }
}
