package com.palmnest.dao.estateserve.impl;

import com.palmnest.dao.estateserve.NeigouDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.InPuchaseUser;
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;

/**
 * @Author HIAPAD
 * @Description TODO
 * @Date 2018/12/21 15:32
 **/
@Repository(value = "neigouDao")
public class NeigouDaoImpl extends GenericDaoHibernate<Object, Long> implements NeigouDao {

	private Logger logger = LoggerFactory.getLogger(NeigouDaoImpl.class);

	public NeigouDaoImpl() {
		super(Object.class);
	}

	@Override
	public InPuchaseUser getPuchaseUser(String openId, String developId, String mobile) {
		String sql = "select * from tb_in_puchase_user where isEnable=1 AND developId='" + developId + "'";
		if (StringUtils.isNotBlank(mobile)) {
			sql += " AND mobile='" + mobile + "'";
		}
		if (StringUtils.isNotBlank(openId)) {
			sql += " AND openId='" + openId + "'";
		}
		List<InPuchaseUser> list = getSession().createSQLQuery(sql).addEntity(InPuchaseUser.class).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getVirListByUser(Integer publicId, String mobile) {

		String sql = "SELECT tu.id userId,tv.id virId,tv.`name` virName,tu.organizationId FROM tb_virtualarchitecture tv LEFT JOIN tb_user tu ON tv.id=tu.virtualArchId WHERE tu.isEnabled=1 AND tu.userType=0 AND tu.userMobile='" + mobile + "' AND tv.isEnabled='1' " +
				" AND tu.organizationId IN (SELECT two.organizationId FROM tr_wxpublicno_org two WHERE two.publicNoId=" + publicId + " GROUP BY two.organizationId)" +
				" GROUP BY tv.id";
		return getJdbcTemplate().queryForList(sql);
	}

	@Override
	public void quitNeigouLogin(String openId) {
		String sql = "DELETE FROM InPuchaseUser WHERE openId=?";
		getSession().createQuery(sql).setString(0, openId).executeUpdate();
	}

	@Override
	public void removeByMobile(String mobile) {
		String sql = "DELETE FROM InPuchaseUser WHERE mobile=?";
		getSession().createQuery(sql).setString(0, mobile).executeUpdate();
	}

	@Override
	public Boolean checkIsPutaway(int orgId, String serviceCode) {
		String sql = " SELECT t1.id FROM tb_serviceprojectallocation t1 LEFT JOIN tb_servicetypeinfo t2 ON t1.serviceTypeId=t2.id " +
				" WHERE t2.isEnabled=1 AND t1.`status`='1' AND t2.serviceCode=? AND t1.projectId=? ";
		List<Integer> list = getSession().createSQLQuery(sql).setString(0, serviceCode).setInteger(1, orgId).list();
		if (CollectionUtils.isEmpty(list)) {
			return false;
		}
		return true;
	}

	/**
	 * 如果没有是什么情况
	 *
	 * @param userMobile
	 * @return
	 */
	@Override
	public InPuchaseUser getPuchaseUser(String userMobile) {

		String sql = "select * from tb_in_puchase_user where isEnable=1 AND mobile=?";

		List<InPuchaseUser> list = getSession().createSQLQuery(sql).addEntity(InPuchaseUser.class).setString(0, userMobile).list();

		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	@Override
	public List<Map<String, Object>> getCompanyListByMobile(String userMobile, String virName) {

		String sql = " SELECT tu.userName,tu.ID userId,tu.userMobile,tv.`name` virName,tv.id virId,tg.`name` orgName,tg.ID orgId " +
				" FROM tb_user tu " +
				" LEFT JOIN tb_virtualarchitecture tv ON tu.virtualArchId = tv.id " +
				" LEFT JOIN tb_organization tg ON tu.organizationId = tg.ID " +
				" WHERE tu.isEnabled = 1 AND tu.userType = 0 AND virtualArchId is not null AND tu.userMobile = '" + userMobile + "' ";
		if (StringUtils.isNotBlank(virName)) {
			sql += " AND tv.`name` LIKE '%" + virName + "%' ";
		}
		sql += " ORDER BY tu.ID ";

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