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.NcCodeDto;
import com.foreverwin.mesnac.meapi.mapper.NcCodeMapper;
import com.foreverwin.mesnac.meapi.model.NcCode;
import com.foreverwin.mesnac.meapi.model.NcGroup;
import com.foreverwin.mesnac.meapi.service.NcCodeService;
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.nonconformance.NCGroupBOHandle;
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;

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


    @Autowired
    private NcCodeMapper ncCodeMapper;

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

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

    @Override
    public List<NcCode> findNcByCategory(String messageType,String code) {
        String site = CommonMethods.getSite();
        String locale = LocaleContextHolder.getLocale().getLanguage();
        return ncCodeMapper.findNcByCategory(site,locale,messageType,code);
    }

    @Override
    public List<NcCode> findSecondaryNcByNc(String ncCode) {
        String site = CommonMethods.getSite();
        String locale = LocaleContextHolder.getLocale().getLanguage();
        return ncCodeMapper.findSecondaryNcByNc(site,locale,ncCode);
    }

    @Override
    public List<NcCode> selectByNcGroup(String ncGroup) {
        String site = CommonMethods.getSite();
        if (StringUtils.isBlank(ncGroup)) {
            throw new BaseException("不良代码组不能为空");
        }
        String ncGroupBO = new NCGroupBOHandle(site, ncGroup).getValue();
        return ncCodeMapper.selectPageByNcGroup(ncGroupBO,LocaleContextHolder.getLocale().getLanguage());
    }

    @Override
    public List<NcCode> findNcCodeByNcGroup(NcGroup ncGroup) {
        String site = CommonMethods.getSite();
        ncGroup.setSite(site);
        return ncCodeMapper.findNcCodeByNcGroup(ncGroup);
    }

    @Override
    public List<NcCodeDto> findNcCodeDescriptionByNcCode(String ncCode) {
        String site = CommonMethods.getSite();
        if(StringUtils.isBlank(ncCode)){
            return null;
        }
        String locale = LocaleContextHolder.getLocale().getLanguage();
        String[] split = ncCode.split(",");

        return ncCodeMapper.findNcCodeDescriptionByNcCode(site,locale,split);
    }

    @Override
    public List<NcCodeDto> findNcCodeByNcGroupList(String ncCode) {
        String site = CommonMethods.getSite();
        String locale = LocaleContextHolder.getLocale().getLanguage();
        String[] split = ncCode.split(",");

        return ncCodeMapper.findNcCodeByNcGroupList(site,locale,split);
    }


}