package com.palmnest.dao.dinner.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.dao.dinner.DeptDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.dinner.Department;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

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

/**
 * @Author HIAPAD
 * @Description TODO
 * @Date 2019/1/3 17:50
 **/
@Repository("deptDao")
public class DeptDaoImpl extends GenericDaoHibernate<Department, Long> implements DeptDao {

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

	public DeptDaoImpl() {
		super(Department.class);
	}


	@Override
	public Page deptList(int pageNo, int pageSize, HashMap<String, String> map) {

		String sql = "SELECT td.id deptId,td.shortName,td.orderNum,tg.`name` orgName,tv.`name` virName,td.`name` deptName,tu.USERNAME creatorName,DATE_FORMAT(td.createTime,'%Y-%m-%d %T') createTime" +
				" FROM tb_department td " +
				" LEFT JOIN tb_virtualarchitecture tv ON td.virId=tv.id " +
				" LEFT JOIN tb_organization tg ON tg.ID=td.orgId " +
				" LEFT JOIN tb_app_user tu ON tu.ID=td.creator " +
				" WHERE td.isEnabled=1 ";

		if (StringUtils.isNotBlank(map.get("virId"))) {
			sql += " AND td.virId =" + map.get("virId");
		}
		if (StringUtils.isNotBlank(map.get("deptId"))) {
			sql += " AND td.id=" + map.get("deptId");
		}
		if (StringUtils.isNotBlank(map.get("orgId"))) {
			sql += " AND td.orgId=" + map.get("orgId");
		}

		sql += " ORDER BY td.orderNum,td.createTime desc";
		return pagedQueryByJdbcTemplate(sql, pageNo, pageSize);
	}

	@Override
	public List<Department> getDeptByVirIdAndDeptName(int virId, String deptName) {
		String sql = "select * from tb_department where virId=" + virId + " and isEnabled=1";
		if (StringUtil.isNotEmpty(deptName)) {
			sql = sql + "  and name like '%" + deptName + "%'  ";
		}
		List<Department> userList = getSession().createSQLQuery(sql).addEntity(Department.class).list();
		return userList;
	}

	@Override
	public Department getDeptByName(String virId, String name) {
		String sql = "from Department where virId=" + virId + " and name='" + name + "' and isEnabled=1";
		List<Department> deptList = getSession().createQuery(sql.toString()).list();
		if (deptList != null && deptList.size() > 0) {
			return deptList.get(0);
		}
		return null;
	}

	@Override
	public Map<String, Object> getDeptInfo(long deptId) {
		String sql = "SELECT td.id,tg.`name` orgName,td.orgId,tv.`name` virName,td.virId,td.`name` deptName,td.shortName,td.orderNum" +
				" FROM tb_department td " +
				" LEFT JOIN tb_virtualarchitecture tv ON td.virId=tv.id " +
				" LEFT JOIN tb_organization tg ON tg.ID=td.orgId " +
				" WHERE td.isEnabled=1 and td.id=" + deptId;
		return getJdbcTemplate().queryForMap(sql);
	}


	@Override
	public List<Department> getDeptByOrgIdAndVirId(String orgId, String virId, String name) {
		String sql = "select * from tb_department where  isEnabled=1";
		if (StringUtil.isNotEmpty(orgId)) {
			sql = sql + "  and orgId=" + orgId + " ";
		}
		if (StringUtil.isNotEmpty(virId)) {
			sql = sql + "  and virId=" + virId + " ";
		}
		if (StringUtil.isNotEmpty(name)) {
			sql = sql + "  and name='" + name + "' ";
		}
		List<Department> deptList = getSession().createSQLQuery(sql).addEntity(Department.class).list();
		return deptList;
	}

	@Override
	public void delUserDept(Long id) {
		String sql = " UPDATE tb_user SET departmentId=NULL WHERE departmentId=" + id;
		getJdbcTemplate().execute(sql);
	}

	@Override
	public Department getDeptByNameAndVirId(int virId, String name) {
		String sql = " from Department where virId=? and name=? and isEnabled=1";
		List<Department> list = getSession().createQuery(sql).setInteger(0, virId).setString(1, name).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
