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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.common.dto.NextNumberDto;
import com.foreverwin.mesnac.common.mapper.NextNumberMapper;
import com.foreverwin.mesnac.common.model.NextNumber;
import com.foreverwin.mesnac.common.service.NextNumberService;
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 Roc Li
 * @since 2020-09-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class NextNumberServiceImpl extends ServiceImpl<NextNumberMapper, NextNumber> implements NextNumberService {

    @Autowired
    private NextNumberMapper nextNumberMapper;

    @Override
    public List<NextNumberDto> selectListBySite(String site) {
        return nextNumberMapper.selectListBySite(site);
    }

    @Override
    public NextNumber selectOne(String site, String numberType) {
        LambdaQueryWrapper<NextNumber> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NextNumber::getSite, site);
        queryWrapper.eq(NextNumber::getNumberType, numberType);
        //---------------------------------------------------------------------------------------------------------------------------------------------
        List<NextNumber> resultList = super.list(queryWrapper);
        //---------------------------------------------------------------------------------------------------------------------------------------------
        return  resultList != null && resultList.size() > 0 ? resultList.get(0) : null;
    }

    public boolean removeBySite(String site) {
        NextNumber entity = new NextNumber();
        entity.setSite(site);
        //----------------------------------------------------------------------------------------------------------------------------------
        QueryWrapper<NextNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        //----------------------------------------------------------------------------------------------------------------------------------
        return super.remove(queryWrapper);
    }

    @Override
    public void batchSave(String site, List<NextNumber> ruleList)
    {
        if (ruleList == null || ruleList.size() == 0) return;
        //----------------------------------------------------------------------------------------------------------------------------------
        removeBySite(site);
        saveOrUpdateBatch(ruleList);
    }
}