//package com.sw.mes.yb.pad.service.impl;
//
//import com.alibaba.fastjson.JSON;
//import com.sw.mes.base.factory.inspect.entity.DefaultCrystalAttrs;
//import com.sw.mes.base.factory.inspect.service.DefaultCrystalAttrService;
//import com.sw.mes.base.workinprocess.dto.CurrentWipStorageStatusDto;
//import com.sw.mes.base.workinprocess.dto.PlanProcessingOrderWipStorageDto;
//import com.sw.mes.base.workinprocess.service.WipStorageService;
//import com.sw.mes.core.base.BaseServiceImpl;
//import com.sw.mes.core.context.BaseContextHandler;
//import com.sw.mes.core.enums.EnumValuesFactory;
//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.core.utils.NumberUtil;
//import com.sw.mes.enumvalue.EnumValuesServer;
//import com.sw.mes.inspect.dao.YbInspectDataDao;
//import com.sw.mes.inspect.entity.YbInspectData;
//import com.sw.mes.utils.StringUtils;
//import com.sw.mes.yb.pad.dao.PieceDetailDao;
//import com.sw.mes.yb.pad.dao.ProductionPaintingLineDetailDao;
//import com.sw.mes.yb.pad.dao.ProductionRecutDao;
//import com.sw.mes.yb.pad.dao.SectionNoDetailDao;
//import com.sw.mes.yb.pad.dto.ProductionBackCuttingRecordDto;
//import com.sw.mes.yb.pad.dto.RecutInfoDetailInfo;
//import com.sw.mes.yb.pad.entity.*;
//import com.sw.mes.yb.pad.service.ProductionPaintingLineDetailExceptionService;
//import com.sw.mes.yb.pad.service.ProductionPaintingLineDetailService;
//import com.sw.mes.yb.pad.service.ProductionRecutService;
//import com.sw.mes.yb.turnstore.dao.ProductionBindingTrayDao;
//import com.sw.mes.yb.turnstore.entity.ProductionBindingTray;
//import org.modelmapper.ModelMapper;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.CollectionUtils;
//
//import java.math.BigDecimal;
//import java.math.RoundingMode;
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Service
//@Transactional
//public class ProductionRecutServiceImpl1 extends BaseServiceImpl<ProductionBackCuttingRecord> implements ProductionRecutService {
//    ProductionRecutDao dao;
//
//    @Autowired
//    public void setDao(ProductionRecutDao dao) {
//        super.setBaseDao(dao);
//        this.dao = dao;
//    }
//
//    @Autowired
//    ModelMapper modelMapper;
//    @Autowired
//    private ProductionBindingTrayDao productionBindingTrayDao;
//
//    @Autowired
//    private WipStorageService wipStorageService;
//
//    @Autowired
//    private ProductionPaintingLineDetailService productionPaintingLineDetailService;
//
//    @Autowired
//    private PieceDetailDao pieceDetailDao;
//
//    @Autowired
//    private ProductionPaintingLineDetailDao productionPaintingLineDetailDao;
//
//    @Autowired
//    private SectionNoDetailDao sectionNoDetailDao;
//
//    @Autowired
//    private DefaultCrystalAttrService defaultCrystalAttrService;
//
//    @Autowired
//    private YbInspectDataDao inspectDataBaseDao;
//
//    @Autowired
//    private ProductionPaintingLineDetailExceptionService productionPaintingLineDetailExceptionService;
//
//    // 通过托号获取晶段基本信息
//    @Override
//    public Result findSegmentNoInfo(String trayNo) {
//        // 通过托盘绑定信息 找到对应晶段
//        List<ProductionBindingTray> byTrayNo = this.productionBindingTrayDao.findByTrayNo(trayNo);
//        if (byTrayNo.size() != 1) {
//            return ResultGenerator.fail("此载具找不到唯一晶段信息");
//        }
//
//        ProductionBindingTray productionBindingTray = byTrayNo.get(0);
//
//        // 晶段编号,用于记录返切画线生成编号
//        String segmentNo = productionBindingTray.getCrystalNo();
//
//
//        // 检验数据提交判断是否在圆棒检验已进站状态
//        wipStorageService.selectWipStorage(segmentNo, EnumValuesFactory.WipStorageCode.圆棒检验.getKey(), true);
//
////        Integer isCutBack = this.inspectDataDAO.getLevelClassBySectionNo(segmentNo);
////        if (isCutBack != null && isCutBack == 0) {
////            return ResultGenerator.fail("单晶编号:【" + segmentNo + "】评级无法进行返切 ");
////        }
//
//        ProductionBackCuttingRecord bySectionNo = findBySegmentNo(segmentNo);
//        if (bySectionNo != null) {
//            return ResultGenerator.fail("该单晶已有反切记录!请勿重复操作");
//        }
//
//        // 用晶段编号去画线记录明细表找到对应的长度信息
//        BigDecimal length = null;
//        List<YbInspectData> inspectDataBases = this.inspectDataBaseDao.findBySectionNo(segmentNo);
//        if (inspectDataBases.size() == 0) {
//            return ResultGenerator.fail("该晶段没有检验数据，无法获取长度");
//        }
//        length = inspectDataBases.get(0).getActualLength();
//        List<SectionNoDetail> sectionNoDetails = sectionNoDetailDao.findBySectionNo(segmentNo);
//        if (sectionNoDetails.size() == 0) {
//            return ResultGenerator.fail("该晶段信息不存在");
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("segmentNo", segmentNo);
//        map.put("segmentId", sectionNoDetails.get(0).getId());
//        map.put("length", length);
//        return ResultGenerator.success(map);
//    }
//
//    @Override
//    public Result findSegmentNoInfoBySectionNo(String sectionNo) {
//        // 检验数据提交判断是否在圆棒检验已进站状态
//        //wipStorageService.selectWipStorage(sectionNo, EnumValuesFactory.WipStorageCode.圆棒检验.getKey(), true);
//
//        //202211-01
//        //判断是否在圆棒检验已进站状态或者截断待进站状态
//        PlanProcessingOrderWipStorageDto planProcessingOrderWipStorageDto = wipStorageService.selectWipStorage2(sectionNo, null);
////        Integer isCutBack = this.inspectDataDAO.getLevelClassBySectionNo(sectionNo);
////        if (isCutBack != null && isCutBack == 0) {
////            return ResultGenerator.fail("单晶编号:【" + sectionNo + "】评级无法进行返切 ");
////        }
//
////        String gradeDataBySectionNo = inspectDataDAO.getGradeDataBySectionNo(sectionNo);
////        if (StringUtils.isBlank(gradeDataBySectionNo) && (!gradeDataBySectionNo.contains("返切") || !gradeDataBySectionNo.contains("反切"))){
////            return ResultGenerator.fail("单晶编号:【" + sectionNo + "】评级无法进行返切 ");
////        }
//
//        ProductionBackCuttingRecord bySectionNo = findBySegmentNo(sectionNo);
//        if (bySectionNo != null) {
//            return ResultGenerator.fail("该单晶已有反切记录!请勿重复操作");
//        }
//
//        // 用晶段编号去画线记录明细表找到对应的长度信息
//        BigDecimal length = null;
//
//        if (planProcessingOrderWipStorageDto.getWipStorage().getProcessName().equals("截断")) {
//            ProductionPaintingLineDetail bySegmentNo = productionPaintingLineDetailService.findBySegmentNo(sectionNo);
//            if (bySegmentNo != null) {
//                length = bySegmentNo.getLength();
//            }
//        } else {
//            List<YbInspectData> inspectDataBases = this.inspectDataBaseDao.findBySectionNo(sectionNo);
//            if (inspectDataBases.size() == 0) {
//                return ResultGenerator.fail("该晶段没有检验数据，无法获取长度");
//            }
//            length = inspectDataBases.get(0).getActualLength();
//        }
//
//
//        List<SectionNoDetail> sectionNoDetails = sectionNoDetailDao.findBySectionNo(sectionNo);
//        if (sectionNoDetails.size() == 0) {
//            return ResultGenerator.fail("该晶段信息不存在");
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("segmentNo", sectionNo);
//        map.put("segmentId", sectionNoDetails.get(0).getId());
//        map.put("length", length);
//        return ResultGenerator.success(map);
//    }
//
//
//    // 根据现有反切规则 获取反切后的编号
//    @Override
//    public Result<List<RecutInfoDetailInfo>> getSegmentNo(ProductionBackCuttingRecordDto backCuttingRecordDto) {
//        // 原有晶段编号
//        String segmentNo = backCuttingRecordDto.getSegmentNo();
//        if (backCuttingRecordDto.getDetail().size() < 1) {
//            throw new BusinessException("切断明细内，至少包含一段晶段");
//        }
//
//        List<RecutInfoDetailInfo> detail = backCuttingRecordDto.getDetail();
//        //晶段数量
//        int segmentCount = 0;
//        //头尾样片数量，不合格反切时使用
//        int headPieceCount = 0, tailPieceCount = 0;
//        boolean isheadPiece = true;
//        for (RecutInfoDetailInfo recutInfoDetailInfo : backCuttingRecordDto.getDetail()) {
//            if ("晶段".equals(recutInfoDetailInfo.getType())) {
//                segmentCount++;
//                isheadPiece = false;
//            }
//            if ("样片".equals(recutInfoDetailInfo.getType())) {
//                if (isheadPiece) {
//                    headPieceCount++;
//                } else {
//                    tailPieceCount++;
//                }
//            }
//        }
//        if (segmentCount > 9) {
//            throw new BusinessException("反切段数不能大于10");
//        }
//        List<SectionNoDetail> sectionNoDetails = sectionNoDetailDao.findBySectionNo(segmentNo);
//        SectionNoDetail sectionNoDetail = sectionNoDetails.get(0);
//
//        int materialIndex = 1;
//        //=========================================================20220907zyy处理回收料编号序号
//        String tSegmentNo;
//        if (!(segmentNo.substring(segmentNo.length() - 1)).equals("Y")) {
//            tSegmentNo = segmentNo;
//        } else {
//            tSegmentNo = segmentNo.substring(0, segmentNo.length() - 1);
//        }
//        List<ProductionPaintingLineDetail> paintingLineDetailByLikeSegmentNo = productionPaintingLineDetailDao.findByLikeSegmentNo(tSegmentNo);
//        if (segmentNo.substring(segmentNo.length() - 2).equals("WY")) {
//            materialIndex = 1;
//        } else if (null != paintingLineDetailByLikeSegmentNo && paintingLineDetailByLikeSegmentNo.size() > 0) {
//            materialIndex = Integer.parseInt(paintingLineDetailByLikeSegmentNo.get(0).getNo().substring(paintingLineDetailByLikeSegmentNo.get(0).getNo().length() - 1)) + 1;
//        }
//        //=========================================================
//
//
//        //============================处理晶棒编号后缀字母，
//        char suffix = 'A';                //用于“Y”结尾的晶段 分段反切
//        char suffix2 = segmentNo.charAt(segmentNo.length() - 1);//用于非“Y”结尾的晶段 分段反切
//        List<SectionNoDetail> bySectionNo = sectionNoDetailDao.findByLikeSectionNo(segmentNo.substring(0, segmentNo.length() - 1));
//        if (null != bySectionNo && bySectionNo.size() > 0) {
//            String lastSectionNo = bySectionNo.get(0).getSectionNo();
//            suffix2 = (char) (lastSectionNo.charAt(lastSectionNo.length() - 1) + 1);
//        } else {
//            suffix2 = (char) (suffix2 + 1);
//        }
//        //============================
//
//
//        //分段反切
//        if (segmentCount > 1) {
//            // 反切规则集合
//            int segmentIndex = 1;
//            tailPieceCount = 0;
//            headPieceCount = 0;
//            //最后一段的序号
//            int lsatSegmentIndexNo = 1;
//            //20220825反切划线新命名方式=========================================================================
//            List<String> detailTypes = detail.stream().map(t -> t.getType()).collect(Collectors.toList());
//            boolean flag = true;
//            for (String detailType : detailTypes) {
//                if (detailType.equals("回收料")) {
//                    flag = false;
//                }
//            }
//
//            if (detailTypes.contains("回收料")) {       //“回收料”+“晶段”  类型
//                for (int i = 0; i < detail.size(); i++) {           //命名规则
//                    //回收料命名
//                    if ("回收料".equals(detail.get(i).getType())) {
//                        detail.get(i).setSegmentNo(segmentNo + "X" + materialIndex);
//                        materialIndex++;
//                    } else if ("晶段".equals(detail.get(i).getType())) {
//                        if ((segmentNo.substring(segmentNo.length() - 1)).equals("Y")) { //晶段结尾是Y，多个晶段
//                            //char suffix='A';
//                            detail.get(i).setTailPieceNum(0);
//                            detail.get(i).setHeadPieceNum(0);
//                            detail.get(i).setMaterialNum(0);
//                            detail.get(i).setSegmentNo(segmentNo.substring(0, segmentNo.length() - 1) + suffix);
//                            //头样片最大序号赋值
//                            detail.get(i).setHeadPieceNum(headPieceCount);
//                            headPieceCount = 0;
//                            if (segmentIndex != 0) {
//                                //尾样片最大序号赋值
//                                detail.get(lsatSegmentIndexNo).setTailPieceNum(tailPieceCount);
//                            }
//                            tailPieceCount = 0;
//                            suffix++;
//                            lsatSegmentIndexNo = i;
//                        } else if (!(segmentNo.substring(segmentNo.length() - 1)).equals("Y")) {//晶段结尾不是Y，多个晶段
//                            detail.get(i).setTailPieceNum(0);
//                            detail.get(i).setHeadPieceNum(0);
//                            detail.get(i).setMaterialNum(0);
//                            detail.get(i).setSegmentNo(segmentNo.substring(0, segmentNo.length() - 1) + suffix2);
//                            suffix2++;
//                            //头样片最大序号赋值
//                            detail.get(i).setHeadPieceNum(headPieceCount);
//                            headPieceCount = 0;
//                            if (segmentIndex != 0) {
//                                //尾样片最大序号赋值
//                                detail.get(lsatSegmentIndexNo).setTailPieceNum(tailPieceCount);
//                            }
//                            tailPieceCount = 0;
//                            //晶段段数++
//                            //segmentIndex++;
//                            lsatSegmentIndexNo = i;
//                        }
//                    } else if ("样片".equals(detail.get(i).getType())) {
//                        if (segmentIndex == 1) {
//                            detail.get(i).setSegmentNo(segmentNo + "F" + segmentIndex + "T1");
//                            headPieceCount++;
//                        } else {
//                            detail.get(i).setSegmentNo(segmentNo + "F" + (segmentIndex - 1) + "W1");
//                            tailPieceCount++;
//                            detail.get(segmentIndex - 1).setTailPieceNum(tailPieceCount);
//                        }
//                    }
//                }
//            }
//            //不包含回收料的情形
//            else if (flag && (segmentNo.substring(segmentNo.length() - 1)).equals("Y")) {          //晶段号最后一位是Y，且类型全部都是晶段
//                for (int i = 0; i < detail.size(); i++) {
//                    detail.get(i).setTailPieceNum(0);
//                    detail.get(i).setHeadPieceNum(0);
//                    detail.get(i).setMaterialNum(0);
//                    detail.get(i).setSegmentNo(segmentNo.substring(0, segmentNo.length() - 1) + suffix);
//                    //头样片最大序号赋值
//                    detail.get(i).setHeadPieceNum(headPieceCount);
//                    headPieceCount = 0;
//                    if (segmentIndex != 0) {
//                        //尾样片最大序号赋值
//                        detail.get(lsatSegmentIndexNo).setTailPieceNum(tailPieceCount);
//                    }
//                    tailPieceCount = 0;
//                    suffix++;
//                    lsatSegmentIndexNo = i;
//                }
//            } else if (flag && !(segmentNo.substring(segmentNo.length() - 1)).equals("Y")) {        //晶段号最后一位 不是Y，且类型都是晶段  (即二次切割)
//                for (int i = 0; i < detail.size(); i++) {
//                    detail.get(i).setTailPieceNum(0);
//                    detail.get(i).setHeadPieceNum(0);
//                    detail.get(i).setMaterialNum(0);
//                    detail.get(i).setSegmentNo(segmentNo.substring(0, segmentNo.length() - 1) + suffix2);
//                    suffix2++;
//                    //头样片最大序号赋值
//                    detail.get(i).setHeadPieceNum(headPieceCount);
//                    headPieceCount = 0;
//                    if (segmentIndex != 0) {
//                        //尾样片最大序号赋值
//                        detail.get(lsatSegmentIndexNo).setTailPieceNum(tailPieceCount);
//                    }
//                    tailPieceCount = 0;
//                    lsatSegmentIndexNo = i;
//                }
//            } else if (!detailTypes.contains("晶段") && !detailTypes.contains("样片") && !detailTypes.contains("回收料")) {
//                for (int i = 0; i < detail.size(); i++) {
//                    throw new BusinessException("【" + detail.get(i).getType() + "】该类型未处理");
//                }
//            }
//            //====================================================================================20220825
//
////            for (int i = 0; i < detail.size(); i++) {
////                if ("晶段".equals(detail.get(i).getType())) {
////                    detail.get(i).setTailPieceNum(0);
////                    detail.get(i).setHeadPieceNum(0);
////                    detail.get(i).setMaterialNum(0);
////                    //detail.get(i).setSegmentNo(segmentNo + "F" + segmentIndex);
////                    detail.get(i).setSegmentNo(segmentNo + "Y" + segmentIndex);
////                    //头样片最大序号赋值
////                    detail.get(i).setHeadPieceNum(headPieceCount);
////                    headPieceCount = 0;
////                    if (segmentIndex != 0) {
////                        //尾样片最大序号赋值
////                        detail.get(lsatSegmentIndexNo).setTailPieceNum(tailPieceCount);
////                    }
////                    tailPieceCount = 0;
////                    //晶段段数++
////                    segmentIndex++;
////                    lsatSegmentIndexNo = i;
////                } else if ("样片".equals(detail.get(i).getType())) {
////                    if (segmentIndex == 1) {
////                        detail.get(i).setSegmentNo(segmentNo + "F" + segmentIndex + "T1");
////                        headPieceCount++;
////                    } else {
////                        detail.get(i).setSegmentNo(segmentNo + "F" + (segmentIndex - 1) + "W1");
////                        tailPieceCount++;
////                        detail.get(segmentIndex - 1).setTailPieceNum(tailPieceCount);
////                    }
////                } else if ("回收料".equals(detail.get(i).getType())) {
////                    detail.get(i).setSegmentNo(segmentNo + "X" + materialIndex);
////                    materialIndex++;
////                } else {
////                    throw new BusinessException("【" + detail.get(i).getType() + "】该类型未处理");
////                }
////            }
//        }
//        // 不合格反切
//        else {
//            if (headPieceCount > 1) {
//                throw new BusinessException("头样片，只能取一片");
//            }
//            if (tailPieceCount > 1) {
//                throw new BusinessException("尾样片，只能取一片");
//            }
//            SectionNoDetail sectionNoDetailOld = new SectionNoDetail();
//            BeanUtils.copyProperties(sectionNoDetail, sectionNoDetailOld);
//            if (sectionNoDetails.size() == 0) {
//                throw new BusinessException("晶段编号不存在");
//            }
//            //不合格反切继承，上一次的头尾样片序号
//            Integer headPieceIndex = NumberUtil.Object2Integer(sectionNoDetail.getHeadPieceNum(), 0);
//            Integer tailPieceIndex = NumberUtil.Object2Integer(sectionNoDetail.getTailPieceNum(), 0);
//            //Integer materialIndex = NumberUtil.Object2Integer(sectionNoDetail.getMaterialNum(), 0);
//            Integer segmentIndex = null;
//            boolean isheadPiece1 = true;
//
//            for (int i = 0; i < detail.size(); i++) {
//                if ("晶段".equals(detail.get(i).getType())) {
//                    // 新的需求：根据晶段的划线就中是否有异常划线记录（返切划线异常详细信息），有：不合格反切后新的晶段编号不变；没有：按照已有规则赋予新的编号
//                    List<ProductionPaintingLineDetailException> bySegmentNo = productionPaintingLineDetailExceptionService.findBySegmentNo(segmentNo);
//                    //划线信息中有异常段信息且还没有切掉异常段
//                    if (!CollectionUtils.isEmpty(bySegmentNo) && bySegmentNo.get(0).getCut() == 0) {
//                        detail.get(i).setSegmentNo(segmentNo);
//                    } else {
//                        //晶段结尾是Y
//                        if ((segmentNo.substring(segmentNo.length() - 1)).equals("Y")) {
//                            detail.get(i).setSegmentNo(segmentNo.substring(0, segmentNo.length() - 1) + suffix);
//                            //晶段结尾不是Y
//                        } else if (!(segmentNo.substring(segmentNo.length() - 1)).equals("Y")) {
//                            detail.get(i).setSegmentNo(segmentNo.substring(0, segmentNo.length() - 1) + suffix2);
//                        }
//                    }
//                    isheadPiece1 = false;
//                    detail.get(i).setHeadPieceNum(headPieceIndex);
//                    segmentIndex = i;
//                } else if ("样片".equals(detail.get(i).getType())) {
//                    if (isheadPiece1) {
//                        detail.get(i).setSegmentNo(segmentNo + "T" + ++headPieceIndex);
//                    } else {
//                        detail.get(i).setSegmentNo(segmentNo + "W" + ++tailPieceIndex);
//                        detail.get(segmentIndex).setTailPieceNum(tailPieceIndex);
//                    }
//                } else if ("回收料".equals(detail.get(i).getType())) {
//                    detail.get(i).setSegmentNo(segmentNo + "X" + materialIndex);
//                    materialIndex++;
//                }
//            }
//            if (segmentIndex != null) {
//                detail.get(segmentIndex).setMaterialNum(materialIndex);
//            }
//        }
//        backCuttingRecordDto.setBackCutType(segmentCount > 1);
//        backCuttingRecordDto.setDetail(detail);
//        return ResultGenerator.success(detail);
//    }
//
//    // 保存反切画线信息
//    @Override
//    public void createBackCuttingRecord(ProductionBackCuttingRecordDto backCuttingRecordDto, BigDecimal weightLossCoefficient, DefaultCrystalAttrs defaultCrystalAttrList, boolean isInheritSample) {
//        backCuttingRecordDto.setStatus(false);
//        List<ProductionPaintingLineDetail> detailList = new ArrayList<>();
//        List<SectionNoDetail> sectionNoDetails = this.sectionNoDetailDao.findBySectionNo(backCuttingRecordDto.getSegmentNo());
//        if (sectionNoDetails == null || sectionNoDetails.size() <= 0) {
//            throw new RuntimeException("该圆棒:" + backCuttingRecordDto.getSegmentNo() + "不存在 ,无法返切");
//        }
//
//        // 通过圆棒编号去圆棒信息表中找到对应属性信息
//        SectionNoDetail sectionNoDetail = sectionNoDetails.get(0);
//        ProductionBackCuttingRecord productionBackCuttingRecord = this.fromDto(backCuttingRecordDto);
//        Integer segmentId = sectionNoDetail.getId().intValue();
//        Long crystalId = sectionNoDetail.getCrystalId();
//        // 获取所有画线信息
//        List<RecutInfoDetailInfo> data = backCuttingRecordDto.getDetail();
//        productionBackCuttingRecord.setSegmentNo(backCuttingRecordDto.getSegmentNo());
//        if (StringUtils.isBlank(productionBackCuttingRecord.getUserName())) {
//            productionBackCuttingRecord.setUserName(BaseContextHandler.getUsername());
//        }
//        productionBackCuttingRecord.setLineEndTime(new Date());
//        ProductionBackCuttingRecord save = dao.save(productionBackCuttingRecord);
//        Long id = save.getId();
//
//        int sectionCount = 0;
//        for (RecutInfoDetailInfo recutInfoDetailInfo : data) {
//            if ("晶段".equals(recutInfoDetailInfo.getType())) {
//                sectionCount++;
//            }
//        }
//        if (sectionCount <= 0) {
//            throw new RuntimeException("反切晶段数量不能为0");
//        }
//        // 晶段集合
//        List<SectionNoDetail> segments = new ArrayList<>();
//        List<PieceDetail> pieceDetails = new ArrayList<>();
//        // 晶段长度
//        BigDecimal barLength = backCuttingRecordDto.getBarLength();
//
//        Integer segmentNum = sectionNoDetail.getSegmentNum();
//        for (int i = 0; i < data.size(); i++) {
//            RecutInfoDetailInfo datum = data.get(i);
//            switch (datum.getType()) {
//                case "样片":
//                case "尾样片":
//                    detailList.add(setLineDetail(segmentId, crystalId, id, datum, EnumValuesServer.LineType.样片.getKey(), segmentNum));
//                    barLength = barLength.subtract(datum.getLength());
//                    //创建样片信息
//                    PieceDetail pieceDetail = new PieceDetail();
//                    pieceDetail.setPieceNo(datum.getSegmentNo());
//                    pieceDetail.setConductiveType(sectionNoDetail.getConductiveType());
//                    pieceDetail.setDopant(sectionNoDetail.getDopant());
//                    pieceDetail.setSpecs(sectionNoDetail.getSpecs());
//                    pieceDetail.setCrystalId(sectionNoDetail.getCrystalId());
//                    pieceDetail.setCrystalNo(sectionNoDetail.getCrystalNo());
//                    pieceDetail.setLotNo(sectionNoDetail.getLotNo());
//                    pieceDetail.setEquipmentCode(sectionNoDetail.getEquipmentCode());
//                    pieceDetail.setProductType(sectionNoDetail.getProductType());
//                    pieceDetail.setProductCode(sectionNoDetail.getProductCode());
//                    pieceDetail.setProductGrade(sectionNoDetail.getProductGrade());
//                    pieceDetail.setSegmentId(sectionNoDetail.getId());
//                    pieceDetails.add(pieceDetail);
//                    break;
//                case "晶段":
//                    // 重置样片数据
//                    detailList.add(setLineDetail(segmentId, crystalId, id, datum, EnumValuesServer.LineType.晶段.getKey(), segmentNum));
//                    SectionNoDetail sectionNoDetail1 = new SectionNoDetail();
//                    //创建晶段数据
//                    if (!backCuttingRecordDto.getBackCutType()) {
//                        //不合格反切的
//                        List<ProductionPaintingLineDetailException> bySegmentNo = productionPaintingLineDetailExceptionService.findBySegmentNo(datum.getSegmentNo());
//                        //有异常段信息 且还没有切掉异常段的，晶段反切后编号不变，不产生新的晶段信息，也不产生新的工单
//                        if (!CollectionUtils.isEmpty(bySegmentNo) && bySegmentNo.get(0).getCut() == 0) {
//                            sectionNoDetail1 = sectionNoDetail;
//                            productionBackCuttingRecord.setSectionNoDetailJson(JSON.toJSONString(sectionNoDetail));
//                            dao.save(productionBackCuttingRecord);
//                        } else {
//                            //没有异常段信息的，晶段反切后生成新编号，产生新的晶段信息、新的工单
//                            sectionNoDetail1.setSectionNo(data.get(i).getSegmentNo());
//                            sectionNoDetail1.setConductiveType(sectionNoDetail.getConductiveType());
//                            sectionNoDetail1.setDopant(sectionNoDetail.getDopant());
//                            sectionNoDetail1.setSpecs(sectionNoDetail.getSpecs());
//                            sectionNoDetail1.setCrystalNo(sectionNoDetail.getCrystalNo());
//                            sectionNoDetail1.setCrystalId(sectionNoDetail.getCrystalId());
//                            // 该圆棒父id则是被反切的圆棒id
//                            sectionNoDetail1.setParentId(sectionNoDetail.getId());
//                            sectionNoDetail1.setLotNo(sectionNoDetail.getLotNo());
//                            sectionNoDetail1.setEquipmentCode(sectionNoDetail.getEquipmentCode());
//                            sectionNoDetail1.setProductType(sectionNoDetail.getProductType());
//                            sectionNoDetail1.setProductCode(sectionNoDetail.getProductCode());
//                            sectionNoDetail1.setProductGrade(sectionNoDetail.getProductGrade());
//                            sectionNoDetail1.setSegmentNum(sectionNoDetail.getSegmentNum());
//                            sectionNoDetail1.setCreateUser(BaseContextHandler.getUsername());
//                            sectionNoDetail1.setCreateDate(new Date());
//                            productionBackCuttingRecord.setSectionNoDetailJson(JSON.toJSONString(sectionNoDetail));
//                            dao.save(productionBackCuttingRecord);
//                        }
//                    } else {
//                        sectionNoDetail1.setSectionNo(data.get(i).getSegmentNo());
//                        sectionNoDetail1.setConductiveType(sectionNoDetail.getConductiveType());
//                        sectionNoDetail1.setDopant(sectionNoDetail.getDopant());
//                        sectionNoDetail1.setSpecs(sectionNoDetail.getSpecs());
//                        sectionNoDetail1.setCrystalNo(sectionNoDetail.getCrystalNo());
//                        sectionNoDetail1.setCrystalId(sectionNoDetail.getCrystalId());
//                        // 该圆棒父id则是被反切的圆棒id
//                        sectionNoDetail1.setParentId(sectionNoDetail.getId());
//                        sectionNoDetail1.setLotNo(sectionNoDetail.getLotNo());
//                        sectionNoDetail1.setEquipmentCode(sectionNoDetail.getEquipmentCode());
//                        sectionNoDetail1.setProductType(sectionNoDetail.getProductType());
//                        sectionNoDetail1.setProductCode(sectionNoDetail.getProductCode());
//                        sectionNoDetail1.setProductGrade(sectionNoDetail.getProductGrade());
//                        sectionNoDetail1.setSegmentNum(sectionNoDetail.getSegmentNum());
//                        sectionNoDetail1.setCreateUser(BaseContextHandler.getUsername());
//                        sectionNoDetail1.setCreateDate(new Date());
//                    }
//                    sectionNoDetail1.setLength(datum.getLength());
//                    if (weightLossCoefficient != null) {
//                        sectionNoDetail1.setWeight(datum.getLength().divide(weightLossCoefficient, 2, RoundingMode.CEILING));
//                    }
//                    sectionNoDetail1.setTailPieceNum(datum.getTailPieceNum());
//                    sectionNoDetail1.setMaterialNum(datum.getMaterialNum());
//                    sectionNoDetail1.setHeadPieceNum(datum.getHeadPieceNum());
//                    sectionNoDetail1.setRetCutSegmentNum(datum.getRetCutSegmentNum());
//                    sectionNoDetail1.setPieceNoHead(null);
//                    sectionNoDetail1.setPieceNoEnd(null);
//                    //继承上一段头样片的信息
//                    if (segments.size() == 0 && isInheritSample) {
//                        sectionNoDetail1.setPieceNoHead(sectionNoDetail.getPieceNoHead());
//                    }
//                    //继承上一段尾巴样片的信息
//                    if (segments.size() == data.size() - 1 && isInheritSample) {
//                        sectionNoDetail1.setPieceNoEnd(sectionNoDetail.getPieceNoEnd());
//                    }
//                    if (i - 1 >= 0 && "样片".equals(data.get(i - 1).getType())) {
//                        sectionNoDetail1.setPieceNoHead(data.get(i - 1).getSegmentNo());
//                    }
//                    if (i + 1 < data.size() && "样片".equals(data.get(i + 1).getType())) {
//                        sectionNoDetail1.setPieceNoEnd(data.get(i + 1).getSegmentNo());
//                    }
//                    sectionNoDetail1.setSegmentNum(sectionNoDetail.getSegmentNum());
//                    String type = defaultCrystalAttrService.getDefaultCrystalAttrManyTimes(defaultCrystalAttrList, sectionNoDetail1.getLength());
//                    sectionNoDetail1.setSectionType(type);
//                    segments.add(sectionNoDetail1);
//                    break;
//                case "回收料":
//                    detailList.add(setLineDetail(segmentId, crystalId, id, datum, EnumValuesServer.LineType.回收料.getKey(), segmentNum));
//                    barLength = barLength.subtract(datum.getLength());
//                    break;
//                default:
//                    throw new BusinessException("分段类型异常");
//            }
//        }
//        productionBackCuttingRecord.setBackLength(barLength);
//        // 保存晶段信息
//        sectionNoDetailDao.saveAll(segments);
//        if (pieceDetails.size() > 0) {
//            // 插入样片信息表
//            this.pieceDetailDao.saveAll(pieceDetails);
//        }
//        // 保存反切后的晶段信息到画线晶段明细表
//        this.productionPaintingLineDetailDao.saveAll(detailList);
//
//        // 检验数据提交判断是否在圆棒检验已进站状态
//        Result<CurrentWipStorageStatusDto> wipStorage = wipStorageService.selectWipStorageResult(backCuttingRecordDto.getSegmentNo());
//        if (EnumValuesFactory.WipStorageCode.圆棒检验.getKey().equals(wipStorage.getData().getWipStorageCode())) {
//            // 进入到截断工序
//            wipStorageService.outStation(backCuttingRecordDto.getSegmentNo(), "D", null, false);
//        }
//    }
//
//    private ProductionPaintingLineDetail setLineDetail(Integer segmentId, Long crystalId, Long id, RecutInfoDetailInfo datum, int type, Integer segmentNum) {
//        ProductionPaintingLineDetail detail1 = new ProductionPaintingLineDetail();
//        detail1.setCreateTime(new Date());
//        detail1.setOperateReversecutDrawId(id);
//        detail1.setCrystalId(crystalId);
//        detail1.setType(type);// 头样片
//        detail1.setNo(datum.getSegmentNo());
//        detail1.setLength(datum.getLength());
//        detail1.setRemark(datum.getRemark());
//        detail1.setSequence(datum.getIndex());
//        // 父级晶段
//        detail1.setParentId(segmentId);
//        // 反切画线
//        detail1.setIsCut(1);
//        // 圆棒
//        detail1.setCrystalType(0);
//        detail1.setSegmentNum(segmentNum);
//        return detail1;
//    }
//
//    @Override
//    public void deleteBackInfoDetail(Long reversecutDrawId) {
//        ProductionBackCuttingRecord productionBackCuttingRecord = get(reversecutDrawId);
//        if (productionBackCuttingRecord != null) {
//            if (productionBackCuttingRecord.getStatus()) {
//                throw new BusinessException(productionBackCuttingRecord.getSegmentNo() + "该反切划线记录执行切断，不能删除");
//            }
//            wipStorageService.selectWipStorage(productionBackCuttingRecord.getSegmentNo(), EnumValuesFactory.WipStorageCode.截断.getKey(), false);
//            wipStorageService.exitStation(productionBackCuttingRecord.getSegmentNo());
//            delete(reversecutDrawId);
//            List<String> segmentNos = this.productionPaintingLineDetailDao.findByOperateRecutDrawId(reversecutDrawId);
//            this.productionPaintingLineDetailDao.deleteByOperateRecutDrawId(reversecutDrawId);
//            if (segmentNos != null && segmentNos.size() > 0) {
//                //由于不合而反切也生成了新的晶棒信息，在还原的时候也要把不合格反切时产生的晶棒详情记录删掉
//                this.sectionNoDetailDao.deleteBySectionNoIn(segmentNos);
//            }
//            if (!productionBackCuttingRecord.getBackCutType()) {
//                //不合格反切还原反切前的晶段信息
//                if (productionBackCuttingRecord.getSectionNoDetailJson() != null) {
//                    SectionNoDetail sectionNoDetail = JSON.parseObject(productionBackCuttingRecord.getSectionNoDetailJson(), SectionNoDetail.class);
//                    sectionNoDetailDao.save(sectionNoDetail);
//                }
//            }
//            // 删除样片表信息
//            List<PieceDetail> PieceNos = this.pieceDetailDao.findByPieceNo(segmentNos);
//            if (PieceNos != null && PieceNos.size() > 0) {
//                this.pieceDetailDao.deleteInBatch(PieceNos);
//            }
//        }
//    }
//
//
//    @Override
//    public ProductionBackCuttingRecord findBySegmentNo(String crystalNo) {
//        List<ProductionBackCuttingRecord> productionBackCuttingRecordList = dao.findBySegmentNoAndStatusOrderByIdDesc(crystalNo, false);
//        if (productionBackCuttingRecordList.size() > 0) {
//            return productionBackCuttingRecordList.get(0);
//        }
//        return null;
//    }
//
//    @Override
//    public List<ProductionBackCuttingRecord> findAllBySegmentNo(String crystalNo) {
//        return dao.findBySegmentNoAndStatusOrderByIdDesc(crystalNo, false);
//    }
//
//    @Override
//    public ProductionBackCuttingRecord findTopBySegmentNoOrderByIdDesc(String code) {
//        return dao.findTopBySegmentNoOrderByIdDesc(code);
//    }
//
//    public ProductionBackCuttingRecord fromDto(ProductionBackCuttingRecordDto dto) {
//        return modelMapper.map(dto, ProductionBackCuttingRecord.class);
//    }
//}
