package cn.pluss.platform.notice.impl;

import cn.pluss.platform.entity.NoticeReadRecord;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.mapper.NoticeReadRecordMapper;
import cn.pluss.platform.notice.NoticeReadRecordService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Djh
 * @since 2021-04-19
 */
@Service
public class NoticeReadRecordServiceImpl extends ServiceImpl<NoticeReadRecordMapper, NoticeReadRecord> implements NoticeReadRecordService {

    @Setter(onMethod_ = {@Autowired})
    private UserAppService userAppService;

    @Override
    public Map<String, Object> getAppNoReadNoticeCount() {
        String billCountStr = "billCount";
        String profitCountStr = "profitCount";
        String systemCountStr = "systemCount";
        String countStr = "count";
        String typeStr = "type";

        UserApp userApp = userAppService.queryUserAppByToken();
        Integer billCount = baseMapper.selectBillCount(userApp.getUserId() + "");
        List<Map<String, Object>> noticeCountList = baseMapper.selectNoticeCount(userApp.getUserId() + "");

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(billCountStr, billCount);

        for (Map<String, Object> map : noticeCountList) {
            if ("0".equals(map.get(typeStr) + "")) {
                resultMap.put(profitCountStr, Integer.parseInt(map.get(countStr) + ""));
            }

            if ("1".equals(map.get(typeStr) + "")) {
                resultMap.put(systemCountStr, Integer.parseInt(map.get(countStr) + ""));
            }
        }

        resultMap.putIfAbsent(profitCountStr, 0);
        resultMap.putIfAbsent(systemCountStr, 0);

        return resultMap;
    }

    @Override
    public Map<String, Object> getAppNoReadArticleCount() {
        String typeStr = "type";
        String countStr = "count";
        String recommendCount = "recommendCount";
        String recommendCount2 = "recommendCount2";
        String informationCount = "informationCount";
        String activityCount = "activityCount";

        UserApp userApp = userAppService.queryUserAppByToken();
        List<Map<String, Object>> articleCountList = baseMapper.selectArticleCount(userApp.getUserId() + "");

        Map<String, Object> resultMap = new HashMap<>();
        for (Map<String, Object> map : articleCountList) {
            if (recommendCount.equals(map.get(typeStr) + "")) {
                resultMap.put(recommendCount, Integer.parseInt(map.get(countStr) + ""));
            }

            if (recommendCount2.equals(map.get(typeStr) + "")) {
                resultMap.put(recommendCount2, Integer.parseInt(map.get(countStr) + ""));
            }

            if (informationCount.equals(map.get(typeStr) + "")) {
                resultMap.put(informationCount, Integer.parseInt(map.get(countStr) + ""));
            }

            if (activityCount.equals(map.get(typeStr) + "")) {
                resultMap.put(activityCount, Integer.parseInt(map.get(countStr) + ""));
            }
        }

        resultMap.putIfAbsent(recommendCount, 0);
        resultMap.putIfAbsent(informationCount, 0);
        resultMap.putIfAbsent(activityCount, 0);

        return resultMap;
    }

    @Override
    public void clearReadFlag(String typeFirst, String typeSecond) {
        UserApp userApp = userAppService.queryUserAppByToken();
        switch (typeFirst) {
            case "1":
                baseMapper.updateBillNoticeRead(userApp.getUserId().toString());
                break;
            case "2":
                baseMapper.updateNoticeRead(userApp.getUserId().toString(), typeSecond);
                break;
            case "3":
                baseMapper.updateEventInfoRead(userApp.getUserId().toString(), typeSecond);
                break;
            default:
        }
    }
}
