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.AdOfflinePushRecordDto;
import com.sptg.ad.api.entity.AdOfflinePushRecord;
import com.sptg.ad.biz.mapper.AdOfflinePushRecordMapper;
import com.sptg.ad.biz.service.AdOfflinePushRecordServiceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;



@Service
public class AdOfflinePushRecordServiceImpl extends ServiceImpl<AdOfflinePushRecordMapper, AdOfflinePushRecord> implements AdOfflinePushRecordServiceInterface {
    @Override
    public Page<AdOfflinePushRecord> showAdOfflinePushRecordList(AdOfflinePushRecordDto adOfflinePushRecordDto) {
        //排序字段 时间降序
        adOfflinePushRecordDto.getPage().setOrderByField("aopr.t_create_date");
        adOfflinePushRecordDto.getPage().setAsc(false);
        //构建查询条件
        Wrapper<AdOfflinePushRecord> wrapper = new EntityWrapper<>();
        //  根据任务类型查询


        if(StringUtils.isNotBlank(adOfflinePushRecordDto.getAdTitle())){
            wrapper.like("ac.s_ad_title", adOfflinePushRecordDto.getAdTitle());
        }

        //发布状态{ -1：发布失败，0：待发布，1：发布成功，2：已撤销 }
//        if(adOfflinePushRecordDto.getPushStatus()!=null){
//            if(adOfflinePushRecordDto.getPushStatus()==1){
//                List<Integer> pushStatus = new ArrayList<>();
//                pushStatus.add(adOfflinePushRecordDto.getPushStatus());
//                pushStatus.add(2);
//                wrapper.in("aopr.i_push_status", pushStatus);
//            }else if(adOfflinePushRecordDto.getPushStatus()==3){
//                //3是临时状态
//                wrapper.in("t.i_push_status", "1");
//            }else{
//                wrapper.eq("t.i_push_status", adOfflinePushRecordDto.getPushStatus());
//            }
//        }
        //查询本级以及下级广告记录
        wrapper.like("aopr.s_org_position",adOfflinePushRecordDto.getOrgPosition());
        Page<AdOfflinePushRecord> page = adOfflinePushRecordDto.getPage();
        page.setRecords(this.baseMapper.showList(wrapper, page));
        return page;
    }

    @Override
    public ResultBean<Boolean> addAdOfflinePushRecord(AdOfflinePushRecord adOfflinePushRecord) {
        boolean result = this.insert(adOfflinePushRecord);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS,result);
        }else{
            return new ResultBean<>(ResultEnum.ERROR);
        }
    }

    @Override
    public ResultBean<Boolean> cancelAdOfflinePushRecord(Long id) {
        if(id!=null){
            AdOfflinePushRecord adOfflinePushRecord = this.selectById(id);
            //发布状态{ -1：发布失败，0：待发布，1：发布成功，2：已撤销 }
            //校验状态--只有待发布的任务才能撤回
            if(adOfflinePushRecord.getPushStatus()==0){

                //撤销
                return new ResultBean<>(ResultEnum.SUCCESS, this.deleteById(id));
            }
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    public AdOfflinePushRecord findAdOfflinePushRecordById(Long id) {
        return this.selectById(id);
    }

    @Override
    public ResultBean<Boolean> updateAdOfflinePushRecord(AdOfflinePushRecord adOfflinePushRecord) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(adOfflinePushRecord));
    }
    @Override
    public Integer getAdOfflinePushRecordByProvinceName(String provinceShortName,String orgPosition) {
        Wrapper<AdOfflinePushRecord> wrapper = new EntityWrapper<>();
        if(null != provinceShortName){
            wrapper.eq("province_short_name",provinceShortName);
        }
        if(null != orgPosition){
            wrapper.like("s_org_position",orgPosition);
        }
        return this.selectCount(wrapper);
    }
    @Override
    public Integer getAdOfflinePushRecordByYearMonth(String year, String month) {
        int strLen =month.length();
        if (strLen <2) {
            while (strLen< 2) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(month);//左补0
                month= sb.toString();
            }
        }
        String yearMonth = year+"-"+month;
        return this.baseMapper.getAdOfflinePushRecordByYearMonth(yearMonth);
    }
}
