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.OperationDto;
import com.foreverwin.mesnac.meapi.mapper.NcGroupMapper;
import com.foreverwin.mesnac.meapi.model.NcGroup;
import com.foreverwin.mesnac.meapi.model.Operation;
import com.foreverwin.mesnac.meapi.service.NcGroupService;
import com.foreverwin.mesnac.meapi.util.StringUtils;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.sap.me.productdefinition.OperationBOHandle;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author robert
 * @since 2021-06-23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class NcGroupServiceImpl extends ServiceImpl<NcGroupMapper, NcGroup> implements NcGroupService {


    @Autowired
    private NcGroupMapper ncGroupMapper;

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

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

    @Override
    public List<NcGroup> selectPageByOperation(OperationDto dto) {
        String site = CommonMethods.getSite();
        if (StringUtils.isBlank(dto.getOperation())) {
            throw new BaseException("工序不能为空");
        }
        String operationBO = new OperationBOHandle(site, dto.getOperation(), "#").getValue();
        String ncGroup="";
        if (dto.getType().equals("Z")){
            ncGroup="QC_";
        }else if (dto.getType().equals("S")){
            ncGroup="E_";
        }else {
            return new ArrayList<>();
        }
        return ncGroupMapper.selectPageByOperation(site, operationBO,ncGroup);
    }

    @Override
    public List<NcGroup> findNgGroupByOperationBo(Operation operation) {
        String site = CommonMethods.getSite();
        operation.setSite(site);
        if(StringUtil.isBlank(operation.getOperation())){
            operation.setHandle("OperationBO"+site+","+operation.getOperation()+",#");
        }
        return ncGroupMapper.findNgGroupByOperationBo(operation);
    }

    @Override
    public List<NcGroup> selectPageByOperationNoPage(String operation) {
        String site = CommonMethods.getSite();
        if (StringUtils.isBlank(operation)) {
            throw new BaseException("工序不能为空");
        }
        String operationBO = new OperationBOHandle(site, operation, "#").getValue();
        return ncGroupMapper.selectPageByOperation(site, operationBO, null);
    }

}