package abacus.springboot.example.impl;

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

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.abacus.pms.dao.Account;
import com.abacus.pms.dao.Brand;
import com.abacus.pms.dao.MoreCategory;
import com.abacus.pms.dao.Product;
import com.abacus.pms.dao.Spec;
import com.abacus.xpos.foundation.dao.BaseArea;
import com.abacus.xpos.foundation.dao.Dept;
import com.abacus.xpos.foundation.dao.Employee;

import abacus.config.dao.AbacusConfigItem;
import abacus.springboot.example.dao.JcBill;
import abacus.springboot.example.pi.AutoCompleteAccessIF;

/**
 * <p>Title:ApiJcBillAccess</p>
   <p>Description: 底层实现</p>
   @author chuanZeng 
   @date 2026年8月18日
 */
@Repository("autoCompleteAccess")
public class AutoCompleteAccess extends MyJpaUtils implements AutoCompleteAccessIF {
	
	private final static Log LOG = LogFactory.getLog(AutoCompleteAccess.class);

	@PersistenceContext
	private EntityManager entityManager;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@SuppressWarnings("unchecked")
	public List<Product> prductAutoComplete(String keyCode, List<String> areaIds) throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from Product ");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(name) like lower('%" + keyCode.replaceAll("'", "") + "%')");
				sql.append(" or lower(id) like lower('%" + keyCode.replaceAll("'", "") + "%')");
				sql.append(" or lower(shortstring) like lower('%" + keyCode.replaceAll("'", "") + "%'))");
				params.put("id", keyCode);
			}
			
//			if(!areaIds.isEmpty()){
//				sql.append(params.size() > 0 ? " and " : " where ");
//				sql.append(" areaid in (:areaid) ");
//				params.put("areaid", areaIds);
//			}
			
			Query query = this.entityManager.createQuery(sql.toString(), Product.class);
			
//			setParameters(query, params);
			
			List<Product> products = query.getResultList();
			
			return products;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> deptAutoComplete(String keyCode, String type) throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from Dept");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(name) like lower(:name)");
				sql.append(" or lower(id) like lower(:id)");
				sql.append(" or lower(shortString) like lower(:shortstring))");
				
				params.put("name","%"+keyCode+"%");
				params.put("id","%"+keyCode+"%");
				params.put("shortstring","%"+keyCode+"%");
			}
			
			if(StringUtils.isNotBlank(type)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" typ = :typ ");
				params.put("typ", Integer.parseInt(type));
			}
			
			Query query = this.entityManager.createQuery(sql.toString(), Dept.class);
			
			setParameters(query, params);
			
			return new ArrayList<Dept>(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MoreCategory> moreCategoryAutoComplete(String keyCode, String areaid, String levels, String lastlevel)
			throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from MoreCategory");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(name) like lower(:name)");
				sql.append(" or lower(id) like lower(:id))");
				
				params.put("name","%"+keyCode+"%");
				params.put("id","%"+keyCode+"%");
			}
			
			if(StringUtils.isNotBlank(areaid)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" areaid = :areaid ");
				params.put("areaid", areaid);
			}
			
			if(StringUtils.isNotBlank(levels)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" levels = :levels ");
				params.put("levels", Integer.parseInt(levels));
			}
			
			if(StringUtils.isNotBlank(lastlevel)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" lastlevel = :lastlevel ");
				params.put("lastlevel", Integer.parseInt(lastlevel));
			}
			
			Query query = this.entityManager.createQuery(sql.toString(), MoreCategory.class);
			
			setParameters(query, params);
			
			return new ArrayList<MoreCategory>(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Brand> brandAutoComplete(String keyCode, String areaid, String levels, String lastlevel)
			throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from Brand");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(name) like lower(:name)");
				sql.append(" or lower(id) like lower(:id))");
				
				params.put("name","%"+keyCode+"%");
				params.put("id","%"+keyCode+"%");
			}
			
			if(StringUtils.isNotBlank(areaid)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" areaid = :areaid ");
				params.put("areaid", areaid);
			}
			
			if(StringUtils.isNotBlank(levels)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" levels = :levels ");
				params.put("levels", Integer.parseInt(levels));
			}
			
			if(StringUtils.isNotBlank(lastlevel)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" lastlevel = :lastlevel ");
				params.put("lastlevel", Integer.parseInt(lastlevel));
			}
			
			Query query = this.entityManager.createQuery(sql.toString(), Brand.class);
			
			setParameters(query, params);
			
			return new ArrayList<Brand>(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Spec> specAutoComplete(String keyCode, String areaid) throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from Spec ");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(name) like lower('%" + keyCode.replaceAll("'", "") + "%')");
				sql.append(" or lower(id) like lower('%" + keyCode.replaceAll("'", "") + "%'))");
			}
			
			if(StringUtils.isNotBlank(areaid)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" areaid = :areaid ");
				params.put("areaid", areaid);
			}
			
			Query query = this.entityManager.createQuery(sql.toString(), Spec.class);
			
			setParameters(query, params);
			
			return new ArrayList<Spec>(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Account> accountAutoComplete(String keyCode, String areaid) throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from Account");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(name) like lower(:name)");
				sql.append(" or lower(id) like lower(:id)");
				sql.append(" or lower(shortString) like lower(:shortstring))");
				
				params.put("name","%"+keyCode+"%");
				params.put("id","%"+keyCode+"%");
				params.put("shortstring","%"+keyCode+"%");
			}
			
			if(StringUtils.isNotBlank(areaid)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" areaid = :areaid ");
				params.put("areaid", areaid);
			}
			
			Query query = this.entityManager.createQuery(sql.toString(), Account.class);
			
			setParameters(query, params);
			
			return new ArrayList<Account>(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> employeeAutoComplete(String keyCode) throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from Employee");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(name) like lower(:name)");
				sql.append(" or lower(id) like lower(:id)");
				sql.append(" or lower(shortString) like lower(:shortstring))");
				
				params.put("name","%"+keyCode+"%");
				params.put("id","%"+keyCode+"%");
				params.put("shortstring","%"+keyCode+"%");
			}
			
			
			Query query = this.entityManager.createQuery(sql.toString(), Employee.class);
			
			setParameters(query, params);
			
			return new ArrayList<Employee>(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<JcBill> jcBillAutoComplete(String keyCode) throws Exception {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from JcBill ");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" lower(id) like lower('%" + keyCode.replaceAll("'", "") + "%')");
				params.put("id", keyCode);
			}
			
			Query query = this.entityManager.createQuery(sql.toString(), JcBill.class);
			
			List<JcBill> products = query.getResultList();
			
			return products;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AbacusConfigItem> abacusConfigItemGroupAutoComplete(String keyCode, String keyGroup, String key) throws Exception {
		try {

			Map<String,Object> params = new HashMap<String, Object>();
			
			StringBuffer sql = new StringBuffer("from AbacusConfigItem");
			if(StringUtils.isNotBlank(keyCode)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" (lower(code) like lower(:code)");
				sql.append(" or lower(name) like lower(:name))");
				
				params.put("code","%"+keyCode+"%");
				params.put("name","%"+keyCode+"%");
			}
			
			if(StringUtils.isNotBlank(keyGroup)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" keyGroup = :keyGroup ");
				params.put("keyGroup", keyGroup);
			}
			if(StringUtils.isNotBlank(key)){
				sql.append(params.size() > 0 ? " and " : " where ");
				sql.append(" key = :key ");
				params.put("key", key);
			}
			
			Query query = this.entityManager.createQuery(sql.toString(), AbacusConfigItem.class);
			
			setParameters(query, params);
			
			return new ArrayList<AbacusConfigItem>(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

	@Override
	public Map<String, Map<String, String>> getGovbs() throws Exception {
		try {
			Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
			
			String sql = "select city, id, name from basearea order by id desc offset 0 rows fetch next 3000 rows only ";
			List<BaseArea> lists = jdbcTemplate.query(sql, new BeanPropertyRowMapper<BaseArea>(BaseArea.class));
			
			if(lists!=null&&!lists.isEmpty()){
				for(BaseArea obj:lists){
					Map<String, String> mapb = map.get(obj.getCity());
					if(mapb==null){
						mapb = new HashMap<String, String>();
						map.put(obj.getCity(), mapb);
					}
					mapb.put(obj.getId(), obj.getName());
				}
			}
			
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
			throw new Exception(e.getMessage());
		}
	}

}











