package org.springblade.modules.nh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.nh.pojo.entity.InefficientSettingEntity;
import org.springblade.modules.nh.pojo.entity.PowerStationEntity;
import org.springblade.modules.nh.pojo.vo.InefficientSettingVO;
import org.springblade.modules.nh.excel.InefficientSettingExcel;
import org.springblade.modules.nh.mapper.InefficientSettingMapper;
import org.springblade.modules.nh.service.IInefficientSettingService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

/**
 * 低效电站设置 服务实现类
 *
 * @author AlexWang
 * @since 2024-06-14
 */
@Service
@AllArgsConstructor
public class InefficientSettingServiceImpl extends BaseServiceImpl<InefficientSettingMapper, InefficientSettingEntity> implements IInefficientSettingService {

	private final BladeRedis bladeRedis;

	@Override
	public IPage<InefficientSettingVO> selectInefficientSettingPage(IPage<InefficientSettingVO> page, InefficientSettingVO inefficientSetting) {
		return page.setRecords(baseMapper.selectInefficientSettingPage(page, inefficientSetting));
	}


	@Override
	public List<InefficientSettingExcel> exportInefficientSetting(Wrapper<InefficientSettingEntity> queryWrapper) {
		List<InefficientSettingExcel> inefficientSettingList = baseMapper.exportInefficientSetting(queryWrapper);
		//inefficientSettingList.forEach(inefficientSetting -> {
		//	inefficientSetting.setTypeName(DictCache.getValue(DictEnum.YES_NO, InefficientSetting.getType()));
		//});
		return inefficientSettingList;
	}


	@Override
	public void importSetting(List<InefficientSettingExcel> data, Boolean isCovered) {

		List<InefficientSettingEntity> inefficientSettingEntityList = new ArrayList<>();

		boolean isRepeat = false;
		for(InefficientSettingExcel item: data){
			String planType = DictCache.getKey(DictEnum.SETTING_PLAN_TYPE,item.getPlanTypeStr());


//			String customerType = DictCache.getKey(DictEnum.SETTING_CUSTOMER_TYPE,item.getCustomerTypeStr());

			LambdaQueryWrapper<InefficientSettingEntity> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(InefficientSettingEntity::getPlanType, planType)
				.eq(InefficientSettingEntity::getCapitalName, item.getCapitalName())
				.eq(InefficientSettingEntity::getProvince,item.getProvince());
//				.eq(InefficientSettingEntity::getCustomerType,customerType);
			if(StringUtil.isNotBlank(item.getCitys())){
				queryWrapper.eq(InefficientSettingEntity::getCitys,item.getCitys());
			}

			if(StringUtil.isNotBlank(item.getRecordTypeStr())){
				String recordType = DictCache.getKey(DictEnum.SETTING_RECORD_TYPE,item.getRecordTypeStr());
				queryWrapper.eq(InefficientSettingEntity::getRecordType,recordType);
			}

			InefficientSettingEntity entity = getOne(queryWrapper);
			if(entity == null){
				entity = new InefficientSettingEntity();
				if(StringUtil.isNotBlank(item.getCapitalName())){
					entity.setCapitalName("广州越秀融资租赁有限公司");
				}
				entity.setPlanType(Integer.parseInt(planType));

				if(StringUtil.isNotBlank(item.getRecordTypeStr())){
					String recordType = DictCache.getKey(DictEnum.SETTING_RECORD_TYPE,item.getRecordTypeStr());
					entity.setRecordType(Integer.parseInt(recordType));
				}
//				entity.setCustomerType(Integer.parseInt(customerType));
//				entity.setRecordType(Integer.parseInt(recordType));
			}
			BeanUtil.copyProperties(item, entity);

			entity.calTotal();

			entity.setYear(DateUtil.format(new Date(),"yyyy"));
			inefficientSettingEntityList.add(entity);
		}
		this.saveOrUpdateBatch(inefficientSettingEntityList);
	}

	@Override
	public InefficientSettingEntity getSettingByStation(PowerStationEntity stationEntity) {
		if(stationEntity == null){return null;}

		String planType = DictCache.getKey(DictEnum.SETTING_PLAN_TYPE,stationEntity.getInstallType());

		String inefficient_setting_key = "setting:";
		String city = stationEntity.getCity().replaceAll("市","");
		String provinceKey = inefficient_setting_key+stationEntity.getCapitalName()+"-"+stationEntity.getProvince()+"-"+planType;
		String cityKey = provinceKey+"-"+city;

		if(bladeRedis.exists(cityKey)){
			return bladeRedis.get(cityKey);
		}
		if(bladeRedis.exists(provinceKey)){
			return bladeRedis.get(provinceKey);
		}

		InefficientSettingEntity ret = null;
		LambdaQueryWrapper<InefficientSettingEntity> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(InefficientSettingEntity::getCapitalName, stationEntity.getCapitalName())
			.eq(InefficientSettingEntity::getProvince, stationEntity.getProvince())
			.like(InefficientSettingEntity::getCitys,"%"+city+"%")
			.eq(InefficientSettingEntity::getPlanType,planType);
		List<InefficientSettingEntity> cityEntitys =this.list(queryWrapper);
		InefficientSettingEntity cityEntity;
		if(CollectionUtil.isNotEmpty(cityEntitys)){
			cityEntity = cityEntitys.get(0);
			bladeRedis.setEx(cityKey, cityEntity,6*60*60L);
			ret = cityEntity;
		}else{
			LambdaQueryWrapper<InefficientSettingEntity> queryWrapper2 = new LambdaQueryWrapper<>();
			queryWrapper2.eq(InefficientSettingEntity::getCapitalName, stationEntity.getCapitalName())
				.eq(InefficientSettingEntity::getProvince, stationEntity.getProvince())
				.eq(InefficientSettingEntity::getPlanType,planType);
			List<InefficientSettingEntity> list =this.list(queryWrapper2);
			ret = CollectionUtil.isNotEmpty(list)?list.get(0):null;
			if(ret != null){
				bladeRedis.setEx(provinceKey, ret,6*60*60L);
			}
		}

		return ret;
	}
}
