package com.palmnest.dao.estateserve.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.estateserve.ServiceProjectAllocationDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.ServiceProjectAllocation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

@Repository("serviceProjectAllocationDao")
public class ServiceProjectAllocationDaoImpl extends GenericDaoHibernate<ServiceProjectAllocation, Integer>
		implements ServiceProjectAllocationDao {

	private Logger LOG = LoggerFactory.getLogger(ServiceProjectAllocationDaoImpl.class);

	public ServiceProjectAllocationDaoImpl() {
		super(ServiceProjectAllocation.class);
	}

	@Override
	public Page serviceProjectAllocationPage(Integer pageNo, Integer pageSize,
											 String whereSql, String orgIds) throws Exception {
		String strSQL = "SELECT * from(SELECT t1.serviceType,t2.permission,t1.id serviceId,t2.id,t1.serviceStatus,if(t2.orderNo is  NULL,0,t2.orderNo) orderNo,t1.isEnabled,t1.serviceName,t1.serviceIcon,t2.roleGroup,t1.projectId,if(t2.`status` is null,0,t2.`status`) serviceallcationStatus,  " +
				" t1.serviceCategory,t2.indate,t2.serviceIcon as customServiceIcon from tb_servicetypeinfo t1"
				+ " LEFT JOIN tb_serviceprojectallocation t2 on t2.serviceTypeId=t1.id  and t2.isEnabled=1 and t2.projectId=" + orgIds + ") t3";
		try {
			if (StringUtils.isNotBlank(whereSql)) {
				strSQL += whereSql;
			}
			return pagedQueryByJdbcTemplate(strSQL, pageNo, pageSize);
		} catch (Exception e) {
			LOG.error("dao find serviceProjectAllocationPage error{}", e);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getServiceProjectAllocationList(
			String orgIds, String type) throws Exception {
		try {
			String strSQL = "SELECT 0 as serviceOpeningStatus,t2.serviceCode serviceNumber,t2.isSignContract,t2.code serviceCode,t2.registerStatus,t2.serviceType,t2.serviceName,t2.serviceRemark,t2.serviceIcon,t2.jumpAddress,t2.skipAddress,t2.linkUrlType,"
					+ " t2.grayAppServiceIcon,t2.grayWechatServiceIcon,t2.serviceCategory,t1.status,t1.orderNo,t2.grayUrl,t1.serviceIcon AS customIcon "
					+ " from tb_serviceprojectallocation t1 inner JOIN tb_servicetypeinfo t2 on t1.serviceTypeId=t2.id and t2.isEnabled=1"
					+ " where t1.isEnabled=1 and t1.`status`in (1,2) and t1.projectId in (" + orgIds + ") and t1.type=" + type
					+ " ORDER BY t1.orderNo+0 asc ";
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			LOG.error("dao list getServiceProjectAllocationList error{}", e);
		}
		return null;
	}

	@Override
	public Map<String, Object> getCompanyObj(String orgIds) throws Exception {
		try {
			String strSQL = "SELECT t4.name,t4.telePhone,t4.address from tb_organization t1"
					+ " inner JOIN tb_build t2 on t1.ID=t2.organizationId and t2.isEnabled=1"
					+ " inner join tr_build_org t3 on t2.id=t3.buildId"
					+ " inner join tb_company t4 on t4.ID=t3.orgId and t4.isEnabled=1"
					+ " where t1.ID=" + orgIds;
			List<Map<String, Object>> resultMap = this.getJdbcTemplate().queryForList(strSQL);
			if (resultMap != null && resultMap.size() > 0) {
				return resultMap.get(0);
			}
		} catch (Exception e) {
			LOG.error("dao list getCompanyObj error{}", e);
		}
		return null;
	}

	@Override
	public ServiceProjectAllocation getServiceProjectAllocation(
			int serviceTypeId, int orgId, String type) {
		String strHQL = "from ServiceProjectAllocation where serviceTypeId=? and projectId=? and type=? ";
		List<ServiceProjectAllocation> list = getSession().createQuery(strHQL)
				.setInteger(0, serviceTypeId).setInteger(1, orgId)
				.setString(2, type).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ServiceProjectAllocation> getServiceProjectAllocationList(
			String serviceTypeId) {
		String strSQL = "from ServiceProjectAllocation where serviceTypeId=? and isEnabled='1'";
		List<ServiceProjectAllocation> list = getSession().createQuery(strSQL).setString(0, serviceTypeId).list();
		return list;
	}

	@Override
	public List<ServiceProjectAllocation> findServiceProjectAllocationList(
			String orgId) {
		String strHQL = "from ServiceProjectAllocation where projectId=? and type='0' and isEnabled='1'";
		List<ServiceProjectAllocation> list = getSession().createQuery(strHQL).setString(0, orgId).list();
		return list;
	}

	@Override
	public void deleteProjectAllocation(String orgId) {
		String strSQL = "DELETE from tb_serviceprojectallocation where tb_serviceprojectallocation.projectId='" + orgId + "'";
		this.getJdbcTemplate().execute(strSQL);
	}

	@Override
	public List<Map<String, Object>> getServiceProjectAllocationList(Integer orgId, Integer type) throws Exception {
		try {
			String strSQL = "SELECT t2.id as id,t2.isSignContract,t2.code serviceCode,t2.registerStatus,t2.serviceType,t2.serviceName,t2.serviceRemark,t2.serviceIcon,t2.jumpAddress,t2.skipAddress,t2.linkUrlType"
					+ " from tb_serviceprojectallocation t1 inner JOIN tb_servicetypeinfo t2 on t1.serviceTypeId=t2.id and t2.isEnabled=1"
					+ " where t1.isEnabled=1 and t1.`status`=1 and t1.projectId =" + orgId + " and t1.type=" + type
					+ " ORDER BY t2.serviceType DESC";
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			LOG.error("dao list getServiceProjectAllocationList error{}", e);
		}
		return null;
	}


	@Override
	public List<ServiceProjectAllocation> listAll() {
		String sql = "SELECT t1.* FROM tb_serviceprojectallocation t1 WHERE t1.isEnabled='1' AND status='1'";

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

	@Override
	public List<Map<String, Object>> getDiscoverServiceList() {
		try {
			String strSQL = "SELECT t2.isSignContract,t2.serviceCode serviceNumber,t2.code serviceCode,t2.registerStatus,t2.serviceType,t2.serviceName,t2.serviceRemark,t2.serviceIcon,t2.jumpAddress,t2.skipAddress,t2.linkUrlType, " +
					" t2.grayAppServiceIcon,t2.grayWechatServiceIcon,t2.serviceCategory,t2.grayUrl from tb_servicetypeinfo t2 " +
					" where t2.isEnabled='1' AND t2.serviceType='2' ORDER BY t2.id ASC";
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			LOG.error("ServiceProjectAllocationDaoImpl getDiscoverServiceList error{}", e);
		}
		return null;
	}

	@Override
	public String getServiceProjectByServiceCode(
			String serviceCode) {
		String orgIds = "";
		StringBuilder sql = new StringBuilder("");
		sql.append(" select GROUP_CONCAT(DISTINCT spt.projectId) orgIds from tb_servicetypeinfo s  ");
		sql.append(" left join tb_serviceprojectallocation spt on s.id=spt.serviceTypeId  ");
		sql.append(" where s.`serviceCode`='" + serviceCode + "'   and s.isEnabled='1' ");
		sql.append(" and spt.`status`='1' and spt.isEnabled='1'  ");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
		if (list != null && list.size() > 0) {
			orgIds = list.get(0).get("orgIds") == null ? "" : list.get(0).get("orgIds").toString();
		}
		return orgIds;
	}

	@Override
	public List<Map<String, Object>> getOpenedFunctionServiceList(int orgId) {
		String sql = " SELECT t1.id, t2.id as serviceId, t2.serviceName 'name' FROM tb_serviceprojectallocation t1 LEFT JOIN tb_servicetypeinfo t2 ON t1.serviceTypeId=t2.id WHERE t1.projectId=" + orgId + " AND t1.type='0' AND t1.isEnabled='1' AND t1.permission=1 order by t1.id";
		return getJdbcTemplate().queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getOpenedFunctionServiceChildList(List<Integer> serviceIdList) {
		String sql = " SELECT id, service_id as serviceId, child_name 'name', `code` FROM tb_servicetypeinfo_child ";
		if (serviceIdList.size() > 0) {
			sql += "	WHERE service_id IN (";
		}

		if (serviceIdList.size() >= 1) {
			sql += serviceIdList.get(0);
		}
		for (int i = 1; i < serviceIdList.size(); i++) {
			sql += "," + serviceIdList.get(i);
		}
		if (serviceIdList.size() > 0) {
			sql += ")";
		}

		return getJdbcTemplate().queryForList(sql);
	}
}
