package com.tuoheng.admin.service.accident;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.AccidentRead;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.AccidentEnum;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.mapper.AccidentReadMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.vo.accident.AccidentPromptlyLookVo;
import com.tuoheng.admin.vo.accident.AccidentTimeAxisVo;
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.List;

/**
 * @Author ChengWang
 * @Date 2023/3/15
 */
@Slf4j
@Service
public class TimeAxisService {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private AccidentReadMapper accidentReadMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 事故时间轴
     * @param accidentId
     * @return
     */
    public JsonResult getTimeAxis(String accidentId) {
        //校验
        if(StringUtils.isEmpty(accidentId)){
            return JsonResult.error(AccidentEnum.ACCIDENT_ID_IS_NULL.getCode(),AccidentEnum.ACCIDENT_ID_IS_NULL.getMsg());
        }
        Accident accident = accidentMapper.selectById(accidentId);
        if(ObjectUtils.isEmpty(accident)){
            return JsonResult.error(AccidentEnum.ACCIDENT_DATA_IS_NULL.getCode(),AccidentEnum.ACCIDENT_DATA_IS_NULL.getMsg());
        }
        AccidentTimeAxisVo vo = new AccidentTimeAxisVo();
        //发生时间
        if(accident.getCreateTime() != null){
            vo.setCreateTime(accident.getCreateTime());
        }
        //立即查看时间 获取应急记录可读表每个用户对应此应急事故已标记可读的时间存list集合
        List<AccidentPromptlyLookVo> list = new ArrayList<>();;
        List<AccidentRead> accidentReadList = accidentReadMapper.selectList(Wrappers.<AccidentRead>lambdaQuery()
                .eq(AccidentRead::getAccidentId, accidentId));
        //遍历每个用户对应事故的时间
        for (AccidentRead accidentRead : accidentReadList) {
            AccidentPromptlyLookVo promptlyLookVo = new AccidentPromptlyLookVo();
            promptlyLookVo.setLookNowTime(accidentRead.getCreateTime());
            User user = userMapper.selectById(accidentRead.getUserId());
            if(StringUtils.isNotEmpty(user.getRealname())){
                promptlyLookVo.setRealName(user.getRealname());
            }
            list.add(promptlyLookVo);
        }
        vo.setLookTimeList(list);
        //核实时间
        if(accident.getVerificationTime() != null){
            vo.setVerificationTime(accident.getVerificationTime());
        }
        //上报时间
        if(accident.getReportTime() != null){
            vo.setReportTime(accident.getReportTime());
        }
        //忽略时间
        if(accident.getIgnoreTime() != null){
            vo.setIgnoreTime(accident.getIgnoreTime());
        }
        //无事故时间
        if(accident.getNoAccidentTime() != null){
            vo.setNoAccidentTime(accident.getNoAccidentTime());
        }
        //结束时间
        if(accident.getEndTime() !=null){
            vo.setEndTime(accident.getEndTime());
        }
        //立即查看已读人数
        vo.setNumber(accidentReadList.size());
        return JsonResult.success(vo);
    }
}
