package com.palmnest.dao.repair.impl;

import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.repair.UserRepairTypeDao;
import com.palmnest.model.repair.UserRepairType;
import org.springframework.stereotype.Repository;

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

@Repository("userRepairTypeDao")
public class UserRepairTypeDaoImpl extends GenericDaoHibernate<UserRepairType, Long> implements UserRepairTypeDao {

	public UserRepairTypeDaoImpl() {
		super(UserRepairType.class);
	}

	@Override
	public List<UserRepairType> findBy(Long repairTypeId, Long roleId) {
		String               strHql = "from UserRepairType where repairTypeId = ? and roleId=?";
		List<UserRepairType> list   = getSession().createQuery(strHql).setLong(0, repairTypeId).setLong(1, roleId).list();
		return list;
	}

	@Override
	public List<UserRepairType> findBy(Long repairTypeId) {
		String               strHql = "from UserRepairType where repairTypeId = ?";
		List<UserRepairType> list   = getSession().createQuery(strHql).setLong(0, repairTypeId).list();
		return list;
	}

	@Override
	public List<Map<String, Object>> findBy(Long repairTypeId, Integer orgId) {
		String strHql = "SELECT distinct urt.* FROM tb_user_repair_type urt left join tr_app_user_orgnization uo on uo.user_id=urt.user_id WHERE urt.repair_type_id = " + repairTypeId;
		if (null != orgId) {
			strHql = strHql + " AND uo.org_id=" + orgId;
		}
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strHql);
		return list;
	}

	@Override
	public UserRepairType findBy(Long userId, Long repairTypeId, Long roleId) {
		String               strHql = "from UserRepairType where userId = ? and repairTypeId = ? and roleId=? ";
		List<UserRepairType> list   = getSession().createQuery(strHql).setLong(0, userId).setLong(1, repairTypeId).setLong(2, roleId).list();
//		list.stream().distinct().collect(Collectors.toList());
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public UserRepairType findByRepair(Long userId, Long repairTypeId) {
		String               strHql = "from UserRepairType where userId = ? and repairTypeId = ? ";
		List<UserRepairType> list   = getSession().createQuery(strHql).setLong(0, userId).setLong(1, repairTypeId).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<UserRepairType> findByUser(Long userId) {
		String               strHql = "from UserRepairType where userId = ?";
		List<UserRepairType> list   = getSession().createQuery(strHql).setLong(0, userId).list();
		return list;
	}

	@Override
	public List<UserRepairType> findByUser(Long userId, Long roleId) {
		String               strHql = "from UserRepairType where userId = ? and roleId=? ";
		List<UserRepairType> list   = getSession().createQuery(strHql).setLong(0, userId).setLong(1, roleId).list();
		return list;
	}

	@Override
	public int removeByUser(Long userId, Long roleId) {
		String sql = "delete from UserRepairType where userId=? and roleId=? ";
		return this.getSession().createQuery(sql).setLong(0, userId).setLong(1, roleId).executeUpdate();
	}

	@Override
	public int removeByRoleId(Long roleId) {
		String sql = "delete from UserRepairType where  roleId=? ";
		return this.getSession().createQuery(sql).setLong(0, roleId).executeUpdate();
	}
}
