/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.baseinfo.rtu.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.modules.baseinfo.region.entity.RegionInfoEntity;
import org.springblade.modules.baseinfo.region.service.IBaseRegionService;
import org.springblade.modules.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.mapper.RtuBaseInfoMapper;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.baseinfo.rtu.vo.RtuInfoVO;
import org.springframework.stereotype.Service;

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

/***
 * Date:2022/8/22
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Service
public class RtuBaseInfoServiceImpl extends BaseServiceImpl<RtuBaseInfoMapper, RtuInfoEntity> implements IRtuBaseInfoService {
	@Resource
	private IBaseRegionService baseInfoRegionService;

	@Override
	public IPage<RtuInfoVO> selectPage(IPage<RtuInfoVO> page, RtuInfoDTO rtuInfoDTO) {
		return page.setRecords(baseMapper.selectPage(page,rtuInfoDTO));
	}

	@Override
	public RtuInfoEntity rtuInfoByCode(String rtuCode) {
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getRtuCode, rtuCode);
		wrapper.eq(RtuInfoEntity::getIsDeleted,0);

		RtuInfoEntity rtuInfoEntity =  this.getOne(wrapper);
		return rtuInfoEntity;
	}


	@Override
	public Long rtuCount( RtuInfoDTO dto) {
		return baseMapper.rtuCount(dto);
	}

	@Override
	public Long rtuCount(String adCode) {
		RtuInfoDTO dto = new RtuInfoDTO();
		dto.setAdCode(adCode);
		return baseMapper.rtuCount(dto);
	}

	@Override
	public List<RtuInfoEntity> rtuList(RtuInfoDTO rtuInfoDTO) {
       return baseMapper.rtuList(rtuInfoDTO);
	}

	@Override
	public RtuInfoEntity  getAdcd(RtuInfoEntity rtuInfoEntity) {
		if (rtuInfoEntity.getAdCity() != null && rtuInfoEntity.getAdDist()!=null) {
			LambdaQueryWrapper<RegionInfoEntity> wrapper = Wrappers.<RegionInfoEntity>query().lambda();
			wrapper.eq(RegionInfoEntity::getAdnm, rtuInfoEntity.getAdCity());
			wrapper.eq(RegionInfoEntity::getIsDeleted, 0);
			List<RegionInfoEntity> regionInfoEntityList = baseInfoRegionService.list(wrapper);
			if (null != regionInfoEntityList && regionInfoEntityList.size() == 1) {
				wrapper = Wrappers.<RegionInfoEntity>query().lambda();
				wrapper.eq(RegionInfoEntity::getAdnm, rtuInfoEntity.getAdDist());
				wrapper.eq(RegionInfoEntity::getParentCode, regionInfoEntityList.get(0).getAdcd());
				wrapper.eq(RegionInfoEntity::getIsDeleted, 0);
				List<RegionInfoEntity> regionInfoEntityList2 = baseInfoRegionService.list(wrapper);
				if (null != regionInfoEntityList2 && regionInfoEntityList2.size() == 1) {
					RtuInfoEntity entity=new RtuInfoEntity();
					entity.setAdCode(regionInfoEntityList2.get(0).getAdcd());
					entity.setAdName(regionInfoEntityList2.get(0).getAdnm());
					return entity;
				}
			}
		}

		List<RtuInfoVO> voList=   baseMapper.getAdcd(rtuInfoEntity.getRtuCode());
        if (null != voList && voList.size()==1){
			RtuInfoEntity entity=new RtuInfoEntity();
			entity.setAdCode(voList.get(0).getAdcd());
			entity.setAdName(voList.get(0).getAdnm());
        	return entity;
		}

		return null;
	}

	@Override
	public Long rtuRainCount(String adCode) {
		return this.baseMapper.rtuRainCount(adCode);
	}

	@Override
	public Long rtuRiverCount(String adCode) {
		return this.baseMapper.rtuRiverCount(adCode);
	}

	@Override
	public Long rtuGroundCount(String adCode) {
		return this.baseMapper.rtuGroundCount(adCode);
	}

}
