package com.cm.websocket.service.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.utils.UUIDUtil;
import com.cm.websocket.dao.INoticeDao;
import com.cm.websocket.pojo.AppSocketMessage;
import com.cm.websocket.pojo.pos.NoticePO;
import com.cm.websocket.pojo.vos.message.NoticeVO;
import com.cm.websocket.service.INoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: NoticeServiceImpl
 * @Description: 消息通知
 * @Author: wanggeng
 * @Date: 2021/3/29 4:36 下午
 * @Version: 1.0
 */
@Service
public class NoticeServiceImpl extends AbstractService implements INoticeService {

    @Autowired
    private INoticeDao noticeDao;

    @Override
    public void save(String userId, NoticeVO.Notice notice) throws Exception {
        if (StringUtils.isBlank(userId)) {
            return;
        }
        if (StringUtils.isBlank(notice.getSystem())) {
            return;
        }
        if (StringUtils.isBlank(notice.getServiceId())) {
            return;
        }
        List<NoticePO> noticePOs = listPO(notice.getSystem(), notice.getModule(), notice.getMenu(), notice.getServiceId(), userId);
        if (!noticePOs.isEmpty()) {
            LOG.debug("通知已经存在");
            return;
        }
        Map<String, Object> params = getHashMap(16);
        params.put("noticeId", UUIDUtil.getUUID());
        params.put("userId", userId);
        params.put("noticeTitle", notice.getTitle());
        params.put("noticeMsg", notice.getMsg());
        params.put("noticeTarget", notice.getTarget());
        params.put("noticeSystem", notice.getSystem());
        params.put("noticeModule", notice.getModule());
        params.put("noticeMenu", notice.getMenu());
        params.put("noticeServiceId", notice.getServiceId());
        params.put("isHandle", 0);
        setSaveInfoByUserId(params, "1");
        noticeDao.save(params);
    }

    @Override
    public void updateHandleStatus(String userId, AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody) {
        if (StringUtils.isBlank(countNeedToDealWithBody.getSystem())) {
            return;
        }
        if (StringUtils.isBlank(countNeedToDealWithBody.getServiceId())) {
            return;
        }
        Map<String, Object> params = getHashMap(6);
        params.put("isHandle", 1);
        params.put("noticeSystem", countNeedToDealWithBody.getSystem());
        params.put("noticeServiceId", countNeedToDealWithBody.getServiceId());
        setUpdateInfoByUserId(params, userId);
        noticeDao.updateHandle(params);
    }

    @Override
    public NoticePO getPO(Map<String, Object> params) {
        return noticeDao.getPO(params);
    }

    @Override
    public NoticePO getPO(String serviceId, String userId) {
        Map<String, Object> params = getHashMap(4);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return getPO(params);
    }

    @Override
    public NoticePO getPO(String system, String serviceId, String userId) {
        Map<String, Object> params = getHashMap(6);
        params.put("noticeSystem", system);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return getPO(params);
    }

    @Override
    public NoticePO getPO(String system, String module, String serviceId, String userId) {
        Map<String, Object> params = getHashMap(8);
        params.put("noticeSystem", system);
        params.put("noticeModule", module);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return getPO(params);
    }

    @Override
    public NoticePO getPO(String system, String module, String menu, String serviceId, String userId) {
        Map<String, Object> params = getHashMap(10);
        params.put("noticeSystem", system);
        params.put("noticeModule", module);
        params.put("noticeMenu", menu);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return getPO(params);
    }

    @Override
    public List<NoticePO> listPO(Map<String, Object> params) {
        return noticeDao.listPO(params);
    }

    @Override
    public List<NoticePO> listPO(String serviceId, String userId) {
        Map<String, Object> params = getHashMap(4);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return listPO(params);
    }

    @Override
    public List<NoticePO> listPO(String system, String serviceId, String userId) {
        Map<String, Object> params = getHashMap(6);
        params.put("noticeSystem", system);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return listPO(params);
    }

    @Override
    public List<NoticePO> listPO(String system, String module, String serviceId, String userId) {
        Map<String, Object> params = getHashMap(8);
        params.put("noticeSystem", system);
        params.put("noticeModule", module);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return listPO(params);
    }

    @Override
    public List<NoticePO> listPO(String system, String module, String menu, String serviceId, String userId) {
        Map<String, Object> params = getHashMap(10);
        params.put("noticeSystem", system);
        params.put("noticeModule", module);
        params.put("noticeMenu", menu);
        params.put("noticeServiceId", serviceId);
        params.put("userId", userId);
        return listPO(params);
    }

    @Override
    public List<AppSocketMessage.CountNeedToDealWithBody> listNoticeCount(String userId, AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody) {
        List<AppSocketMessage.CountNeedToDealWithBody> noticeCounts = new ArrayList<>();
        Map<String, Object> params = getHashMap(10);
        params.put("userId", userId);
        params.put("isHandle", 0);
        if (StringUtils.isBlank(countNeedToDealWithBody.getSystem())) {
            // 系统为空，按系统统计
            List<String> systems = listSystems(userId);
            for (String system : systems) {
                params.put("noticeSystem", system);
                AppSocketMessage.CountNeedToDealWithBody systemCount = new AppSocketMessage.CountNeedToDealWithBody();
                systemCount.setSystem(system);
                systemCount.setCount(count(params));
                noticeCounts.add(systemCount);
            }
        } else if (StringUtils.isBlank(countNeedToDealWithBody.getModule())) {
            // 模块为空，按模块统计
            List<String> modules = listModules(userId, countNeedToDealWithBody.getSystem());
            params.put("noticeSystem", countNeedToDealWithBody.getSystem());
            for (String module : modules) {
                AppSocketMessage.CountNeedToDealWithBody systemCount = new AppSocketMessage.CountNeedToDealWithBody();
                systemCount.setSystem(countNeedToDealWithBody.getSystem());
                systemCount.setModule(module);
                params.put("noticeModule", module);
                systemCount.setCount(count(params));
                noticeCounts.add(systemCount);
            }
        } else if (StringUtils.isBlank(countNeedToDealWithBody.getMenu())) {
            // 菜单为空，按菜单统计
            List<String> menus = listMenus(userId, countNeedToDealWithBody.getSystem(), countNeedToDealWithBody.getModule());
            params.put("noticeSystem", countNeedToDealWithBody.getSystem());
            params.put("noticeModule", countNeedToDealWithBody.getModule());
            for (String menu : menus) {
                AppSocketMessage.CountNeedToDealWithBody systemCount = new AppSocketMessage.CountNeedToDealWithBody();
                systemCount.setSystem(countNeedToDealWithBody.getSystem());
                systemCount.setModule(countNeedToDealWithBody.getModule());
                systemCount.setMenu(menu);
                params.put("noticeMenu", menu);
                systemCount.setCount(count(params));
                noticeCounts.add(systemCount);
            }
        } else {
            // 都不为空，具体菜单数量
            AppSocketMessage.CountNeedToDealWithBody systemCount = new AppSocketMessage.CountNeedToDealWithBody();
            systemCount.setSystem(countNeedToDealWithBody.getSystem());
            systemCount.setModule(countNeedToDealWithBody.getModule());
            systemCount.setMenu(countNeedToDealWithBody.getMenu());
            params.put("noticeSystem", countNeedToDealWithBody.getSystem());
            params.put("noticeModule", countNeedToDealWithBody.getModule());
            params.put("noticeModule", countNeedToDealWithBody.getModule());
            params.put("noticeMenu", countNeedToDealWithBody.getMenu());
            systemCount.setCount(count(params));
            noticeCounts.add(systemCount);
        }
        return noticeCounts;
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = noticeDao.count(params);
        return count == null ? 0 : count;
    }

    @Override
    public void deleteByServiceIds(List<String> serviceIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("serviceIds", serviceIds);
        noticeDao.delete(params);
    }

    @Override
    public List<NoticePO> listPO(List<String> serviceIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("serviceIds", serviceIds);
        return listPO(params);
    }

    /**
     * 系统列表
     *
     * @param userId
     * @return
     */
    private List<String> listSystems(String userId) {
        Map<String, Object> params = getHashMap(2);
        params.put("userId", userId);
        return noticeDao.listSystems(params);
    }

    /**
     * 模块列表
     *
     * @param userId
     * @return
     */
    private List<String> listModules(String userId, String noticeSystem) {
        Map<String, Object> params = getHashMap(4);
        params.put("userId", userId);
        params.put("noticeSystem", noticeSystem);
        return noticeDao.listModules(params);
    }

    /**
     * 菜单列表
     *
     * @param userId
     * @return
     */
    private List<String> listMenus(String userId, String noticeSystem, String noticeModule) {
        Map<String, Object> params = getHashMap(6);
        params.put("userId", userId);
        params.put("noticeSystem", noticeSystem);
        params.put("noticeModule", noticeModule);
        return noticeDao.listMenus(params);
    }

}
