package com.rongwei.bscommon.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.ApsReportOutputDao;
import com.rongwei.bscommon.sys.service.*;
import com.rongwei.bscommon.sys.utils.QRCodeGenerator;
import com.rongwei.bscommon.sys.utils.StringUtil;
import com.rongwei.bsentity.domain.*;
import com.rongwei.bsentity.vo.InsertMyReportOutputReq;
import com.rongwei.commonservice.serial.service.SysSerialNumberService;
import com.rongwei.rwadmincommon.system.domain.SysDictDo;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.rwcommonentity.commonservers.vo.SysSerialVo;
import com.rongwei.safecommon.fegin.CXAdminFeginClient;
import com.rongwei.safecommon.utils.CXCommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.rongwei.safecommon.utils.SaveConstans.CheckLevel.B;

/**
 *
 */
@Service
public class ApsReportOutputServiceImpl extends ServiceImpl<ApsReportOutputDao, ApsReportOutputDo>
        implements ApsReportOutputService {
    @Autowired
    private ApsProcessOperationOutMaterService apsProcessOperationOutMaterService;
    @Resource
    private CXAdminFeginClient cxAdminFeginClient;
    @Autowired
    private ApsReportRecordsService apsReportRecordsService;
    @Autowired
    private ApsProcessOperationService apsProcessOperationService;
    @Autowired
    private ApsReportOutputDao apsReportOutputDao;
    @Autowired
    private SysSerialNumberService sysSerialNumberService;
    @Autowired
    private ApsProcessOperationSplitService apsProcessOperationSplitService;

    @Override
    @Transactional
    public R insertMyReportOutput(InsertMyReportOutputReq req) {
        //最终新增的子表数据
        List<ApsReportOutputDo> resList = new LinkedList<>();

        //查询工序作业输出物料子表信息
        List<ApsProcessOperationOutMaterDo> list = apsProcessOperationOutMaterService.list(new QueryWrapper<ApsProcessOperationOutMaterDo>().lambda().eq(ApsProcessOperationOutMaterDo::getMainid, req.getProcessId()));

        //默认检验结果设置
        String insepctionLevel = req.getInsepctionLevel();
        String qualified = req.getQualified();

        if (req.getIfBlankProcess().equals("是")) {
            if (req.getBlankType().equals("铸轧")) {
                for (ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo : list) {
                    int outval = apsProcessOperationOutMaterDo.getOutval();

                    for (int i = 1; i <= outval; i++) {
                        //插入记录物料子表
                        ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();

                        apsReportOutputDo.setMainid(req.getMainId());
                        apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
                        /**
                         * 需求变更：输出物料重新拼接
                         */
                        if (ObjectUtil.isNotEmpty(apsProcessOperationOutMaterDo.getOutputmaterial())){
                            apsReportOutputDo.setOutputmaterial(StringUtil.getSubstringBeforeLastStar(apsProcessOperationOutMaterDo.getOutputmaterial(),"*"));
                        }
                        apsReportOutputDo.setLevel(req.getLevel());
                        //生成批次号
                        SysSerialVo vo = new SysSerialVo();
                        vo.setModulecode("aps_report_output_" + req.getDeviceId());
                        vo.setRuleTemplateStr(req.getMark() + req.getYearCode() + req.getMonthCode() + req.getEquipmentCode() + req.getCodeRule());
                        vo.setResetrule("date:yyyyMM");
                        vo.setIfautomaticreset("y");
                        vo.setInitValue(0L);
                        String code = sysSerialNumberService.getCodeByTemplate(vo);
                        if (StringUtils.isNotBlank(code)) {
                            //取出批次号后三位
                            String lastThreeChars = code.substring(code.length() - 3);
                            //判断如果code最后三位如果为000时再次调用序列号接口/修复跨月流水号问题
                            if ("000".equals(lastThreeChars)) {
                                code = sysSerialNumberService.getCodeByTemplate(vo);
                                lastThreeChars = code.substring(code.length() - 3);
                            }
                            //当工序为‘铸轧’时添加序列号将序列号默认为批次号后三位
                            apsReportOutputDo.setSerialnumber(Long.parseLong(lastThreeChars));
                        }
                        apsReportOutputDo.setOutputnumber(code);
                        JSONObject json = new JSONObject();
                        json.put("batchNumber", code);
                        //生成二维码
                        String qrCode = QRCodeGenerator.stringToQRCodeBase64(json.toJSONString(), 200, 200, "png");
                        apsReportOutputDo.setQrcode(qrCode);
                        apsReportOutputDo.setId(SecurityUtil.getUUID());
                        apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
                        apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
                        apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
                        apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
                        apsReportOutputDo.setInsepctionlevel(insepctionLevel);
                        apsReportOutputDo.setQualified(qualified);
                        resList.add(apsReportOutputDo);
                    }
                }
            }
            if (req.getBlankType().equals("热轧")) {
                int j = 0;
                for (ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo : list) {
                    int outval = apsProcessOperationOutMaterDo.getOutval();
                    for (int i = 1; i <= outval; i++) {
                        j++;
                        //插入记录物料子表
                        ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
                        apsReportOutputDo.setId(SecurityUtil.getUUID());
                        apsReportOutputDo.setMainid(req.getMainId());
                        apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
                        /**
                         * 需求变更：输出物料重新拼接
                         */
                        if (ObjectUtil.isNotEmpty(apsProcessOperationOutMaterDo.getOutputmaterial())){
                            apsReportOutputDo.setOutputmaterial(StringUtil.getSubstringBeforeLastStar(apsProcessOperationOutMaterDo.getOutputmaterial(),"*"));
                        }
                        apsReportOutputDo.setLevel(req.getLevel());
                        //生成批次号
                        String code = req.getMark() + req.getYearCode() + req.getMonthCode() + req.getEquipmentCode() + req.getHeatNumber() + j;
                        apsReportOutputDo.setOutputnumber(code);
                        JSONObject json = new JSONObject();
                        json.put("batchNumber", code);
                        //生成二维码
                        String qrCode = QRCodeGenerator.stringToQRCodeBase64(json.toJSONString(), 200, 200, "png");
                        apsReportOutputDo.setQrcode(qrCode);
                        apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
                        apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
                        apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
                        apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
                        apsReportOutputDo.setInsepctionlevel(insepctionLevel);
                        apsReportOutputDo.setQualified(qualified);
                        resList.add(apsReportOutputDo);
                    }
                }
            }
        }
        if (req.getIfBlankProcess().equals("否")) {
            //如果输出物料数量=1，并且单卷输出数量=1，批次号不变
            if (list.size() == 1 && list.get(0).getOutval() == 1) {
                //插入记录物料子表
                ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
                apsReportOutputDo.setId(SecurityUtil.getUUID());
                apsReportOutputDo.setMainid(req.getMainId());
                apsReportOutputDo.setOutputid(list.get(0).getId());
                /**
                 * 需求变更：输出物料重新拼接
                 */
                if (ObjectUtil.isNotEmpty(list.get(0).getOutputmaterial())){
                    apsReportOutputDo.setOutputmaterial(StringUtil.getSubstringBeforeLastStar(list.get(0).getOutputmaterial(),"*"));
                }
                apsReportOutputDo.setOutputnumber(req.getInputBatchNumber());
                apsReportOutputDo.setLevel(req.getLevel());
                apsReportOutputDo.setThickness(list.get(0).getThickness());
                apsReportOutputDo.setWidth(list.get(0).getProwidth());
                apsReportOutputDo.setLength(list.get(0).getProlength());
                apsReportOutputDo.setWeight(list.get(0).getSinglerollweight());
                apsReportOutputDo.setInsepctionlevel(insepctionLevel);
                apsReportOutputDo.setQualified(qualified);
                JSONObject json = new JSONObject();
                json.put("batchNumber", req.getInputBatchNumber());
                //生成二维码
                String qrCode = QRCodeGenerator.stringToQRCodeBase64(json.toJSONString(), 200, 200, "png");
                apsReportOutputDo.setQrcode(qrCode);
                resList.add(apsReportOutputDo);
            } else {//输出物料数量>1，或者单卷输出数量>1
                //查询工序作业信息
//                ApsProcessOperationDo apsProcessOperationDo = apsProcessOperationService.getById(req.getProcessId());
                //查询工序作业分切方式子表
                List<ApsProcessOperationSplitDo> apsProcessOperationSplitDos = apsProcessOperationSplitService.list(new QueryWrapper<ApsProcessOperationSplitDo>().lambda().eq(ApsProcessOperationSplitDo::getMainid, req.getProcessId()));
                //分切工序
//                if ("分切".equals(apsProcessOperationDo.getProcess())) {
                    /**
                     * 新增需求：如果输出物料分切方式不为空，则按照分切方式生成分卷批次号
                     * 分卷批次号={批次号}-{分切车次序列号1-2位}-{分卷序列号1-2位}
                     * {分切车次序列号1-2位}：表示分切第几车次；
                     * {分卷序列号1-2位}：表示该料卷的该车次第几个分卷；
                     * 如果输出物料分切方式为空，或者分切车次=0
                     * 分卷批次号={批次号}-{分卷序列号1-2位}{分卷序列号1-2位}：表示该料卷的第几个分卷
                     */
                    if (ObjectUtil.isNotEmpty(apsProcessOperationSplitDos)){
                        //根据分切车次排序
                        apsProcessOperationSplitDos = apsProcessOperationSplitDos.stream().sorted(Comparator.comparing(ApsProcessOperationSplitDo::getSplittrainno)).collect(Collectors.toList());
                        apsProcessOperationSplitDos.forEach(info->{
                            autoGenerateOutPutNumber(list,info,resList,req);
                        });
                    }else {//非分切工序
                        //分卷开始初始化值
                        int j = 1;
                        for (ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo : list) {
                            int outval = apsProcessOperationOutMaterDo.getOutval();
                            for (int i = 1; i <= outval; i++) {
                                //插入记录物料子表
                                ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();

                                apsReportOutputDo.setMainid(req.getMainId());
                                apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
                                /**
                                 * 需求变更：输出物料重新拼接
                                 */
                                if (ObjectUtil.isNotEmpty(apsProcessOperationOutMaterDo.getOutputmaterial())){
                                    apsReportOutputDo.setOutputmaterial(StringUtil.getSubstringBeforeLastStar(apsProcessOperationOutMaterDo.getOutputmaterial(),"*"));
                                }
                                apsReportOutputDo.setLevel(req.getLevel());
                                //生成批次号
                                //不用走系统生成的批次号了，按照输入的批次号就行分卷就行
                                String code = req.getInputBatchNumber() + "-" + j;
                                j++;

//                        //生成批次号
//                        SysSerialVo vo = new SysSerialVo();
//                        vo.setModulecode("aps_report_output_" + req.getInputBatchNumber());
//                        vo.setRuleTemplateStr(req.getInputBatchNumber() + "-" + "@{serialNumber:#00}");
//                        vo.setResetrule("date:yyyyMM");
//                        vo.setIfautomaticreset("y");
//                        vo.setInitValue(0L);
//                        Map<String, Object> codeData = cxAdminFeginClient.getSerialNumberCode(vo).getData();
//                        String code = codeData.get("code").toString();

                                apsReportOutputDo.setOutputnumber(code);
                                JSONObject json = new JSONObject();
                                json.put("batchNumber", code);
                                //生成二维码
                                String qrCode = QRCodeGenerator.stringToQRCodeBase64(json.toJSONString(), 200, 200, "png");
                                apsReportOutputDo.setQrcode(qrCode);

                                apsReportOutputDo.setId(SecurityUtil.getUUID());
                                apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
                                apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
                                apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
                                apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
                                apsReportOutputDo.setInsepctionlevel(insepctionLevel);
                                apsReportOutputDo.setQualified(qualified);
                                resList.add(apsReportOutputDo);
                            }
                        }
                    }
//                    else {
//                        int count = 1;
//                        for (ApsProcessOperationOutMaterDo info : list) {
//                            //输出卷数
//                            Integer outval = info.getOutval();
//                            for (int i = 1; i <= outval; i++) {
//                                //插入记录物料子表
//                                ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
//                                apsReportOutputDo.setMainid(req.getMainId());
//                                apsReportOutputDo.setOutputid(info.getId());
//                                /**
//                                 * 需求变更：输出物料重新拼接
//                                 */
//                                if (ObjectUtil.isNotEmpty(info.getOutputmaterial())){
//                                    apsReportOutputDo.setOutputmaterial(StringUtil.getSubstringBeforeLastStar(info.getOutputmaterial(),"*"));
//                                }
//                                apsReportOutputDo.setLevel(req.getLevel());
//                                //生成批次号
//                                String code = req.getInputBatchNumber() + "-" + count;
//                                apsReportOutputDo.setOutputnumber(code);
//                                JSONObject json = new JSONObject();
//                                json.put("batchNumber", code);
//                                //生成二维码
//                                String qrCode = QRCodeGenerator.stringToQRCodeBase64(json.toJSONString(), 200, 200, "png");
//                                apsReportOutputDo.setQrcode(qrCode);
//                                apsReportOutputDo.setId(SecurityUtil.getUUID());
//                                apsReportOutputDo.setThickness(info.getThickness());
//                                apsReportOutputDo.setWidth(info.getProwidth());
//                                apsReportOutputDo.setLength(info.getProlength());
//                                apsReportOutputDo.setWeight(info.getSinglerollweight());
//                                apsReportOutputDo.setInsepctionlevel(insepctionLevel);
//                                apsReportOutputDo.setQualified(qualified);
//                                resList.add(apsReportOutputDo);
//                                count++;
//                            }
//                        }
//                    }
//                    int j = 1;//初始化分切车次
//                    for (ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo : list) {
//                        //每个输出物料的分切车次
//                        Integer splittrainno = apsProcessOperationOutMaterDo.getSplittrainno();
//                        //输出卷数
//                        Integer outval = apsProcessOperationOutMaterDo.getOutval();
//                        //每次分切的卷数
//                        int count = outval / splittrainno;
//                        //分切车次循环
//                        for (int k = 0; k < splittrainno; k++) {
//                            //分卷循环
//                            for (int i = 1; i <= count; i++) {
//                                //插入记录物料子表
//                                ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
//
//                                apsReportOutputDo.setMainid(req.getMainId());
//                                apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
//                                /**
//                                 * 需求变更：输出物料重新拼接
//                                 */
//                                if (ObjectUtil.isNotEmpty(apsProcessOperationOutMaterDo.getOutputmaterial())){
//                                    apsReportOutputDo.setOutputmaterial(StringUtil.getSubstringBeforeLastStar(apsProcessOperationOutMaterDo.getOutputmaterial(),"*"));
//                                }
//                                apsReportOutputDo.setLevel(req.getLevel());
//                                //生成批次号
//                                String code = req.getInputBatchNumber() + "-" + j + "-" + i;
//
//                                apsReportOutputDo.setOutputnumber(code);
//                                JSONObject json = new JSONObject();
//                                json.put("batchNumber", code);
//                                //生成二维码
//                                String qrCode = QRCodeGenerator.stringToQRCodeBase64(json.toJSONString(), 200, 200, "png");
//                                apsReportOutputDo.setQrcode(qrCode);
//
//                                apsReportOutputDo.setId(SecurityUtil.getUUID());
//                                apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
//                                apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
//                                apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
//                                apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
//                                apsReportOutputDo.setInsepctionlevel(insepctionLevel);
//                                apsReportOutputDo.setQualified(qualified);
//                                resList.add(apsReportOutputDo);
//
//                            }
//                            j++;
//                        }
//                    }
//                }
            }
        }

        //插入子表
        //获取当前用户信息
        //获取当前用户的所属工厂
        SysUserVo currentUser = CXCommonUtils.getCurrentUser();
        String tenantId = CXCommonUtils.getCurrentUserFactoryId(currentUser);
        for (ApsReportOutputDo apsReportOutputDo : resList) {
            apsReportOutputDo.setTenantid(tenantId);
        }
        this.saveBatch(resList);
        //更新主表中的输出物料
        List<String> outPutMaterialList = resList.stream().map(ApsReportOutputDo::getOutputmaterial).distinct().collect(Collectors.toList());
        String outPutMaterials = String.join(",", outPutMaterialList);
        ApsReportRecordsDo apsReportRecordsDo = new ApsReportRecordsDo();
        apsReportRecordsDo.setId(req.getMainId());
        apsReportRecordsDo.setReportoutmaterrequest(outPutMaterials);
        apsReportRecordsService.updateById(apsReportRecordsDo);
        return R.ok();
    }

    @Override
    public List<SysDictDo> getQualityDefectsDictsByType() {
        return apsReportOutputDao.getQualityDefectsDictsByType();
    }

    @Override
    public String selectDescByReportId(String reportId) {
        return apsReportOutputDao.selectDescByReportId(reportId);
    }

    private void autoGenerateOutPutNumber(List<ApsProcessOperationOutMaterDo> apsProcessOperationOutMaterDos,ApsProcessOperationSplitDo apsProcessOperationSplitDo,List<ApsReportOutputDo> resList,InsertMyReportOutputReq req){
        String oneMaterId = apsProcessOperationSplitDo.getOutmaterone();
        Integer oneRollNum = apsProcessOperationSplitDo.getRollnumone();
        Integer count = 1;
        if (ObjectUtil.isNotEmpty(oneMaterId)&&oneRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowOneOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(oneMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowOneOutMater.get(0),resList,oneRollNum,req,apsProcessOperationSplitDo,count);
        }
        String twoMaterId = apsProcessOperationSplitDo.getOutmatertwo();
        Integer twoRollNum = apsProcessOperationSplitDo.getRollnumtwo();
        if (ObjectUtil.isNotEmpty(twoMaterId)&&twoRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowTwoOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(twoMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowTwoOutMater.get(0),resList,twoRollNum,req,apsProcessOperationSplitDo,count);
        }
        String thrMaterId = apsProcessOperationSplitDo.getOutmaterthr();
        Integer thrRollNum = apsProcessOperationSplitDo.getRollnumthr();
        if (ObjectUtil.isNotEmpty(thrMaterId)&&thrRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowThrOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(thrMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowThrOutMater.get(0),resList,thrRollNum,req,apsProcessOperationSplitDo,count);
        }
        String fouMaterId = apsProcessOperationSplitDo.getOutmaterfou();
        Integer fouRollNum = apsProcessOperationSplitDo.getRollnumfou();
        if (ObjectUtil.isNotEmpty(fouMaterId)&&fouRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowFouOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(fouMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowFouOutMater.get(0),resList,fouRollNum,req,apsProcessOperationSplitDo,count);
        }
        String fivMaterId = apsProcessOperationSplitDo.getOutmaterfiv();
        Integer fivRollNum = apsProcessOperationSplitDo.getRollnumfiv();
        if (ObjectUtil.isNotEmpty(fivMaterId)&&fivRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowFivOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(fivMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowFivOutMater.get(0),resList,fivRollNum,req,apsProcessOperationSplitDo,count);
        }
        String sixMaterId = apsProcessOperationSplitDo.getOutmatersix();
        Integer sixRollNum = apsProcessOperationSplitDo.getRollnumsix();
        if (ObjectUtil.isNotEmpty(sixMaterId)&&sixRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowSixOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(sixMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowSixOutMater.get(0),resList,sixRollNum,req,apsProcessOperationSplitDo,count);
        }
        String sevMaterId = apsProcessOperationSplitDo.getOutmatersev();
        Integer sevRollNum = apsProcessOperationSplitDo.getRollnumsev();
        if (ObjectUtil.isNotEmpty(sevMaterId)&&sevRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowSevOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(sevMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowSevOutMater.get(0),resList,sevRollNum,req,apsProcessOperationSplitDo,count);
        }
        String eigMaterId = apsProcessOperationSplitDo.getOutmatereig();
        Integer eigRollNum = apsProcessOperationSplitDo.getRollnumeig();
        if (ObjectUtil.isNotEmpty(eigMaterId)&&eigRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowEigOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(eigMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowEigOutMater.get(0),resList,eigRollNum,req,apsProcessOperationSplitDo,count);
        }
        String ninMaterId = apsProcessOperationSplitDo.getOutmaternin();
        Integer ninRollNum = apsProcessOperationSplitDo.getRollnumnin();
        if (ObjectUtil.isNotEmpty(ninMaterId)&&ninRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowNinOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(ninMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowNinOutMater.get(0),resList,ninRollNum,req,apsProcessOperationSplitDo,count);
        }
        String tenMaterId = apsProcessOperationSplitDo.getOutmaterten();
        Integer tenRollNum = apsProcessOperationSplitDo.getRollnumten();
        if (ObjectUtil.isNotEmpty(tenMaterId)&&tenRollNum!= null){
            List<ApsProcessOperationOutMaterDo> nowTenOutMater = apsProcessOperationOutMaterDos.stream().filter(p -> p.getId().equals(tenMaterId)).collect(Collectors.toList());
            count = handleOnePutNumber(nowTenOutMater.get(0),resList,tenRollNum,req,apsProcessOperationSplitDo,count);
        }
    }

    private Integer handleOnePutNumber(ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo,List<ApsReportOutputDo> resList,Integer rollNum,InsertMyReportOutputReq req,ApsProcessOperationSplitDo apsProcessOperationSplitDo,Integer count) {
        for (int i=1;i<=rollNum;i++){
            //插入记录物料子表
            ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
            apsReportOutputDo.setMainid(req.getMainId());
            apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
            if (ObjectUtil.isNotEmpty(apsProcessOperationOutMaterDo.getOutputmaterial())){
                apsReportOutputDo.setOutputmaterial(StringUtil.getSubstringBeforeLastStar(apsProcessOperationOutMaterDo.getOutputmaterial(),"*"));
            }
            apsReportOutputDo.setLevel(req.getLevel());
            //生成批次号
            String code = req.getInputBatchNumber() + "-" + apsProcessOperationSplitDo.getSplittrainno() + "-" + count;
            apsReportOutputDo.setOutputnumber(code);
            JSONObject json = new JSONObject();
            json.put("batchNumber", code);
            //生成二维码
            String qrCode = QRCodeGenerator.stringToQRCodeBase64(json.toJSONString(), 200, 200, "png");
            apsReportOutputDo.setQrcode(qrCode);
            apsReportOutputDo.setId(SecurityUtil.getUUID());
            apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
            apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
            apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
            apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
            apsReportOutputDo.setInsepctionlevel(req.getInsepctionLevel());
            apsReportOutputDo.setQualified(req.getQualified());
            resList.add(apsReportOutputDo);
            count++;
        }
        return count;
    }

//    public R insertMyReportOutputWithOutBatchNumber(InsertMyReportOutputReq req) {
//        //最终新增的子表数据
//        List<ApsReportOutputDo> resList = new LinkedList<>();
//
//        //查询工序作业输出物料子表信息
//        List<ApsProcessOperationOutMaterDo> list = apsProcessOperationOutMaterService.list(new QueryWrapper<ApsProcessOperationOutMaterDo>().lambda().eq(ApsProcessOperationOutMaterDo::getMainid, req.getProcessId()));
//
//        if (req.getIfBlankProcess().equals("是")) {
//            if (req.getBlankType().equals("铸轧")) {
//                for (ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo : list) {
//                    int outval = apsProcessOperationOutMaterDo.getOutval();
//
//                    for (int i = 1; i <= outval; i++) {
//                        //插入记录物料子表
//                        ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
//
//                        apsReportOutputDo.setMainid(req.getMainId());
//                        apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
//                        apsReportOutputDo.setOutputmaterial(apsProcessOperationOutMaterDo.getOutputmaterial());
//                        apsReportOutputDo.setId(SecurityUtil.getUUID());
//                        apsReportOutputDo.setLevel(req.getLevel());
//                        apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
//                        apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
//                        apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
//                        apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
//                        apsReportOutputDo.setInsepctionlevel(B);
//                        apsReportOutputDo.setQualified("是");
//                        resList.add(apsReportOutputDo);
//                    }
//                }
//            }
//            if (req.getBlankType().equals("热轧")) {
//                for (ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo : list) {
//                    int outval = apsProcessOperationOutMaterDo.getOutval();
//                    for (int i = 1; i <= outval; i++) {
//                        //插入记录物料子表
//                        ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
//                        apsReportOutputDo.setId(SecurityUtil.getUUID());
//                        apsReportOutputDo.setMainid(req.getMainId());
//                        apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
//                        apsReportOutputDo.setOutputmaterial(apsProcessOperationOutMaterDo.getOutputmaterial());
//                        apsReportOutputDo.setLevel(req.getLevel());
//                        apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
//                        apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
//                        apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
//                        apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
//                        apsReportOutputDo.setInsepctionlevel(B);
//                        apsReportOutputDo.setQualified("是");
//                        resList.add(apsReportOutputDo);
//                    }
//                }
//            }
//        }
//        if (req.getIfBlankProcess().equals("否")) {
//            //如果输出物料数量=1，并且单卷输出数量=1，批次号不变
//            if (list.size() == 1 && list.get(0).getOutval() == 1) {
//                //插入记录物料子表
//                ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
//                apsReportOutputDo.setId(SecurityUtil.getUUID());
//                apsReportOutputDo.setMainid(req.getMainId());
//                apsReportOutputDo.setOutputid(list.get(0).getId());
//                apsReportOutputDo.setOutputmaterial(list.get(0).getOutputmaterial());
//                apsReportOutputDo.setLevel(req.getLevel());
//                apsReportOutputDo.setThickness(list.get(0).getThickness());
//                apsReportOutputDo.setWidth(list.get(0).getProwidth());
//                apsReportOutputDo.setLength(list.get(0).getProlength());
//                apsReportOutputDo.setWeight(list.get(0).getSinglerollweight());
//                apsReportOutputDo.setInsepctionlevel(B);
//                apsReportOutputDo.setQualified("是");
//                resList.add(apsReportOutputDo);
//            } else {//输出物料数量>1，或者单卷输出数量>1
//                for (ApsProcessOperationOutMaterDo apsProcessOperationOutMaterDo : list) {
//                    int outval = apsProcessOperationOutMaterDo.getOutval();
//                    for (int i = 1; i <= outval; i++) {
//                        //插入记录物料子表
//                        ApsReportOutputDo apsReportOutputDo = new ApsReportOutputDo();
//
//                        apsReportOutputDo.setMainid(req.getMainId());
//                        apsReportOutputDo.setOutputid(apsProcessOperationOutMaterDo.getId());
//                        apsReportOutputDo.setOutputmaterial(apsProcessOperationOutMaterDo.getOutputmaterial());
//                        apsReportOutputDo.setId(SecurityUtil.getUUID());
//                        apsReportOutputDo.setLevel(req.getLevel());
//                        apsReportOutputDo.setThickness(apsProcessOperationOutMaterDo.getThickness());
//                        apsReportOutputDo.setWidth(apsProcessOperationOutMaterDo.getProwidth());
//                        apsReportOutputDo.setLength(apsProcessOperationOutMaterDo.getProlength());
//                        apsReportOutputDo.setWeight(apsProcessOperationOutMaterDo.getSinglerollweight());
//                        apsReportOutputDo.setInsepctionlevel(B);
//                        apsReportOutputDo.setQualified("是");
//                        resList.add(apsReportOutputDo);
//                    }
//                }
//            }
//        }
//
//        //插入子表
//        //获取当前用户信息
//        //获取当前用户的所属工厂
//        SysUserVo currentUser = CXCommonUtils.getCurrentUser();
//        String tenantId = CXCommonUtils.getCurrentUserFactoryId(currentUser);
//        for (ApsReportOutputDo apsReportOutputDo : resList) {
//            apsReportOutputDo.setTenantid(tenantId);
//        }
//        this.saveBatch(resList);
//        return R.ok();
//    }
}




