package com.sw.mes.fb.checkout.service.impl;


import com.alibaba.fastjson.JSON;
import com.sw.mes.base.factory.init.entity.CrystalMaterialRelation;
import com.sw.mes.base.factory.init.service.CrystalMaterialRelationService;
import com.sw.mes.base.factory.inspect.entity.FbAppearRule;
import com.sw.mes.base.factory.inspect.service.FbAppearRuleService;
import com.sw.mes.base.factory.product.service.FbProductModelService;
import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.entity.SysLog;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.base.system.service.SysLogService;
import com.sw.mes.base.workinprocess.entity.ProcessLog;
import com.sw.mes.base.workinprocess.entity.WipStorage;
import com.sw.mes.base.workinprocess.service.PlanProcessingOrderService;
import com.sw.mes.base.workinprocess.service.ProcessLogService;
import com.sw.mes.base.workinprocess.service.WipStorageService;
import com.sw.mes.config.rabbit.FbInspectDataBaseChangeAsync;
import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.dto.GetFbInfoDto;
import com.sw.mes.core.dto.PieceCheckDto;
import com.sw.mes.core.dto.mes.pc.fb.FbCheckOutDataDto;
import com.sw.mes.core.dto.wms.ReportSapWorkinghoursDto;
import com.sw.mes.core.entity.FbCheckOutData;
import com.sw.mes.core.enums.EnumValues;
import com.sw.mes.core.enums.EnumValuesFactory;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultCode;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.core.utils.IpUtils;
import com.sw.mes.fb.aio.entity.EapGrindingMachine;
import com.sw.mes.fb.aio.service.EapGrindingHoursService;
import com.sw.mes.fb.aio.service.EapGrindingMachineService;
import com.sw.mes.fb.checkout.dao.FbCheckOutDataDao;
import com.sw.mes.fb.checkout.dao.FbJoinDao;
import com.sw.mes.fb.checkout.dao.RecutDetailDao;
import com.sw.mes.fb.checkout.dto.FbCheckOutRecordDto;
import com.sw.mes.fb.checkout.dto.PointDto;
import com.sw.mes.fb.checkout.entity.FbCheckOutRecord;
import com.sw.mes.fb.checkout.entity.FbJoin;
import com.sw.mes.fb.checkout.entity.OperateCheckOutDataLog;
import com.sw.mes.fb.checkout.entity.RecutDetail;
import com.sw.mes.fb.checkout.service.FbCheckOutDataService;
import com.sw.mes.fb.checkout.service.FbCheckOutRecordService;
import com.sw.mes.fb.checkout.service.OperateCheckOutDataLogService;
import com.sw.mes.fb.turnstore.entity.FbTurnstoreTrayItem;
import com.sw.mes.fb.turnstore.service.FbTurnstoreTrayItemService;
import com.sw.mes.fb.utils.StringUtils;
import com.sw.mes.grade.service.GradeService;
import com.sw.mes.mcs.service.MesToMcsService;
import com.sw.mes.mcs.utils.ResultDto;
import com.sw.mes.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Slf4j
@Transactional
public class FbCheckOutDataServiceImpl extends BaseServiceImpl<FbCheckOutData> implements FbCheckOutDataService {
    FbCheckOutDataDao dao;

    @Resource
    WipStorageService wipStorageService;

    @Autowired
    FbCheckOutRecordService fbCheckOutRecordService;

    @Autowired
    private RecutDetailDao recutDetailDao;

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    YbFeignClients ybFeignService;

    @Autowired
    FbJoinDao fbJoinDao;

//    @Autowired
//    FbCheckOutDataService fbCheckOutDataService;

    @Autowired
    EapGrindingHoursService eapGrindingHoursService;

    @Autowired
    private FbTurnstoreTrayItemService fbTurnstoreTrayItemService;

    @Autowired
    private FbAppearRuleService inspectAppearRuleService;

    @Autowired
    private EapGrindingMachineService eapGrindingMachineService;

    @Autowired
    private OperateCheckOutDataLogService operateCheckOutDataLogService;

    @Autowired
    private DictionaryItemService dictionaryItemService;

    @Autowired
    private YbFeignClients ybFeignClients;

    @Autowired
    private GradeService gradeService;

    @Autowired
    private FbInspectDataBaseChangeAsync fbInspectDataBaseChangeAsync;

    @Autowired
    private PlanProcessingOrderService planProcessingOrderService;

    @Autowired
    private FbProductModelService fbProductModelService;

    @Autowired
    MesToMcsService mesToMcsService;

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private ProcessLogService processLogService;

    @Autowired
    private CrystalMaterialRelationService crystalMaterialRelationService;


    @Autowired
    public void setDao(FbCheckOutDataDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }

    @Override
    public FbCheckOutData findDataByFbSectionNo(String fbSectionNo) {
        List<FbCheckOutData> fbCheckOutDataList = dao.findDataByFbSectionNo(fbSectionNo);
        if (null != fbCheckOutDataList && fbCheckOutDataList.size() > 0) {
            return fbCheckOutDataList.get(0);
        }
        return null;
    }

    @Override
    public String findDataByFbSectionNo1(String point) {

        PointDto pointDto = new PointDto();
        pointDto.setIp(point);
        ResultDto fbSectionNo = mesToMcsService.getFbSectionNo(pointDto);
        if (null != fbSectionNo.getData()) {
            String s = fbSectionNo.getData().toString();
            return s;
        }
        throw new RuntimeException("当前点位不存在到位单晶");
    }

    @Override
    public void sendFbSectionAndIp(String fbSectionNo, String ip) {

        PointDto pointDto = new PointDto();
        pointDto.setIp(ip);
        pointDto.setSn(fbSectionNo);
        mesToMcsService.sendFbSectionAndIp(pointDto);
    }

    @Override
    public void sendSapWorkHour(String fbSectionNo) {
        Result result = ybFeignService.findData(fbSectionNo,  "刀剖工时", fbSectionNo);
        ReportSapWorkinghoursDto reportSapWorkinghoursDto = new ReportSapWorkinghoursDto();
        if(ResultCode.SUCCESS.getCode() == result.getCode()){
            Date date = new Date();
            ProcessLog crystalInProcessLogs = processLogService.findByProcessingOrderCodeAndProcessLogName(fbSectionNo, "【刀剖】工序进站");
            long time = crystalInProcessLogs.getCreateTime().getTime();
            DecimalFormat decimalFormat = new DecimalFormat("#.##");
            double result1 = (double)(date.getTime() - time) / 3600000;
            String formattedResult = decimalFormat.format(result1);

            reportSapWorkinghoursDto.setCnfTimeUom("H");
            reportSapWorkinghoursDto.setCnfTime(formattedResult);
            reportSapWorkinghoursDto.setStatus(false);
            reportSapWorkinghoursDto.setPlant("5041");
            //工单编号：刀剖工单
            CrystalMaterialRelation crystalMaterialRelation = crystalMaterialRelationService.findBySectionNoAndDeviceType(fbSectionNo, 3);
            reportSapWorkinghoursDto.setProductionOrderId(crystalMaterialRelation.getOrderNo());
            reportSapWorkinghoursDto.setCnfDate(date);
            reportSapWorkinghoursDto.setProcName("刀剖工时");
            reportSapWorkinghoursDto.setRemark(fbSectionNo);
            reportSapWorkinghoursDto.setCrystalNo(fbSectionNo);

            if (null != result.getData()) {
                SysLog syslog = new SysLog();
                syslog.setCreateTime(new Date());
                syslog.setType("info");
                syslog.setCreateUserName("SYSTEM");
                syslog.setContent(JSON.toJSONString(reportSapWorkinghoursDto));
                syslog.setResult("刀剖工时：晶棒【" + fbSectionNo + "】已经报过工时");
                syslog.setMethodName("handleReportConsumption()");
                syslog.setServerAddr("");
                syslog.setReqType("HTTP");
                syslog.setReqIp(IpUtils.getLocalIP());
                sysLogService.save(syslog);
                return;
            } else {
                ybFeignService.create(reportSapWorkinghoursDto);
            }

        } else {
            log.info(fbSectionNo + "：查询刀剖工时失败");
        }

    }

    @Override
    public void sendNewSapWorkHour(FbCheckOutRecordDto fbCheckOutRecordDto, String returnWorkOrder) {

        ReportSapWorkinghoursDto reportSapWorkinghoursDto = new ReportSapWorkinghoursDto();
        Date date = new Date();
        double mxTime = getStationTime(fbCheckOutRecordDto.getFbSectionNo(), "【磨削】工序进站", "【磨削】工序出站，进入【方棒检验】工序待进站");
        double dpTime = getStationTime(fbCheckOutRecordDto.getFbSectionNo(), "【刀剖】工序进站", "【刀剖】工序出站，进入【磨削】工序待进站");
        String formattedResult = new BigDecimal(mxTime).add(new BigDecimal(dpTime)).divide(new BigDecimal(3600000), 2, RoundingMode.HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP).toString();

        reportSapWorkinghoursDto.setCnfTimeUom("H");
        reportSapWorkinghoursDto.setCnfTime(formattedResult);
        reportSapWorkinghoursDto.setStatus(false);
        reportSapWorkinghoursDto.setPlant("5041");
        //工单编号：刀剖工单
        CrystalMaterialRelation crystalMaterialRelation = crystalMaterialRelationService.findBySectionNoAndDeviceType(fbCheckOutRecordDto.getFbSectionNo(), 3);
        reportSapWorkinghoursDto.setProductionOrderId(crystalMaterialRelation.getOrderNo());
        reportSapWorkinghoursDto.setCnfDate(date);
        reportSapWorkinghoursDto.setProcName("刀剖工时");
        reportSapWorkinghoursDto.setRemark(fbCheckOutRecordDto.getCheckoutName());
        reportSapWorkinghoursDto.setCrystalNo(fbCheckOutRecordDto.getFbSectionNo());

        Result<List<ReportSapWorkinghoursDto>> result = ybFeignService.findData(fbCheckOutRecordDto.getCheckoutName(), "刀剖工时", fbCheckOutRecordDto.getFbSectionNo());
        List<ReportSapWorkinghoursDto> data = result.getData();
        if(fbCheckOutRecordDto.getCheckoutName().equals("一次检验")){
            //查询报工记录
            if(ResultCode.SUCCESS.getCode() == result.getCode()){
                if(data.size() < 1){
                    ybFeignService.create(reportSapWorkinghoursDto);
                    return;
                } else {
                    SysLog syslog = new SysLog();
                    syslog.setCreateTime(new Date());
                    syslog.setType("info");
                    syslog.setCreateUserName("SYSTEM");
                    syslog.setContent(JSON.toJSONString(reportSapWorkinghoursDto));
                    syslog.setResult("刀剖工时：晶棒【" + fbCheckOutRecordDto.getFbSectionNo() + "】已经报过工时");
                    syslog.setMethodName("sendNewSapWorkHour()");
                    syslog.setServerAddr("");
                    syslog.setReqType("HTTP");
                    syslog.setReqIp(IpUtils.getLocalIP());
                    sysLogService.save(syslog);
                }
            } else {
                log.info(fbCheckOutRecordDto.getFbSectionNo() + "：查询刀剖工时失败");
            }
        }

        if(fbCheckOutRecordDto.getCheckoutName().equals("退货复检")) {
            //返工工单
            reportSapWorkinghoursDto.setProductionOrderId(returnWorkOrder);
            //查询报工记录
            if(ResultCode.SUCCESS.getCode() == result.getCode()){
                DictionaryItem dictionaryItem = dictionaryItemService.findByThemeValueAndNameAndEnable("standardTime", "刀剖工时", true);
                if(data.size() < 1){
                    if(null != dictionaryItem){
                        reportSapWorkinghoursDto.setCnfTime(dictionaryItem.getValue());
                    } else {
                        reportSapWorkinghoursDto.setCnfTime("0");
                    }
                    ybFeignService.create(reportSapWorkinghoursDto);
                    return;
                }

                //查询发起入库记录
                List<FbTurnstoreTrayItem> bySectionNoValidate = fbTurnstoreTrayItemService.findBySectionNoValidate(fbCheckOutRecordDto.getFbSectionNo());
                if(data.size() == bySectionNoValidate.size()){
                    if(null != dictionaryItem){
                        reportSapWorkinghoursDto.setCnfTime(dictionaryItem.getValue());
                    } else {
                        reportSapWorkinghoursDto.setCnfTime("0");
                    }
                    ybFeignService.create(reportSapWorkinghoursDto);
                }
            } else {
                log.info(fbCheckOutRecordDto.getFbSectionNo() + "：查询刀剖工时失败");
            }
        }
    }

    public double getStationTime(String code, String inStation, String outStation) {
        ProcessLog crystalInProcessLogs = processLogService.findByProcessingOrderCodeAndProcessLogName(code, inStation);
        if(null != crystalInProcessLogs){
            double startTime = crystalInProcessLogs.getCreateTime().getTime();
            ProcessLog crystalOutProcessLogs = processLogService.findByProcessingOrderCodeAndProcessLogName(code, outStation);
            if(null != crystalOutProcessLogs){
                double endTime = crystalOutProcessLogs.getCreateTime().getTime();
                double crystalTime = endTime - startTime;
                return crystalTime;
            }
            return 0;
        }
        return 0;
    }


    @Override
    public List<FbCheckOutData> findCheckOutDataByFbSectionNoIns(List<String> fbSectionNos) {
        return dao.findCheckOutDataByFbSectionNoIns(fbSectionNos);
    }


    @Override
    public Map<String, Object> findBySectionNoMap(String sectionNo) {
        return dao.findBySectionNoMap(sectionNo);
    }

    @Override
    public void updateOrderTypeByFbSectionNoList(List<String> fbSectionNoList, String orderType, String checkoutName) {
        if (CollectionUtils.isNotEmpty(fbSectionNoList)) {
            dao.updateOrderTypeByFbSectionNoList(fbSectionNoList, orderType, checkoutName);
            //更新托号中状态
            //fbTurnstoreTrayItemService.updateTrayItemState(fbSectionNoList);
        }
    }


    @Override
    @Transactional
    public Result readResultsImportMk(MultipartFile file) throws Exception {
        Map<String, List<Map<String, Object>>> excelMap;
        try {
            excelMap = ExcelUtil.getExcelInfo(file);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("文件导入异常！" + e.getMessage());
        }

        if (!excelMap.isEmpty()) {
            List<FbCheckOutRecord> fbCheckOutRecords = new ArrayList<>();
            List<FbCheckOutData> fbCheckOutDatas = new ArrayList<>();
            BigDecimal bigDecimal = BigDecimal.ZERO;

            Collection<List<Map<String, Object>>> collection = excelMap.values();
            Iterator<List<Map<String, Object>>> iterator = collection.iterator();
            //遍历excel文件的内容
            while (iterator.hasNext()) {
                List<Map<String, Object>> list = iterator.next();
                for (Map<String, Object> map : list) {
                    FbCheckOutRecordDto fbCheckOutRecordDto = new FbCheckOutRecordDto();

                    //1对单晶条码编号(方)的校验
                    String fbSectionNo = StringUtils.valueOf(map.get("单晶条码编号(方)"));
                    if (StringUtils.isBlank(fbSectionNo)) {
                        throw new Exception("请输入单晶条码编号(方)！");
                    }

                    // 检验数据提交判断是否在方棒检验待进站状态
                    wipStorageService.selectWipStorage(fbSectionNo, EnumValuesFactory.WipStorageCode.方棒检验.getKey(), false);

                    RecutDetail FbInfo = recutDetailDao.findBySectionNoFb(fbSectionNo);
                    if (FbInfo == null) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "方棒编号不存在");
                    }
                    FbCheckOutRecord fbCheckOutRecord1 = fbCheckOutRecordService.findFbCheckOutRecord(fbSectionNo);
                    if (fbCheckOutRecord1 != null) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "方棒检验数据已存在");
                    }
                    fbCheckOutRecordDto.setFbSectionNo(fbSectionNo);


                    GetFbInfoDto getFbInfoDto = getFbInfoBySectionNofb(fbSectionNo);
                    if (getFbInfoDto != null) {
                        //导电类型
                        if (StringUtils.isBlank(getFbInfoDto.getConductionType())) {
                            throw new Exception("请输入导电类型！");
                        } else {
                            if (!"P".equals(getFbInfoDto.getConductionType()) && !"N".equals(getFbInfoDto.getConductionType()) && !"P/N".equals(getFbInfoDto.getConductionType())) {
                                throw new Exception("该方棒单晶编号：" + fbSectionNo + "导电类型错误！");
                            }
                            fbCheckOutRecordDto.setConductionType(getFbInfoDto.getConductionType());
                        }
                        //方棒长度,必填
                        String length = StringUtils.valueOf(map.get("方棒长度"));
                        if (StringUtils.isBlank(length)) {
                            throw new Exception("请输入方棒长度！");
                        } else {
                            BigDecimal decimalLength = new BigDecimal(length);
                            fbCheckOutRecordDto.setLength(decimalLength);
                            //拼棒总长
                            fbCheckOutRecordDto.setSpellBarLength(decimalLength);
                        }
                        //圆棒产品实验等级
                        fbCheckOutRecordDto.setYbExperiLevel(getFbInfoDto.getYbExperiLevel());
                        //产品等级/产品等级
                        if (StringUtils.isBlank(getFbInfoDto.getConductionType())) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "请输入产品等级名称！");
                        } else {
                            fbCheckOutRecordDto.setProductLevel(getFbInfoDto.getProductLevel());
                        }
                        //圆棒单晶编号
                        fbCheckOutRecordDto.setYbSectionNo(getFbInfoDto.getYbSectionNo());
                        //产品类型
                        if (StringUtils.isBlank(getFbInfoDto.getProductType())) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "产品类型为空！");
                        } else {
                            fbCheckOutRecordDto.setProductType(getFbInfoDto.getProductType());
                        }
                        //拼数
                        fbCheckOutRecordDto.setSticksCount(getFbInfoDto.getSticksCount());
                        //订单号
                        fbCheckOutRecordDto.setOrderNo(getFbInfoDto.getOrderNo());
                    }
//                    else {
//                        //4.导电类型,必填
//                        String conductionType = StringUtils.valueOf(map.get("导电类型"));
//                        if (StringUtils.isBlank(conductionType)) {
//                            throw new Exception("请输入导电类型！");
//                        }
//                        //判断导电类型
//                        if (!"P".equals(conductionType) && !"N".equals(conductionType) && !"P/N".equals(conductionType)) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "导电类型错误！");
//                        }
//
//                        fbCheckOutRecordDto.setConductionType(conductionType);
//
//                        //7.方棒长度,必填
//                        String length = StringUtils.valueOf(map.get("方棒长度"));
//                        if (!StringUtils.isBlank(length)) {
//                            try {
//                                BigDecimal decimalLength = new BigDecimal(length);
//                                fbCheckOutRecordDto.setLength(decimalLength);
//                            } catch (Exception e) {
//                                throw new Exception("该方棒单晶编号：" + fbSectionNo + "请输入方棒长度！");
//                            }
//                        }
//
//                        //6.产品等级名称,必填
//                        String productLevel = StringUtils.valueOf(map.get("产品等级名称"));
//                        if (StringUtils.isBlank(productLevel)) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "请输入产品等级名称！");
//                        }
//                        fbCheckOutRecordDto.setProductLevel(productLevel);
//
//                        //8.拼棒总长,必填
//                        String spellBarLength = StringUtils.valueOf(map.get("拼棒总长(mm)"));
//                        if (!StringUtils.isBlank(spellBarLength)) {
//                            try {
//                                BigDecimal bigDecimalSpellBarLength = new BigDecimal(spellBarLength);
//                                fbCheckOutRecordDto.setSpellBarLength(bigDecimalSpellBarLength);
//                            } catch (Exception e) {
//                                throw new Exception("该方棒单晶编号：" + fbSectionNo + "拼棒总长格式错误！");
//                            }
//                        }
//                    }

                    //2.检验类型,必填 查询站点判断方棒检验类型  上一站点为开方机，为一次检验，上一站点为截断机，为反切检验
                    Result<WipStorage> wipStorageResult = wipStorageService.selectLastWipStorageResult(fbSectionNo);
                    WipStorage data = wipStorageResult.getData();
                    String processName = data.getProcessName();
                    String checkoutName = null;
                    if (!StringUtils.isBlank(processName)) {
                        if (processName.equals("开方")) {
                            checkoutName = "一次检验";
                            //领料来源
                            fbCheckOutRecordDto.setMaterialSourceName("磨削机");
                            fbCheckOutRecordDto.setMaterialSourceValue("磨削机");
                            //复检类型  reCheckType
                            fbCheckOutRecordDto.setReCheckType("0");
                        } else if (processName.equals("截断")) {
                            checkoutName = "返切复检";
                            //领料来源
                            fbCheckOutRecordDto.setMaterialSourceName("切断");
                            fbCheckOutRecordDto.setMaterialSourceValue("切断");
                            //复检类型
                            List<WipStorage> byProcessingByOrderCode = wipStorageService.findByProcessingByOrderCode(fbSectionNo);
                            int num = 0;
                            if (null != byProcessingByOrderCode && byProcessingByOrderCode.size() > 0) {
                                for (WipStorage wipStorage : byProcessingByOrderCode) {
                                    if (wipStorage.getProcessName().equals("截断")) {
                                        num++;
                                    }
                                }
                            }
                            if (num > 1) {
                                fbCheckOutRecordDto.setReCheckType(String.valueOf(num - 1));
                            } else {
                                fbCheckOutRecordDto.setReCheckType("1");
                            }
                        } else {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "站点有误");
                        }
                    }
//                    String checkoutName = StringUtils.valueOf(map.get("检验类型"));
//                    if (StringUtils.isBlank(checkoutName)) {
//                        throw new Exception("请输入检验类型！");
//                    }
                    //判断检验类型
                    if (!EnumValues.CheckoutName.一次检验.getValue().equals(checkoutName) && !EnumValues.CheckoutName.返切复检.getValue().equals(checkoutName)
                            && !EnumValues.CheckoutName.粘方棒复检.getValue().equals(checkoutName) && !EnumValues.CheckoutName.退库复检.getValue().equals(checkoutName)
                            && !EnumValues.CheckoutName.退货复检.getValue().equals(checkoutName)) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "检验类型错误！");
                    }
                    fbCheckOutRecordDto.setCheckoutName(checkoutName);

                    //3.规格,必填
                    String specification = StringUtils.valueOf(map.get("规格"));
                    if (StringUtils.isBlank(specification)) {
                        throw new Exception("请输入规格！");
                    }

                    //判断规格
//                    if (!EnumValues.Specification.规格247.getValue().equals(specification) && !EnumValues.Specification.规格223.getValue().equals(specification)
//                            && !EnumValues.Specification.规格295.getValue().equals(specification) && !EnumValues.Specification.规格306.getValue().equals(specification)
//                            && !EnumValues.Specification.规格307.getValue().equals(specification)) {
//                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "规格错误！");
//                    }
                    fbCheckOutRecordDto.setSpecification(specification);

//                    //5.产品类型,必填
//                    String productType = StringUtils.valueOf(map.get("产品类型"));
//                    if (StringUtils.isBlank(productType)) {
//                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "请输入产品类型！");
//                    }
//                    fbCheckOutRecordDto.setProductType(productType);
                    //=========================================非必填字段===============================

                    String lineScratch = StringUtils.valueOf(map.get("线痕扣减(mm)"));
                    if (!StringUtils.isBlank(lineScratch)) {
                        try {
                            BigDecimal bigDecimalLineScratch = new BigDecimal(lineScratch);
                            fbCheckOutRecordDto.setLineScratch(bigDecimalLineScratch);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "线痕格式错误！");
                        }
                    }

//                    String scratch = StringUtils.valueOf(map.get("划痕(mm)"));
//                    if (!StringUtils.isBlank(scratch)) {
//                        try {
//                            BigDecimal bigDecimalScratch = new BigDecimal(scratch);
//                            fbCheckOutRecordDto.setScratch(bigDecimalScratch);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "划痕格式错误！");
//                        }
//                    }

                    String noFriction = StringUtils.valueOf(map.get("未磨到扣减(mm)"));
                    if (!StringUtils.isBlank(noFriction)) {
                        try {
                            BigDecimal bigDecimalNoFriction = new BigDecimal(noFriction);
                            fbCheckOutRecordDto.setNoFriction(bigDecimalNoFriction);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "未磨到格式错误！");
                        }
                    }

                    //线痕长度
                    fbCheckOutRecordDto.setLineScratch(bigDecimal);
                    //划痕长度
                    fbCheckOutRecordDto.setScratch(bigDecimal);
                    //未磨到长度
                    fbCheckOutRecordDto.setNoFriction(bigDecimal);

//                    String wheelHighLights = StringUtils.valueOf(map.get("倒角亮点(mm)"));
//                    if (!StringUtils.isBlank(wheelHighLights)) {
//                        try {
//                            BigDecimal bigDecimalWheelHighLights = new BigDecimal(wheelHighLights);
//                            fbCheckOutRecordDto.setWheelHighLights(bigDecimalWheelHighLights);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "砂轮亮点格式错误！");
//                        }
//                    }
                    fbCheckOutRecordDto.setWheelHighLights(bigDecimal);

//                    String wheelPrintReduce = StringUtils.valueOf(map.get("砂轮印扣减(mm)"));
//                    if (!StringUtils.isBlank(wheelPrintReduce)) {
//                        try {
//                            BigDecimal bigDecimalWheelPrintReduce = new BigDecimal(wheelPrintReduce);
//                            fbCheckOutRecordDto.setWheelPrintReduce(bigDecimalWheelPrintReduce);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "砂轮印扣减格式错误！");
//                        }
//                    }
                    fbCheckOutRecordDto.setWheelPrintReduce(bigDecimal);

//                    String sizeException = StringUtils.valueOf(map.get("尺寸异常(mm)"));
//                    if (!StringUtils.isBlank(sizeException)) {
//                        try {
//                            BigDecimal bigDecimalSizeException = new BigDecimal(sizeException);
//                            fbCheckOutRecordDto.setSizeException(bigDecimalSizeException);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "尺寸异常格式错误！");
//                        }
//                    }
                    fbCheckOutRecordDto.setSizeException(bigDecimal);

//                    String roughnessAbnormality = StringUtils.valueOf(map.get("粗糙度异常(mm)"));
//                    if (!StringUtils.isBlank(roughnessAbnormality)) {
//                        try {
//                            BigDecimal bigDecimalRoughnessAbnormality = new BigDecimal(roughnessAbnormality);
//                            fbCheckOutRecordDto.setRoughnessAbnormality(bigDecimalRoughnessAbnormality);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "粗糙度异常格式错误！");
//                        }
//                    }
                    fbCheckOutRecordDto.setRoughnessAbnormality(bigDecimal);

//                    String bevelDeduction = StringUtils.valueOf(map.get("斜面扣减(mm)"));
//                    if (!StringUtils.isBlank(bevelDeduction)) {
//                        try {
//                            BigDecimal bigDecimalBevelDeduction = new BigDecimal(bevelDeduction);
//                            fbCheckOutRecordDto.setBevelDeduction(bigDecimalBevelDeduction);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "斜面扣减格式错误！");
//                        }
//                    }
                    fbCheckOutRecordDto.setBevelDeduction(bigDecimal);

                    String collapseLossSh = StringUtils.valueOf(map.get("崩边扣减上(mm)"));
                    if (!StringUtils.isBlank(collapseLossSh)) {
                        try {
                            BigDecimal bigDecimalCollapseLossSh = new BigDecimal(collapseLossSh);
                            fbCheckOutRecordDto.setCollapseLossSh(bigDecimalCollapseLossSh);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "崩边扣减上格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setCollapseLossSh(bigDecimal);
                    }

                    String collapseLossZh = StringUtils.valueOf(map.get("崩边扣减中(mm)"));
                    if (!StringUtils.isBlank(collapseLossZh)) {
                        try {
                            BigDecimal bigDecimalCollapseLossZh = new BigDecimal(collapseLossZh);
                            fbCheckOutRecordDto.setCollapseLossZh(bigDecimalCollapseLossZh);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "崩边扣减中格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setCollapseLossZh(bigDecimal);
                    }

                    String collapseLossXia = StringUtils.valueOf(map.get("崩边扣减下(mm)"));
                    if (!StringUtils.isBlank(collapseLossXia)) {
                        try {
                            BigDecimal bigDecimalCollapseLossXia = new BigDecimal(collapseLossXia);
                            fbCheckOutRecordDto.setCollapseLossXia(bigDecimalCollapseLossXia);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "崩边扣减下格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setCollapseLossXia(bigDecimal);
                    }

                    String diameterMin = StringUtils.valueOf(map.get("直径min"));
                    if (!StringUtils.isBlank(diameterMin)) {
                        try {
                            BigDecimal bigDecimalDiameterMin = new BigDecimal(diameterMin);
                            fbCheckOutRecordDto.setDiameterMin(bigDecimalDiameterMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "直径min格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setDiameterMin(bigDecimal);
                    }

                    String diameterMax = StringUtils.valueOf(map.get("直径max"));
                    if (!StringUtils.isBlank(diameterMax)) {
                        try {
                            BigDecimal bigDecimalDiameterMax = new BigDecimal(diameterMax);
                            fbCheckOutRecordDto.setDiameterMax(bigDecimalDiameterMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "直径max格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setDiameterMax(bigDecimal);
                    }

                    String sideLengthMin = StringUtils.valueOf(map.get("边距min"));
                    if (!StringUtils.isBlank(sideLengthMin)) {
                        try {
                            BigDecimal bigDecimalSideLengthMin = new BigDecimal(sideLengthMin);
                            fbCheckOutRecordDto.setSideLengthMin(bigDecimalSideLengthMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "边距min格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setSideLengthMin(bigDecimal);
                    }

                    String sideLengthMax = StringUtils.valueOf(map.get("边距max"));
                    if (!StringUtils.isBlank(sideLengthMax)) {
                        try {
                            BigDecimal bigDecimalSideLengthMax = new BigDecimal(sideLengthMax);
                            fbCheckOutRecordDto.setSideLengthMax(bigDecimalSideLengthMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "边距max格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setSideLengthMax(bigDecimal);
                    }


                    fbCheckOutRecordDto.setVerticalityMin(bigDecimal);
                    fbCheckOutRecordDto.setVerticalityMax(bigDecimal);
//                    String verticalityMin = StringUtils.valueOf(map.get("垂直度min"));
//                    if (!StringUtils.isBlank(verticalityMin)) {
//                        try {
//                            BigDecimal bigDecimalVerticalityMin = new BigDecimal(verticalityMin);
//                            fbCheckOutRecordDto.setVerticalityMin(bigDecimalVerticalityMin);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "垂直度min格式错误！");
//                        }
//                    }
//
//                    String verticalityMax = StringUtils.valueOf(map.get("垂直度max"));
//                    if (!StringUtils.isBlank(verticalityMax)) {
//                        try {
//                            BigDecimal bigDecimalVerticalityMax = new BigDecimal(verticalityMax);
//                            fbCheckOutRecordDto.setVerticalityMax(bigDecimalVerticalityMax);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "垂直度max格式错误！");
//                        }
//                    }

//                    String weight = StringUtils.valueOf(map.get("方棒称重"));
//                    if (!StringUtils.isBlank(weight)) {
//                        try {
//                            BigDecimal bigDecimalWeight = new BigDecimal(weight);
//                            fbCheckOutRecordDto.setWeight(bigDecimalWeight);
//                        } catch (Exception e) {
//                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "方棒称重错误！");
//                        }
//                    }


                    String chordLengthMin = StringUtils.valueOf(map.get("弦长min"));
                    if (!StringUtils.isBlank(chordLengthMin)) {
                        try {
                            BigDecimal bigDecimalChordLengthMin = new BigDecimal(chordLengthMin);
                            fbCheckOutRecordDto.setChordLengthMin(bigDecimalChordLengthMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "弦长min格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setChordLengthMin(bigDecimal);
                    }

                    String chordLengthMax = StringUtils.valueOf(map.get("弦长max"));
                    if (!StringUtils.isBlank(chordLengthMax)) {
                        try {
                            BigDecimal bigDecimalChordLengthMax = new BigDecimal(chordLengthMax);
                            fbCheckOutRecordDto.setChordLengthMax(bigDecimalChordLengthMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "弦长max格式错误！");
                        }
                    } else {
                        fbCheckOutRecordDto.setChordLengthMax(bigDecimal);
                    }

//                    String squareRodRemark = StringUtils.valueOf(map.get("方棒备注"));
//                    fbCheckOutRecordDto.setSquareRodRemark(squareRodRemark);
//
//                    String otherRemark = StringUtils.valueOf(map.get("其他备注"));
//                    fbCheckOutRecordDto.setOtherRemark(otherRemark);
//
//                    String checkUserName = StringUtils.valueOf(map.get("检验员"));
//                    fbCheckOutRecordDto.setCheckUserName(checkUserName);
                    //方棒理论称重
                    String length = StringUtils.valueOf(map.get("方棒长度"));
                    DictionaryItem byNameAndThemeId = dictionaryItemService.findByNameAndThemeId(specification, 73);
                    BigDecimal bigDecimalProjectionMax = new BigDecimal(length);
                    BigDecimal xishu = new BigDecimal(byNameAndThemeId.getValue());
                    BigDecimal divide = bigDecimalProjectionMax.divide(xishu, BigDecimal.ROUND_FLOOR);
                    fbCheckOutRecordDto.setWeight(divide);


                    //方棒称重
                    fbCheckOutRecordDto.setTheoryWeightFb(bigDecimal);
                    //检验时间
                    fbCheckOutRecordDto.setCheckTime(new Date());
                    //检验人
                    fbCheckOutRecordDto.setCheckUserName(BaseContextHandler.getUsername());
                    FbCheckOutRecord fbCheckOutRecord = this.fromDto(fbCheckOutRecordDto);
                    fbCheckOutRecords.add(fbCheckOutRecord);

                    // 数据同步到方棒检验数据库
                    FbCheckOutData fbCheckOutData = toData(fbCheckOutRecordDto);
                    fbCheckOutDatas.add(fbCheckOutData);

                    //掺杂剂
                    fbCheckOutRecordDto.setDopant(getFbInfoDto.getDopant());
                    //单晶段号
                    fbCheckOutRecordDto.setPiecewiseNum(getFbInfoDto.getPiecewiseNum());
                    //头尾识别
                    fbCheckOutRecordDto.setHeadTailRecognition(getFbInfoDto.getHeadTailRecognition());
                    //圆棒最终评级
                    fbCheckOutRecordDto.setProductLevel(getFbInfoDto.getProductLevel());
                    //一体机编号
                    fbCheckOutRecordDto.setDeviceNo(data.getEquipmentCode());
                    //查询方棒检验数据库
                    FbCheckOutRecord fbCheckOutRecord2 = fbCheckOutRecordService.findFbCheckOutRecord(fbCheckOutRecordDto.getFbSectionNo());
                    if (fbCheckOutRecord2 == null) {
                        fbCheckOutRecord2 = new FbCheckOutRecord();
                    }
                    //fbCheckOutRecord = modelMapper.map(fbCheckOutRecordDto, FbCheckOutRecord.class);
                    BeanUtil.copyBeanPropWithOutNullValue(fbCheckOutRecord2, fbCheckOutRecordDto, "id");
                    fbCheckOutRecordService.saveOrUpdateFbCheckOutRecord(fbCheckOutRecord2, false, false);
                }
//                fbCheckOutRecordService.saveAll(fbCheckOutRecords);
//                fbCheckOutDataService.saveAll(fbCheckOutDatas);
            }
        }
        return ResultGenerator.success();
    }


    @Override
    public void download(HttpServletResponse response) throws Exception {
        InputStream fis = this.getClass().getResourceAsStream("template/方棒检验导入模板.xls");
        ;
        BufferedInputStream bis = null;
        byte[] buffer = new byte[1024];
        try {

            // 设置response的Header
            response.setHeader("Content-disposition", "attachment;filename=方棒检验导入模板.xls");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");  //xlsx
            response.setCharacterEncoding("utf-8");
            // 以流的形式下载文件。
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println(ex);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    @Transactional
    public Result readResultsImport(MultipartFile file) throws Exception {
        Map<String, List<Map<String, Object>>> excelMap;
        try {
            excelMap = ExcelUtil.getExcelInfo(file);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("文件导入异常！" + e.getMessage());
        }

        if (!excelMap.isEmpty()) {
            /*
             * 给方棒检验，和方棒检验数据库赋值
             */
            List<FbCheckOutRecord> fbCheckOutRecords = new ArrayList<>();
            List<FbCheckOutData> fbCheckOutDatas = new ArrayList<>();

            List<FbCheckOutRecordDto> fbCheckOutRecordDtos = new ArrayList<>();
            List<FbCheckOutDataDto> fbCheckOutDataDtos = new ArrayList<>();

            Collection<List<Map<String, Object>>> collection = excelMap.values();
            Iterator<List<Map<String, Object>>> iterator = collection.iterator();
            //遍历excel文件的内容
            while (iterator.hasNext()) {
                List<Map<String, Object>> list = iterator.next();

                // 增加导入数量最低限制 1000
//                if (list.size() < 1000) {
//                    throw new Exception("每批次导入限制最少是[1000]，该批次为：" + list.size());
//                }
//                // 增加导入数量最多限制 10000
//                if (list.size() > 10000) {
//                    throw new Exception("每批次导入限制最多是[10000]，该批次为：" + list.size());
//                }
                for (Map<String, Object> map : list) {
                    FbCheckOutRecordDto fbCheckOutRecordDto = new FbCheckOutRecordDto();
                    String ybSectionNo = StringUtils.valueOf(map.get("单晶条码编号(圆)"));
//                    if (StringUtils.isBlank(ybSectionNo)) {
//                        throw new Exception("请输入单晶条码编号(圆)！");
//                    }
                    String fbSectionNo = StringUtils.valueOf(map.get("单晶条码编号(方)"));

                    if (StringUtils.isBlank(fbSectionNo)) {
                        throw new Exception("请输入单晶条码编号(方)！");
                    }

                    // 检验数据提交判断是否在方棒检验待进站状态
                    wipStorageService.selectWipStorage(fbSectionNo, EnumValuesFactory.WipStorageCode.方棒检验.getKey(), false);

                    RecutDetail FbInfo = recutDetailDao.findBySectionNoFb(fbSectionNo);
                    if (FbInfo == null) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "方棒编号不存在");
                    }
                    FbCheckOutRecord fbCheckOutRecord1 = fbCheckOutRecordService.findFbCheckOutRecord(fbCheckOutRecordDto.getFbSectionNo());
                    if (fbCheckOutRecord1 != null) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "方棒检验数据已存在");
                    }

                    /*
                     * 圆棒信息如果有，用圆棒信息的数据  没有的话用页面录入的数据
                     */
                    //通过方棒编号找到圆棒信息dto
                    //Result<GetFbInfoDto> fbInfoBySectionNofb = fbCheckOutRecordService.getFbInfoBySectionNofb(fbSectionNo);
                    //同上面的方法
                    GetFbInfoDto getFbInfoDto = getFbInfoBySectionNofb(fbSectionNo);
                    if (getFbInfoDto != null) {
                        //导电类型
                        fbCheckOutRecordDto.setConductionType(getFbInfoDto.getConductionType());
                        //圆棒入库长度
//                       fbCheckOutRecordDto.setInstoreLengthYb(getFbInfoDto.getInstoreLengthYb());
                        //方棒长度
                        fbCheckOutRecordDto.setLength(getFbInfoDto.getLength());
                        //圆棒产品实验等级
                        fbCheckOutRecordDto.setYbExperiLevel(getFbInfoDto.getYbExperiLevel());
                        //产品等级/产品等级
                        fbCheckOutRecordDto.setProductLevel(getFbInfoDto.getProductLevel());
                        //圆棒单晶编号
                        fbCheckOutRecordDto.setYbSectionNo(getFbInfoDto.getYbSectionNo());
                        //拼棒总长
                        fbCheckOutRecordDto.setSpellBarLength(getFbInfoDto.getSpellBarLength());
                        //拼数
                        fbCheckOutRecordDto.setSticksCount(getFbInfoDto.getSticksCount());
                        //直径判定
//                        fbCheckOutRecordDto.setDiamDecide(getFbInfoDto.getDiamDecide());
                        //细
//                        fbCheckOutRecordDto.setFineDiameter(getFbInfoDto.getFineDiameter());
                        //细减长
//                        fbCheckOutRecordDto.setFineCutLength(getFbInfoDto.getFineCutLength());
                        //订单号
                        fbCheckOutRecordDto.setOrderNo(getFbInfoDto.getOrderNo());
                    } else {
                        //必填
                        String conductionType = StringUtils.valueOf(map.get("导电类型"));
                        String ybExperiLevel = StringUtils.valueOf(map.get("产品实验等级名称"));
                        String productLevel = StringUtils.valueOf(map.get("产品等级名称"));
                        String spellBarLength = StringUtils.valueOf(map.get("拼棒总长(mm)"));
                        String sticksCount = StringUtils.valueOf(map.get("拼数"));
                        //判断导电类型
                        if (!"P".equals(conductionType) && !"N".equals(conductionType) && !"P/N".equals(conductionType)) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "导电类型错误！");
                        }
                        fbCheckOutRecordDto.setConductionType(conductionType);


                        try {
                            BigDecimal length = new BigDecimal(StringUtils.valueOf(map.get("方棒长度")));
                            fbCheckOutRecordDto.setLength(length);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "方棒长度错误！");
                        }

                        if (!StringUtils.isBlank(spellBarLength)) {
                            try {
                                BigDecimal bigDecimalSpellBarLength = new BigDecimal(spellBarLength);
                                fbCheckOutRecordDto.setSpellBarLength(bigDecimalSpellBarLength);
                            } catch (Exception e) {
                                throw new Exception("该方棒单晶编号：" + fbSectionNo + "拼棒总长格式错误！");
                            }
                        }


                        if (!StringUtils.isBlank(sticksCount)) {
                            try {
                                Integer integerSticksCount = new Integer(sticksCount);
                                fbCheckOutRecordDto.setSticksCount(integerSticksCount);
                            } catch (Exception e) {
                                throw new Exception("该方棒单晶编号：" + fbSectionNo + "拼数格式错误！");
                            }
                        }

                        fbCheckOutRecordDto.setYbExperiLevel(ybExperiLevel);
                        fbCheckOutRecordDto.setProductLevel(productLevel);
                        fbCheckOutRecordDto.setYbSectionNo(ybSectionNo);
                    }

                    String deviceNo = StringUtils.valueOf(map.get("一体机生产编号"));
                    String flatThrowCode = StringUtils.valueOf(map.get("平抛机生产编号"));
                    String outerCircleCode = StringUtils.valueOf(map.get("外圆机生产编号"));
                    //必填
                    String materialSourceName = StringUtils.valueOf(map.get("领料来源"));
                    String station = StringUtils.valueOf(map.get("打托工位"));
                    //必填
                    String crystalDirection = StringUtils.valueOf(map.get("晶棒方向"));
                    //必填
                    String checkoutName = StringUtils.valueOf(map.get("检验类型"));
                    String squareRodRemark = StringUtils.valueOf(map.get("方棒备注"));
                    String otherRemark = StringUtils.valueOf(map.get("其他备注"));
                    //必填
                    String productLevelFb = StringUtils.valueOf(map.get("检验入库类别"));
                    //必填
                    String specification = StringUtils.valueOf(map.get("规格"));


                    String flatOrientation = StringUtils.valueOf(map.get("平边晶向"));
                    String orientation = StringUtils.valueOf(map.get("晶体晶向"));
                    String checkUserName = StringUtils.valueOf(map.get("检验员"));
                    //必填
                    String checkTime = StringUtils.valueOf(map.get("检验时间"));
                    BigDecimal brokenGreyNoEdge = new BigDecimal(StringUtils.valueOf(map.get("崩损灰皮非边缘(mm)")));


                    fbCheckOutRecordDto.setFbSectionNo(fbSectionNo);
                    fbCheckOutRecordDto.setDeviceNo(deviceNo);
                    fbCheckOutRecordDto.setFlatThrowCode(flatThrowCode);
                    fbCheckOutRecordDto.setOuterCircleCode(outerCircleCode);
                    fbCheckOutRecordDto.setStation(station);
                    fbCheckOutRecordDto.setSquareRodRemark(squareRodRemark);
                    fbCheckOutRecordDto.setOtherRemark(otherRemark);

                    fbCheckOutRecordDto.setFlatOrientation(flatOrientation);
                    fbCheckOutRecordDto.setOrientation(orientation);
                    fbCheckOutRecordDto.setCheckUserName(checkUserName);
                    fbCheckOutRecordDto.setBrokenGreyNoEdge(brokenGreyNoEdge);


                    //判断晶段方向
                    if (!"东".equals(crystalDirection) && !"西".equals(crystalDirection)) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "晶棒方向错误！");
                    }

                    //判断领料来源
                    if (!EnumValues.MaterialSourceName.磨削机.getValue().equals(materialSourceName) && !EnumValues.MaterialSourceName.粘方棒.getValue().equals(materialSourceName)
                            && !EnumValues.MaterialSourceName.WMS.getValue().equals(materialSourceName)) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "领料来源错误！");
                    }
                    //判断检验类型
                    if (!EnumValues.CheckoutName.一次检验.getValue().equals(checkoutName) && !EnumValues.CheckoutName.返切复检.getValue().equals(checkoutName)
                            && !EnumValues.CheckoutName.粘方棒复检.getValue().equals(checkoutName) && !EnumValues.CheckoutName.退库复检.getValue().equals(checkoutName)
                            && !EnumValues.CheckoutName.退货复检.getValue().equals(checkoutName)) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "检验类型错误！");
                    }
                    //判断检验入库类别
                    if (!EnumValues.ProductLevelFb.正常.getValue().equals(productLevelFb) && !EnumValues.ProductLevelFb.二类.getValue().equals(productLevelFb)
                            && !EnumValues.ProductLevelFb.新二类.getValue().equals(productLevelFb) && !EnumValues.ProductLevelFb.返修.getValue().equals(productLevelFb)
                            && !EnumValues.ProductLevelFb.退料.getValue().equals(productLevelFb) && !EnumValues.ProductLevelFb.B1单晶.getValue().equals(productLevelFb)
                            && !EnumValues.ProductLevelFb.B2单晶.getValue().equals(productLevelFb) && !EnumValues.ProductLevelFb.籽晶.getValue().equals(productLevelFb)
                            && !EnumValues.ProductLevelFb.返切.getValue().equals(productLevelFb)) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "检验入库类别错误！");
                    }
                    //判断规格
                    if (!EnumValues.Specification.规格247.getValue().equals(specification) && !EnumValues.Specification.规格223.getValue().equals(specification)
                            && !EnumValues.Specification.规格295.getValue().equals(specification) && !EnumValues.Specification.规格306.getValue().equals(specification)
                            && !EnumValues.Specification.规格307.getValue().equals(specification)) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "规格错误！");
                    }
                    try {
                        if (StringUtils.isBlank(checkTime)) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "检验日期为空！");
                        }
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = sdf.parse(checkTime);
                        fbCheckOutRecordDto.setCheckTime(date);
                    } catch (Exception e) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "检验日期格式错误");
                    }

                    fbCheckOutRecordDto.setCrystalDirection(crystalDirection);
                    fbCheckOutRecordDto.setMaterialSourceName(materialSourceName);
                    fbCheckOutRecordDto.setCheckoutName(checkoutName);
                    fbCheckOutRecordDto.setProductLevelFb(productLevelFb);
                    fbCheckOutRecordDto.setSpecification(specification);


                    try {
                        BigDecimal weight = new BigDecimal(StringUtils.valueOf(map.get("方棒称重")));
                        fbCheckOutRecordDto.setWeight(weight);
                    } catch (Exception e) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "方棒称重错误！");
                    }

                    try {
                        BigDecimal theoryWeightFb = new BigDecimal(StringUtils.valueOf(map.get("理论重量(kg)")));
                        fbCheckOutRecordDto.setTheoryWeightFb(theoryWeightFb);
                    } catch (Exception e) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "理论重量错误！");
                    }

                    try {
                        BigDecimal consumeLengthYb = new BigDecimal(StringUtils.valueOf(map.get("圆棒消耗长度")));
                        fbCheckOutRecordDto.setConsumeLengthYb(consumeLengthYb);
                    } catch (Exception e) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "圆棒消耗长度错误！");
                    }

                    try {
                        BigDecimal instoreLengthFb = new BigDecimal(StringUtils.valueOf(map.get("入库长度")));
                        fbCheckOutRecordDto.setInstoreLengthFb(instoreLengthFb);
                    } catch (Exception e) {
                        throw new Exception("该方棒单晶编号：" + fbSectionNo + "入库长度错误！");
                    }

                    String lengthSlope = StringUtils.valueOf(map.get("缝宽(mm)"));
                    if (!StringUtils.isBlank(lengthSlope)) {
                        try {
                            BigDecimal bigDecimalLengthSlope = new BigDecimal(lengthSlope);
                            fbCheckOutRecordDto.setLengthSlope(bigDecimalLengthSlope);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "缝宽格式错误！");
                        }
                    }


                    String headSlope = StringUtils.valueOf(map.get("头斜(mm)"));
                    if (!StringUtils.isBlank(headSlope)) {
                        try {
                            BigDecimal bigDecimalHeadSlope = new BigDecimal(headSlope);
                            fbCheckOutRecordDto.setHeadSlope(bigDecimalHeadSlope);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "头斜格式错误！");
                        }
                    }

                    String tailSlope = StringUtils.valueOf(map.get("尾斜(mm)"));
                    if (!StringUtils.isBlank(tailSlope)) {
                        try {
                            BigDecimal bigDecimalTailSlope = new BigDecimal(tailSlope);
                            fbCheckOutRecordDto.setTailSlope(bigDecimalTailSlope);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "尾斜格式错误！");
                        }
                    }

                    String recutLength = StringUtils.valueOf(map.get("返切长度(mm)"));
                    if (!StringUtils.isBlank(recutLength)) {
                        try {
                            BigDecimal bigDecimalRecutLength = new BigDecimal(recutLength);
                            fbCheckOutRecordDto.setRecutLength(bigDecimalRecutLength);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "返切长度格式错误！");
                        }
                    }

                    String concessionLength = StringUtils.valueOf(map.get("放行长度(mm)"));
                    if (!StringUtils.isBlank(concessionLength)) {
                        try {
                            BigDecimal bigDecimalConcessionLength = new BigDecimal(concessionLength);
                            fbCheckOutRecordDto.setConcessionLength(bigDecimalConcessionLength);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "放行长度格式错误！");
                        }
                    }

                    String lineScratch = StringUtils.valueOf(map.get("线痕(mm)"));
                    if (!StringUtils.isBlank(lineScratch)) {
                        try {
                            BigDecimal bigDecimalLineScratch = new BigDecimal(lineScratch);
                            fbCheckOutRecordDto.setLineScratch(bigDecimalLineScratch);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "线痕格式错误！");
                        }
                    }

                    String scratch = StringUtils.valueOf(map.get("划痕(mm)"));
                    if (!StringUtils.isBlank(scratch)) {
                        try {
                            BigDecimal bigDecimalScratch = new BigDecimal(scratch);
                            fbCheckOutRecordDto.setScratch(bigDecimalScratch);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "划痕格式错误！");
                        }
                    }


                    String knifeScratch = StringUtils.valueOf(map.get("刀痕(mm)"));
                    if (!StringUtils.isBlank(knifeScratch)) {
                        try {
                            BigDecimal bigDecimalknifeScratch = new BigDecimal(knifeScratch);
                            fbCheckOutRecordDto.setKnifeScratch(bigDecimalknifeScratch);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "刀痕格式错误！");
                        }
                    }

                    String collapseLoss = StringUtils.valueOf(map.get("崩损(mm)"));
                    if (!StringUtils.isBlank(collapseLoss)) {
                        try {
                            BigDecimal bigDecimalCollapseLoss = new BigDecimal(collapseLoss);
                            fbCheckOutRecordDto.setCollapseLoss(bigDecimalCollapseLoss);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "崩损格式错误！");
                        }
                    }


                    String noFriction = StringUtils.valueOf(map.get("未磨到(mm)"));
                    if (!StringUtils.isBlank(noFriction)) {
                        try {
                            BigDecimal bigDecimalNoFriction = new BigDecimal(noFriction);
                            fbCheckOutRecordDto.setNoFriction(bigDecimalNoFriction);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "未磨到格式错误！");
                        }
                    }


                    String crystalGrayness = StringUtils.valueOf(map.get("晶体灰皮(mm)"));
                    if (!StringUtils.isBlank(crystalGrayness)) {
                        try {
                            BigDecimal bigDecimalCrystalGrayness = new BigDecimal(crystalGrayness);
                            fbCheckOutRecordDto.setCrystalGrayness(bigDecimalCrystalGrayness);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "晶体灰皮格式错误！");
                        }
                    }

                    String squareRodGrayness = StringUtils.valueOf(map.get("方棒灰皮(mm)"));
                    if (!StringUtils.isBlank(squareRodGrayness)) {
                        try {
                            BigDecimal bigDecimalSquareRodGrayness = new BigDecimal(squareRodGrayness);
                            fbCheckOutRecordDto.setSquareRodGrayness(bigDecimalSquareRodGrayness);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "晶体灰皮格式错误！");
                        }
                    }

                    String greyHideEdge = StringUtils.valueOf(map.get("灰皮边缘(mm)"));
                    if (!StringUtils.isBlank(greyHideEdge)) {
                        try {
                            BigDecimal bigDecimalGreyHideEdge = new BigDecimal(greyHideEdge);
                            fbCheckOutRecordDto.setSquareRodGrayness(bigDecimalGreyHideEdge);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "灰皮边缘格式错误！");
                        }
                    }

                    String greyHideCenter = StringUtils.valueOf(map.get("灰皮非边缘(mm)"));
                    if (!StringUtils.isBlank(greyHideCenter)) {
                        try {
                            BigDecimal bigDecimalGreyHideCenter = new BigDecimal(greyHideCenter);
                            fbCheckOutRecordDto.setGreyHideCenter(bigDecimalGreyHideCenter);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "灰皮非边缘格式错误！");
                        }
                    }

                    String crack = StringUtils.valueOf(map.get("裂纹(mm)"));
                    if (!StringUtils.isBlank(crack)) {
                        try {
                            BigDecimal bigDecimalCrack = new BigDecimal(crack);
                            fbCheckOutRecordDto.setCrack(bigDecimalCrack);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "裂纹格式错误！");
                        }
                    }

                    String sizeException = StringUtils.valueOf(map.get("尺寸异常(mm)"));
                    if (!StringUtils.isBlank(sizeException)) {
                        try {
                            BigDecimal bigDecimalSizeException = new BigDecimal(sizeException);
                            fbCheckOutRecordDto.setSizeException(bigDecimalSizeException);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "尺寸异常格式错误！");
                        }
                    }


                    String waterMark = StringUtils.valueOf(map.get("水印(mm)"));
                    if (!StringUtils.isBlank(waterMark)) {
                        try {
                            BigDecimal bigDecimalWaterMark = new BigDecimal(waterMark);
                            fbCheckOutRecordDto.setWaterMark(bigDecimalWaterMark);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "水印格式错误！");
                        }
                    }

                    String hotSpot = StringUtils.valueOf(map.get("亮斑(mm)"));
                    if (!StringUtils.isBlank(hotSpot)) {
                        try {
                            BigDecimal bigDecimalHotSpot = new BigDecimal(hotSpot);
                            fbCheckOutRecordDto.setHotSpot(bigDecimalHotSpot);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "亮斑格式错误！");
                        }
                    }

                    String ridgeBroken = StringUtils.valueOf(map.get("棱崩(mm)"));
                    if (!StringUtils.isBlank(ridgeBroken)) {
                        try {
                            BigDecimal bigDecimalRidgeBroken = new BigDecimal(ridgeBroken);
                            fbCheckOutRecordDto.setHotSpot(bigDecimalRidgeBroken);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "棱崩格式错误！");
                        }
                    }

                    String yinYangFace = StringUtils.valueOf(map.get("阴阳面(mm)"));
                    if (!StringUtils.isBlank(yinYangFace)) {
                        try {
                            BigDecimal bigDecimalYinYangFace = new BigDecimal(yinYangFace);
                            fbCheckOutRecordDto.setYinYangFace(bigDecimalYinYangFace);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "阴阳面格式错误！");
                        }
                    }

                    String wheelHighLights = StringUtils.valueOf(map.get("砂轮亮点(mm)"));
                    if (!StringUtils.isBlank(wheelHighLights)) {
                        try {
                            BigDecimal bigDecimalWheelHighLights = new BigDecimal(wheelHighLights);
                            fbCheckOutRecordDto.setWheelHighLights(bigDecimalWheelHighLights);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "砂轮亮点格式错误！");
                        }
                    }

                    String roundHandle = StringUtils.valueOf(map.get("外圆手感(mm)"));
                    if (!StringUtils.isBlank(roundHandle)) {
                        try {
                            BigDecimal bigDecimalRoundHandle = new BigDecimal(roundHandle);
                            fbCheckOutRecordDto.setRoundHandle(bigDecimalRoundHandle);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "外圆手感格式错误！");
                        }
                    }

                    String flatHandle = StringUtils.valueOf(map.get("平面手感(mm)"));
                    if (!StringUtils.isBlank(flatHandle)) {
                        try {
                            BigDecimal bigDecimalFlatHandle = new BigDecimal(flatHandle);
                            fbCheckOutRecordDto.setRoundHandle(bigDecimalFlatHandle);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "平面手感格式错误！");
                        }
                    }

                    String wheelMark = StringUtils.valueOf(map.get("砂轮印(mm)"));
                    if (!StringUtils.isBlank(wheelMark)) {
                        try {
                            BigDecimal bigDecimalWheelMark = new BigDecimal(wheelMark);
                            fbCheckOutRecordDto.setWheelMark(bigDecimalWheelMark);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "砂轮印格式错误！");
                        }
                    }


                    String colourDifference = StringUtils.valueOf(map.get(""));
                    if (!StringUtils.isBlank(colourDifference)) {
                        try {
                            BigDecimal bigDecimalColourDifference = new BigDecimal(colourDifference);
                            fbCheckOutRecordDto.setColourDifference(bigDecimalColourDifference);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "色差格式错误！");
                        }
                    }


                    String collapse = StringUtils.valueOf(map.get("塌边(mm)"));
                    if (!StringUtils.isBlank(collapse)) {
                        try {
                            BigDecimal bigDecimalCollapse = new BigDecimal(collapse);
                            fbCheckOutRecordDto.setCollapse(bigDecimalCollapse);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "塌边格式错误！");
                        }
                    }

                    String rollerPrint = StringUtils.valueOf(map.get("滚轮印(mm)"));
                    if (!StringUtils.isBlank(rollerPrint)) {
                        try {
                            BigDecimal bigDecimalRollerPrint = new BigDecimal(rollerPrint);
                            fbCheckOutRecordDto.setRollerPrint(bigDecimalRollerPrint);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "滚轮印格式错误！");
                        }
                    }

                    String diameterMin = StringUtils.valueOf(map.get("直径min"));
                    if (!StringUtils.isBlank(diameterMin)) {
                        try {
                            BigDecimal bigDecimalDiameterMin = new BigDecimal(diameterMin);
                            fbCheckOutRecordDto.setDiameterMin(bigDecimalDiameterMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "直径min格式错误！");
                        }
                    }

                    String diameterMax = StringUtils.valueOf(map.get("直径max"));
                    if (!StringUtils.isBlank(diameterMax)) {
                        try {
                            BigDecimal bigDecimalDiameterMax = new BigDecimal(diameterMax);
                            fbCheckOutRecordDto.setDiameterMax(bigDecimalDiameterMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "直径max格式错误！");
                        }
                    }

                    String sideLengthMin = StringUtils.valueOf(map.get("边长min"));
                    if (!StringUtils.isBlank(sideLengthMin)) {
                        try {
                            BigDecimal bigDecimalSideLengthMin = new BigDecimal(sideLengthMin);
                            fbCheckOutRecordDto.setSideLengthMin(bigDecimalSideLengthMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "边长min格式错误！");
                        }
                    }

                    String sideLengthMax = StringUtils.valueOf(map.get("边长max"));
                    if (!StringUtils.isBlank(sideLengthMax)) {
                        try {
                            BigDecimal bigDecimalSideLengthMax = new BigDecimal(sideLengthMax);
                            fbCheckOutRecordDto.setSideLengthMax(bigDecimalSideLengthMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "边长min格式错误！");
                        }
                    }

                    String verticalityMin = StringUtils.valueOf(map.get("垂直度min"));
                    if (!StringUtils.isBlank(verticalityMin)) {
                        try {
                            BigDecimal bigDecimalVerticalityMin = new BigDecimal(verticalityMin);
                            fbCheckOutRecordDto.setVerticalityMin(bigDecimalVerticalityMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "垂直度min格式错误！");
                        }
                    }

                    String verticalityMax = StringUtils.valueOf(map.get("垂直度max"));
                    if (!StringUtils.isBlank(verticalityMax)) {
                        try {
                            BigDecimal bigDecimalVerticalityMax = new BigDecimal(verticalityMax);
                            fbCheckOutRecordDto.setVerticalityMax(bigDecimalVerticalityMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "垂直度max格式错误！");
                        }
                    }

                    String projectionMin = StringUtils.valueOf(map.get("弧长投影min"));
                    if (!StringUtils.isBlank(projectionMin)) {
                        try {
                            BigDecimal bigDecimalProjectionMin = new BigDecimal(projectionMin);
                            fbCheckOutRecordDto.setProjectionMin(bigDecimalProjectionMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "弧长投影min格式错误！");
                        }
                    }

                    String projectionMax = StringUtils.valueOf(map.get("弧长投影max"));
                    if (!StringUtils.isBlank(projectionMax)) {
                        try {
                            BigDecimal bigDecimalProjectionMax = new BigDecimal(projectionMax);
                            fbCheckOutRecordDto.setProjectionMax(bigDecimalProjectionMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "弧长投影max格式错误！");
                        }
                    }

                    String chordLengthMin = StringUtils.valueOf(map.get("弦长min"));
                    if (!StringUtils.isBlank(chordLengthMin)) {
                        try {
                            BigDecimal bigDecimalChordLengthMin = new BigDecimal(chordLengthMin);
                            fbCheckOutRecordDto.setChordLengthMin(bigDecimalChordLengthMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "弦长min格式错误！");
                        }
                    }

                    String chordLengthMax = StringUtils.valueOf(map.get("弦长max"));
                    if (!StringUtils.isBlank(chordLengthMax)) {
                        try {
                            BigDecimal bigDecimalChordLengthMax = new BigDecimal(chordLengthMax);
                            fbCheckOutRecordDto.setChordLengthMax(bigDecimalChordLengthMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "弦长max格式错误！");
                        }
                    }

                    String planeTaperMin = StringUtils.valueOf(map.get("平面锥度min"));
                    if (!StringUtils.isBlank(planeTaperMin)) {
                        try {
                            BigDecimal bigDecimalPlaneTaperMin = new BigDecimal(planeTaperMin);
                            fbCheckOutRecordDto.setPlaneTaperMin(bigDecimalPlaneTaperMin);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "平面锥度min格式错误！");
                        }
                    }

                    String planeTaperMax = StringUtils.valueOf(map.get("平面锥度max"));
                    if (!StringUtils.isBlank(planeTaperMax)) {
                        try {
                            BigDecimal bigDecimalPlaneTaperMax = new BigDecimal(planeTaperMax);
                            fbCheckOutRecordDto.setPlaneTaperMax(bigDecimalPlaneTaperMax);
                        } catch (Exception e) {
                            throw new Exception("该方棒单晶编号：" + fbSectionNo + "平面锥度max格式错误！");
                        }
                    }

                    FbCheckOutRecord fbCheckOutRecord = this.fromDto(fbCheckOutRecordDto);
                    fbCheckOutRecords.add(fbCheckOutRecord);
                    fbCheckOutRecordDtos.add(fbCheckOutRecordDto);

                    // 数据同步到方棒检验数据库
                    FbCheckOutData fbCheckOutData = toData(fbCheckOutRecordDto);
                    // 目前写死
                    fbCheckOutData.setOrderType("ZH01");
                    fbCheckOutDatas.add(fbCheckOutData);
                }
                fbCheckOutRecordService.saveAll(fbCheckOutRecords);
                saveAll(fbCheckOutDatas);
            }


        }

        return ResultGenerator.success();
    }

    @Override
    public void saveAll(List<FbCheckOutData> fbCheckOutDatas) {
        dao.saveAll(fbCheckOutDatas);
    }

    @Override
    public Boolean isSpotCheck(String fbSectionNo) {

        //1.查询磨削记录
        EapGrindingMachine eapGrindingMachine = eapGrindingMachineService.findTopByFbSectionNoOrderByIdDesc(fbSectionNo);
        if (eapGrindingMachine == null) {
            return false;
        }
        Boolean is4m = false;
        if (is4m) {
            return true;
        } else {
            //根据磨削记录中磨削机编号，查询外观尺寸检验规则
            List<FbAppearRule> fbAppearRuleList = inspectAppearRuleService.findAll();
            if (eapGrindingMachine.getEquipNo() == null) {
                return false;
            }
            String[] eapNoSplit = eapGrindingMachine.getEquipNo().split("");

            FbAppearRule fbAppearRule = null;
            for (FbAppearRule appearRule : fbAppearRuleList) {
                if (appearRule.getSeries() == null) {
                    continue;
                }
                String[] split = appearRule.getSeries().split("");
                Boolean notEquals = false;
                for (int i = 0; i < split.length; i++) {
                    if (!split[i].equals("*") && !split[i].equals(eapNoSplit[i])) {
                        notEquals = true;
                        break;
                    }
                }
                if (!notEquals) {
                    fbAppearRule = appearRule;
                    break;
                }
            }
//            if (fbAppearRule.getCheckType())
            // 根据外观尺寸检验规则中的时间和方棒编号查询磨削记录，安时间排序，判断当前晶段在该时间段内是第几根
//            List<EapGrindingMachine> eapGrindingMachineList = eapGrindingMachineService.findByTime();


        }
        // if（4m){
        // 抽检
        // }else{
        //  1.根据磨削记录中磨削机编号，查询外观尺寸检验规则
        //   2.根据外观尺寸检验规则中的时间和方棒编号查询磨削记录，安时间排序，判断当前晶段在该时间段内是第几根
        //      3.根据当前晶段根数是否和外观尺寸检验规则中是否匹配
        //      if（匹配）{
        //      抽检
        //      }else{
        //      不抽检
        //      }
        // }
        //
        // ，判断是否为4m，是需检验，否查询 外观检验规则（），符合规则的检验
        return false;
    }

    @Override
    public String getSectionComposeBySectionNo(String fbSectionNo) {
        return dao.getSectionComposeBySectionNo(fbSectionNo);
    }

    @Override
    public List<String> getNos() {
        return dao.getNos();
    }

    @Override
    public void updateOrderNoBySectionNo(String sectionNo, String orderNo) {
        dao.updateOrderNoBySectionNo(sectionNo, orderNo);
    }

    @Override
    public void saveAndLog(FbCheckOutData fbCheckOutData, OperateCheckOutDataLog operateCheckOutDataLog) {
        dao.save(fbCheckOutData);
        operateCheckOutDataLogService.saveOrUpdate(operateCheckOutDataLog);
    }


    public FbCheckOutRecord fromDto(FbCheckOutRecordDto dto) {
        return modelMapper.map(dto, FbCheckOutRecord.class);
    }

    public FbCheckOutData toData(FbCheckOutRecordDto dto) {
        FbCheckOutData data = new FbCheckOutData();

        BeanUtils.copyProperties(dto, data, BeanUtil.getIgnoreProperties(dto, null, null));
        return data;
    }

    public GetFbInfoDto getFbInfoBySectionNofb(String sectionNofb) throws Exception {
        // 通过方棒编号去追溯表中找到对应的圆棒id 编号信息
        RecutDetail bySectionNoFb = this.recutDetailDao.findBySectionNoFb(sectionNofb);
        if (bySectionNoFb == null) {
            throw new Exception("该方棒" + sectionNofb + "无法获取对应数据,请手动录入");
        }
        Result<GetFbInfoDto> fbInfoByIdResult = ybFeignService.findFbInfoBySectionNoYb(bySectionNoFb.getSectionNo());
        if (fbInfoByIdResult.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new Exception(fbInfoByIdResult.getMessage());
        }
        GetFbInfoDto fbInfoById = fbInfoByIdResult.getData();
        // 拼棒信息(未解绑)
        FbJoin byGroupNo = this.fbJoinDao.findByGroupNoAndState("%" + sectionNofb + "%", 0);
        if (byGroupNo != null) {
            fbInfoById.setSticksCount(byGroupNo.getStickCount());
            fbInfoById.setSpellBarLength(byGroupNo.getTotalLength());
        }

        return fbInfoById;
    }


    @Override
    public void updateWeight(FbCheckOutData updateDto) {
        dao.updateWeight(updateDto.getId(), updateDto.getWeight());
    }

    @Override
    public void updatePieceCheckData(PieceCheckDto pieceCheckDto) {
        FbCheckOutData fbCheckOutData = findDataByFbSectionNo(pieceCheckDto.getSectionNoFb());
        if (fbCheckOutData != null) {
            if (pieceCheckDto.getOxygenContentFirsta() != null) {
                fbCheckOutData.setOxygenContentFirsta(pieceCheckDto.getOxygenContentFirsta());
            }
            if (pieceCheckDto.getCarbonContentFirsta() != null) {
                fbCheckOutData.setCarbonContentFirsta(pieceCheckDto.getCarbonContentFirsta());
            }
            if (pieceCheckDto.getOxygenContentEndb() != null) {
                fbCheckOutData.setOxygenContentEndb(pieceCheckDto.getOxygenContentEndb());
            }
            if (pieceCheckDto.getCarbonContentEndb() != null) {
                fbCheckOutData.setCarbonContentEndb(pieceCheckDto.getCarbonContentEndb());
            }
            if (pieceCheckDto.getOxygenContentHead() != null) {
                fbCheckOutData.setTOxygenContent(pieceCheckDto.getOxygenContentHead());
            }
            if (pieceCheckDto.getOxygenContentTail() != null) {
                fbCheckOutData.setWOxygenContent(pieceCheckDto.getOxygenContentTail());
            }
            if (pieceCheckDto.getCarbonContentHead() != null) {
                fbCheckOutData.setTCarbonContent(pieceCheckDto.getCarbonContentHead());
            }
            if (pieceCheckDto.getCarbonContentTail() != null) {
                fbCheckOutData.setWCarbonContent(pieceCheckDto.getCarbonContentTail());
            }
            fbCheckOutRecordService.saveDataAndGrade(fbCheckOutData, "");
        }
    }

    @Override
    public String findSumWeightByOrderNoAndBackCut(String orderNo, int backCut) {
        return dao.findSumWeightByOrderNoAndBackCut(orderNo,backCut);
    }

    @Override
    public FbCheckOutData findDataByLikeFbSectionNo(String fbSectionNo) {
      return dao.findDataByLikeFbSectionNo("%"+fbSectionNo +"%");
    }

    @Override
    public FbCheckOutData updateBackCutByFbSectionNo(String fbSectionNo,Integer backCut) {
        FbCheckOutData fbCheckOutData = this.findDataByFbSectionNo(fbSectionNo);
        if (null != fbCheckOutData){
            fbCheckOutData.setBackCut(backCut);
            this.saveOrUpdate(fbCheckOutData);
        }
        FbCheckOutRecord fbCheckOutRecord = fbCheckOutRecordService.findFbCheckOutRecord(fbSectionNo);
        if (null != fbCheckOutRecord){
            fbCheckOutRecord.setBackCut(backCut);
            fbCheckOutRecordService.saveOrUpdate(fbCheckOutRecord);
        }
        return null;
    }
}
