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

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.ad.api.dto.AdPushDeviceDetailsDto;
import com.sptg.ad.api.entity.AdPushDeviceDetails;
import com.sptg.ad.biz.mapper.AdPushDeviceDetailsMapper;
import com.sptg.ad.biz.service.AdPushDeviceDetailsServiceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * title: AdContentImgServiceImpl
 * package: com.sptg.ad.biz.security.impl
 * description: 广告任务发布到设备-接口实现类
 *
 *               
 *                 
 */
@Service
public class AdPushDeviceDetailsServiceImpl extends ServiceImpl<AdPushDeviceDetailsMapper, AdPushDeviceDetails> implements AdPushDeviceDetailsServiceInterface {

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> addAdPushDeviceDetails(AdPushDeviceDetails adPushDeviceDetails) {
        //构建查询条件
        Wrapper<AdPushDeviceDetails> wrapper = new EntityWrapper<>();
        wrapper.eq("l_task_id", adPushDeviceDetails.getTaskId());
        wrapper.eq("l_device_id",adPushDeviceDetails.getDeviceId());
        //旧记录--获取记录id
         Object obj = this.selectObj(wrapper);
        if(obj!=null){
            adPushDeviceDetails.setId((Long)obj);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,this.insertOrUpdate(adPushDeviceDetails));
    }

    @Override
    public Page<AdPushDeviceDetails> showAdPushDeviceDetails(AdPushDeviceDetailsDto adPushDeviceDetailsDto) {
        //排序字段 时间降序
        adPushDeviceDetailsDto.getPage().setOrderByField("l_id");
        adPushDeviceDetailsDto.getPage().setAsc(true);
        //构建查询条件
        Wrapper<AdPushDeviceDetails> wrapper = new EntityWrapper<>();
        //根据小区名字查询
        if(StringUtils.isNotBlank(adPushDeviceDetailsDto.getXqName())){
            wrapper.like("s_xq_name",adPushDeviceDetailsDto.getXqName());
        }
        //根据单元名字查询
        if(StringUtils.isNotBlank(adPushDeviceDetailsDto.getDyName())){
            wrapper.like("s_dy_name",adPushDeviceDetailsDto.getDyName());
        }
        //根据设备名字查询
        if(StringUtils.isNotBlank(adPushDeviceDetailsDto.getDeviceName())){
            wrapper.like("s_device_name",adPushDeviceDetailsDto.getDeviceName());
        }
        //根据设备序列号查询
        if(StringUtils.isNotBlank(adPushDeviceDetailsDto.getSerialNumber())){
            wrapper.like("s_serial_number",adPushDeviceDetailsDto.getSerialNumber());
        }
        //根据发布状态查询
        if(adPushDeviceDetailsDto.getPushStatus()!=null){
            wrapper.eq("i_push_status",adPushDeviceDetailsDto.getPushStatus());
        }
        //任务id--必须字段
        wrapper.eq("l_task_id", adPushDeviceDetailsDto.getTaskId());
        return this.selectPage(adPushDeviceDetailsDto.getPage(), wrapper);
    }

    @Override
    public List<AdPushDeviceDetails> getListByTaskId(Long taskId) {
        //构建查询条件
        Wrapper<AdPushDeviceDetails> wrapper = new EntityWrapper<>();
        //任务id--必须字段
        wrapper.eq("l_task_id", taskId);
        //推送状态：{ 0：未推送， 1：推送成功，2：推送失败，3：推送中 }
        wrapper.eq("i_push_status", 2);
        return this.selectList(wrapper);
    }
}
