package com.yh.saas.platform.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.common.support.util.query.QueryUtils;
import com.yh.saas.platform.entity.ThermalRouting;
import com.yh.saas.platform.mapper.ThermalRoutingMapper;
import com.yh.saas.platform.service.IThermalRoutingService;

@Service
public class ThermalRoutingServiceImpl extends ServiceImpl<ThermalRoutingMapper, ThermalRouting>
		implements IThermalRoutingService {

	@Override
	@Transactional
	public void renewalHead(ThermalRouting thermalRouting) {
		if (StringUtils.isEmpty(thermalRouting.getAccountId())) {
			thermalRouting.setAccountId(AuthSecurityUtils.getCurrentUserInfo().getAccountId());
		}
		thermalRouting.setCurrentHead(thermalRouting.getCurrentHead() + 1);
		boolean isSuccess = this.update(thermalRouting, QueryUtils.convertExampleToWrapper(thermalRouting));
		if (isSuccess) {
			return;
		}

		thermalRouting.setCurrentHead(thermalRouting.getCurrentHead() - 1);
		thermalRouting.setId(IdGenerator.generateUUID());
		this.insert(thermalRouting);
	}

	@Override
	public List<ThermalRouting> findHottestRouting(ThermalRouting thermalRouting) {
		if (StringUtils.isEmpty(thermalRouting.getAccountId())) {
			thermalRouting.setAccountId(AuthSecurityUtils.getCurrentUserInfo().getAccountId());
		}
		thermalRouting.setCurrentPage(1);
		thermalRouting.setOrderColunm("updateCount");
		thermalRouting.setOrder("descing");
		Page<ThermalRouting> thermalRoutingPage = this.selectPage(thermalRouting.getQueryPage(),
				QueryUtils.convertExampleToWrapper(thermalRouting));
		return thermalRoutingPage.getRecords();

	}
}
