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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.compress.utils.Lists;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.nh.excel.AlarmCodeExcel;
import org.springblade.modules.nh.factory.ginlong.dto.alarmlist.GetAlarmListR;
import org.springblade.modules.nh.mapper.AlarmCodeMapper;
import org.springblade.modules.nh.pojo.entity.AlarmCodeEntity;
import org.springblade.modules.nh.pojo.entity.AlarmInfoEntity;
import org.springblade.modules.nh.pojo.vo.AlarmCodeVO;
import org.springblade.modules.nh.service.IAlarmCodeService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 故障代码 服务实现类
 *
 * @author AlexWang
 * @since 2024-06-06
 */
@Service
public class AlarmCodeServiceImpl extends BaseServiceImpl<AlarmCodeMapper, AlarmCodeEntity> implements IAlarmCodeService {

	@Override
	public IPage<AlarmCodeVO> selectAlarmCodePage(IPage<AlarmCodeVO> page, AlarmCodeVO alarmCode) {
		return page.setRecords(baseMapper.selectAlarmCodePage(page, alarmCode));
	}


	@Override
	public List<AlarmCodeExcel> exportAlarmCode(Wrapper<AlarmCodeEntity> queryWrapper) {
		List<AlarmCodeExcel> alarmCodeList = baseMapper.exportAlarmCode(queryWrapper);
		//alarmCodeList.forEach(alarmCode -> {
		//	alarmCode.setTypeName(DictCache.getValue(DictEnum.YES_NO, AlarmCode.getType()));
		//});
		return alarmCodeList;
	}

	@Override
	public void syncFaultCode(String sysType, List<GetAlarmListR> list) {

		Set<String> codes = list.stream().map(GetAlarmListR::getAlarmCode).collect(Collectors.toSet());
		List<AlarmCodeEntity> exists = findByCodeAndSource(new ArrayList<>(codes), sysType);
		Map<String, AlarmCodeEntity> codeMap = exists.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e));
		Set<String> newsExists = new HashSet<>();
		for (GetAlarmListR alarm : list) {
			AlarmCodeEntity r = codeMap.get(alarm.getAlarmCode());
			if (r == null) {
				String key = sysType + "$-$" + alarm.getAlarmCode();
				if (!newsExists.contains(key)) {
					r = new AlarmCodeEntity();
					r.setBrand(sysType);
					r.setCode(alarm.getAlarmCode());
					r.setReason(alarm.getAlarmMsg());
//					r.setContent(alarm.getAlarmMsg());

					if(StringUtil.isNotBlank(alarm.getAdvice())
						&& !StringUtil.containsAny(alarm.getAdvice(),"已恢复")){
						r.setSuggestion(alarm.getAdvice());
					}

					r.setStatus(1);
					r.setLevel(Integer.parseInt(alarm.getAlarmLevel()));
					newsExists.add(key);
					exists.add(r);
				}
			} else {
				r.setReason(alarm.getAlarmMsg());
//				r.setContent(alarm.getAlarmMsg());
			}
		}
		this.saveOrUpdateBatch(exists);
	}

	public List<AlarmCodeEntity> findByCodeAndSource(List<String> codes, String source) {
		if (codes.isEmpty()) {
			return Lists.newArrayList();
		}
		LambdaQueryWrapper<AlarmCodeEntity> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AlarmCodeEntity::getBrand,source)
			.in(AlarmCodeEntity::getCode,codes);
		return this.list(queryWrapper);
	}
}
