package com.palmnest.dao.washcar.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.washcar.CustomerDao;
import com.palmnest.model.washcar.Customer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户dao实现类
 *
 * @author longmeizhen
 */
@Slf4j
@Repository("customerDao")
public class CustomerDaoImpl extends GenericDaoHibernate<Customer, Long> implements CustomerDao {


	public CustomerDaoImpl() {
		super(Customer.class);
	}

	@Override
	public String deleteCustomer(Long custId, String userId) throws Exception {
		String sql   = "update tb_customer set isEnabled='0',modifier='" + userId + "',modifyTime=SYSDATE() where id=" + custId;
		int    count = getSession().createSQLQuery(sql).executeUpdate();
		return String.valueOf(count);
	}

	@Override
	public List<Customer> getAll() {
		String         strHql   = "from Customer where isEnabled='1' ";
		List<Customer> custList = getSession().createQuery(strHql).list();
		return custList;
	}

	@Override
	public Page getCustList(String orgIds, String mobile, String userName, String carNumber, String searchStr, int pageNo, int pageSize)
			throws Exception {
		StringBuilder sql = new StringBuilder(" select id,userName,mobile,dept,position,carNumber,carType,isEnabled ");
		sql.append(" ,createTime,creator,modifyTime,modifier,parkSpace,serialNumber,orgId from tb_customer ");
		sql.append(" where isEnabled='1' ");
		if (StringUtils.isNotEmpty(searchStr)) {
			sql.append(" and (mobile like '%" + searchStr + "%' or carNumber like '%" + searchStr + "%' ) ");
		}
		if (StringUtils.isNotEmpty(mobile)) {
			sql.append(" and mobile like '%" + mobile + "%' ");
		}
		if (StringUtils.isNotEmpty(userName)) {
			sql.append(" and userName like '%" + userName + "%' ");
		}
		if (StringUtils.isNotEmpty(carNumber)) {
			sql.append(" and carNumber like '%" + carNumber + "%' ");
		}
		if (StringUtils.isNotBlank(orgIds)) {
			String[] orgStr = orgIds.split(";");
			String   str    = "";
			if (orgStr != null && orgStr.length > 0) {
				for (int i = 0; i < orgStr.length; i++) {
					str = str + orgStr[i] + ",";
				}
			}
			if (StringUtils.isNotEmpty(str)) {
				str = str.substring(0, str.length() - 1);
				sql.append(" and orgId in (" + str + ") ");
			}

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

	@Override
	public Customer getCustByMobile(String orgId, String mobile) throws Exception {
		String         strHql   = "from Customer where isEnabled='1'  and mobile=? and orgId=? ";
		List<Customer> custList = getSession().createQuery(strHql).setString(0, mobile).setString(1, orgId).list();
		if (custList != null && custList.size() > 0) {
			return custList.get(0);
		} else {
			return null;
		}

	}

	@Override
	public Customer getCustByOpenId(String orgId, String openId) throws Exception {
		String         strHql   = "from Customer where isEnabled='1'  and openId=? and orgId=? ";
		List<Customer> custList = getSession().createQuery(strHql).setString(0, openId).setString(1, orgId).list();
		if (custList != null && custList.size() > 0) {
			return custList.get(0);
		} else {
			return null;
		}
	}

}
