/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2023/4/26
 */
package org.springblade.modules.warn.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.etl.dto.EtlWarningInfoDTO;
import org.springblade.etl.entity.EtlWarningInfoEntity;
import org.springblade.etl.service.IEtlWarningService;
import org.springblade.modules.warn.entity.InsideWarnInfoEntity;
import org.springblade.modules.warn.entity.OutsideWarnInfoEntity;
import org.springblade.modules.warn.entity.OutsideWarnTimeEntity;
import org.springblade.modules.warn.entity.WarnResponseInfoEntity;
import org.springblade.modules.warn.service.IInsideWarnInfoService;
import org.springblade.modules.warn.service.IOutsideWarnInfoService;
import org.springblade.modules.warn.service.IOutsideWarnTimeService;
import org.springblade.modules.warn.service.IWarnResponseInfoService;
import org.springblade.modules.warn.vo.WarnDetailInfoVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 * Date:2023/4/26
 * Title:ETL接口
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/warn")
@AllArgsConstructor
@Api(value = "宏图预警信息查询接口", tags = "直接从宏图数据库查询")
@Slf4j
public class WarnInfoController extends BladeController {
    @Resource
    private IEtlWarningService etlWarningService;

    @Resource
    private IInsideWarnInfoService insideWarnInfoService;

    @Resource
    private IOutsideWarnTimeService outsideWarnTimeService;

    @Resource
    private IOutsideWarnInfoService outsideWarnInfoService;

    @Resource
    private IWarnResponseInfoService warnResponseInfoService;

    @GetMapping("/detail/{warnId}")
    @ApiOperationSupport(order = 1)
    public R<WarnDetailInfoVO> getWarnDetail(@PathVariable(name = "warnId") String warnId) {
        if (Func.isNull(warnId)) {
            return R.fail("参数错误");
        }
        WarnDetailInfoVO vo = new WarnDetailInfoVO();
        vo.setWarnId(warnId);
        EtlWarningInfoDTO etlWarningInfoDTO = new EtlWarningInfoDTO();
        etlWarningInfoDTO.setWarnId(warnId);
        EtlWarningInfoEntity etlWarningInfoEntity = etlWarningService.getByWarnId(etlWarningInfoDTO);
        if (Func.isNull(etlWarningInfoEntity)) {
            return R.fail("查询失败");
        }
        vo.setWarnName(etlWarningInfoEntity.getWarnName());
        vo.setWarnTime(etlWarningInfoEntity.getWarnTime());
        vo.setWarnInfo(etlWarningInfoEntity.getWarnInfo());

        InsideWarnInfoEntity insideWarnInfoDTO = new InsideWarnInfoEntity();
        insideWarnInfoDTO.setWarnId(warnId);
        List<InsideWarnInfoEntity> insideWarnInfoEntityList = insideWarnInfoService.selectList(insideWarnInfoDTO);
        if (Func.notNull(insideWarnInfoEntityList)) {
            vo.setInsideWarnInfoList(insideWarnInfoEntityList);
        } else {
            vo.setInsideWarnInfoList(new ArrayList<>(0));
        }

        OutsideWarnTimeEntity outsideWarnTimeDTO = new OutsideWarnTimeEntity();
        outsideWarnTimeDTO.setWarnId(warnId);
        List<OutsideWarnTimeEntity> outsideWarnTimeEntityList = outsideWarnTimeService.selectList(outsideWarnTimeDTO);
        if (Func.notNull(outsideWarnTimeEntityList) && outsideWarnTimeEntityList.size() > 0) {
            vo.setOusideWarnStatus(1);
            vo.setExtTime(outsideWarnTimeEntityList.get(0).getExtTime());
        } else {
            vo.setOusideWarnStatus(0);
        }
        if (vo.getOusideWarnStatus() == 1) {
            OutsideWarnInfoEntity outsideWarnInfoDTO = new OutsideWarnInfoEntity();
            outsideWarnInfoDTO.setWarnId(warnId);
            List<OutsideWarnInfoEntity> outsideWarnInfoEntityList = outsideWarnInfoService.selectList(outsideWarnInfoDTO);
            if (Func.notNull(outsideWarnInfoEntityList)) {
                vo.setOutsideWarnInfoList(outsideWarnInfoEntityList);
            } else {
                vo.setOutsideWarnInfoList(new ArrayList<>(0));
            }
        }

        WarnResponseInfoEntity warnResponseInfoDTO = new WarnResponseInfoEntity();
        warnResponseInfoDTO.setWarnId(warnId);
        List<WarnResponseInfoEntity> warnResponseInfoEntityList = warnResponseInfoService.selectList(warnResponseInfoDTO);
        if (Func.notNull(warnResponseInfoEntityList) && warnResponseInfoEntityList.size() > 0) {
            vo.setReplyTm(warnResponseInfoEntityList.get(0).getReplyTm());
            vo.setWarnResponseInfoList(warnResponseInfoEntityList);
            vo.setWarnResponseStatus(1);
        } else {
            vo.setWarnResponseStatus(0);
            vo.setWarnResponseInfoList(new ArrayList<>(0));
        }
        return R.data(vo);
    }
}
