package com.slodon.b2b2c.job;

import com.slodon.b2b2c.core.constant.StoreTplConst;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.TimeUtil;
import com.slodon.b2b2c.model.seller.StoreModel;
import com.slodon.b2b2c.seller.pojo.Store;
import com.slodon.b2b2c.starter.mq.entity.MessageSendVO;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.slodon.b2b2c.core.constant.StarterConfigConst.MQ_EXCHANGE_NAME;
import static com.slodon.b2b2c.core.constant.StarterConfigConst.MQ_QUEUE_NAME_SELLER_MSG;

@Slf4j
@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   //2.开启定时任务
public class SellerJob {

    @Resource
    private StoreModel storeModel;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 系统定时更新店铺商品总销量、店铺商品总浏览量
     * 每隔十分钟执行一次
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    @SchedulerLock(name = "jobSetStoreSalesAndLookVolume", lockAtMostFor = "600000", lockAtLeastFor = "540000")
    public void jobSetStoreSalesAndLookVolume() {
        log.info("jobSetStoreSalesAndLookVolume() start");
        try {
            boolean jobResult = storeModel.jobSetStoreSalesAndLookVolume();
            AssertUtil.isTrue(!jobResult, "[jobSetStoreSalesAndLookVolume] 定时更新店铺商品总销量、总浏览量失败");
        } catch (Exception e) {
            log.error("jobSetStoreSalesAndLookVolume()", e);
        }
        log.info("jobSetStoreSalesAndLookVolume() end");
    }

    /**
     * 定时任务设定商家的评分，用户评论各项求平均值设置为商家各项的综合评分
     * 每天早上七点执行
     */
    @Scheduled(cron = "0 0 7 * * ?")
    @SchedulerLock(name = "jobSetStoreScore", lockAtMostFor = "86400000", lockAtLeastFor = "82800000")
    public void jobSetStoreScore() {
        log.info("jobSetStoreScore() start");
        try {
            boolean jobResult = storeModel.jobSetStoreScore();
            AssertUtil.isTrue(!jobResult, "[jobSetStoreScore] 定时任务设定商家的评分时失败");
        } catch (Exception e) {
            log.error("jobSetStoreScore()", e);
        }
        log.info("jobSetStoreScore() end");
    }

    /**
     * 定时任务设定商家各项统计数据
     * 每隔十分钟执行一次
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    @SchedulerLock(name = "jobStoreStatistics", lockAtMostFor = "600000", lockAtLeastFor = "540000")
    public void jobStoreStatistics() {
        log.info("jobStoreStatistics() start");
        try {
            boolean jobResult = storeModel.jobStoreStatistics();
            AssertUtil.isTrue(!jobResult, "[jobStoreStatistics] 定时任务设定商家各项统计数据时失败");
        } catch (Exception e) {
            log.error("jobStoreStatistics()", e);
        }
        log.info("jobStoreStatistics() end");
    }

    /**
     * 系统自动完成冻结或续签店铺
     * 对即将到期的店铺发送到期通知
     * 每天00:05:00执行一次，如果store表的end_time小于当前时间，查询续签表，1.没有续签成功的申请：冻结店铺;2.有续签成功的，续签，更新店铺等级、end_time
     */
//    @Scheduled(cron = "0 5 0 * * ?")
//    @SchedulerLock(name = "jobSystemStoreManage", lockAtMostFor = "86400000", lockAtLeastFor = "82800000")
    public void jobSystemStoreManage() {
        log.info("jobSystemStoreManage() start");
        try {
            List<Store> storeList = storeModel.jobSystemStoreManage();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (!CollectionUtils.isEmpty(storeList)) {
                for (Store store : storeList) {
                    String msgLinkInfo = "{\"type\":\"store_news\"}";
                    String[] templateParam = {store.getStoreName(), TimeUtil.getDateTimeString(store.getStoreExpireTime())};
                    MessageSendVO messageSendVO = new MessageSendVO(store.getStoreId(), StoreTplConst.STORE_END_REMINDER, msgLinkInfo, templateParam);
                    //发送到mq
                    rabbitTemplate.convertAndSend(MQ_EXCHANGE_NAME, MQ_QUEUE_NAME_SELLER_MSG, messageSendVO);
                }
            }
        } catch (Exception e) {
            log.error("jobSystemStoreManage()", e);
        }
        log.info("jobSystemStoreManage() end");
    }

}
