package com.iformall.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iformall.domain.po.WxActivity;
import com.iformall.domain.po.WxActivityJoin;
import com.iformall.domain.po.WxCampaign;
import com.iformall.domain.po.msg.WxMsgRecord;
import com.iformall.enums.*;
import com.iformall.mapper.WxActivityJoinMapper;
import com.iformall.mapper.WxActivityMapper;
import com.iformall.mapper.WxCampaignMapper;
import com.iformall.mq.MqBaseProducer;
import com.iformall.utils.DateUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author gongbiao
 */
@Component
public class ActivitySchedule {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxActivityMapper wxActivityMapper;

    @Autowired
    private WxActivityJoinMapper wxActivityJoinMapper;

    @Autowired
    private MqBaseProducer mqBaseProducer;

    @Autowired
    private WxCampaignMapper wxCampaignMapper;

    /**
     * 每天12点执行
     */
    @Scheduled(cron = "0 0 12 * * ?")
    public void sendMsg() {
        logger.info("限时活动参与提醒任务启动");
        WxActivity wxActivity = new WxActivity();
        wxActivity.setStatus(EnumActivityStatus.INJECT_CAMPAIGN.getCode());
        List<WxActivity> wxActivitys = wxActivityMapper.queryNotify(wxActivity);
        WxActivityJoin wxActivityJoin;
        for (WxActivity activity : wxActivitys) {
            wxActivityJoin = new WxActivityJoin();
            wxActivityJoin.setTenantId(activity.getTenantId());
            wxActivityJoin.setActivityId(activity.getId());
            wxActivityJoin.setStatus(EnumActivityJoinStatus.CONFIRMED.getCode());
            List<WxActivityJoin> list = wxActivityJoinMapper.findList(wxActivityJoin);
            for (WxActivityJoin activityJoin : list) {
                send(activity, activityJoin);
            }
        }
    }

    private void send(WxActivity activity, WxActivityJoin activityJoin) {
        logger.info("限时活动参与提醒短信发送开始");
        String phone = activityJoin.getPhone();
        String person = activityJoin.getName();
        person = StringUtils.isNotEmpty(person) ? person : "";
        String time = DateUtils.date2String(activity.getActivityStartTime());
        String party = activity.getTitle();
        WxMsgRecord wxMsgRecord = new WxMsgRecord();
        wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode());
        wxMsgRecord.setModelType(EnumMsgModel.TIME_LIMIT_CAMPAIGN_NOTIFY.getCode());
        wxMsgRecord.setReceiver(phone);
        wxMsgRecord.setTenantId(activityJoin.getTenantId());
        Map<String, String> map = new HashedMap();
        map.put("person", person);
        map.put("party", party);
        map.put("time", time);
        wxMsgRecord.setDynamicContentMap(map);
        mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
        logger.info("限时活动参与提醒短信发送结束");

    }

    /**
     * 5分钟一次
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void activityExipred() {
        logger.info("活动报名结束任务");
        WxActivity activity = new WxActivity();
        activity.setStatus(EnumActivityStatus.INJECT_CAMPAIGN.getCode());
        activity.setIsExpired(EnumActivityExpired.YES.getCode());
        activity.setUpdateTime(new Date());
        wxActivityMapper.activityExipred(activity);
        wxActivityJoinMapper.activityExpired();
    }

    /**
     * 5分钟一次
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void activityCompletion() {
        logger.info("活动结束任务");
        WxActivity activity = new WxActivity();
        activity.setStatus(EnumActivityStatus.STATUS_TAKE_OFFF.getCode());
        activity.setUpdateTime(new Date());
        wxActivityMapper.activityCompletion(activity);
        List<WxActivity> activityList = new ArrayList();
        //宣传页下线
        WxCampaign campaign = new WxCampaign();
        campaign.setStatus(EnumCampaignStatus.STATUS_THROW_IN.getCode());
        campaign.setType(EnumCampaignType.PAGEPATH.getCode());
        campaign.setProduceType(EnumCampaignProductType.ACTIVITY_JOIN.getCode());
        List<WxCampaign> campaignList = wxCampaignMapper.selectList(new QueryWrapper(campaign));
        for (WxCampaign wxCampaign : campaignList) {
            WxActivity wxActivity = wxActivityMapper.selectById(wxCampaign.getProduceId());
            if (wxActivity.getStatus().equals(EnumActivityStatus.STATUS_TAKE_OFFF.getCode())) {
                activityList.add(wxActivity);
                wxCampaign.setStatus(EnumCampaignStatus.STATUS_TAKE_OFFF.getCode());
                wxCampaign.setUpdateTime(new Date());
                wxCampaignMapper.updateById(wxCampaign);
            }
        }
    }


}