/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/10/19
 */
package org.springblade.modules.baseinfo.monitorsite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointRelEntity;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointRelService;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointService;
import org.springblade.modules.baseinfo.monitorsite.dto.MonitorSiteInfoDTO;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteInfoEntity;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteRelEntity;
import org.springblade.modules.baseinfo.monitorsite.mapper.MonitorSiteMapper;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteRelService;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteService;
import org.springblade.modules.baseinfo.monitorsite.vo.MonitorSiteInfoVO;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.rtumanage.entity.RtuStatusEntity;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springframework.stereotype.Service;

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

/***
 * Date:2023/10/19
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Service
public class MonitorSiteServiceImpl extends BaseServiceImpl<MonitorSiteMapper, MonitorSiteInfoEntity> implements IMonitorSiteService {

    @Resource
    private IMonitorPointService monitorPointService;

    @Resource
    private IMonitorPointRelService pointRelService;

    @Resource
    private IMonitorSiteRelService siteRelService;

    @Resource
    private IRtuBaseInfoService rtuBaseInfoService;

    @Resource
    private IRtuManageService rtuManageService;


    @Override
    public IPage<MonitorSiteInfoVO> selectPage(IPage<MonitorSiteInfoVO> page, MonitorSiteInfoEntity dto) {
        return page.setRecords(baseMapper.selectPage(page, dto));
    }

    @Override
    public List<MonitorSiteInfoEntity> selectAll(MonitorSiteInfoEntity dto) {
        return baseMapper.selectAll(dto);
    }

    @Override
    public Long getMonitorSiteCount(MonitorSiteInfoDTO dto) {
        return baseMapper.getMonitorSiteCount(dto);
    }

    @Override
    public boolean removeRelPoints(String siteCode) {
        //删除点位
        LambdaQueryWrapper<MonitorPointRelEntity> wrapper = Wrappers.<MonitorPointRelEntity>query().lambda();
        wrapper.eq(MonitorPointRelEntity::getIsDeleted, 0);
        wrapper.eq(MonitorPointRelEntity::getWiustCd, siteCode);
        List<MonitorPointRelEntity> list = pointRelService.list(wrapper);
        for (MonitorPointRelEntity entity : list) {
            monitorPointService.removePoint(entity.getMpCd());
        }
        return true;
    }

    @Override
    public boolean removeSite(String wiuCode) {
        LambdaQueryWrapper<MonitorSiteRelEntity> wrapper = Wrappers.<MonitorSiteRelEntity>query().lambda();
        wrapper.eq(MonitorSiteRelEntity::getIsDeleted, 0);
        wrapper.eq(MonitorSiteRelEntity::getWiuCd, wiuCode);
        List<MonitorSiteRelEntity> relEntityList = siteRelService.list(wrapper);
        for (MonitorSiteRelEntity relEntity : relEntityList) {
            LambdaQueryWrapper<MonitorSiteInfoEntity> wrapper2 = Wrappers.<MonitorSiteInfoEntity>query().lambda();
            wrapper2.eq(MonitorSiteInfoEntity::getIsDeleted, 0);
            wrapper2.eq(MonitorSiteInfoEntity::getWiustCd, relEntity.getWiustCd());
            List<MonitorSiteInfoEntity> list = this.list(wrapper2);
            for (MonitorSiteInfoEntity entity : list) {
                //删除站点下的点位
                this.removeRelPoints(entity.getWiustCd());
                //删除RTU
                RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.rtuInfoByRtuCode(entity.getWiustCd());
                if (Func.notNull(rtuInfoEntity)) {
                    rtuBaseInfoService.deleteLogic(Func.toLongList(Func.toStr(rtuInfoEntity.getId())));
                }
                //删除RTU状态
//                LambdaQueryWrapper<RtuStatusEntity> wrapper3 = Wrappers.<RtuStatusEntity>query().lambda();
//                wrapper3.eq(RtuStatusEntity::getIsDeleted, 0);
//                wrapper3.eq(RtuStatusEntity::getRtuCode, entity.getWiustCd());
//                RtuStatusEntity rtuStatusEntity = rtuManageService.getOne(wrapper3);
//                if (Func.notNull(rtuStatusEntity)) {
//                    rtuManageService.deleteLogic(Func.toLongList(Func.toStr(rtuStatusEntity.getId())));
//                }
                //删除站点
                this.deleteLogic(Func.toLongList(Func.toStr(entity.getId())));
            }
            //删除站点关联
            siteRelService.removeRel(relEntity.getWiustCd());
        }
        return true;
    }
}
