package com.palmnest.dao.repair.impl;

import java.util.Map;

import org.springframework.stereotype.Repository;

import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.repair.RepairServiceDao;
import com.palmnest.model.repair.RepairService;

@Repository("repairServiceDao")
public class RepairServiceDaoImpl extends GenericDaoHibernate<RepairService, Long> implements RepairServiceDao {

	public RepairServiceDaoImpl() {
		super(RepairService.class);

	}

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

		StringBuilder sql = new StringBuilder("SELECT	id,	s_no,	`status`,	repair_username,	repair_user_id,	repair_phone,	company_name,	remarks,	build_name,	build_floor,	build_room," +
				"	build_id,	pic_url,	source_type,	repair_amount,	status_date,	created_by,	created_at,DATE_FORMAT(created_at,'%Y-%m-%d %H:%i') created_at_str,	updated_by,	updated_at,DATE_FORMAT(updated_at,'%Y-%m-%d %H:%i') updated_at_str " +
				" FROM" +
				"	tb_repair_service");
		sql.append(" where 1=1 ");

		if (params.get("status") != null && !params.get("status").equals("")) {
			sql.append(" and `status` =" + params.get("status"));
		}
		if (params.get("startTime") != null && !params.get("startTime").equals("")) {
			sql.append(" and created_at >= 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 created_at <= str_to_date('" + params.get("endTime") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("repair_phone") != null && !params.get("repair_phone").equals("")) {
			sql.append(" and repair_phone like '%" + params.get("repair_phone") + "%'");
		}
		if (params.get("repair_username") != null && !params.get("repair_username").equals("")) {
			sql.append(" and repair_username like '%" + params.get("repair_username") + "%'");
		}
		if (params.get("s_no") != null && !params.get("s_no").equals("")) {
			sql.append(" and s_no like '%" + params.get("s_no") + "%'");
		}

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

		if (params.get("orgStr") != null && !params.get("orgStr").equals("")) {
			sql.append(" and org_id in(" + params.get("orgStr") + ")");
		}
		if (params.get("params") != null && !params.get("params").equals("")) {
			sql.append(" and (repair_phone like '%" + params.get("params") + "%' or  repair_username like '%" + params.get("params") + "%') ");
		}

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

	@Override
	public String getNo() {
		StringBuilder sql = new StringBuilder("SELECT s_no from tb_repair_service ORDER BY created_at desc LIMIT 1");

		Object o = getSession().createSQLQuery(sql.toString()).uniqueResult();
		if (o == null) return "";
		return o.toString();
	}


}
