package com.tofly.base.service.handler.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.base.em.ZscbMessage;
import com.tofly.base.entity.Qfkhjbxx;
import com.tofly.base.service.QfkhjbxxService;
import com.tofly.base.service.UserService;
import com.tofly.base.service.handler.MessageRemind;
import com.tofly.base.service.messge.MessagepushService;
import com.tofly.base.service.messge.MessagerecipientService;
import com.tofly.base.vo.UserPerssionByMenuVO;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.pojo.User;
import com.tofly.pojo.message.Messagepush;
import com.tofly.pojo.message.Messagerecipient;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FlashWJL
 * @date 2021/5/31 10:22
 */
@Service
public class MessageRemindImpl implements MessageRemind {
    @Autowired
    private  MessagepushService messagepushService;
    @Autowired
    private  MessagerecipientService messagerecipientService;
    @Autowired
    private UserService userService;
    @Autowired
    private QfkhjbxxService qfkhjbxxService;

    @Value("${zscb.price.month}")
    private String monthPriceRoute;
    @Value("${zscb.price.day}")
    private String dayWaterRoute;
    @Value("${zscb.finance.day}")
    private String financeRoute;
    @Value("${meter.zj}")
    private String meterZjRoute;
    @Value("${minflow.night.warn}")
    private String minFlowRoute;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean monthMessagePrice() throws Exception{
        Messagepush messagepush = new Messagepush();
        messagepush.setMessage(ZscbMessage.MONTH_PRICE.getMessage());
        messagepush.setTopic(ZscbMessage.MONTH_PRICE.getTopic());
        messagepush.setType(ZscbMessage.MONTH_PRICE.getType());
        messagepush.setTableName(ZscbMessage.MONTH_PRICE.getTableName());
        messagepush.setRouteIp(monthPriceRoute);
        //这里暂时先默认写一个ID，具体创建这个消息的是谁，后面问下需求
        messagepush.setCreateUser(1L);
        boolean save = messagepushService.save(messagepush);
        if (save) {
            Long resultId = messagepush.getId();
            //获取菜单下的用户ID
            ResultRespone<List<UserPerssionByMenuVO>> resultRespone = userService.getUserAndDepatmentByPerssion(ZscbMessage.DAY_POD_WATER.getMenuId());
            List<UserPerssionByMenuVO> userPerssionByMenuVOList = resultRespone.getResult();
            if (StringUtil.isNotNull(userPerssionByMenuVOList) && userPerssionByMenuVOList.size() > 0) {
                List<Long> userIdList = userPerssionByMenuVOList.stream().map(User::getId).collect(Collectors.toList());
                //指定任务人接收消息
                //这里没有批量的，所以暂时只好轮训接收
                userIdList.forEach(k -> {
                    Messagerecipient messagerecipient = new Messagerecipient();
                    messagerecipient.setMessageId(resultId);
                    messagerecipient.setRecipient(k);
                    messagerecipient.setFlag("0");
                    messagerecipientService.save(messagerecipient);
                });
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean dayMessagePodWater() throws Exception{
        Messagepush messagepush = new Messagepush();
        messagepush.setMessage(ZscbMessage.DAY_POD_WATER.getMessage());
        messagepush.setTopic(ZscbMessage.DAY_POD_WATER.getTopic());
        messagepush.setType(ZscbMessage.DAY_POD_WATER.getType());
        messagepush.setTableName(ZscbMessage.DAY_POD_WATER.getTableName());
        //这里暂时先默认写一个ID，具体创建这个消息的是谁，后面问下需求
        messagepush.setCreateUser(1L);
        messagepush.setRouteIp(dayWaterRoute);
        boolean save = messagepushService.save(messagepush);
        if (save) {
            Long resultId = messagepush.getId();
            //获取菜单下的用户ID
            ResultRespone<List<UserPerssionByMenuVO>> resultRespone = userService.getUserAndDepatmentByPerssion(ZscbMessage.DAY_POD_WATER.getMenuId());
            List<UserPerssionByMenuVO> userPerssionByMenuVOList = resultRespone.getResult();
            if (StringUtil.isNotNull(userPerssionByMenuVOList) && userPerssionByMenuVOList.size() > 0) {
                List<Long> userIdList = userPerssionByMenuVOList.stream().map(User::getId).collect(Collectors.toList());
                //指定任务人接收消息
                //这里没有批量的，所以暂时只好轮训接收
                userIdList.forEach(k -> {
                    Messagerecipient messagerecipient = new Messagerecipient();
                    messagerecipient.setMessageId(resultId);
                    messagerecipient.setRecipient(k);
                    messagerecipient.setFlag("0");
                    messagerecipientService.save(messagerecipient);
                });
            }
        }

        return true;
    }

    /**
     * 财务填报每月提醒
     *
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean financeIsAllowed() throws Exception {
        Messagepush messagepush = new Messagepush();
        messagepush.setMessage(ZscbMessage.FINANCE_MONTH.getMessage());
        messagepush.setTopic(ZscbMessage.FINANCE_MONTH.getTopic());
        messagepush.setType(ZscbMessage.FINANCE_MONTH.getType());
        messagepush.setTableName(ZscbMessage.FINANCE_MONTH.getTableName());
        messagepush.setRouteIp(financeRoute);
        //这里暂时先默认写一个ID，具体创建这个消息的是谁，后面问下需求
        messagepush.setCreateUser(1L);
        boolean save = messagepushService.save(messagepush);
        if (save) {
            Long resultId = messagepush.getId();
            //获取菜单下的用户ID
            ResultRespone<List<UserPerssionByMenuVO>> resultRespone = userService.getUserAndDepatmentByPerssion(ZscbMessage.FINANCE_MONTH.getMenuId());
            List<UserPerssionByMenuVO> userPerssionByMenuVOList = resultRespone.getResult();
            if (StringUtil.isNotNull(userPerssionByMenuVOList) && userPerssionByMenuVOList.size() > 0) {
                List<Long> userIdList = userPerssionByMenuVOList.stream().map(User::getId).collect(Collectors.toList());
                //指定任务人接收消息
                //这里没有批量的，所以暂时只好轮训接收
                userIdList.forEach(k -> {
                    Messagerecipient messagerecipient = new Messagerecipient();
                    messagerecipient.setMessageId(resultId);
                    messagerecipient.setRecipient(k);
                    messagerecipient.setFlag("0");
                    messagerecipientService.save(messagerecipient);
                });
            }
        }
        return true;
    }

    /**
     * 夜间最小流量报警
     *
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean minFlowWarn() throws Exception {
        Messagepush messagepush = new Messagepush();
        messagepush.setMessage(ZscbMessage.MIN_FLOW_WARN.getMessage());
        messagepush.setTopic(ZscbMessage.MIN_FLOW_WARN.getTopic());
        messagepush.setType(ZscbMessage.MIN_FLOW_WARN.getType());
        messagepush.setTableName(ZscbMessage.MIN_FLOW_WARN.getTableName());
        messagepush.setRouteIp(minFlowRoute);
        //这里暂时先默认写一个ID，具体创建这个消息的是谁，后面问下需求
        messagepush.setCreateUser(1L);
        boolean save = messagepushService.save(messagepush);
        if (save) {
            Long resultId = messagepush.getId();
            //获取菜单下的用户ID
            ResultRespone<List<UserPerssionByMenuVO>> resultRespone = userService.getUserAndDepatmentByPerssion(ZscbMessage.MIN_FLOW_WARN.getMenuId());
            List<UserPerssionByMenuVO> userPerssionByMenuVOList = resultRespone.getResult();
            if (StringUtil.isNotNull(userPerssionByMenuVOList) && userPerssionByMenuVOList.size() > 0) {
                List<Long> userIdList = userPerssionByMenuVOList.stream().map(User::getId).collect(Collectors.toList());
                //指定任务人接收消息
                //这里没有批量的，所以暂时只好轮训接收
                userIdList.forEach(k -> {
                    Messagerecipient messagerecipient = new Messagerecipient();
                    messagerecipient.setMessageId(resultId);
                    messagerecipient.setRecipient(k);
                    messagerecipient.setFlag("0");
                    messagerecipientService.save(messagerecipient);
                });
            }
        }
        return true;
    }

    @Override
    @SneakyThrows
    public boolean getShouldZjCount() {
        //获取当前月份
        String yearMonth = Date8Util.getYearMonth();
        int shouldNum = qfkhjbxxService.count(Wrappers.<Qfkhjbxx>lambdaQuery().le(Qfkhjbxx::getXcZjrq , yearMonth));
        Messagepush messagepush = new Messagepush();
        messagepush.setMessage(ZscbMessage.SHOULD_ZJ_METER.getMessage());
        messagepush.setTopic(ZscbMessage.SHOULD_ZJ_METER.getTopic() + shouldNum);
        messagepush.setType(ZscbMessage.SHOULD_ZJ_METER.getType());
        messagepush.setTableName(ZscbMessage.SHOULD_ZJ_METER.getTableName());
        messagepush.setRouteIp(meterZjRoute);
        //这里暂时先默认写一个ID，具体创建这个消息的是谁，后面问下需求
        messagepush.setCreateUser(1L);
        boolean save = messagepushService.save(messagepush);
        if (save) {
            Long resultId = messagepush.getId();
            //获取菜单下的用户ID
            ResultRespone<List<UserPerssionByMenuVO>> resultRespone = userService.getUserAndDepatmentByPerssion(ZscbMessage.SHOULD_ZJ_METER.getMenuId());
            List<UserPerssionByMenuVO> userPerssionByMenuVOList = resultRespone.getResult();
            if (StringUtil.isNotNull(userPerssionByMenuVOList) && userPerssionByMenuVOList.size() > 0) {
                List<Long> userIdList = userPerssionByMenuVOList.stream().map(User::getId).collect(Collectors.toList());
                //指定任务人接收消息
                //这里没有批量的，所以暂时只好轮训接收
                userIdList.forEach(k -> {
                    Messagerecipient messagerecipient = new Messagerecipient();
                    messagerecipient.setMessageId(resultId);
                    messagerecipient.setRecipient(k);
                    messagerecipient.setFlag("0");
                    messagerecipientService.save(messagerecipient);
                });
            }
        }
        return true;
    }
}
