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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.EquBomMapperDao;
import com.rongwei.bscommon.sys.dao.EquOverhaulInventoryMapperDao;
import com.rongwei.bscommon.sys.service.AspCheckItemsService;
import com.rongwei.bscommon.sys.service.EquBomService;
import com.rongwei.bscommon.sys.service.EquOverhaulInventoryService;
import com.rongwei.bsentity.domain.AspCheckItems;
import com.rongwei.bsentity.domain.EquBomDo;
import com.rongwei.bsentity.domain.EquOverhaulInventoryDo;
import com.rongwei.bsentity.dto.EquBomCopyDto;
import com.rongwei.rwcommon.base.exception.CustomException;
import com.rongwei.rwcommon.utils.SecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备子系统——设备Bom 服务实现类
 * </p>
 *
 * @author sc
 * @since 2023-11-24
 */
@Service
public class EquBomServiceImpl extends ServiceImpl<EquBomMapperDao, EquBomDo> implements EquBomService {

    private static final Logger logger = LoggerFactory.getLogger(EquBomServiceImpl.class);

    @Autowired
    private AspCheckItemsService aspCheckItemsService;


    @Override
    @Transactional
    public void copyChilds(EquBomCopyDto request) {
        if (ObjectUtil.isEmpty(request.getFromEquId())){
            throw new CustomException("当前物料ID不可为空");
        }
        if (ObjectUtil.isEmpty(request.getToEquId())){
            throw new CustomException("复制到物料ID不可为空");
        }
        //查询目标设备的设备信息
        AspCheckItems toEqu = aspCheckItemsService.getById(request.getToEquId());

        //迭代复制
        copyChildsByParentId(request.getFromEquId(), request.getToEquId(), toEqu.getCheckitemareaid(), toEqu.getChargeorgcid());

    }

    private void copyChildsByParentId(String fromEquId, String toEquId, String checkItemAreaId, String chargeOrgcId) {
        //根据父，查询子集(来源)
        List<EquBomDo> equFromBomDos = this.baseMapper.selectList(new QueryWrapper<EquBomDo>().lambda().eq(EquBomDo::getPid, fromEquId));
        //根据父，查询子集(目标)
        List<EquBomDo> equToBomDos = this.baseMapper.selectList(new QueryWrapper<EquBomDo>().lambda().eq(EquBomDo::getPid, toEquId));
        //获取目标的子集编号
        List<String> toBomMaterialNumList = equToBomDos.stream().map(item -> {
            return item.getMaterialNum();
        }).collect(Collectors.toList());

        //遍历子集
        for (EquBomDo equBomDo : equFromBomDos) {
            //判断子集的编号是否重复
            //如果不重复，则复制
            if (!toBomMaterialNumList.contains(equBomDo.getMaterialNum())){
                EquBomDo newEquBomDo = BeanUtil.copyProperties(equBomDo, EquBomDo.class);
                newEquBomDo.setPid(toEquId);
                //防止主键冲突
                newEquBomDo.setId(SecurityUtil.getUUID());
//                newEquBomDo.setFullPid();
                //增加设备的树字段
                newEquBomDo.setCheckItemAreaId(checkItemAreaId);
                newEquBomDo.setChargeOrgId(chargeOrgcId);
                //复制
                this.save(newEquBomDo);

                //开始迭代
                copyChildsByParentId(equBomDo.getId(), newEquBomDo.getId(), checkItemAreaId, chargeOrgcId);

            }

        }
    }
}
