package com.tuoheng.admin.service.accident;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.AccidentRead;
import com.tuoheng.admin.enums.AccidentStatusEnum;
import com.tuoheng.admin.enums.FlagEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.mapper.AccidentReadMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author ChengWang
 * @Date 2023/3/14
 */
@Slf4j
@Service
public class AccidentNoticeService {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private AccidentReadMapper accidentReadMapper;


    /**
     * 告警弹窗通知下发
     * @return
     */
    public JsonResult notice() {
        //当前登录用户
        String tenantId = CurrentUserUtil.getTenantId();
        String userId = CurrentUserUtil.getUserId();
        if(StringUtils.isEmpty(userId)){
            throw new ServiceException("当前用户id为空");
        }
        if(StringUtils.isEmpty(tenantId)){
            throw new ServiceException("当前用户的租户id为空");
        }
        //查询应急表是否有应急数据
        List<Accident> accidentList = accidentMapper.selectList(new LambdaQueryWrapper<Accident>()
                .eq(Accident::getTenantId, tenantId)
                .eq(Accident::getStatus, AccidentStatusEnum.UNTREATED.getCode())
                .eq(Accident::getFlag, FlagEnum.INSPECTION_ACCIDENT.getCode())
                .eq(Accident::getMark, MarkEnum.VALID.getCode())
                .orderByAsc(Accident::getCreateTime));
        if(CollectionUtils.isEmpty(accidentList) || accidentList.size() == 0){
            return JsonResult.success("巡检任务无应急记录产生");
        }
        //当前用户对应的应急记为未读才出现弹窗
        List<Accident>  oldList = new ArrayList<>();
        List<Accident>  newList = new ArrayList<>();
        //获取当前用户下对应的应急已读数据
        List<AccidentRead> accidentReadList = accidentReadMapper.selectList(new LambdaQueryWrapper<AccidentRead>()
                .eq(AccidentRead::getUserId, userId)
                .eq(AccidentRead::getStatus, 3));
        if(CollectionUtils.isEmpty(accidentReadList) || accidentReadList.size()==0){
            return JsonResult.success(accidentList);
        }
        //数据刷选
        for (AccidentRead accidentRead : accidentReadList) {
            //获取预警记录表中的预警id做比较
            for (Accident accident : accidentList) {
                if (accident.getId().equals(accidentRead.getAccidentId())){
                    oldList.add(accident);
                }
            }
        }
        List<String> accidentIdList = oldList.stream().map(o -> o.getId()).collect(Collectors.toList());

        if(CollectionUtils.isEmpty(accidentIdList) || accidentIdList.size() == 0){
            //查询当前用户租户对应的预警数据
            return JsonResult.success(accidentList);
        }
        newList = accidentMapper.selectList(Wrappers.<Accident>lambdaQuery()
                .in(Accident::getId, accidentIdList));
        accidentList.removeAll(newList);
        //下面可以同样实现，只是要查数据库接口影响性能
//        newList = accidentMapper.selectList(Wrappers.<Accident>lambdaQuery()
//                .notIn(Accident::getId, accidentIdList)
//                .eq(Accident::getTenantId, tenantId)
//                .eq(Accident::getStatus, AccidentStatusEnum.UNTREATED.getCode())
//                .eq(Accident::getFlag, FlagEnum.INSPECTION_ACCIDENT.getCode())
//                .eq(Accident::getMark, MarkEnum.VALID.getCode()));
//
        //对应list集合排序
        accidentList = accidentList.stream().sorted(Comparator.comparing(Accident::getCreateTime)).collect(Collectors.toList());

        return JsonResult.success(accidentList);
    }
}
