/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/28
 */
package org.springblade.modules.yjxt.business.rain.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.region.tree.entity.RegionTreeNodeEntity;
import org.springblade.modules.baseinfo.region.yw.service.IRegionInfoService;
import org.springblade.modules.baseinfo.stbase.entity.MvAttStBaseEntity;
import org.springblade.modules.baseinfo.stbase.service.IMvAttStBaseService;
import org.springblade.modules.business.data.dto.RtuDataRainDTO;
import org.springblade.modules.yjxt.base.map.entity.DangerAreaEntity;
import org.springblade.modules.yjxt.base.map.service.IDangerAreaService;
import org.springblade.modules.yjxt.business.rain.service.IYjRainDataService;
import org.springblade.modules.yjxt.business.rain.vo.YjRainDataVO;
import org.springblade.modules.yjxt.business.warn.entity.OriginalWarningInfoEntity;
import org.springblade.modules.yjxt.business.warn.service.IOriginalWarningService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


/***
 * Date:2022/8/28
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/yj/rain")
@AllArgsConstructor
@Api(value = "降水实时数据管理", tags = "测站实时数据管理")
public class YjRainDataController extends BladeController {
    private final IYjRainDataService rainDataService;
    private final IDeptRegionService deptRegionService;
    private final IRegionInfoService regionService;
    private final IMvAttStBaseService mvAttStBaseService;
    private final IDangerAreaService dangerAreaService;
    private final IOriginalWarningService originalWarningService;

    /**
     * 分页
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "rainCountType", value = "雨量统计类型", paramType = "query", dataType = "int"),
    })
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页")
    public R<IPage<YjRainDataVO>> yjPage(@ApiIgnore RtuDataRainDTO rtuDataRainDTO, Query query) {
        BladeUser user = AuthUtil.getUser();
        if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
            if (Func.isNull(rtuDataRainDTO.getAdCode())) {
                rtuDataRainDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
            }
        } else {
            if (Func.isNull(rtuDataRainDTO.getAdCode())) {
                LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
                wrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
                wrapper.last("limit 1");
                DeptRegionEntity deptRegionEntity = deptRegionService.getOne(wrapper);
                if (Func.isNull(deptRegionEntity)) {
                    return R.fail("查询失败");
                }
                rtuDataRainDTO.setAdCode(deptRegionEntity.getAdcd());
            }
        }
        if (Func.notNull(rtuDataRainDTO.getAdCode())) {
            RegionTreeNodeEntity node = this.regionService.getRegionNode(user.getRoleName(), user.getDeptId(), rtuDataRainDTO.getAdCode());
            if (Func.notNull(node)) {
                rtuDataRainDTO.setExcludeCode(node.getExclude());
            }
        }
        IPage<YjRainDataVO> pages = rainDataService.selectYjPage(Condition.getPage(query), rtuDataRainDTO);
        List<YjRainDataVO> list = pages.getRecords();
        for (YjRainDataVO vo : list) {
            if (Func.notNull(rtuDataRainDTO.getRainCountType())) {
                if (rtuDataRainDTO.getRainCountType() == 1) {
                    vo.setIntvLable("近1小时");
                    vo.setDrp(vo.getH1drp());
                    vo.setTm(vo.getH1tm());
                } else if (rtuDataRainDTO.getRainCountType() == 3) {
                    vo.setIntvLable("近3小时");
                    vo.setDrp(vo.getH3drp());
                    vo.setTm(vo.getH3tm());
                } else if (rtuDataRainDTO.getRainCountType() == 6) {
                    vo.setIntvLable("近6小时");
                    vo.setDrp(vo.getH6drp());
                    vo.setTm(vo.getH6tm());
                } else if (rtuDataRainDTO.getRainCountType() == 12) {
                    vo.setIntvLable("近12小时");
                    vo.setDrp(vo.getH12drp());
                    vo.setTm(vo.getH12tm());
                } else if (rtuDataRainDTO.getRainCountType() == 24) {
                    vo.setIntvLable("近24小时");
                    vo.setDrp(vo.getH24drp());
                    vo.setTm(vo.getH24tm());
                } else {
                    if (vo.getIntv() != null && vo.getIntv() == 0.05) {
                        vo.setIntvLable("5分钟");
                    } else if (vo.getIntv() != null && vo.getIntv() == 1.0) {
                        vo.setIntvLable("小时");
                    }
                }
            } else {
                if (vo.getIntv() != null && vo.getIntv() == 0.05) {
                    vo.setIntvLable("5分钟");
                } else if (vo.getIntv() != null && vo.getIntv() == 1.0) {
                    vo.setIntvLable("小时");
                }
            }
            LambdaQueryWrapper<MvAttStBaseEntity> rtuwrapper = Wrappers.<MvAttStBaseEntity>query().lambda();
            rtuwrapper.eq(MvAttStBaseEntity::getStCode, vo.getRtuCode());
            rtuwrapper.last("limit 1");
            MvAttStBaseEntity rtuInfoEntity = mvAttStBaseService.getOne(rtuwrapper);
            if (Func.notNull(rtuInfoEntity)) {
                vo.setLng(Func.toStr(rtuInfoEntity.getStLong()));
                vo.setLat(Func.toStr(rtuInfoEntity.getStLat()));
                //查询预警信息
                LambdaQueryWrapper<OriginalWarningInfoEntity> warnwrapper = Wrappers.<OriginalWarningInfoEntity>query().lambda();
                warnwrapper.eq(OriginalWarningInfoEntity::getStcd, rtuInfoEntity.getStCode());
                warnwrapper.last("limit 1");
                OriginalWarningInfoEntity warningInfoEntity= originalWarningService.getOne(warnwrapper);
                if (Func.notNull(warningInfoEntity)){
                    vo.setIsWarn(1);
                    vo.setWarnId(warningInfoEntity.getWarnId());
                    vo.setWarnName(warningInfoEntity.getWarnName());
                }else{
                    vo.setIsWarn(0);
                }
                //查询危险区、转移路线等信息
                LambdaQueryWrapper<DangerAreaEntity> dangerareawrapper = Wrappers.<DangerAreaEntity>query().lambda();
                dangerareawrapper.eq(DangerAreaEntity::getStCode, rtuInfoEntity.getStCode());
              //  dangerareawrapper.last("limit 1");
                List<DangerAreaEntity> dangerAreaEntityList = dangerAreaService.list(dangerareawrapper);
                if (Func.notNull(dangerAreaEntityList)) {
                    vo.setIsLinkDanger(1);
                    vo.setDangerAreaList(dangerAreaEntityList);
                   // vo.setDangerAreaPid(dangerAreaEntity.getDangerAreaPid());
                   // vo.setDangerAreaName(dangerAreaEntity.getDangerAreaName());
//                    if (Func.notNull(dangerAreaEntity.getAdcd())) {
//                        //转移路线
//                        DangerAreaLeaveLineDTO lineDTO = new DangerAreaLeaveLineDTO();
//                        lineDTO.setAdcd(dangerAreaEntity.getAdcd());
//                        List<DangerAreaLeaveLineVO> lineVOList = dangerAreaLeaveLineService.selectAdDangerArea(lineDTO);
//                        if (Func.notNull(lineVOList)) {
//                            vo.setLeaveLines(lineVOList);
//                        } else {
//                            vo.setLeaveLines(new ArrayList<>(0));
//                        }
//                    } else {
//                        vo.setLeaveLines(new ArrayList<>(0));
//                    }
//                    //群众注册
//                    DangerAreaResidentInfoDTO dangerAreaResidentInfoDTO = new DangerAreaResidentInfoDTO();
//                    dangerAreaResidentInfoDTO.setIsHouseholder(1);
//                    dangerAreaResidentInfoDTO.setDangerAreaPid(vo.getDangerAreaPid());
//                    List<DangerAreaResidentInfoEntity> familyList = residentService.getFamilyPoints(dangerAreaResidentInfoDTO);
//                    if (Func.notNull(familyList) && familyList.size() > 0) {
//                        vo.setFamilyPoints(familyList);
//                        vo.setFamilyCount((long) familyList.size());
//                    } else {
//                        vo.setFamilyCount(0L);
//                    }
                } else {
                    vo.setIsLinkDanger(0);
                }
            }

        }
        return R.data(pages);
    }

}
