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

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.ApsProcessOperationOutMaterService;
import com.rongwei.bscommon.sys.service.ApsReportRecordsService;
import com.rongwei.bscommon.sys.utils.QRCodeGenerator;
import com.rongwei.bsentity.domain.ApsProcessOperationOutMaterDo;
import com.rongwei.bsentity.domain.ApsReportOutputDo;
import com.rongwei.bsentity.domain.ApsReportRecordsDo;
import com.rongwei.bsentity.vo.InsertMyReportOutputReq;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
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 com.rongwei.bscommon.sys.service.ApsReportOutputService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
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;

    @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()));

        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.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);
                        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(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());
                        //生成批次号
                        String code = req.getMark() + req.getYearCode() + req.getMonthCode() + req.getEquipmentCode() + req.getHeatNumber() + 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.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.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(B);
//                apsReportOutputDo.setQualified("是");
                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
                //分卷开始初始化值
                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());
                        apsReportOutputDo.setOutputmaterial(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(B);
//                        apsReportOutputDo.setQualified("是");
                        resList.add(apsReportOutputDo);
                    }
                }
            }
        }

        //插入子表
        //获取当前用户信息
        //获取当前用户的所属工厂
        SysUserVo currentUser = CXCommonUtils.getCurrentUser();
        String tenantId = currentUser.getOrganizationDoList().get(0).getFullpid().split(",")[1];
        for (ApsReportOutputDo apsReportOutputDo : resList) {
            apsReportOutputDo.setTenantid(tenantId);
        }
        this.saveBatch(resList);

        //更新主表中的待检验批次号
        List<String> batchNumberList = resList.stream().map(ApsReportOutputDo::getOutputnumber).collect(Collectors.toList());
        String batchNumbers = String.join(",", batchNumberList);
        ApsReportRecordsDo apsReportRecordsDo = new ApsReportRecordsDo();
        apsReportRecordsDo.setId(req.getMainId());
        apsReportRecordsDo.setNeedcheckbatchnumber(batchNumbers);
        apsReportRecordsService.updateById(apsReportRecordsDo);
        return R.ok();
    }

    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 = currentUser.getOrganizationDoList().get(0).getFullpid().split(",")[1];
        for (ApsReportOutputDo apsReportOutputDo : resList) {
            apsReportOutputDo.setTenantid(tenantId);
        }
        this.saveBatch(resList);
        return R.ok();
    }
}




