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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.meapi.mapper.OperationMapper;
import com.foreverwin.mesnac.meapi.model.Operation;
import com.foreverwin.mesnac.meapi.service.OperationService;
import com.foreverwin.modular.core.exception.BaseException;
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 Robert
 * @since 2021-06-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements OperationService {


    @Autowired
    private OperationMapper operationMapper;


    @Override
    public IPage<Operation> selectPage(Page<Operation> page, QueryWrapper<Operation> queryWrapper) {
        Locale locale = LocaleContextHolder.getLocale();
        page.setRecords(this.operationMapper.selectPage(locale.getLanguage(),page,queryWrapper));
        return page;
    }

    @Override
    public IPage<Operation> selectOperationStep(Page<Operation> page, QueryWrapper<Operation> queryWrapper) {
        Locale locale = LocaleContextHolder.getLocale();
        return  operationMapper.selectOperationStep(page,locale.getLanguage(),queryWrapper);
    }
    @Override
    public List<Operation> selectList(Operation operation) {
        QueryWrapper<Operation> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(operation);
        return super.list(queryWrapper);
    }

    @Override
    public Operation getCurrentRevisionRef(String site,String operation) {
        Operation currentRevisionRef = operationMapper.getCurrentRevisionRef(site, operation);
        if (currentRevisionRef==null){
            throw new BaseException("工序【" +operation+ "】的当前版本不存在");
        }
        return currentRevisionRef;
    }

    @Override
    public Operation queryOperationByErpWorkCenter(String site, String erpWorkCenter) {
        return operationMapper.queryOperationByErpWorkCenter(site, erpWorkCenter);
    }

    @Override
    public Operation getOperationByRouterStepId(String site, String router, String stepId) {
        return operationMapper.selectOperationByRouterStepId(site, router, stepId);
    }


}