package com.palmnest.dao.property.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.property.AboutOrgDao;
import com.palmnest.model.property.AboutOrg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

@Repository("aboutOrgDao")
public class AboutOrgDaoImpl extends GenericDaoHibernate<AboutOrg, Long> implements AboutOrgDao {

	public AboutOrgDaoImpl() {
		super(AboutOrg.class);
	}

	@Override
	public List<AboutOrg> findByOrg(Integer orgId) {
		String strHQL = "from AboutOrg where orgId=? and isEnabled=1 ";
		return getSession().createQuery(strHQL).setInteger(0, orgId).list();
	}

	@Override
	public Page getList(Map<String, String> params) {
		StringBuilder sql = new StringBuilder("select t.id id,t.title title,t.content content,t.org_id orgId ,tr.name orgName, "
				+ "DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i') createTime,t.creator creator,t.hot_line_name hotLineName,t.hot_line hotLine,t.type,t.file_ids fileIds "
				+ ",DATE_FORMAT(t.modify_time,'%Y-%m-%d %H:%i') modifyTime,t.modifier modifier "
				+ "from tb_about_org t "
				+ "join tb_organization tr on t.org_id=tr.id where 1=1 ");

		if (params.get("startTime") != null && !params.get("startTime").equals("")) {
			sql.append(" and t.create_time >= str_to_date('" + params.get("startTime") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("endTime") != null && !params.get("endTime").equals("")) {
			sql.append(" and t.create_time <= str_to_date('" + params.get("endTime") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}
		if (StringUtils.isNotBlank(params.get("title"))) {
			sql.append(" and t.title like '%" + params.get("title") + "%'");
		}
		if (StringUtils.isNotBlank(params.get("orgId"))) {
			sql.append(" and t.org_id in (" + params.get("orgId") + ")");
		}

		if (StringUtils.isNotBlank(params.get("type"))) {
			sql.append(" and t.type = (" + params.get("type") + ")");
		}


		sql.append(" order by t.modify_time desc");
		return pagedQueryByJdbcTemplate(sql.toString(), Integer.parseInt(params.get("pageNo")), Integer.parseInt(params.get("pageSize")));
	}

}
