package com.foreverwin.mesnac.meapi.service.impl;

import com.foreverwin.modular.core.util.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.Bom;
import com.foreverwin.mesnac.meapi.mapper.BomMapper;
import com.foreverwin.mesnac.meapi.service.BomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.meapi.service.BomComponentService;
import com.foreverwin.mesnac.meapi.service.ItemService;
import com.foreverwin.mesnac.meapi.model.BomComponent;
import com.sap.me.productdefinition.OperationBOHandle;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Leon.L
 * @since 2021-06-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BomServiceImpl extends ServiceImpl<BomMapper, Bom> implements BomService {


    @Autowired
    ItemService itemService;
    @Autowired
    private BomMapper bomMapper;
    @Autowired
    private BomComponentService bomComponentService;


    @Override
    public IPage<Bom> selectPage(FrontPage<Bom> frontPage, Bom bom) {
        QueryWrapper<Bom> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(bom);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<Bom> selectList(Bom bom) {
        QueryWrapper<Bom> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(bom);
        return super.list(queryWrapper);
    }

    @Override
    public Bom getMaxRevisionBom(String site, String bom) {
        List<Bom> list = bomMapper.selectBomList4RevisionDesc(site, bom);
        if (list == null || list.size() <= 0) {
            return null;
        }

        return list.get(0);
    }

    @Override
    public Bom getBomInfo(String site, String bomBo) {
        Bom bom = null;
        if(bomBo.endsWith("#")) {
            String[] temp = bomBo.split(",");
            String bomName = temp[1];
            String type = temp[2];
            Bom bom1 = new Bom();
            bom1.setSite(site);
            bom1.setBom(bomName);
            bom1.setBomType(type);
            bom1.setCurrentRevision("true");
            List<Bom> bomList = this.selectList(bom1);
            bom = bomList != null && bomList.size() > 0 ? bomList.get(0) : null;
        } else {
            bom = bomMapper.selectById(bomBo);
        }
        if(bom != null) {
            List<BomComponent> bomComponentList = bomComponentService.listByBomBo(bom.getHandle());
            bom.setBomComponentList(bomComponentList);
        }
        return bom;
    }

    @Override
    public  List<BomComponent> getBomOperationComponent(String site, String bomBo,String operation) {
        Bom bom = null;
        if(bomBo.endsWith("#")) {
            String[] temp = bomBo.split(",");
            String bomName = temp[1];
            String type = temp[2];
            Bom bom1 = new Bom();
            bom1.setSite(site);
            bom1.setBom(bomName);
            bom1.setBomType(type);
            bom1.setCurrentRevision("true");
            List<Bom> bomList = this.selectList(bom1);
            bom = bomList != null && bomList.size() > 0 ? bomList.get(0) : null;
        } else {
            bom = bomMapper.selectById(bomBo);
        }
        if(bom != null) {
            String operationBo = new OperationBOHandle(site,operation,"#").getValue();
            List<BomComponent> bomComponentList = bomComponentService.selectBomComponentByOperation( bomBo, operationBo );
            return  bomComponentList;
        }
        return  null;
    }

    @Override
    public List<BomComponent> validateItemMatchBomAndOperation(String site, String bomBo, String operation, String itemBo) {
        String operationBo = new OperationBOHandle(site,operation,"#").getValue();
        List<BomComponent> bomComponents = bomComponentService.selectBomComponentByOperation( bomBo, operationBo );
        List<BomComponent> collect = bomComponents.stream().filter(e -> itemService.selectCurrent(e.getComponentGbo()).equals(itemBo)).collect(Collectors.toList());
        return collect;
    }
}