package com.ximai.mes.pro.service.impl.productionSolution;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ximai.common.utils.data.DataUtil;
import com.ximai.common.utils.data.ExceptionUtil;
import com.ximai.common.utils.data.StringUtils;
import com.ximai.mes.md.domain.MdItem;
import com.ximai.mes.md.domain.MdWorkstation;
import com.ximai.mes.md.mapper.MdItemMapper;
import com.ximai.mes.md.mapper.MdWorkstationMapper;
import com.ximai.mes.pro.domain.ProProcess;
import com.ximai.mes.pro.domain.productionSolution.*;
import com.ximai.mes.pro.domain.prototypeRequest.ProPrototypeRequestProcessQcindex;
import com.ximai.mes.pro.domain.vo.*;
import com.ximai.mes.pro.mapper.ProProcessMapper;
import com.ximai.mes.pro.mapper.productionSolution.*;
import com.ximai.mes.pro.service.productionSolution.IProProductionSolutionProcessQcindexService;
import com.ximai.mes.pro.service.productionSolution.IProProductionSolutionProcessToolService;
import com.ximai.mes.pro.service.productionSolution.IProProductionSolutionService;
import com.ximai.system.strategy.AutoCodeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 生产版本Service业务层处理
 *
 * @author zht
 * @date 2024-01-23
 */
@Service
public class ProProductionSolutionServiceImpl implements IProProductionSolutionService {
    @Autowired
    private ProProductionSolutionMapper proProductionSolutionMapper;

    @Autowired
    private ProProductionSolutionBomMapper proProductionSolutionBomMapper;

    @Autowired
    private ProProductionSolutionProcessMapper proProductionSolutionProcessMapper;

    @Autowired
    private ProProductionSolutionProcessItemMapper proProductionSolutionProcessItemMapper;

    @Autowired
    private ProProductionSolutionProcessToolMapper proProductionSolutionProcessToolMapper;

    @Autowired
    private ProProductionSolutionProcessQcindexMapper proProductionSolutionProcessQcindexMapper;

    @Autowired
    private IProProductionSolutionProcessToolService ProProductionSolutionProcessToolService;

    ;
    @Autowired
    private IProProductionSolutionProcessQcindexService proProductionSolutionProcessQcindexService;

    @Autowired
    private ProProductionSolutionBomHeadMapper proProductionSolutionBomHeadMapper;

    @Autowired
    private ProProductionSolutionBomItemMapper proProductionSolutionBomItemMapper;

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Autowired
    private MdItemMapper mdItemMapper;

    @Autowired
    private ProProductionSolutionSpecificationSheetMapper proProductionSolutionSpecificationSheetMapper;

    @Autowired
    private MdWorkstationMapper mdWorkstationMapper;

    @Autowired
    private ProProcessMapper proProcessMapper;

    @Autowired
    private ProProductionSolutionServiceImpl proProductionSolutionService;

    /**
     * 查询生产版本
     *
     * @param productionSolutionId 生产版本主键
     * @return 生产版本
     */
    @Override
    public ProProductionSolutionVo selectProProductionSolutionByProductionSolutionId(Long productionSolutionId) {

        ProProductionSolution proProductionSolution = proProductionSolutionMapper.selectProProductionSolutionByProductionSolutionId(productionSolutionId);
        // 根据id获取头表信息
        QueryWrapper<ProProductionSolutionBomHead> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("production_solution_id", productionSolutionId);
        ProProductionSolutionBomHead proProductionSolutionBomHead = proProductionSolutionBomHeadMapper.selectOne(objectQueryWrapper);

        ProProductionSolutionBomHeadVo proProductionSolutionBomHeadVo = new ProProductionSolutionBomHeadVo();
        // 获取bom行表信息
        if (StringUtils.isNotNull(proProductionSolutionBomHead)) {
            BeanUtils.copyProperties(proProductionSolutionBomHead, proProductionSolutionBomHeadVo);
            QueryWrapper<ProProductionSolutionBomItem> proProductionSolutionBomItemQueryWrapper = new QueryWrapper<>();
            proProductionSolutionBomItemQueryWrapper.eq("bom_head_id", proProductionSolutionBomHead.getBomHeadId());
            List<ProProductionSolutionBomItem> proProductionSolutionBomItems = proProductionSolutionBomItemMapper.selectList(proProductionSolutionBomItemQueryWrapper);

            // 封装行信息
            proProductionSolutionBomHeadVo.setBomItemList(proProductionSolutionBomItems);
        }

        List<ProProductionSolutionProcessVo> processList = proProductionSolutionProcessMapper.selectProProductionSolutionProcessByProductionSolutionId(productionSolutionId);
        for (ProProductionSolutionProcessVo proProductionSolutionProcessVo : processList) {
            Long productionSolutionProcessId = proProductionSolutionProcessVo.getProductionSolutionProcessId();
            List<ProProductionSolutionProcessItem> processItemList = proProductionSolutionProcessItemMapper.selectProProductionSolutionProcessItemByProductionSolutionProcessId(productionSolutionProcessId);
            proProductionSolutionProcessVo.setProcessItemList(processItemList);
        }
        ProProductionSolutionVo proProductionSolutionVo = proProductionSolution.deepCopyObj(ProProductionSolutionVo.class);

        //查询工装量具
        ProProductionSolutionProcessTool toolQuery = new ProProductionSolutionProcessTool();
        toolQuery.setPrototypeSolutionId(proProductionSolution.getProductionSolutionId());

        List<ProProductionSolutionProcessToolVo> toolList = ProProductionSolutionProcessToolService.selectProProductionSolutionProcessToolVoList(toolQuery);

        Map<Long, List<ProProductionSolutionProcessToolVo>> toolMap = toolList.stream().collect(Collectors.groupingBy(ProProductionSolutionProcessToolVo::getPrototypeSolutionProcessId));
        //查询检验项
        ProPrototypeRequestProcessQcindex qcindex = new ProPrototypeRequestProcessQcindex();
        toolQuery.setPrototypeSolutionId(proProductionSolution.getProductionSolutionId());
        List<ProProductionSolutionProcessQcindexVo> qcindexList = proProductionSolutionProcessQcindexService.selectProProductionSolutionProcessQcindexVoList(qcindex);
        Map<Long, List<ProProductionSolutionProcessQcindexVo>> qcindexMap = qcindexList.stream().collect(Collectors.groupingBy(ProProductionSolutionProcessQcindexVo::getPrototypeSolutionProcessId));
        for (ProProductionSolutionProcessVo proProductionSolutionProcess : processList) {
            proProductionSolutionProcess.setToolList(toolMap.getOrDefault(proProductionSolutionProcess.getProductionSolutionProcessId(), new ArrayList<>()));
            proProductionSolutionProcess.setQcindexList(qcindexMap.getOrDefault(proProductionSolutionProcess.getProductionSolutionProcessId(), new ArrayList<>()));

        }
        proProductionSolutionVo.setBomHead(proProductionSolutionBomHeadVo);
        proProductionSolutionVo.setProcessList(processList);

        // 查询规格书
        ProProductionSolutionSpecificationSheet proProductionSolutionSpecificationSheet = proProductionSolutionSpecificationSheetMapper.selectSpecificationSheetByProductionSolutionId(productionSolutionId);
        proProductionSolutionVo.setSpecificationSheet(proProductionSolutionSpecificationSheet);
        return proProductionSolutionVo;
    }

    /**
     * 查询生产版本列表
     *
     * @param proProductionSolution 生产版本
     * @return 生产版本
     */
    @Override
    public List<ProProductionSolution> selectProProductionSolutionList(ProProductionSolution proProductionSolution) {
        return proProductionSolutionMapper.selectProProductionSolutionList(proProductionSolution);
    }

    /**
     * 新增生产版本
     *
     * @return 结果
     */
    @Override
    public int insertProProductionSolution(ProProductionSolutionVo proProductionSolutionVo) {
        if (!StringUtils.isNotNull(proProductionSolutionVo)) {
            return 9;
        }
        ProProductionSolution proProductionSolution = new ProProductionSolution();
        BeanUtils.copyProperties(proProductionSolutionVo, proProductionSolution);
        // 数据转换

        String itemCode = proProductionSolutionVo.getSapItemCode();
        if (StringUtils.isNotNull(itemCode)) {
            MdItem mdItem = mdItemMapper.selectMdItemByCodes(itemCode);
            ExceptionUtil.checkTrueThrowException(ObjectUtil.isEmpty(mdItem), "没有匹配到成品物料编码");
            if (ObjectUtil.isNotEmpty(mdItem)) {
                proProductionSolution.setItemId(mdItem.getItemId());
            }
        }


        // 保存主数据
        int i = proProductionSolutionMapper.insertProProductionSolution(proProductionSolution);
        if (i != 1) {
            return 2;
        }
        Long productionSolutionId = proProductionSolution.getProductionSolutionId();

        // 保存BOM信息
        // 保存头表
        ProProductionSolutionBomHeadVo bomHead = proProductionSolutionVo.getBomHead();
        if (StringUtils.isNotNull(bomHead)) {
            ProProductionSolutionBomHead proProductionSolutionBomHead = new ProProductionSolutionBomHead();
            BeanUtils.copyProperties(bomHead, proProductionSolutionBomHead);
            proProductionSolutionBomHead.setItemId(proProductionSolution.getItemId());
            proProductionSolutionBomHead.setProductionSolutionId(productionSolutionId);
            i = proProductionSolutionBomHeadMapper.insert(proProductionSolutionBomHead);
            if (i != 1) {
                return 2;
            }
            Long bomHeadId = proProductionSolutionBomHead.getBomHeadId();
            // 保存行表信息
            List<ProProductionSolutionBomItem> bomItemList = bomHead.getBomItemList();
            if (StringUtils.isNotNull(bomItemList)) {
                for (ProProductionSolutionBomItem proProductionSolutionBomItem : bomItemList) {
                    proProductionSolutionBomItem.setBomHeadId(bomHeadId);
                    // 查詢物料信息--獲取itemId
                    MdItem item = mdItemMapper.selectMdItemBySapItemCode(proProductionSolutionBomItem.getSapItemCode());
                    ExceptionUtil.checkTrueThrowException(Objects.isNull(item),"没有匹配到bom物料");
                    if (StringUtils.isNotNull(item)) {
                        proProductionSolutionBomItem.setItemId(item.getItemId());
                        i = proProductionSolutionBomItemMapper.insert(proProductionSolutionBomItem);
                        if (i != 1) {
                            return 2;
                        }
                    }

                }
            }
        }
        // 保存工序
        List<ProProductionSolutionProcessVo> processList = proProductionSolutionVo.getProcessList();
        if (StringUtils.isNotNull(processList)) {
            for (ProProductionSolutionProcessVo proProductionSolutionProcessVo : processList) {
                // 保存工序主表信息
                ProProductionSolutionProcess proProductionSolutionProcess = new ProProductionSolutionProcess();
                BeanUtils.copyProperties(proProductionSolutionProcessVo, proProductionSolutionProcess);
                if (proProductionSolutionProcessVo.getQuantity() == null) {
                    proProductionSolutionProcess.setQuantity(new BigDecimal("1000.000"));
                }
                if (StringUtils.isEmpty(proProductionSolutionProcessVo.getUnitOfMeasure())) {
                    proProductionSolutionProcess.setUnitOfMeasure(proProductionSolutionVo.getUnitOfMeasure());
                }
                if (StringUtils.isEmpty(proProductionSolutionProcessVo.getStdWorkingTimeUom())) {
                    proProductionSolutionProcess.setStdWorkingTimeUom("MIN");
                }

                if (StringUtils.isNotNull(proProductionSolutionProcessVo.getEmployeesNum())) {
                    proProductionSolutionProcess.setEmployeesNum(1);
                }
                //判断工作中心是否为空
                ExceptionUtil.checkTrueThrowException(StringUtils.isNull(proProductionSolutionProcessVo.getWorkstationCode()), "没有匹配工作中心编码");
                //根据编码查看mes的工作中心
                MdWorkstation mdWorkstation = mdWorkstationMapper.selectMdWorkstationByWorkstationCodes(proProductionSolutionProcessVo.getWorkstationCode());
                if (ObjectUtil.isNotEmpty(mdWorkstation)) {
                    proProductionSolutionProcess.setWorkstationId(mdWorkstation.getWorkstationId());
                } else {
                    ExceptionUtil.checkTrueThrowException(true, "没有匹配到对应的工作中心");
                }
                //判断工序是否为空
                ExceptionUtil.checkTrueThrowException(StringUtils.isNull(proProductionSolutionProcessVo.getProcessCode()), "没有工序编码");
                //根据查看mes的工序
                ProProcess proProcess = proProcessMapper.selectProProcessByProcessCodes(proProductionSolutionProcessVo.getProcessCode());
                if (ObjectUtil.isNotEmpty(proProcess)) {
                    proProductionSolutionProcess.setProcessId(proProcess.getProcessId());
                    proProductionSolutionProcess.setProcessName(proProcess.getProcessName());
                } else {
                    ExceptionUtil.checkTrueThrowException(true, "没有工序编码");
                }

                proProductionSolutionProcess.setProductionSolutionId(productionSolutionId);
                i = proProductionSolutionProcessMapper.insertProProductionSolutionProcess(proProductionSolutionProcess);
                if (i != 1) {
                    return 2;
                }
                Long productionSolutionProcessId = proProductionSolutionProcess.getProductionSolutionProcessId();

                // 保存工序物料信息
                List<ProProductionSolutionProcessItem> processItemList = proProductionSolutionProcessVo.getProcessItemList();
                if (StringUtils.isNotNull(processItemList)) {
                    for (ProProductionSolutionProcessItem proProductionSolutionProcessItem : processItemList) {
                        proProductionSolutionProcessItem.setProductionSolutionProcessId(productionSolutionProcessId);
                        MdItem item = mdItemMapper.selectMdItemBySapItemCode(proProductionSolutionProcessItem.getSapItemCode());
                        ExceptionUtil.checkTrueThrowException(StringUtils.isNull(item), "没有匹配到对应的工序物料");
                        if (StringUtils.isNotNull(item)) {
                            proProductionSolutionProcessItem.setItemId(item.getItemId());
                            proProductionSolutionProcessItem.setSapItemName(item.getItemName());
                            i = proProductionSolutionProcessItemMapper.insertProProductionSolutionProcessItem(proProductionSolutionProcessItem);
                            if (i != 1) {
                                return 2;
                            }
                        }

                    }
                }

                // 保存质检信息
                List<ProProductionSolutionProcessQcindexVo> qcindexList = proProductionSolutionProcessVo.getQcindexList();
                if (StringUtils.isNotNull(qcindexList)) {
                    for (ProProductionSolutionProcessQcindexVo proProductionSolutionProcessQcindexVo : qcindexList) {
                        ProProductionSolutionProcessQcindex proProductionSolutionProcessQcindex = new ProProductionSolutionProcessQcindex();
                        BeanUtils.copyProperties(proProductionSolutionProcessQcindexVo, proProductionSolutionProcessQcindex);
                        proProductionSolutionProcessQcindex.setPrototypeSolutionId(productionSolutionId);
                        proProductionSolutionProcessQcindex.setPrototypeSolutionProcessId(productionSolutionProcessId);
                        i = proProductionSolutionProcessQcindexMapper.insertProProductionSolutionProcessQcindex(proProductionSolutionProcessQcindex);
                        if (i != 1) {
                            return 2;
                        }
                    }
                }

                // 保存工装信息
                List<ProProductionSolutionProcessToolVo> toolList = proProductionSolutionProcessVo.getToolList();
                if (StringUtils.isNotNull(toolList)) {
                    for (ProProductionSolutionProcessToolVo proProductionSolutionProcessToolVo : toolList) {
                        ProProductionSolutionProcessTool proProductionSolutionProcessTool = new ProProductionSolutionProcessTool();
                        BeanUtils.copyProperties(proProductionSolutionProcessToolVo, proProductionSolutionProcessTool);
                        MdItem mdItem = mdItemMapper.selectMdItemByCodea(proProductionSolutionProcessToolVo.getItemCode());
                        proProductionSolutionProcessTool.setToolId(mdItem.getItemId());
                        proProductionSolutionProcessTool.setPrototypeSolutionId(productionSolutionId);
                        proProductionSolutionProcessTool.setPrototypeSolutionProcessId(productionSolutionProcessId);
                        BigDecimal normalData = DataUtil.getNormalData(proProductionSolutionProcessToolVo.getQuantity(), BigDecimal.ONE);
                        proProductionSolutionProcessTool.setQuantity(normalData.longValue());

                        i = proProductionSolutionProcessToolMapper.insertProProductionSolutionProcessTool(proProductionSolutionProcessTool);
                        if (i != 1) {
                            return 2;
                        }
                    }
                }


            }
        }

        // 保存规格书
        ProProductionSolutionSpecificationSheet specificationSheet = proProductionSolutionVo.getSpecificationSheet();
        String frontColorName = specificationSheet.getFrontColorName();
        if (StringUtils.isNotNull(frontColorName)) {
            frontColorName = frontColorName.replace("[", "").replace("]", "").replace("\"", "");
            specificationSheet.setFrontColorName(frontColorName);
        }
        String oppositeColorName = specificationSheet.getOppositeColorName();
        if (StringUtils.isNotNull(oppositeColorName)) {
            oppositeColorName = oppositeColorName.replace("[", "").replace("]", "").replace("\"", "");
            specificationSheet.setOppositeColorName(oppositeColorName);
        }
        specificationSheet.setProductionSolutionId(productionSolutionId);
        proProductionSolutionSpecificationSheetMapper.insertProProductionSolutionSpecificationSheet(specificationSheet);

        return i;
    }


    /**
     * 修改生产版本
     * mes中修改只需要接收Ep信息就行-直接改
     *
     * @return 结果
     */
    @Override
    public int updateProProductionSolution(ProProductionSolutionVo proProductionSolutionVo) {
        ProProductionSolution proProductionSolution = new ProProductionSolution();
        if (StringUtils.isNotEmpty(proProductionSolutionVo.getSapPrototypeMakeCode())) {
            proProductionSolution = proProductionSolutionMapper.selectProProductionSolutionBySapPrototypeMakeCode(proProductionSolutionVo.getSapPrototypeMakeCode());
        } else {
            proProductionSolution = proProductionSolutionMapper.selectByGroupKeyAndGroupCounterAndSapItemCode(proProductionSolutionVo.getGroupKey(), proProductionSolutionVo.getGroupCounter(), proProductionSolutionVo.getSapItemCode());
        }
        int i = 1;
        if (ObjectUtil.isEmpty(proProductionSolution)) {
            insertProProductionSolution(proProductionSolutionVo);
        } else {

            //删除主数据下对应的子表
            //删除bom物料表
            Long productionSolutionId = proProductionSolution.getProductionSolutionId();
            QueryWrapper<ProProductionSolutionBomHead> productionSolutionId1 = new QueryWrapper<ProProductionSolutionBomHead>().eq("production_solution_id", productionSolutionId);
            ProProductionSolutionBomHead proProductionSolutionBomHead = proProductionSolutionBomHeadMapper.selectOne(productionSolutionId1);
            //删除bom数据
            if (ObjectUtil.isNotEmpty(proProductionSolutionBomHead)) {
                Long bomHeadId = proProductionSolutionBomHead.getBomHeadId();
                //删除bom表体
                QueryWrapper<ProProductionSolutionBomItem> bomHeadId1 = new QueryWrapper<ProProductionSolutionBomItem>().eq("bom_head_id", bomHeadId);
                List<ProProductionSolutionBomItem> SolutionBomItems = proProductionSolutionBomItemMapper.selectList(bomHeadId1);
                List<Long> bomItemIdList = SolutionBomItems.stream().map(ProProductionSolutionBomItem::getBomItemId).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(bomItemIdList)) {
                    i = proProductionSolutionBomItemMapper.deleteBatchIds(bomItemIdList);


                }
                //删除bom表头
                i = proProductionSolutionBomHeadMapper.deleteById(bomHeadId);


            }

            //删除工序
            ProProductionSolutionProcess proProductionSolutionProcess = new ProProductionSolutionProcess();
            proProductionSolutionProcess.setProductionSolutionId(productionSolutionId);
            List<ProProductionSolutionProcessVo> proProductionSolutionProcessVos = proProductionSolutionProcessMapper.selectProProductionSolutionProcessList(proProductionSolutionProcess);
            if (CollUtil.isNotEmpty(proProductionSolutionProcessVos)) {
                for (ProProductionSolutionProcessVo pro : proProductionSolutionProcessVos) {
                    Long productionSolutionProcessId = pro.getProductionSolutionProcessId();
                    //查看工序关联的物料下的数据
                    ProProductionSolutionProcessItem proProductionSolutionProcessItem = new ProProductionSolutionProcessItem();
                    proProductionSolutionProcessItem.setProductionSolutionProcessId(productionSolutionProcessId);
                    List<ProProductionSolutionProcessItem> proProductionSolutionProcessItems = proProductionSolutionProcessItemMapper.selectProProductionSolutionProcessItemList(proProductionSolutionProcessItem);
                    if (CollUtil.isNotEmpty(proProductionSolutionProcessItems)) {
                        List<Long> ProcessItemList = proProductionSolutionProcessItems.stream().map(ProProductionSolutionProcessItem::getProductionSolutionProcessItemId).collect(Collectors.toList());
                        for (Long itemId : ProcessItemList) {
                            //删除工序下的关联物料
                            i = proProductionSolutionProcessItemMapper.deleteProProductionSolutionProcessItemByProductionSolutionProcessItemId(itemId);

                        }
                    }
                    // 查看保存质检信息
                    ProProductionSolutionProcessQcindex proProductionSolutionProcessQcindex = new ProProductionSolutionProcessQcindex();
                    proProductionSolutionProcessQcindex.setPrototypeSolutionId(productionSolutionProcessId);
                    List<ProProductionSolutionProcessQcindex> proProductionSolutionProcessQcindices = proProductionSolutionProcessQcindexMapper.selectProProductionSolutionProcessQcindexList(proProductionSolutionProcessQcindex);
                    if (CollUtil.isNotEmpty(proProductionSolutionProcessItems)) {
                        List<Long> ProcessQcindexIdList = proProductionSolutionProcessQcindices.stream().map(ProProductionSolutionProcessQcindex::getPrototypeSolutionProcessQcindexId).collect(Collectors.toList());
                        for (Long qcindexId : ProcessQcindexIdList) {
                            //删除工序下质检信息
                            i = proProductionSolutionProcessQcindexMapper.deleteProProductionSolutionProcessQcindexByPrototypeRequestProcessQcindexId(qcindexId);

                        }
                    }
                    ProProductionSolutionProcessTool proProductionSolutionProcessTool = new ProProductionSolutionProcessTool();
                    proProductionSolutionProcessTool.setPrototypeSolutionProcessId(productionSolutionProcessId);
                    List<ProProductionSolutionProcessTool> proProductionSolutionProcessTools = proProductionSolutionProcessToolMapper.selectProProductionSolutionProcessToolList(proProductionSolutionProcessTool);
                    if (CollUtil.isNotEmpty(proProductionSolutionProcessTools)) {
                        List<Long> ProcessToolId = proProductionSolutionProcessTools.stream().map(ProProductionSolutionProcessTool::getPrototypeSolutionProcessToolId).collect(Collectors.toList());
                        for (Long ToolId : ProcessToolId) {
                            i = proProductionSolutionProcessToolMapper.deleteProProductionSolutionProcessToolByPrototypeSolutionProcessToolId(ToolId);

                        }
                    }
                    i = proProductionSolutionProcessMapper.deleteProProductionSolutionProcessByProductionSolutionProcessId(productionSolutionProcessId);

                }
                //删除规格书的信息

                ProProductionSolutionSpecificationSheet proProductionSolutionSpecificationSheet = proProductionSolutionSpecificationSheetMapper.selectSpecificationSheetByProductionSolutionId(productionSolutionId);
                if (ObjectUtil.isNotEmpty(proProductionSolutionSpecificationSheet)) {
                    i = proProductionSolutionSpecificationSheetMapper.deleteProProductionSolutionSpecificationSheetBySpecificationSheetId(proProductionSolutionSpecificationSheet.getSpecificationSheetId());

                }


                //根据ep传来的数据进行新增

                //新增bom物料
                ProProductionSolutionBomHeadVo bomHead = proProductionSolutionVo.getBomHead();
                if (Objects.nonNull(bomHead)) {
                    ProProductionSolutionBomHead proProductionSolutionBomHead1 = new ProProductionSolutionBomHead();
                    BeanUtils.copyProperties(bomHead, proProductionSolutionBomHead1);
                    proProductionSolutionBomHead1.setProductionSolutionId(productionSolutionId);
                    proProductionSolutionBomHead1.setItemId(proProductionSolution.getItemId());
                    i = proProductionSolutionBomHeadMapper.insert(proProductionSolutionBomHead1);
                    if (i != 1) {
                        return 2;
                    }

                    Long bomHeadId = proProductionSolutionBomHead1.getBomHeadId();
                    List<ProProductionSolutionBomItem> bomItemList = bomHead.getBomItemList();
                    if (CollUtil.isNotEmpty(bomItemList)) {

                        for (ProProductionSolutionBomItem bomItem : bomItemList) {
                            bomItem.setBomHeadId(bomHeadId);
                            MdItem mdItem = mdItemMapper.selectMdItemBySapItemCode(bomItem.getSapItemCode());
                            ExceptionUtil.checkTrueThrowException(ObjectUtil.isEmpty(mdItem), "没有匹配到对应的BOM物料");
                            if (ObjectUtil.isNotEmpty(mdItem)) {
                                bomItem.setItemId(mdItem.getItemId());
                                i = proProductionSolutionBomItemMapper.insert(bomItem);
                                if (i != 1) {
                                    return 2;
                                }

                            }
                        }
                    }
                }

                //新增工序
                List<ProProductionSolutionProcessVo> processList = proProductionSolutionVo.getProcessList();
                if (CollUtil.isNotEmpty(processList)) {
                    for (ProProductionSolutionProcessVo processVo : processList) {
                        ProProductionSolutionProcess proProductionSolutionProcess1 = new ProProductionSolutionProcess();
                        BeanUtils.copyProperties(processVo, proProductionSolutionProcess1);
                        if (processVo.getQuantity() == null) {
                            proProductionSolutionProcess1.setQuantity(new BigDecimal("1000.000"));
                        }
                        if (StringUtils.isEmpty(processVo.getUnitOfMeasure())) {
                            proProductionSolutionProcess1.setUnitOfMeasure(processVo.getUnitOfMeasure());
                        }
                        if (StringUtils.isEmpty(processVo.getStdWorkingTimeUom())) {
                            proProductionSolutionProcess1.setStdWorkingTimeUom("MIN");
                        }
                        if (StringUtils.isNotNull(processVo.getEmployeesNum())) {
                            proProductionSolutionProcess1.setEmployeesNum(1);
                        }
                        //判断工作中心是否为空
                        ExceptionUtil.checkTrueThrowException(StringUtils.isNull(processVo.getWorkstationCode()), "没有工作中心编码");
                        MdWorkstation mdWorkstation = mdWorkstationMapper.selectMdWorkstationByWorkstationCodes(processVo.getWorkstationCode());
                        if (ObjectUtil.isNotEmpty(mdWorkstation)) {
                            proProductionSolutionProcess1.setWorkstationId(mdWorkstation.getWorkstationId());
                        } else {
                            ExceptionUtil.checkTrueThrowException(true, "没有匹配到对应的工作中心");
                        }
                        //判断工序是否为空
                        ExceptionUtil.checkTrueThrowException(StringUtils.isNull(processVo.getProcessCode()), "没有工序编码");
                        //根据查看mes的工序
                        ProProcess proProcess = proProcessMapper.selectProProcessByProcessCodes(processVo.getProcessCode());
                        if (ObjectUtil.isNotEmpty(proProcess)) {
                            proProductionSolutionProcess1.setProcessId(proProcess.getProcessId());
                            proProductionSolutionProcess1.setProcessName(proProcess.getProcessName());
                        } else {
                            ExceptionUtil.checkTrueThrowException(true, "没有匹配到对应的工序");
                        }
                        proProductionSolutionProcess1.setProductionSolutionId(productionSolutionId);
                        i = proProductionSolutionProcessMapper.insertProProductionSolutionProcess(proProductionSolutionProcess1);
                        if (i != 1) {
                            return 2;
                        }
                        Long productionSolutionProcessId = proProductionSolutionProcess1.getProductionSolutionProcessId();
                        //保持工序物料
                        List<ProProductionSolutionProcessItem> processItemList = processVo.getProcessItemList();
                        if (CollUtil.isNotEmpty(processItemList)) {
                            for (ProProductionSolutionProcessItem processItem : processItemList) {
                                ProProductionSolutionProcessItem proProductionSolutionProcessItem = new ProProductionSolutionProcessItem();
                                BeanUtils.copyProperties(processItem, proProductionSolutionProcessItem);
                                proProductionSolutionProcessItem.setProductionSolutionProcessId(productionSolutionProcessId);
                                MdItem mdItem = mdItemMapper.selectMdItemBySapItemCode(processItem.getSapItemCode());
                                ExceptionUtil.checkTrueThrowException(ObjectUtil.isEmpty(mdItem), "没有匹配到对应的工序物料");
                                if (ObjectUtil.isNotEmpty(mdItem)) {
                                    proProductionSolutionProcessItem.setItemId(mdItem.getItemId());
                                    proProductionSolutionProcessItem.setSapItemName(mdItem.getItemName());
                                    i = proProductionSolutionProcessItemMapper.insertProProductionSolutionProcessItem(proProductionSolutionProcessItem);
                                    if (i != 1) {
                                        return 2;
                                    }

                                }
                            }

                        }
                        // 保存质检信息
                        List<ProProductionSolutionProcessQcindexVo> qcindexList = processVo.getQcindexList();
                        if (CollUtil.isNotEmpty(qcindexList)) {
                            for (ProProductionSolutionProcessQcindexVo qcindexVo : qcindexList) {
                                ProProductionSolutionProcessQcindex solutionProcessQcindex = new ProProductionSolutionProcessQcindex();
                                BeanUtils.copyProperties(qcindexVo, solutionProcessQcindex);
                                solutionProcessQcindex.setPrototypeSolutionId(productionSolutionId);
                                solutionProcessQcindex.setPrototypeSolutionProcessId(productionSolutionProcessId);
                                i = proProductionSolutionProcessQcindexMapper.insertProProductionSolutionProcessQcindex(solutionProcessQcindex);
                                if (i != 1) {
                                    return 2;
                                }
                            }
                        }



                        //保持工装量具
                        List<ProProductionSolutionProcessToolVo> toolList = processVo.getToolList();
                        if (CollUtil.isNotEmpty(toolList)) {
                            for (ProProductionSolutionProcessToolVo toolVo : toolList) {
                                ProProductionSolutionProcessTool processTool = new ProProductionSolutionProcessTool();
                                BeanUtils.copyProperties(toolVo, processTool);
                                MdItem mdItem = mdItemMapper.selectMdItemByCodea(toolVo.getItemCode());
                                processTool.setToolId(mdItem.getItemId());
                                processTool.setPrototypeSolutionId(productionSolutionId);
                                processTool.setPrototypeSolutionProcessId(productionSolutionProcessId);
                                BigDecimal normalData = DataUtil.getNormalData(processTool.getQuantity(), BigDecimal.ONE);
                                processTool.setQuantity(normalData.longValue());
                                i = proProductionSolutionProcessToolMapper.insertProProductionSolutionProcessTool(processTool);
                                if (i != 1) {
                                    return 2;
                                }
                            }
                        }

                    }
                }

                //保持生产规格书
                ProProductionSolutionSpecificationSheet specificationSheet = proProductionSolutionVo.getSpecificationSheet();
                String frontColorName = specificationSheet.getFrontColorName();
                if (StringUtils.isNotNull(frontColorName)) {
                    frontColorName = frontColorName.replace("[", "").replace("]", "").replace("\"", "");
                    specificationSheet.setFrontColorName(frontColorName);
                }
                String oppositeColorName = specificationSheet.getOppositeColorName();
                if (StringUtils.isNotNull(oppositeColorName)) {
                    oppositeColorName = oppositeColorName.replace("[", "").replace("]", "").replace("\"", "");
                    specificationSheet.setOppositeColorName(oppositeColorName);
                }
                specificationSheet.setProductionSolutionId(productionSolutionId);
                i = proProductionSolutionSpecificationSheetMapper.insertProProductionSolutionSpecificationSheet(specificationSheet);
                if (i != 1) {
                    return 2;
                }
            }

        }

        return i;
    }

    /**
     * 批量删除生产版本
     *
     * @param productionSolutionIds 需要删除的生产版本主键
     * @return 结果
     */
    @Override
    public int deleteProProductionSolutionByProductionSolutionIds(Long[] productionSolutionIds) {
        if (StringUtils.isNotNull(productionSolutionIds)) {
            for (Long productionSolutionId : productionSolutionIds) {
                deleteProProductionSolutionByProductionSolutionId(productionSolutionId);
            }
        }
        return 1;
    }

    /**
     * 删除生产版本信息
     *
     * @param productionSolutionId 生产版本主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProProductionSolutionByProductionSolutionId(Long productionSolutionId) {
        if (StringUtils.isNull(productionSolutionId)) {
            return 3;
        }
        ProProductionSolution proProductionSolution = proProductionSolutionMapper.selectProProductionSolutionByProductionSolutionId(productionSolutionId);
        int i = 1;
        if (StringUtils.isNotNull(proProductionSolution)) {
            i = proProductionSolutionMapper.deleteProProductionSolutionByProductionSolutionId(productionSolutionId);
            if (i != 1) {
                return 4;
            }
            // 删除BOM信息
            // 获取头表信息
            QueryWrapper<ProProductionSolutionBomHead> bomHeadQueryWrapper = new QueryWrapper<ProProductionSolutionBomHead>().eq("production_solution_id", productionSolutionId);
            ProProductionSolutionBomHead proProductionSolutionBomHead = proProductionSolutionBomHeadMapper.selectOne(bomHeadQueryWrapper);
            if (StringUtils.isNotNull(proProductionSolutionBomHead)) {
                Long bomHeadId = proProductionSolutionBomHead.getBomHeadId();
                // 获取行表信息
                QueryWrapper<ProProductionSolutionBomItem> bomItemQueryWrapper = new QueryWrapper<ProProductionSolutionBomItem>().eq("bom_head_id", bomHeadId);
                List<ProProductionSolutionBomItem> proProductionSolutionBomItems = proProductionSolutionBomItemMapper.selectList(bomItemQueryWrapper);
                if (StringUtils.isNotNull(proProductionSolutionBomItems)&&!proProductionSolutionBomItems.isEmpty()) {
                    List<Long> ids = proProductionSolutionBomItems.stream().map(ProProductionSolutionBomItem::getBomItemId).collect(Collectors.toList());
                    proProductionSolutionBomItemMapper.deleteBatchIds(ids);
                }
                i = proProductionSolutionBomHeadMapper.deleteById(bomHeadId);
                if (i != 1) {
                    return 4;
                }
            }
            // 删除工序信息
            ProProductionSolutionProcess proProductionSolutionProcess = new ProProductionSolutionProcess();
            proProductionSolutionProcess.setProductionSolutionId(productionSolutionId);
            List<ProProductionSolutionProcessVo> proProductionSolutionProcesses = proProductionSolutionProcessMapper.selectProProductionSolutionProcessList(proProductionSolutionProcess);
            if (StringUtils.isNotNull(proProductionSolutionProcesses)) {
                for (ProProductionSolutionProcessVo productionSolutionProcess : proProductionSolutionProcesses) {
                    Long productionSolutionProcessId = productionSolutionProcess.getProductionSolutionProcessId();
                    i = proProductionSolutionProcessMapper.deleteProProductionSolutionProcessByProductionSolutionProcessId(productionSolutionProcessId);
                    if (i != 1) {
                        return 4;
                    }
                    // 删除工序物料
                    List<ProProductionSolutionProcessItem> proProductionSolutionProcessItems = proProductionSolutionProcessItemMapper.selectProProductionSolutionProcessItemByProductionSolutionProcessId(productionSolutionProcessId);
                    if (StringUtils.isNotNull(proProductionSolutionProcessItems)) {
                        for (ProProductionSolutionProcessItem proProductionSolutionProcessItem : proProductionSolutionProcessItems) {
                            Long productionSolutionProcessItemId = proProductionSolutionProcessItem.getProductionSolutionProcessItemId();
                            i = proProductionSolutionProcessItemMapper.deleteProProductionSolutionProcessItemByProductionSolutionProcessItemId(productionSolutionProcessItemId);
                            if (i != 1) {
                                return 4;
                            }
                        }
                    }
                    // 删除工装
                    List<ProProductionSolutionProcessTool> proProductionSolutionProcessTools = proProductionSolutionProcessToolMapper.selectProProductionSolutionProcessToolByPrototypeSolutionProcessId(productionSolutionProcessId);
                    if (StringUtils.isNotNull(proProductionSolutionProcessTools)) {
                        for (ProProductionSolutionProcessTool proProductionSolutionProcessTool : proProductionSolutionProcessTools) {
                            Long prototypeSolutionProcessToolId = proProductionSolutionProcessTool.getPrototypeSolutionProcessToolId();
                            i = proProductionSolutionProcessToolMapper.deleteProProductionSolutionProcessToolByPrototypeSolutionProcessToolId(prototypeSolutionProcessToolId);
                            if (i != 1) {
                                return 4;
                            }
                        }
                    }

                    // 删除质检
                    List<ProProductionSolutionProcessQcindex> proProductionSolutionProcessQcindices = proProductionSolutionProcessQcindexMapper.selectselectProProductionSolutionProcessQcindexByPrototypeRequestProcessId(productionSolutionProcessId);
                    if (StringUtils.isNotNull(proProductionSolutionProcessQcindices)) {
                        for (ProProductionSolutionProcessQcindex proProductionSolutionProcessQcindex : proProductionSolutionProcessQcindices) {
                            i = proProductionSolutionProcessQcindexMapper.deleteProProductionSolutionProcessQcindexByPrototypeRequestProcessQcindexId(proProductionSolutionProcessQcindex.getPrototypeSolutionProcessQcindexId());
                            if (i != 1) {
                                return 4;
                            }
                        }
                    }
                }
            }

            // 删除规格书
            ProProductionSolutionSpecificationSheet proProductionSolutionSpecificationSheet = proProductionSolutionSpecificationSheetMapper.selectSpecificationSheetByProductionSolutionId(productionSolutionId);
            if(proProductionSolutionSpecificationSheet!=null){
                i = proProductionSolutionSpecificationSheetMapper.deleteProProductionSolutionSpecificationSheetBySpecificationSheetId(proProductionSolutionSpecificationSheet.getSpecificationSheetId());
                if (i != 1) {
                    return 4;
                }
            }
        }
        return i;
    }

    /**
     * 接收EP生产版本数据
     */
    @Override
    @Transactional
    public int productionSolutionCreate(ProProductionSolutionVo productionSolutionVo) {

        // 判断标识
        char updateLog = productionSolutionVo.getUpdateLog();
        ProProductionSolution proProductionSolution = new ProProductionSolution();
        BeanUtils.copyProperties(productionSolutionVo, proProductionSolution);
        int i;
        if (updateLog == 'I') {

            // 新增
            i = insertProProductionSolution(productionSolutionVo);
        } else if (updateLog == 'D') {
            // 删除
            i = deleteProProductionSolutionByProductionSolutionId(productionSolutionVo.getProductionSolutionId());
        } else {
            // 修改
            i = updateProProductionSolution(productionSolutionVo);
        }

        return i;

    }

    @Override
    public int submitProProductionSolution(ProProductionSolutionVo proProductionSolutionVo) {
        return 0;
    }

    /**
     * 转为生产版本
     */
    public int buildProductionSolution(Object object) {

        ProProductionSolutionVo proProductionSolutionVo = new ProProductionSolutionVo();
        BeanUtils.copyProperties(object, proProductionSolutionVo);
        return insertProProductionSolution(proProductionSolutionVo);
    }

    @Override
    public ProProductionSolution selectByGroupKeyAndGroupCounterAndSapItemCode(String groupKey, String groupCounter, String sapItemCode) {
        return proProductionSolutionMapper.selectByGroupKeyAndGroupCounterAndSapItemCode(groupKey, groupCounter, sapItemCode);
    }

}
