package com.palmnest.dao.estateserve.impl;

import com.palmnest.dao.estateserve.ServeConfigDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.ServiceFunctionRelation;
import com.palmnest.model.estateserve.ServiceProjectAllocation;
import com.palmnest.model.estateserve.ServiceTypeInfo;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author HXT
 * @Description TODO
 * @Date 2018/8/13 17:58
 **/
@Slf4j
@Repository("serveConfigDao")
public class ServeConfigDaoImpl extends GenericDaoHibernate<ServiceFunctionRelation, Long> implements ServeConfigDao {

	public ServeConfigDaoImpl() {
		super(ServiceFunctionRelation.class);
	}


	@Override
	public void deleteByServerIdAndType(Integer serverId, Integer port) {
		String hql = "delete from ServiceFunctionRelation where serviceId=? and type=?";
		getSession().createQuery(hql).setInteger(0, serverId).setInteger(1, port).executeUpdate();
	}

	@Override
	public List<ServiceFunctionRelation> getFunctionById(Integer serverId, Integer port) {
		String hql = "from ServiceFunctionRelation where 1=1 and serviceId=?";
		if (null != port) {
			hql += " and type=?";
		}
		Query query = getSession().createQuery(hql).setInteger(0, serverId);

		if (null != port) {
			query.setInteger(1, port);
		}
		List<ServiceFunctionRelation> list = query.list();
		return list;
	}

	@Override
	public List<ServiceTypeInfo> getServiceList() {
		String sql = "from ServiceTypeInfo where isEnabled=1 and serviceStatus=0 and serviceType=0 order by createTime desc";
		List<ServiceTypeInfo> list = getSession().createQuery(sql).list();
		return list;
	}

	@Override
	public List<Map<String, Object>> getFunctionListByProjectId(Integer projectId) {
		try {
			String sql = "SELECT t4.ID AS id,t4.FUNCTION_NAME AS function_name FROM tb_serviceprojectallocation t1 INNER JOIN tb_servicetypeinfo t2 ON t1.serviceTypeId = t2.id " +
					" INNER JOIN tr_service_function t3 ON t2.id = t3.service_id INNER JOIN tb_app_function t4 ON t3.function_id = t4.ID " +
					" WHERE t1.isEnabled = 1 AND t1.`status` = 1 AND t1.projectId =" + projectId + " AND t1.type = 0 AND t2.isEnabled = 1 ORDER BY t2.serviceType DESC";

			return getJdbcTemplate().queryForList(sql);
		} catch (Exception e) {
			log.error("dao list getFunctionListByProjectId error{}", e);
		}
		return null;
	}

	@Override
	public void deleteByFuncId(Integer funcId) {
		String hql = "delete from ServiceFunctionRelation where functionId=?";
		getSession().createQuery(hql).setInteger(0, funcId).executeUpdate();
	}

	@Override
	public ServiceTypeInfo getServiceTypeInfoByFuncId(String funcIds) {
		String sql = "SELECT t1.* FROM tb_servicetypeinfo t1 LEFT JOIN tr_service_function t2 ON t1.id=t2.service_id WHERE t1.isEnabled='1' AND t2.type='1' AND t2.function_id in (" + funcIds + ") GROUP BY t2.service_id";

		List<ServiceTypeInfo> list = getSession().createSQLQuery(sql).addEntity(ServiceTypeInfo.class).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ServiceProjectAllocation getServiceOrgInfo(int serviceId, String orgId) {
		String sql = "SELECT t1.* FROM tb_serviceprojectallocation t1 WHERE t1.isEnabled='1' AND t1.serviceTypeId='" + serviceId + "' AND t1.projectId in (" + orgId + ")";

		List<ServiceProjectAllocation> list = getSession().createSQLQuery(sql).addEntity(ServiceProjectAllocation.class).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
