/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/5/22
 */
package org.springblade.etl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.etl.dto.EtlRainDataDTO;
import org.springblade.etl.entity.EtlRainDataEntity;
import org.springblade.etl.service.IEtlRainDataService;
import org.springblade.modules.business.data.entity.RtuDataRainEntity;
import org.springblade.modules.business.data.service.IRtuDataRainService;

import java.util.Date;
import java.util.List;


/***
 * Date:2022/5/22
 * Title: 降水抽取
 * Description：每小时同步一次
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
public class EtlRainDataTask {

	public static RtuDataRainEntity dataSync(EtlRainDataDTO etlRainDataDTO, IEtlRainDataService etlRainDataService, IRtuDataRainService rtuDataRainService) {
		List<EtlRainDataEntity> rainDataEntityList = etlRainDataService.selectLastList(etlRainDataDTO);
		if (null != rainDataEntityList && rainDataEntityList.size() > 0) {
			EtlRainDataEntity entity = rainDataEntityList.get(0);
			LambdaQueryWrapper<RtuDataRainEntity> dataWrapper = Wrappers.<RtuDataRainEntity>query().lambda();
			dataWrapper.eq(RtuDataRainEntity::getRtuCode, entity.getRtuCode());
			dataWrapper.eq(RtuDataRainEntity::getIsDeleted, 0);
			RtuDataRainEntity rainEntity = rtuDataRainService.getOne(dataWrapper);
			if (null == rainEntity) {
				rainEntity = new RtuDataRainEntity();
				rainEntity.setRtuCode(entity.getRtuCode());
				rainEntity.setCreateTime(new Date());
				rainEntity.setStatus(1);
				rainEntity.setIsDeleted(0);
			}
			rainEntity.setUpdateTime(new Date());
			rainEntity.setDrp(entity.getDrp());
			rainEntity.setIntv(entity.getIntv());
			rainEntity.setTm(entity.getTm());
			rtuDataRainService.saveOrUpdate(rainEntity);
			return rainEntity;
		}
		return null;
	}
}
