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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.meapi.dto.BomComponentDto;
import com.foreverwin.mesnac.meapi.mapper.BomComponentMapper;
import com.foreverwin.mesnac.meapi.model.BomComponent;
import com.foreverwin.mesnac.meapi.model.CustomFields;
import com.foreverwin.mesnac.meapi.service.BomComponentService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.sap.me.frame.Utils;
import com.sap.me.productdefinition.ItemBOHandle;
import com.sap.me.productdefinition.OperationBOHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Philip
 * @since 2021-6-23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BomComponentServiceImpl extends ServiceImpl<BomComponentMapper, BomComponent> implements BomComponentService {


    @Autowired
    private BomComponentMapper bomComponentMapper;

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

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

    @Override
    public List<BomComponent> listByBomBo(String bomBo) {
        Locale locale = LocaleContextHolder.getLocale();
        return bomComponentMapper.selectByBomBo(locale.getLanguage(), bomBo);
    }

    @Override
    public List<BomComponent> selectListByShopOrderBo(String shopOrderBo) {
        return bomComponentMapper.selectListByShopOrderBo(shopOrderBo);
    }

    @Override
    public List<BomComponent> selectListByShopOrderBoItemBo(String shopOrderBo, String itemBo) {
        return bomComponentMapper.selectListByShopOrderBoItemBo(shopOrderBo, itemBo, itemBo.split(",")[1]);
    }

    @Override
    public boolean validateMainComponent(String bomBo, String componentBo) {
        CustomFields customFields = bomComponentMapper.getBomComponentMainFlag(bomBo, componentBo);
        if (customFields==null) {
            return false;
        }
        return "A".equals(customFields.getValue().toUpperCase());
    }

    @Override
    public boolean validateComponentOwnerBom(String bomBo, String item) {
        QueryWrapper<BomComponent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BOM_BO", bomBo);
        List<BomComponent> bomComponentList = bomComponentMapper.selectList(queryWrapper);
        boolean ownerComponent = false;
        if (Utils.isNotEmpty(bomComponentList)) {
            for (BomComponent bomComponent : bomComponentList) {
                if (Utils.isNotEmpty(item) && item.equals(new ItemBOHandle(bomComponent.getComponentGbo()).getItem())) {
                    ownerComponent = true;
                    break;
                }
            }
        }
        return ownerComponent;
    }


    @Override
    public List<BomComponent> selectBomComponentByOperation(String bomBo, String operationBo) {
        return bomComponentMapper.selectBomComponentByOperation(bomBo, operationBo);
    }

    @Override
    public Integer getNeedAssemblyQty(String bomBo, String operationBo) {
        return bomComponentMapper.selectNeedAssemblyQty(bomBo, operationBo);
    }

    @Override
    public List<BomComponent> selectListBySfcBoOAndOperationBo(String site, String SfcBo, String operation) {
        String OperationBo = new OperationBOHandle(site, operation, "#").getValue();
        return bomComponentMapper.selectListBySfcBoOAndOperationBo(SfcBo, OperationBo);
    }

    @Override
    public List<BomComponent> selectListByBomBoAndBomItemNo(String bomBo, List<String> bomItemNoList) {
        return bomComponentMapper.selectListByBomBoAndBomItemNo(bomBo, bomItemNoList);
    }

    @Override
    public List<BomComponent> selectShopOrderBomComponentByWorkCenterBo(String workCenterBO) {
        return bomComponentMapper.selectShopOrderBomComponentByWorkCenterBo(workCenterBO);
    }

    @Override
    public  List<BomComponentDto> getSfcBomComponent(String sfcBo) {
        return bomComponentMapper.getSfcBomComponent(CommonMethods.getSite(),sfcBo);
    }

}