package com.foreverwin.mesnac.quality.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.common.util.StringUtil;
import com.foreverwin.mesnac.quality.mapper.InspectionProjectMapper;
import com.foreverwin.mesnac.quality.model.InspectionProject;
import com.foreverwin.mesnac.quality.service.InspectionProjectService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * <p>
 * 检验项目维护-主 服务实现类
 * </p>
 *
 * @author Robert
 * @since 2021-06-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InspectionProjectServiceImpl extends ServiceImpl<InspectionProjectMapper, InspectionProject> implements InspectionProjectService {


    @Autowired
    private InspectionProjectMapper inspectionProjectMapper;

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

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

    @Override
    public String InspectProjectNoGenerationRules(InspectionProject inspectionProject) {
        if(StringUtil.isEmpty(inspectionProject.getItem())){
            inspectionProject.setInspectProjectNo(inspectionProject.getInspectType()+",*"+",*"+",*"+",*");
            inspectionProject.setItemBo("*");
            inspectionProject.setItem("*");
            inspectionProject.setOperation("*");
            inspectionProject.setStepId("*");
            inspectionProject.setSubStep("*");
        }else if(StringUtil.isEmpty(inspectionProject.getOperation())){
            inspectionProject.setInspectProjectNo(inspectionProject.getInspectType()+","+inspectionProject.getItem()+",*"+",*"+",*");
            inspectionProject.setOperation("*");
            inspectionProject.setStepId("*");
            inspectionProject.setSubStep("*");

        }else if(StringUtil.isEmpty(inspectionProject.getSubStep())){
            inspectionProject.setInspectProjectNo(inspectionProject.getInspectType()+","+inspectionProject.getItem()+","
                    +inspectionProject.getOperation()+","+inspectionProject.getStepId()+",*");
            inspectionProject.setSubStep("*");
        }else {
            inspectionProject.setInspectProjectNo(inspectionProject.getInspectType()+","+inspectionProject.getItem()+","
                    +inspectionProject.getOperation()+","+inspectionProject.getStepId()+","+inspectionProject.getSubStep());
        }
        return inspectionProject.getInspectProjectNo();
    }

    @Override
    public String findRevisionByCurrentRevision(String inspectProjectNo) {
        String site = CommonMethods.getSite();
        return inspectionProjectMapper.findRevisionByCurrentRevision(site,inspectProjectNo);
    }


}