package com.fs.course.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.StringUtils;

import com.fs.gtPush.service.uniPush2Service;
import com.fs.jpush.service.JpushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.MarketPushMapper;
import com.fs.course.domain.MarketPush;
import com.fs.course.service.IMarketPushService;

/**
 * 营销活动推送Service业务层处理
 *
 * @author fs
 * @date 2025-03-25
 */
@Service
public class MarketPushServiceImpl extends ServiceImpl<MarketPushMapper, MarketPush> implements IMarketPushService {
    @Autowired
    private MarketPushMapper marketPushMapper;
    @Autowired
    private JpushService jpushService;
    @Autowired
    private uniPush2Service uniPush2Service;
    private final Map<Long, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
    /**
     * 查询营销活动推送
     *
     * @param id 营销活动推送主键
     * @return 营销活动推送
     */
    @Override
    public MarketPush selectMarketPushById(Long id)
    {
        return baseMapper.selectMarketPushById(id);
    }

    /**
     * 查询营销活动推送列表
     *
     * @param marketPush 营销活动推送
     * @return 营销活动推送
     */
    @Override
    public List<MarketPush> selectMarketPushList(MarketPush marketPush)
    {
        List<MarketPush> marketPushes = baseMapper.selectMarketPushList(marketPush);
        for (MarketPush push : marketPushes) {
            if (StringUtils.isNotEmpty(push.getImgUrl())){
                push.setImgUrl(push.getImgUrl().split(",")[0]);
            }
        }
        return marketPushes;
    }

    /**
     * 新增营销活动推送
     *
     * @param marketPush 营销活动推送
     * @return 结果
     */
    @Override
    public int insertMarketPush(MarketPush marketPush)
    {
        marketPush.setCreateTime(DateUtils.getNowDate());
        int i = marketPushMapper.insertMarketPush(marketPush);
        marketPush.setId(marketPushMapper.getLastInsertId());
        /*pushScheduler.cancelAllTasks();  // 取消旧任务
        pushScheduler.schedulePushTask(marketPush);  // 重新调度新任务*/
        return i;
    }

    /**
     * 修改营销活动推送
     *
     * @param marketPush 营销活动推送
     * @return 结果
     */
    @Override
    public int updateMarketPush(MarketPush marketPush)
    {
        marketPush.setUpdateTime(DateUtils.getNowDate());
        /*pushScheduler.cancelPushTask(marketPush.getId());  // 取消旧任务
        pushScheduler.schedulePushTask(marketPush);  // 重新调度新任务*/
        return baseMapper.updateMarketPush(marketPush);
    }

    /**
     * 批量删除营销活动推送
     *
     * @param ids 需要删除的营销活动推送主键
     * @return 结果
     */
    @Override
    public int deleteMarketPushByIds(Long[] ids)
    {
        return baseMapper.deleteMarketPushByIds(ids);
    }

    /**
     * 删除营销活动推送信息
     *
     * @param id 营销活动推送主键
     * @return 结果
     */
    @Override
    public int deleteMarketPushById(Long id)
    {
        return baseMapper.deleteMarketPushById(id);
    }

    @Override
    public List<MarketPush> getEnabledMarketPushes() {
        return marketPushMapper.findByStatus(1);  // status=1 表示启用
    }

    @Override
    public void sendPushNotification(MarketPush marketPush) throws APIConnectionException, APIRequestException {
       /* Map<String, String> extrasMap = new HashMap<>();
        List<String> userIds = new ArrayList<>();
        //jpushService.sendPush(marketPush.getTitle(),marketPush.getCintent(),extrasMap, newTags);
        uniPush2Service.pushAll(userIds,marketPush.getId(),marketPush.getUrl(),marketPush.getImgUrl(),marketPush.getTitle(),marketPush.getCintent(), PushLogTypeEnum.MARKET.getValue(), PushLogDesTypeEnum.MARKET_PUSH.getValue());
        //uniPush2Service.pushOne(29l,null,marketPush.getUrl()!=null?marketPush.getUrl():null,marketPush.getTitle(),marketPush.getCintent(), PushLogTypeEnum.MARKET.getValue(), PushLogDesTypeEnum.MARKET_PUSH.getValue());
        marketPush.setPushCount(marketPush.getPushCount()+1);
        marketPushMapper.updateMarketPush(marketPush);*/
    }

    /**
     * 取消所有推送任务
     */
    public void cancelAllTasks() {
        for (ScheduledFuture<?> future : scheduledTasks.values()) {
            future.cancel(true);
        }
        scheduledTasks.clear();
    }

    /**
     * 取消单个推送任务
     */
    public void cancelPushTask(Long pushId) {
        ScheduledFuture<?> future = scheduledTasks.get(pushId);
        if (future != null) {
            future.cancel(true);
            scheduledTasks.remove(pushId);
        }
    }
}
