package com.tuoheng.admin.service.warningrecord.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.WarningConverMapper;
import com.tuoheng.admin.conver.WarningRecordConverMapper;
import com.tuoheng.admin.entity.domain.Question;
import com.tuoheng.admin.entity.domain.ThMission;
import com.tuoheng.admin.entity.domain.Warning;
import com.tuoheng.admin.entity.domain.WarningRecord;
import com.tuoheng.admin.entity.request.warningrecord.QueryWarningRecordListByWarningIdRequest;
import com.tuoheng.admin.entity.vo.warning.WarningListVo;
import com.tuoheng.admin.entity.vo.warning.WarningRecordVo;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.MarkTypeEnum;
import com.tuoheng.admin.enums.TaskStatusEnum;
import com.tuoheng.admin.enums.UpdateOrCreateEnum;
import com.tuoheng.admin.mapper.ThMissionMapper;
import com.tuoheng.admin.mapper.WarningMapper;
import com.tuoheng.admin.mapper.WarningRecordMapper;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.system.entity.User;
import com.tuoheng.system.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 查询预警记录业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-22
 */
@Slf4j
@Service
public class QueryWarningRecordListByWarningIdService {

    @Autowired
    private WarningMapper warningMapper;

    @Autowired
    private WarningRecordMapper warningRecordMapper;

    @Autowired
    private ThMissionMapper missionMapper;

    public JsonResult getListByWarningId(QueryWarningRecordListByWarningIdRequest request) {
//        log.info("进入根据预警ID查询预警记录列表业务");
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();
        request.setTenantId(tenantId);

        JsonResult result = this.check(tenantId, request);
        if (0 != result.getCode()) {
            log.info("进入根据预警ID查询预警记录列表业务：校验失败：{}", result.getMsg());
            return result;
        }
        List<WarningRecord> warningRecordList = warningRecordMapper.getList(request);
        if (CollectionUtil.isEmpty(warningRecordList)) {
            log.info("预警记录列表数据为空");
        }

        // 构造返回结果对象
        List<WarningRecordVo> warningRecordVoList = this.buildWarningRecordVoList(warningRecordList);
        return JsonResult.success(warningRecordVoList);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param request
     * @return
     */
    private JsonResult check(Integer tenantId, QueryWarningRecordListByWarningIdRequest request) {
        // 判断预警id是否为空
        if (ObjectUtil.isNull(request.getWarningId())) {
            throw new ServiceException("预警ID为空");
        }
        Warning warning = warningMapper.selectOne(new LambdaQueryWrapper<Warning>()
                .eq(Warning::getId, request.getWarningId())
                .eq(Warning::getTenantId, tenantId)
                .eq(Warning::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(warning)) {
            throw new ServiceException("预警信息不存在");
        }
        return JsonResult.success();
    }

    private List<WarningRecordVo> buildWarningRecordVoList(List<WarningRecord> warningRecordList) {
        List<WarningRecordVo> warningRecordVoList = WarningRecordConverMapper.INSTANCE.fromWarningRecordListToWarningRecordListVoList(warningRecordList);
        ThMission mission;
        for (WarningRecordVo warningRecordVo : warningRecordVoList) {
            mission = missionMapper.selectOne(new LambdaQueryWrapper<ThMission>()
                    .eq(ThMission::getId, warningRecordVo.getEmergencyMissionId())
                    .eq(ThMission::getMark, MarkTypeEnum.VALID.getCode()));
            warningRecordVo.setStatus(mission.getStatus());
        }
        return warningRecordVoList;
    }

}
