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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointInfoEntity;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointRelEntity;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointRelService;
import org.springblade.modules.baseinfo.monitorpoint.vo.MonitorPointInfoVO;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteInfoEntity;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteRelEntity;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteRelService;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteService;
import org.springblade.modules.baseinfo.wateruseconsumer.entity.WaterUseConsumerInfoEntity;
import org.springblade.modules.baseinfo.wateruseconsumer.service.IWaterUseConsumerService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;


import java.util.Objects;

/***
 * Date:2023/10/19
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
public class MonitorPointWrapper extends BaseEntityWrapper<MonitorPointInfoEntity, MonitorPointInfoVO> {
    public static MonitorPointWrapper build() {
        return new MonitorPointWrapper();
    }

    private static IDeptService deptService;

    private static IDeptService getDeptService() {
        if (deptService == null) {
            deptService = SpringUtil.getBean(IDeptService.class);
        }
        return deptService;
    }

    private static IMonitorPointRelService monitorPointRelService;

    private static IMonitorPointRelService getMonitorPointRelService() {
        if (monitorPointRelService == null) {
            monitorPointRelService = SpringUtil.getBean(IMonitorPointRelService.class);
        }
        return monitorPointRelService;
    }

    private static IMonitorSiteRelService monitorSiteRelService;

    private static IMonitorSiteRelService getMonitorSiteRelService() {
        if (monitorSiteRelService == null) {
            monitorSiteRelService = SpringUtil.getBean(IMonitorSiteRelService.class);
        }
        return monitorSiteRelService;
    }

    private static IMonitorSiteService monitorSiteService;

    private static IMonitorSiteService getMonitorSiteService() {
        if (monitorSiteService == null) {
            monitorSiteService = SpringUtil.getBean(IMonitorSiteService.class);
        }
        return monitorSiteService;
    }


    private static IWaterUseConsumerService waterUseConsumerService;

    private static IWaterUseConsumerService getWaterUseConsumerService() {
        if (waterUseConsumerService == null) {
            waterUseConsumerService = SpringUtil.getBean(IWaterUseConsumerService.class);
        }
        return waterUseConsumerService;
    }

    @Override
    public MonitorPointInfoVO entityVO(MonitorPointInfoEntity entity) {
        MonitorPointInfoVO vo = Objects.requireNonNull(BeanUtil.copy(entity, MonitorPointInfoVO.class));
        if (vo.getWiustNm() == null) {
            LambdaQueryWrapper<MonitorPointRelEntity> wrapper = Wrappers.<MonitorPointRelEntity>query().lambda();
            wrapper.eq(MonitorPointRelEntity::getMpCd, entity.getMpCd());
            wrapper.last("limit 1");
            MonitorPointRelEntity relEntity = getMonitorPointRelService().getOne(wrapper);
            if (null != relEntity) {
                LambdaQueryWrapper<MonitorSiteInfoEntity> wrapper2 = Wrappers.<MonitorSiteInfoEntity>query().lambda();
                wrapper2.eq(MonitorSiteInfoEntity::getWiustCd, relEntity.getWiustCd());
                wrapper2.last("limit 1");
                MonitorSiteInfoEntity siteInfoEntity = getMonitorSiteService().getOne(wrapper2);
                if (null != siteInfoEntity) {
                    vo.setWiustCd(siteInfoEntity.getWiustCd());
                    vo.setWiustNm(siteInfoEntity.getWiustNm());
                }
            }
        }
        if (vo.getWiuNm() == null) {
            LambdaQueryWrapper<MonitorSiteRelEntity> wrapper = Wrappers.<MonitorSiteRelEntity>query().lambda();
            wrapper.eq(MonitorSiteRelEntity::getWiustCd, vo.getWiustCd());
            wrapper.last("limit 1");
            MonitorSiteRelEntity relEntity = getMonitorSiteRelService().getOne(wrapper);
            if (null != relEntity) {
                LambdaQueryWrapper<WaterUseConsumerInfoEntity> wrapper2 = Wrappers.<WaterUseConsumerInfoEntity>query().lambda();
                wrapper2.eq(WaterUseConsumerInfoEntity::getWiuCd, relEntity.getWiuCd());
                wrapper2.last("limit 1");
                WaterUseConsumerInfoEntity waterUseConsumerInfoEntity = getWaterUseConsumerService().getOne(wrapper2);
                if (null != waterUseConsumerInfoEntity) {
                    vo.setWiuCd(waterUseConsumerInfoEntity.getWiuCd());
                    vo.setWiuNm(waterUseConsumerInfoEntity.getWiuNm());
                }
            }
        }
        if (vo.getOrgName() == null) {
            LambdaQueryWrapper<WaterUseConsumerInfoEntity> wrapper2 = Wrappers.<WaterUseConsumerInfoEntity>query().lambda();
            wrapper2.eq(WaterUseConsumerInfoEntity::getWiuCd, vo.getWiuCd());
            wrapper2.last("limit 1");
            WaterUseConsumerInfoEntity waterUseConsumerInfoEntity = getWaterUseConsumerService().getOne(wrapper2);
            if (null != waterUseConsumerInfoEntity) {
                Dept dept = getDeptService().getById(waterUseConsumerInfoEntity.getOrgId());
                if (null != dept) {
                    vo.setOrgId(waterUseConsumerInfoEntity.getOrgId());
                    vo.setOrgName(dept.getDeptName());
                }
            }
        }
        return vo;
    }
}
