/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/10/19
 */
package org.springblade.modules.baseinfo.monitorsite.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.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.vo.MonitorSiteInfoVO;
import org.springblade.modules.baseinfo.region.service.IBaseInfoRegionService;
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 MonitorSiteWrapper extends BaseEntityWrapper<MonitorSiteInfoEntity, MonitorSiteInfoVO> {
    public static MonitorSiteWrapper build() {
        return new MonitorSiteWrapper();
    }

    private static IDeptService deptService;

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

    private static IMonitorSiteRelService monitorSiteRelService;

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

    private static IWaterUseConsumerService waterUseConsumerService;

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

    @Override
    public MonitorSiteInfoVO entityVO(MonitorSiteInfoEntity entity) {
        MonitorSiteInfoVO vo = Objects.requireNonNull(BeanUtil.copy(entity, MonitorSiteInfoVO.class));
        if (vo.getWiuNm() == null) {
            LambdaQueryWrapper<MonitorSiteRelEntity> wrapper = Wrappers.<MonitorSiteRelEntity>query().lambda();
            wrapper.eq(MonitorSiteRelEntity::getWiustCd, entity.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<MonitorSiteRelEntity> wrapper = Wrappers.<MonitorSiteRelEntity>query().lambda();
            wrapper.eq(MonitorSiteRelEntity::getWiustCd, entity.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) {
                    Dept dept = getDeptService().getById(waterUseConsumerInfoEntity.getOrgId());
                    if (null != dept) {
                        vo.setOrgId(waterUseConsumerInfoEntity.getOrgId());
                        vo.setOrgName(dept.getDeptName());
                    }
                }
            }
        }
        return vo;
    }
}
