package com.palmnest.dao.virtfood.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.virtfood.VirtFoodDao;
import com.palmnest.model.ScopeEntity;
import com.palmnest.model.virtfood.VirtFood;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

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

@Repository("virtFoodDao")
public class VirtFoodDaoImpl extends GenericDaoHibernate<VirtFood, Long> implements VirtFoodDao {

	public VirtFoodDaoImpl() {
		super(VirtFood.class);
	}

	@Override
	public Page findPageBy(Map<String, Object> params, int pageSize, int pageNo) {

		StringBuilder sql = new StringBuilder("SELECT a.id,a.food_type,a.virt_id,c.`name` as virt_name,tb.iorg_name,a.food_name,b.`name` AS org_name,a.account_number,a.account_password,a.org_id,a.card_no," +
				"a.approval_status,a.enabled,a.created_by,a.created_at,a.updated_by,a.updated_at " +
				"FROM tb_virt_food AS a " +
				"LEFT JOIN tb_organization b ON a.org_id = b.ID " +
				"LEFT JOIN tb_virtualarchitecture c on a.virt_id = c.id " +
				"LEFT JOIN (SELECT GROUP_CONCAT(itb.org_name) as iorg_name,itb.food_id from ( " +
				"SELECT ta.*,tb.`name` as org_name from tb_virt_food_org ta LEFT JOIN tb_organization tb on ta.org_id=tb.ID " +
				") itb GROUP BY itb.food_id) tb on a.id=tb.food_id ");
		sql.append(" where 1=1 ");

		if (params.get("food_name") != null && !params.get("food_name").equals("")) {
			sql.append(" and a.food_name like '%" + params.get("food_name") + "%'");
		}
		if (params.get("org_name") != null && !params.get("org_name").equals("")) {
			sql.append(" and b.`name` like '%" + params.get("org_name") + "%'");
		}

		if (params.get("orgStr") != null && !params.get("orgStr").equals("")) {
			sql.append(" and a.org_id in(" + params.get("orgStr") + ")");
		}

		sql.append(" order by created_at desc");
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	@Override
	public Integer insertFoodOrg(Long food_id, Long org_id) {

		String sql = "INSERT INTO `tb_virt_food_org` ( `food_id`, `org_id`) VALUES ( " + food_id + ", " + org_id + ");";
		return getSession().createSQLQuery(sql).executeUpdate();
	}

	@Override
	public Integer updateEnable(VirtFood food) {

		String sql = "UPDATE tb_virt_food SET enabled =" + food.getEnabled() + " where id=" + food.getId() + "";
		return getSession().createSQLQuery(sql).executeUpdate();
	}

	@Override
	public Page getProjects(int pageSize, int pageNo, String projectName) {

		String sql = "SELECT  t.org_id,org.name from (" +
				"(SELECT id,org_id from tb_virt_food where enabled=1 and approval_status =2)" +
				"UNION " +
				"(SELECT food_id as id,org_id from tb_virt_food_org where food_id  in (SELECT id from tb_virt_food where enabled=1 and approval_status =2))" +
				") t " +
				"LEFT JOIN tb_organization org on t.org_id = org.ID " +
				"where org.isEnabled=1 " +
				"GROUP BY t.org_id";
		if (StringUtils.isNotEmpty(projectName)) {
			sql = "SELECT  t.org_id,org.name from (" +
					"(SELECT id,org_id from tb_virt_food where enabled=1 and approval_status =2)" +
					"UNION " +
					"(SELECT food_id as id,org_id from tb_virt_food_org where food_id  in (SELECT id from tb_virt_food where enabled=1 and approval_status =2))" +
					") t " +
					"LEFT JOIN tb_organization org on t.org_id = org.ID " +
					"where org.isEnabled=1 and org.name like '%" + projectName + "%' GROUP BY t.org_id";
		}
		//List<WxProjects> list=getSession().createQuery(sql).list();
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	@Override
	public Page getProjectsOfLate(ScopeEntity scopeEntity, int pageSize, int pageNo) {

		String sql = "SELECT  t.org_id,org.name from (" +
				"				(SELECT id,org_id from tb_virt_food where enabled=1 and approval_status =2)" +
				"				UNION " +
				"				(SELECT food_id as id,org_id from tb_virt_food_org where food_id  in (SELECT id from tb_virt_food where enabled=1 and approval_status =2))" +
				"				) t " +
				"				LEFT JOIN tb_organization org on t.org_id = org.ID " +
				"where org.isEnabled=1 " +
				"AND org.lng >= " + scopeEntity.getMinlng() +
				"AND org.lng <= " + scopeEntity.getMaxlng() +
				"AND org.lat >= " + scopeEntity.getMinlat() +
				"AND org.lat <= " + scopeEntity.getMaxlat() +
				"				GROUP BY t.org_id";

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

	@Override
	public VirtFood getBySpid(String spid) {

		String         strHql = "from VirtFood where card_no=?  ";
		List<VirtFood> list   = getSession().createQuery(strHql).setString(0, spid).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<VirtFood> findAll(Map<String, Object> params) {

		StringBuilder sql = new StringBuilder("from VirtFood ");
		sql.append(" where 1=1 ");

		if (params.get("virtId") != null && !params.get("virtId").equals("")) {
			sql.append(" and virt_id=" + params.get("virtId"));
		}
		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and org_id =" + params.get("orgId"));
		}
		if (params.get("food_name") != null && !params.get("food_name").equals("")) {
			sql.append(" and food_name ='" + params.get("food_name") + "'");
		}
		return getSession().createQuery(sql.toString()).list();
	}

	@Override
	public String getFoodIdByOrgId(String orgId) {

		String strHql = "SELECT food_id from tb_virt_food_org where org_id=  " + orgId;
		Object obj    = getSession().createSQLQuery(strHql).uniqueResult();
		if (obj == null)
			return "";
		return obj.toString();
	}

	@Override
	public Integer deleteFoodOrg(Long food_id, Long org_id) {

		String sql = "DELETE FROM tb_virt_food_org where food_id=" + food_id + " AND org_id=" + org_id;
		return getSession().createSQLQuery(sql).executeUpdate();
	}


}
