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

import com.sw.mes.base.factory.material.entity.MaterialModel;
import com.sw.mes.base.factory.material.service.MaterialModelService;
import com.sw.mes.core.annotation.Jinko;
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.yb.production.dto.ProductionMaterialReturnRecordDto;
import com.sw.mes.yb.production.entity.ProductionMaterialReturnRecord;
import com.sw.mes.yb.production.service.ProductionMaterialReturnRecordService;
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.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.ObjectUtils;
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.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Jinko(value = "退料查询列表", author = "wxl")
@RestController
@RequestMapping("/productionMaterialReturnRecord")
@Slf4j
@Api(description = "退料记录")
public class ProductionMaterialReturnRecordController {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ProductionMaterialReturnRecordService productionMaterialReturnRecordService;

    @Autowired
    private MaterialModelService materialModelService;

    @ApiOperation(value = "分页查询退料明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<ProductionMaterialReturnRecordDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        Specification<ProductionMaterialReturnRecord> specification = DynamicSpecifications.bySearchFilter(request, ProductionMaterialReturnRecord.class);
        Page<ProductionMaterialReturnRecord> page = productionMaterialReturnRecordService.findByExample(specification, PageUtil.createPageable(requestPage));
        List<ProductionMaterialReturnRecordDto> dtos = page.getContent().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
        ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
    }


    @ApiOperation(value = "导出查询退料明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/export")
    public Result export(ServletRequest request, HttpServletResponse response)  {
        Specification<ProductionMaterialReturnRecord> specification = DynamicSpecifications.bySearchFilter(request, ProductionMaterialReturnRecord.class);
        List<ProductionMaterialReturnRecord> list = productionMaterialReturnRecordService.findByExampleSort(specification, Sort.by(Sort.Direction.DESC, "id"));
        List<ProductionMaterialReturnRecordDto> dtos = list.stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
        for (ProductionMaterialReturnRecordDto dto : dtos) {
            MaterialModel materialModel = materialModelService.findByCode(dto.getProductCode());
            if (!ObjectUtils.isEmpty(materialModel)){
                dto.setProductName(materialModel.getName());
            }
            switch (dto.getAgainNum()){
                case 0:
                    dto.setRawTypeName("初始装料");
                    break;
                case 1:
                    dto.setRawTypeName("初始复投");
                    break;
                default:
                    dto.setRawTypeName("第"+dto.getAgainNum()+"次复投");
                    break;
            }
        }

        try {
            EasyExcelUtils.write(response, ProductionMaterialReturnRecordDto.class, "退料记录", "退料记录", dtos);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultGenerator.fail("导出失败!"+e.getMessage());
        }
        return ResultGenerator.success();
    }


    private ProductionMaterialReturnRecordDto toLazyDto(ProductionMaterialReturnRecord entity) {
        return modelMapper.map(entity, ProductionMaterialReturnRecordDto.class);
    }
}
