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.AdAdsenseDto;
import com.sptg.ad.api.entity.AdAdsense;
import com.sptg.ad.api.entity.AdContent;
import com.sptg.ad.api.entity.AdMaterial;
import com.sptg.ad.biz.mapper.AdAdsenseMapper;
import com.sptg.ad.biz.service.AdAdsenseServiceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SymbolConstant;
import com.sptg.common.core.util.CommonUtil;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.feigns.DeviceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

@Service
public class AdAdsenseServiceImpl extends ServiceImpl<AdAdsenseMapper, AdAdsense> implements AdAdsenseServiceInterface
{



    @Override
    public Page<AdAdsense> showAdAdsenseList(AdAdsenseDto adAdsenseDto) {
        if(adAdsenseDto.getIsOfflinePushSearch() != null && adAdsenseDto.getIsOfflinePushSearch() == 1){
            Wrapper<AdAdsense> wrapper = new EntityWrapper<>();
            wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
            wrapper.in("i_adsense_type",new Integer[]{2,3});
            if(adAdsenseDto.getResidentialId()!= null){
                wrapper.eq("l_residential_id",adAdsenseDto.getResidentialId());
            }
            Page<AdAdsense> page = adAdsenseDto.getPage();
            page.setRecords(this.baseMapper.showAdAdsenseList(wrapper,adAdsenseDto.getPage()));
            return page;
        }
        //排序字段 时间降序
//        adAdsenseDto.getPage().setOrderByField("l_create_date");
//        adAdsenseDto.getPage().setAsc(false);

        //构建查询条件
        Wrapper<AdAdsense> wrapper = new EntityWrapper<>();

        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);

        if(adAdsenseDto.getResidentialId()!= null){
            wrapper.eq("l_residential_id",adAdsenseDto.getResidentialId());
        }
        //xml文件sql查询
        Page<AdAdsense> page = adAdsenseDto.getPage();
        page.setRecords(this.baseMapper.showAdAdsenseList(wrapper,adAdsenseDto.getPage()));

        return page;
    }

    @Override
    public ResultBean<Boolean> addAdAdsense(AdAdsense adAdsense) {
        int materialNumber = this.getAllAdsenseCount().getData();
        adAdsense.setAdsenseNumber(SymbolConstant.GGW_NO+ CommonUtil.autoZeroFilling(6,materialNumber));
        boolean result = this.insertOrUpdate(adAdsense);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS);
        }else{
            return new ResultBean<>("200","操作失败");
        }
    }

    @Override
    public ResultBean<Boolean> deleteAdAdsense(String ids) {
        String[] id = ids.split(",");
        if(id!=null && ids.length()>0){
            List<String> list = Arrays.asList(id);
            Wrapper<AdAdsense> wrapper = new EntityWrapper<>();
            wrapper.in("l_id", list);
            //需要修改状态的广告位
            AdAdsense adAdsense = new AdAdsense();
            adAdsense.setStatus(OperationConstant.MINUS_ONE);
            return new ResultBean<>(ResultEnum.SUCCESS, this.update(adAdsense,wrapper));
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> deleteAdsenseList(AdAdsenseDto adAdsenseDto) {
        List<String> ids = adAdsenseDto.getIds();
        for(String id : ids){
            //  获取账号信息
            AdAdsense adAdsense = this.selectById(Long.valueOf(id));
            Assert.notNull(adAdsense,"记录不存在,请核实后重新操作");
            //  标记为删除状态
            adAdsense.setStatus(OperationConstant.MINUS_ONE);
            this.updateById(adAdsense);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,true);
    }

    @Override
    public AdAdsense findAdAdsenseById(Long id) {
        return this.selectById(id);
    }

    @Override
    public ResultBean<Boolean> updateAdsenseStatus(Long id, String adsenseStatus) {
        boolean flag = this.baseMapper.updateAdsenseStatus(id,adsenseStatus);
        if(flag){
            return new ResultBean<>(ResultEnum.SUCCESS);
        }else{
            return new ResultBean<>("200","操作失败");
        }
    }
    @Override
    public ResultBean<Integer> getAllAdsenseCount(){
        //构建查询条件
        Wrapper<AdAdsense> wrapper = new EntityWrapper<>();
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectCount(wrapper));
    }
}
