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

import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.enums.SectionIndexRangeEnum;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.inspect.dao.YbInspectDataDao;
import com.sw.mes.yb.pad.service.ProductionRecutService;
import com.sw.mes.yb.printer.dto.DocketPrintDto;
import com.sw.mes.yb.printer.dto.DocketPrintResultDto;
import com.sw.mes.yb.printer.service.DocketPrintService;
import com.sw.mes.yb.production.dto.*;
import com.sw.mes.yb.production.entity.LongCrystal;
import com.sw.mes.yb.production.service.LongCrystalService;
import com.sw.mes.yb.turnstore.service.ProductionBindingTrayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: GaoXinJian
 * @Date: 2022-01-20 11:43
 */
@RestController
@RequestMapping("/docketPrint")
@Slf4j
@Api(description = "单据打印接口接口")
public class DocketPrintController {
    @Autowired
    private DocketPrintService docketPrintService;
    @Autowired
    private YbInspectDataDao inspectDataBaseDao;
    @Autowired
    private ProductionRecutService productionRecutService;
    @Autowired
    private ProductionBindingTrayService productionBindingTrayService;
    @Autowired
    private LongCrystalService longCrystalService;

    @ApiOperation(value = "pc单据打印")
    @PostMapping(value = "/docketPrint")
    public Result docketPrint(@ApiParam("单据打印实体") @RequestBody List<DocketPrintDto> docketPrintDtoList) {
        Long d1 = System.currentTimeMillis();
        DocketPrintResultDto docketPrintResultDto = docketPrintService.docketPrint(docketPrintDtoList);
        System.out.println(System.currentTimeMillis() - d1);
        return ResultGenerator.success(docketPrintResultDto);
    }
/*    @ApiOperation(value = "pad单据打印")
    @GetMapping(value = "/printShow")
    public Result printShow(@ApiParam("pad单据打印") @RequestParam(required = false) String trayNo, @RequestParam(required = false) String trayCode) {
        if (StringUtils.isBlank(trayNo) && StringUtils.isBlank(trayCode)) {
            return ResultGenerator.fail("单晶编号与托盘编号不能同时为空");
        }
        if (StringUtils.isBlank(trayNo)) {
            ProductionBindingTray productionBindingTray = productionBindingTrayService.getByTrayNoAndLocationNo(trayCode, "1");
            if (productionBindingTray == null) {
                throw new BusinessException("【" + trayCode + "】" + "托盘编号不存在");
            }
            if (StringUtils.isBlank(productionBindingTray.getCrystalNo())) {
                throw new BusinessException("【" + trayCode + "】托盘，未查到绑定单晶");
            }
            trayNo = productionBindingTray.getCrystalNo();
        }
        List<YbInspectData> inspectDataBases = inspectDataBaseDao.findBySectionNo(trayNo);
        if (inspectDataBases.size() == 0) {
            throw new BusinessException("检验数据不存在");
        }
        YbInspectData inspectDataBase = inspectDataBases.get(0);
        DocketPrintDto docketPrintDto = new DocketPrintDto();
        if (StringUtils.isNotBlank(inspectDataBase.getGradeData())) {
            ProductionBackCuttingRecord productionBackCuttingRecord = productionRecutService.findTopBySegmentNoOrderByIdDesc(trayNo);
            if (productionBackCuttingRecord == null) {
                throw new BusinessException("【" + trayNo + "】：为反切单晶，不存在反切数据，请检查");
            }
            //打印反切标签
            docketPrintDto.setPrintNum(1);
            docketPrintDto.setPrintModel("圆棒反切标签");
            docketPrintDto.setCode(trayNo);
            docketPrintDto.setIsPreview(true);
            docketPrintDto.setIsAssociated(false);
            docketPrintDto.setPrintType("圆棒反切标签");
        } else {
            //打印合格证标签
            docketPrintDto.setPrintNum(2);
            docketPrintDto.setPrintModel("合格证标签");
            docketPrintDto.setCode(trayNo);
            docketPrintDto.setIsPreview(true);
            docketPrintDto.setIsAssociated(false);
            docketPrintDto.setPrintType("合格证标签");
        }
        List<DocketPrintDto> docketPrintDtos = new ArrayList<>();
        docketPrintDtos.add(docketPrintDto);
        DocketPrintResultDto docketPrintResultDto = docketPrintService.docketPrint(docketPrintDtos);
        if (docketPrintResultDto.getFailedNum() > 0) {
            return ResultGenerator.fail("打印失败：" + docketPrintResultDto.getDocketPrintFailedDetailList().get(0).getDescription());
        }
        return ResultGenerator.success(docketPrintResultDto.getPreviewList());
    }*/


    @ApiOperation(value = "pad单据打印")
    @GetMapping(value = "/printShow")
    public Result printShow(@ApiParam("pad单据打印") @RequestParam(required = false) String trayNo, @RequestParam(required = false) String trayCode) {

        if(null != trayNo){
            String fromMark = trayNo.substring(SectionIndexRangeEnum.基地追溯产品类型.getBeginIndex(), SectionIndexRangeEnum.基地追溯产品类型.getEndIndex());
            if (fromMark.matches("[A-Z]+")) {
                if (trayNo.length() == SectionIndexRangeEnum.毛棒范围.getLength()) {
                    LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(trayNo);
                    if (null == byCrystalNo) {
                        throw new BusinessException("系统中未查到单晶" + "【" + trayNo + "】");
                    }
                } else if (trayNo.length() > SectionIndexRangeEnum.毛棒范围.getLength()) {
                    LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(trayNo.substring(SectionIndexRangeEnum.毛棒范围.getBeginIndex(), SectionIndexRangeEnum.毛棒范围.getEndIndex()));
                    if (null == byCrystalNo) {
                        throw new BusinessException("系统中未查到单晶" + "【" + trayNo + "】");
                    }
                } else {
                    throw new BusinessException("单晶编号异常" + "【" + trayNo + "】");
                }
            } else {
                if (trayNo.length() == 12) {
                    LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(trayNo);
                    if (null == byCrystalNo) {
                        throw new BusinessException("系统中未查到单晶" + "【" + trayNo + "】");
                    }
                } else if (trayNo.length() > 12) {
                    LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(trayNo.substring(0, 12));
                    if (null == byCrystalNo) {
                        throw new BusinessException("系统中未查到单晶" + "【" + trayNo + "】");
                    }
                } else {
                    throw new BusinessException("单晶编号异常" + "【" + trayNo + "】");
                }
            }
        } else {
            throw new BusinessException("单晶编号不可以为空");
        }

        DocketPrintDto docketPrintDto = new DocketPrintDto();
        docketPrintDto.setPrintNum(1);
        docketPrintDto.setPrintModel("PDA补签");
        docketPrintDto.setCode(trayNo);
        docketPrintDto.setIsPreview(true);
        docketPrintDto.setIsAssociated(false);
        docketPrintDto.setPrintType("PDA补签");
        List<DocketPrintDto> docketPrintDtos = new ArrayList<>();
        docketPrintDtos.add(docketPrintDto);
        DocketPrintResultDto docketPrintResultDto = docketPrintService.docketPrint1(docketPrintDtos);
        if (docketPrintResultDto.getFailedNum() > 0) {
            return ResultGenerator.fail("打印失败：" + docketPrintResultDto.getDocketPrintFailedDetailList().get(0).getDescription());
        }
        return ResultGenerator.success(docketPrintResultDto.getPreviewList());
    }

    /**
     * 合格证标签打印查询
     * @param docketPrintDto
     * @return
     */
    @PostMapping(value = "/certificateLabelFind")
    public Result certificateLabelFind(@ApiParam("单据打印实体") @RequestBody DocketPrintDto docketPrintDto) {
        DocketPrintResultDto docketPrintResultDto = docketPrintService.certificateLabelFind(docketPrintDto);
        return ResultGenerator.success(docketPrintResultDto);
    }



    @ApiOperation(value = "打印配料任务配方方案")
    @PostMapping("/printLoadingRecipe")
    public Result printLoadingRecipe(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintLoadingRecipeDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printLoadingRecipe(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @ApiOperation(value = "打印灌料任务配方方案")
    @PostMapping("/printFillingRecipe")
    public Result printFillingRecipe(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintFillingRecipeDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printFillingRecipe(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }


    @ApiOperation(value = "Android打印小箱成箱(成品)标签/pc补打")
    @PostMapping("/printCleanBox")
    public Result printCleanBox(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintCleanBoxDto dto) {
        List preview;
        try {
            preview = docketPrintService.printCleanBox(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @ApiOperation(value = "划线标签打印")
    @PostMapping("/printPaintLabel")
    public Result printPaintLabel(@Valid @RequestBody @ApiParam(value = "打印信息") PrintPaintLabelDto dto) {
        List preview;
        try {
            preview = docketPrintService.printPaintLabel(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @Jinko(author = "yyl", value = "Android打印组托入库标签/pc补打")
    @ApiOperation(value = "Android打印组托入库标签/pc补打")
    @PostMapping("/printCleanGroup")
    public Result printCleanGroup(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintCleanGroupDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printCleanGroup(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @Jinko(author = "yyl", value = "Android打印返工标签/pc补打")
    @ApiOperation(value = "Android打印返工标签/pc补打")
    @PostMapping("/printReWorking")
    public Result printCleanGroup(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintReWorkingDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printReWorking(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @Jinko(author = "yyl", value = "Android打印回收料标签/pc补打")
    @ApiOperation(value = "打印回收料标签")
    @PostMapping("/printRecycledMaterial")
    public Result printRecycledMaterial(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintRecycledMaterialDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printRecycledMaterial(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @ApiOperation(value = "pc籽晶标签打印")
    @PostMapping(value = "/crystallonPrint")
    public Result crystallonPrint(@ApiParam("单据打印实体") @RequestBody List<DocketPrintDto> docketPrintDtoList) {
        Map<String, List<String>> map = docketPrintService.crystallonPrint(docketPrintDtoList);
        return ResultGenerator.success(map);
    }

    @ApiOperation(value = "Android打印回收料组托标签")
    @PostMapping("/printRecycledGroup")
    public Result printCleanGroup(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintRecycledGroupDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printRecycledGroup(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @ApiOperation(value = "打印载具箱标签")
    @PostMapping("/printBoxNo")
    public Result printBoxNo(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintBoxNoDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printBoxNo(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }


    @Jinko(author = "yyl", value = "Android打印分选成箱标签/pc补打")
    @ApiOperation(value = "打印分选成箱标签")
    @PostMapping("/printCleanFxToBox")
    public Result printCleanFxToBox(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintCleanFxToBoxDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printCleanFxToBox(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @ApiOperation(value = "打印拆花篮装箱标签")
    @PostMapping("/printCleanPackagingBox")
    public Result printCleanPackagingBox(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintCleanPackingBoxDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printCleanPackagingBox(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }
    @Jinko(author = "yyl", value = "打印单晶余料标签")
    @ApiOperation(value = "打印单晶余料标签")
    @PostMapping("/printSurplusInfo")
    public Result printSurplusInfo(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintSurplusInfoDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printSurplusInfo(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    /**
     * 打印籽晶标签
     * @param dto
     * @return
     */
    @ApiOperation(value = "打印籽晶标签")
    @PostMapping("/printSeedCode")
    public Result printSeedCode(@Valid @RequestBody @ApiParam(value = "打印信息") DocketSeedCodePrintDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printBristleMaterial(dto);
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }
    /**
     * 打印母合金标签
     * @param dto
     * @return
     */
    @ApiOperation(value = "打印母合金标签")
    @PostMapping("/printMasterAlloy")
    public Result printMasterAlloy(@Valid @RequestBody @ApiParam(value = "打印信息") DocketMasterAlloyPrintDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printMasterAlloy(dto);
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }
    /**
     * 打印配料单标签
     * @param dto
     * @return
     */
    @ApiOperation(value = "打印配料单标签")
    @PostMapping("/printIngredientList")
    public Result printIngredientList(@Valid @RequestBody @ApiParam(value = "打印信息") DocketIngredientListPrintDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.printIngredientList(dto);
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }

    @Jinko(author = "yyl", value = "打印余料采集标签")
    @ApiOperation(value = "打印余料采集标签")
    @PostMapping("/recycledGroupPrint")
    public Result recycledGroupPrint(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPrintRecycledInfoDto dto) {
        List<String> preview;
        try {
            preview = docketPrintService.recycledGroupPrint(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success(preview);
    }
    /**
     * 打印圆棒信息条码
     * @param dto
     * @return
     */
//    @ApiOperation(value = "打印圆棒物料信息条码")
//    @PostMapping("/printPoleMaterial")
//    public Result printPoleMaterial(@Valid @RequestBody @ApiParam(value = "打印信息") DocketPolePrintDto dto) {
//        List<String> preview;
//        try {
//            preview = docketPrintService.printPoleMaterial(dto);
//        } catch (Exception e) {
//            return ResultGenerator.fail(e.getMessage());
//        }
//        return ResultGenerator.success(preview);
//    }
}
