package com.org.hktshop.base;

import org.springframework.beans.factory.annotation.Autowired;

import com.org.hktshop.entity.Page;
import com.org.hktshop.utils.ExecSqlLib;
import com.org.hktshop.utils.StringUtils;

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

/**
 * 业务逻辑层基类<br/>
 * 继承基类后必须配置CacheConfig(cacheNames="")
 * mxk
 */
public  abstract class BaseService<T extends BaseModel> implements IBaseService<T> {
	
	@Autowired
	protected  IBaseDaoJdbc<T> baseJdbcDao;
	
	
	/***
	 * 查询单表行数
	 */
	@Override
	public int selectCountJdbc(T record){
		int count = 0;
		try {
			String sql = ExecSqlLib.GetCountSql(record);
			count =  baseJdbcDao.selectCount(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	/***
	 * 查詢單表集合
	 */
	@Override
	public List<T> queryList(T record){
		List<T> list = null;
		try {
			String sql = ExecSqlLib.GetQuerySql(record);
			list = baseJdbcDao.queryList(sql,record);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/***
//	 * 根據ID獲取表對應數據
	 * @param id
	 * @param record
	 * @return
	 */
	@Override
	public T queryByIdJbdc(String id,T record) {
		T t = null;
		try {
			Map<String, Object> ps = new HashMap<>();
			ps.put("id", id);
			String sql = ExecSqlLib.GetQuerySql(ps,record);
			t =  baseJdbcDao.query(sql, record);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}

	/****
	 * 根据map条件查询 单条信息
	 * @param queryMap
	 * @param
	 * @return
	 */
	public T queryOne(Map queryMap, T record){
		T t = null;
		try {
			String sql = ExecSqlLib.GetQuerySql(queryMap,record);
			t =  baseJdbcDao.query(sql, record);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}
	
	/***
	 * 更新獲或插入數據
	 */
	@Override
	public boolean updateJdbc(T record) {
		boolean b = true;
		try {
			String sql = "";
			if (record.getId() == null || "".equals(record.getId())) {
				sql = ExecSqlLib.GetInsertSql(record);
			} else {
				sql = ExecSqlLib.GetUpdateSql(record);
			}
			b = baseJdbcDao.execute(sql);
		} catch (Exception e) {
			b = false;
			e.printStackTrace();
		}
		return b;
	}
	
	/***
	 * 根据ID删除一条数据
	 * @param id
	 * @return
	 */
	@Override
	public boolean deleteById(String id,T record){
		boolean b = true;
		try {
			if(!StringUtils.isNull(id)){
				String sql = ExecSqlLib.GetDeleteByIdSql(id, record);
				b = baseJdbcDao.execute(sql);

			}
		} catch (Exception e) {
			e.printStackTrace();
			b = false;
		}
		return b;
	}

	@Override
	public boolean delete(T record){
		boolean b = true;
		try {
			if(!StringUtils.isNull(record)){
				String sql = ExecSqlLib.GetDeleteSql(record);
				b = baseJdbcDao.execute(sql);

			}
		} catch (Exception e) {
			e.printStackTrace();
			b = false;
		}
		return b;
	}
	
	/***
	 * /***
	 * 查询某一页数据
	 * @return
	 */
	@Override
	public Page<T> queryPages(T record) {
		// TODO Auto-generated method stub
		Page<T> page  = new Page<T>();
		try {
			String sql = ExecSqlLib.GetQuerySql(record);
			page = baseJdbcDao.queryPage(sql,record);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();;
		}
		return page;
	}

	/***
	 * /***
	 * 查询某一页数据
	 * @return
	 */
//	public Page<T> queryPageArray(T record) {
//		// TODO Auto-generated method stub
//		Page<T> page  = new Page<T>();
//		try {
//			String sql = ExecSqlLib.GetQuerySql(record);
//			page = baseJdbcDao.queryPageArray(sql, record.getPageNum(), record.getPageSize());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return page;
//	}
}
