package cn.pluss.platform.job;

import cn.pluss.platform.activityEnroll.ActivityEnrollService;
import cn.pluss.platform.cash.CashService;
import cn.pluss.platform.deviceMerchantBuy.DeviceMerchantBuyService;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.notification.NotificationService;
import cn.pluss.platform.officialGoods.exchange.OfficialGoodsExchangeService;
import cn.pluss.platform.suggest.SuggestService;
import cn.pluss.platform.transferCash.MerchantTransferCashService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

@Configuration
@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
public class NotificationJob {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    //1、资金管理-提现管理
    @Autowired
    private CashService cashService;

    //2、系统配置-意见反馈
    @Autowired
    private SuggestService suggestService;

    //3、活动管理-提现管理
    @Autowired
    private MerchantTransferCashService merchantTransferCashService;

    //4、活动管理-兑换详情
    @Autowired
    private OfficialGoodsExchangeService officialGoodsExchangeService;

    //5、活动管理-活动申请列表
    @Autowired
    private ActivityEnrollService activityEnrollService;

    //6、设备管理-设备订单
    @Autowired
    private DeviceMerchantBuyService deviceMerchantBuyService;

    //7、推广员管理-推广员列表
    @Autowired
    private MerchantChannelStatusService merchantChannelStatusService;

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private NotificationService notificationService;

    @Scheduled(fixedDelay = 5 * 60 * 1000)
    public void notificationQuery() {
        log.info("===============>检查消息通知数据定时任务开始<==================");
        Date startTime;
        Notification notification = notificationService.selectOne();
        startTime = notification.getCreateTime();
        Date endTime = new Date();
        notification = new Notification();
        QueryWrapper<Cash> cashQueryWrapper = new QueryWrapper<Cash>().gt("createDt", startTime).le("createDt", endTime);
        List<Cash> cashList = cashService.list(cashQueryWrapper);
        if (cashList.size() != 0) {
            for (Cash cash : cashList) {
                notification.setTitle("资金管理-提现管理");
                notification.setContent("用户" + cash.getUserName() + "申请提现！");
                notification.setCreateTime(cash.getCreateDt());
                notificationService.save(notification);
            }
        }
        Suggest suggest = new Suggest();
        suggest.setStartTime(startTime);
        suggest.setEndTime(endTime);
        List<Suggest> suggestList = suggestService.querySuggestList(suggest);
        if (suggestList.size() != 0) {
            for (Suggest value : suggestList) {
                notification.setTitle("系统配置-意见反馈");
                notification.setContent(value.getContent());
                notification.setCreateTime(value.getCreateDt());
                notificationService.save(notification);
            }
        }
        QueryWrapper<MerchantTransferCash> merchantTransferCashQueryWrapper = new QueryWrapper<MerchantTransferCash>()
                .gt("createTime", startTime).le("createTime", endTime);
        List<MerchantTransferCash> merchantTransferCashList = merchantTransferCashService.list(merchantTransferCashQueryWrapper);
        if (merchantTransferCashList.size() != 0) {
            for (MerchantTransferCash merchantTransferCash : merchantTransferCashList) {
                notification.setTitle("活动管理-提现管理");
                notification.setContent("用户" + merchantTransferCash.getUserName() + "申请提现！");
                notification.setCreateTime(merchantTransferCash.getCreateTime());
                notificationService.save(notification);
            }
        }
        QueryWrapper<OfficialGoodsExchange> officialGoodsExchangeQueryWrapper = new QueryWrapper<OfficialGoodsExchange>()
                .gt("createTime", startTime).le("createTime", endTime);
        List<OfficialGoodsExchange> officialGoodsExchangeList = officialGoodsExchangeService.list(officialGoodsExchangeQueryWrapper);
        if (officialGoodsExchangeList.size() != 0) {
            for (OfficialGoodsExchange officialGoodsExchange : officialGoodsExchangeList) {
                notification.setTitle("活动管理-兑换详情");
                notification.setContent(officialGoodsExchange.getUserName() + "申请兑换商品！");
                notification.setCreateTime(officialGoodsExchange.getCreateTime());
                notificationService.save(notification);
            }
        }
        QueryWrapper<ActivityEnroll> activityEnrollQueryWrapper = new QueryWrapper<ActivityEnroll>()
                .gt("createTime", startTime).le("createTime", endTime);
        List<ActivityEnroll> activityEnrollList = activityEnrollService.list(activityEnrollQueryWrapper);
        if (activityEnrollList.size() != 0) {
            for (ActivityEnroll activityEnroll : activityEnrollList) {
                notification.setTitle("活动管理-活动申请列表");
                notification.setContent(activityEnroll.getMerchantName() + "申请开通" + activityEnroll.getActName() + "的活动！");
                notification.setCreateTime(activityEnroll.getCreateTime());
                notificationService.save(notification);
            }
        }
        DeviceMerchantBuy deviceMerchantBuy = new DeviceMerchantBuy();
        deviceMerchantBuy.setStartTime(startTime);
        deviceMerchantBuy.setEndTime(endTime);
        deviceMerchantBuy.setStatus("1");
        List<DeviceMerchantBuy> deviceMerchantBuyList = deviceMerchantBuyService.queryDeviceMerchantBuyList(deviceMerchantBuy);
        if (deviceMerchantBuyList.size() != 0) {
            for (DeviceMerchantBuy merchantBuy : deviceMerchantBuyList) {
                notification.setTitle("设备管理-设备订单");
                notification.setContent(merchantBuy.getMerchantName()
                        + merchantBuy.getPayName() + "购买了" + merchantBuy.getQuantity() + "个"
                        + merchantBuy.getVenderModel() + "机具");
                notification.setCreateTime(merchantBuy.getCreateDt());
                notificationService.save(notification);
            }
        }
        QueryWrapper<MerchantChannelStatus> merchantChannelStatusQueryWrapper = new QueryWrapper<MerchantChannelStatus>()
                .gt("createTime", startTime).le("createTime", endTime).eq("thirdStatus",MerchantChannelStatus.AUDIT_THIRD_STATUS_WAITING);
        List<MerchantChannelStatus> merchantChannelStatusList = merchantChannelStatusService.list(merchantChannelStatusQueryWrapper);
        if (merchantChannelStatusList.size() != 0){
            for (MerchantChannelStatus merchantChannelStatus : merchantChannelStatusList){
                notification.setTitle("推广员管理-推广员列表");
                Integer userId = userAppService.getUserId(merchantChannelStatus.getMerchantCode());
                notification.setContent("用户Id:"+userId+",申请进件！");
                notification.setCreateTime(merchantChannelStatus.getCreateTime());
                notificationService.save(notification);
            }
        }
        log.info("===============>检查消息通知数据定时任务结束<==================");
    }
}