package com.sptg.ad.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.ad.api.entity.AdPushThresholdValue;
import com.sptg.ad.biz.mapper.AdPushThresholdValueMapper;
import com.sptg.ad.biz.service.AdPushThresholdValueServiceInterface;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * title: AdPushThresholdValueServiceImpl
 * package: com.sptg.ad.biz.service.impl
 * description:广告发布阈值-服务层实现类
 *
 *                
 *                
 */
@Service
public class AdPushThresholdValueServiceImpl extends ServiceImpl<AdPushThresholdValueMapper, AdPushThresholdValue> implements AdPushThresholdValueServiceInterface {

    @Override
    public AdPushThresholdValue getAdPushThresholdValue(AdPushThresholdValue adPushThresholdValue){
        Wrapper<AdPushThresholdValue> wrapper=new EntityWrapper<>();
        //广告发布阈值类型
        wrapper.eq("i_type",adPushThresholdValue.getType());
        //机构位置信息
        wrapper.eq("s_org_position",adPushThresholdValue.getOrgPosition());
        List<AdPushThresholdValue> adPushThresholdValues = super.selectList(wrapper);
        if(null!=adPushThresholdValues&&!adPushThresholdValues.isEmpty()){
            return adPushThresholdValues.get(0);
        }
        return null;
    }

    @Override
    public boolean saveOrUpdate(AdPushThresholdValue adPushThresholdValue){
        boolean result;
        AdPushThresholdValue thresholdValue=null;
        Wrapper<AdPushThresholdValue> wrapper=new EntityWrapper<>();
        if(null!=adPushThresholdValue.getId()){
            thresholdValue=super.selectById(adPushThresholdValue.getId());
        }else{
            //广告发布阈值类型
            wrapper.eq("i_type",adPushThresholdValue.getType());
            //机构位置信息
            wrapper.eq("s_org_position",adPushThresholdValue.getOrgPosition());
            List<AdPushThresholdValue> adPushThresholdValues = super.selectList(wrapper);
            if(null!=adPushThresholdValues&&!adPushThresholdValues.isEmpty()){
                thresholdValue=adPushThresholdValues.get(0);
            }
        }
        if(null==thresholdValue){
            result=super.insert(adPushThresholdValue);
        }else{
            thresholdValue.setCount(adPushThresholdValue.getCount());
            result=super.updateById(thresholdValue);
        }
        return result;
    }
}
