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


import com.sw.mes.core.api.system.FbFeignClients;
import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.entity.FbCheckOutData;
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.RequestPage;
import com.sw.mes.inspect.entity.YbInspectData;
import com.sw.mes.inspect.service.YbInspectDataService;
import com.sw.mes.yb.pad.dto.RecutDto;
import com.sw.mes.yb.pad.entity.*;
import com.sw.mes.yb.pad.entity.dto.CheckSheetDto;
import com.sw.mes.yb.pad.entity.dto.EapCheckSheetDto;
import com.sw.mes.yb.pad.service.*;
import com.sw.mes.yb.production.entity.LongCrystal;
import com.sw.mes.yb.production.service.LongCrystalService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/CheckSheet")
@Slf4j
@Api(description = "盘点接口")
public class CheckSheetController {

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private CheckSheetService checkSheetService;

    @Autowired
    private FbFeignClients fbFeignClients;

    @Autowired
    private YbInspectDataService ybInspectDataService;

    @Autowired
    private FbReCutService fbReCutService;

    @Autowired
    private ProductionRecutService productionRecutService;

    @Autowired
    private ProductionPaintingLineDetailService productionPaintingLineDetailService;

    @Autowired
    private LongCrystalService longCrystalService;

    @Autowired
    private ProductionPaintingLineService productionPaintingLineService;

    @ApiOperation(value = "分页查询盘点信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_createDate", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "search_LTE_createDate", value = "结束时间", dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<ResponsePage<CheckSheet>> list(ServletRequest request, RequestPage page) {
        Specification<CheckSheet> specification = DynamicSpecifications.bySearchFilter(request, CheckSheet.class);
        List<CheckSheet> checkSheets = checkSheetService.findByExample(specification, page);
        ResponsePage responsePage = new ResponsePage(checkSheets, page.getTotal());
        return ResultGenerator.success(responsePage);
    }

    @ApiOperation(value = "接收EAP盘点信息")
    @PostMapping(value = "/reciveCheckInformation")
    public Result reciveCheckInformation(@RequestBody @Valid EapCheckSheetDto eapCheckSheetDto) {
        checkSheetService.processData(eapCheckSheetDto);
        return ResultGenerator.success("操作成功");
    }

    @ApiOperation(value = "创建盘点信息")
    @PostMapping(value = "/create")
    public Result create(@RequestBody @Valid CheckSheetDto checkSheetDto) {

        String sn = checkSheetDto.getSectionNo().toUpperCase();
        checkSheetDto.setCreateDate(new Date());
        String location = checkSheetDto.getLocation();
        checkSheetDto.setProcess(location.substring(location.indexOf("\\") + 1, location.lastIndexOf("\\")));
        CheckSheet checkSheet = fromDto(checkSheetDto);
        checkSheet.setSectionNo(sn);
        CheckSheet bySectionNo = checkSheetService.findBySectionNo(sn);
        if(null != bySectionNo){
            checkSheet.setId(bySectionNo.getId());
        }
        checkSheetService.saveOrUpdate(checkSheet);
        return ResultGenerator.success("操作成功");
    }

    @ApiOperation(value = "查询晶棒信息")
    @GetMapping(value = "/query")
    public Result query(@RequestParam String sectionNo) {

        CheckSheetDto checkSheetDto = new CheckSheetDto();

        Result<FbCheckOutData> fbCheckOutDataByFbSectionNo = fbFeignClients.findFbCheckOutDataByFbSectionNo(sectionNo.toUpperCase());
        if(null != fbCheckOutDataByFbSectionNo.getData()){
            FbCheckOutData data = fbCheckOutDataByFbSectionNo.getData();
            checkSheetDto.setLength(data.getLength());
            checkSheetDto.setWeight(data.getWeight());
            checkSheetDto.setLevel(data.getProductLevelFb());
            checkSheetDto.setMaterialStatus("成品方棒");
            checkSheetDto.setSpec(data.getSpecification());
            checkSheetDto.setDopant(data.getDopant());
            return ResultGenerator.success(checkSheetDto);
        }

        YbInspectData bySectionNo = ybInspectDataService.findBySectionNo(sectionNo);
        if(null != bySectionNo){
            checkSheetDto.setLength(bySectionNo.getActualLength());
            checkSheetDto.setWeight(bySectionNo.getTheoryWeight());
            checkSheetDto.setLevel(bySectionNo.getProductLevel());
            checkSheetDto.setMaterialStatus("半成品");
            checkSheetDto.setSpec(bySectionNo.getProductTypeSecond());
            checkSheetDto.setDopant(bySectionNo.getDopant());
            return ResultGenerator.success(checkSheetDto);
        }

        LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(sectionNo);
        if(null != byCrystalNo){
            checkSheetDto.setLength(byCrystalNo.getLength());
            checkSheetDto.setWeight(byCrystalNo.getWeight());
            checkSheetDto.setMaterialStatus("半成品");
            checkSheetDto.setSpec(byCrystalNo.getSpecs());
            checkSheetDto.setDopant(byCrystalNo.getDopant());
            return ResultGenerator.success(checkSheetDto);
        }
        //划线明细数据
        ProductionPaintingLineDetail paintingLineDetail = productionPaintingLineDetailService.findBySegmentNo(sectionNo);
        if (null != paintingLineDetail){
            checkSheetDto.setLength(paintingLineDetail.getLength());
            checkSheetDto.setWeight(paintingLineDetail.getWeight());
            checkSheetDto.setMaterialStatus("半成品");
            ProductionPaintingLine productionPaintingLine = productionPaintingLineService.get(paintingLineDetail.getOperateDrawId());
          if(null!= productionPaintingLine){
              checkSheetDto.setDopant(productionPaintingLine.getDopant());
          }
            return  ResultGenerator.success(checkSheetDto);
        }
//        //方棒反切记录
//        ReCut reCut = fbReCutService.findReCutInfoBySectionNo(sectionNo);
//        if (null != reCut){
//            checkSheetDto.setLength(reCut.getRecutLength());
//            return ResultGenerator.success(checkSheetDto);
//        }
//        //圆棒反切长度
//        ProductionBackCuttingRecord backCuttingRecord = productionRecutService.findBySegmentNo(sectionNo);
//        if (null != backCuttingRecord){
//            checkSheetDto.setLength(backCuttingRecord.getCutLength());
//            return ResultGenerator.success(checkSheetDto);
//        }

        return ResultGenerator.fail("未查询到晶体信息");
    }

    @ApiOperation(value = "删除盘点信息")
    @PostMapping("/delete")
    public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Long[] ids) {
        for (Long id : ids) {
            checkSheetService.delete(id);
        }
        return ResultGenerator.success();
    }


    public CheckSheet fromDto(CheckSheetDto dto) {
        return modelMapper.map(dto, CheckSheet.class);
    }
}
