package com.palmnest.service;

import com.palmnest.application.bean.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component("innerOtherService")
public class InnerOtherDatasoureService {
	/*private InnerOracleDao innerOracleDao;
	
	@Autowired
	public void setInnerOracleDao(InnerOracleDao innerOracleDao) {
		this.innerOracleDao = innerOracleDao;
	}
	
	public List getUserList(String persionname,String deptname){
		return innerOracleDao.getUserList(persionname, deptname);
	}*/

	@Qualifier("mysoftJdbcTemplate")
	@Resource
	private JdbcTemplate mysoftJdbcTemplate;


	/**
	 * 根据接待编号，获取明源工单状态
	 *
	 * @param receptionNo
	 * @return
	 */
	public List getReceiveListByNo(String receptionNo) {
		String strSQL = "select status from k_receive where receivecode='" + receptionNo + "'";
		List   list   = mysoftJdbcTemplate.queryForList(strSQL);
		return list;
	}


	/**
	 * 获取员工列表
	 *
	 * @param persionname
	 * @param deptname
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page getUserList(String persionname, String deptname, int pageNo, int pageSize) {

		String sql = "select u.accountno,u.username,u.deptname,u.mobile,u.companyname,u.id,u.sex,u.jobname,u.userstatus,u.pdjb,u.indutydate,u.zgxl from dc_cl_user u where 1=1";

		if (StringUtils.isNotEmpty(persionname)) {
			sql += " and username like '%" + persionname + "%'";
		}

		if (StringUtils.isNotEmpty(deptname)) {
			sql += " and deptname like '%" + deptname + "%'";
		}

		return pagedQueryByJdbcTemplate(sql, pageNo, pageSize);
	}

	/**
	 * 根据员工号 获取员工信息
	 *
	 * @param accountno
	 * @return
	 */
	public List getUserByNo(String accountno) {

		String sql = "select * from dc_cl_user where 1=1";

		if (StringUtils.isNotEmpty(accountno)) {
			sql += " and accountno ='" + accountno + "'";
		}

		List list = mysoftJdbcTemplate.queryForList(sql);

		return list;
	}

	/**
	 * 分页查询
	 *
	 * @param sql
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pagedQueryByJdbcTemplate(String sql, int pageNo, int pageSize) {

		if (StringUtils.isEmpty(sql)) {
			return new Page();
		}

		if (pageNo == 0) {
			pageNo = 1;
		}

		// Count查询
		//如果传过来的页码大于等于Integer.MAX_VALUE，则表示查询全部，否则按照分页查询:即页码小于的时候查询总数
		long totalCount = 0;
		if (pageSize < Integer.MAX_VALUE) {
			String countQueryString = "";
			countQueryString = " select count(*) num from (" + sql + ") tabs";
        	/*if(sql.toLowerCase().indexOf("union")>0){//union 语句
        		countQueryString = " select count(*) num from ("+sql+") tabs";
        	}else{
        		countQueryString = " select count(*) num " + removeSelect(removeOrders(sql)); 
        	}*/
			totalCount = mysoftJdbcTemplate.queryForLong(countQueryString);
			if (totalCount < 1) {
				return new Page();
			}
		}

		// 获取分页查询的SQL,现在只支持mysql数据库，如果其他数据库需要扩展
		//如果页大小为0，则查询所有信息
		if (pageSize != 0) {
			//sql  = this.getMySqlPageSql(sql, pageNo, pageSize);
			sql = this.getOraclePageSql(sql, pageNo, pageSize);
		}

		System.out.println(sql);

		List list = mysoftJdbcTemplate.queryForList(sql);

		if (pageSize == 0) {
			pageSize = list.size();
		}

		//int startIndex = Page.getStartOfPage(pageNo, pageSize);
		return new Page(pageNo, totalCount, pageSize, list);
	}


	public String getMySqlPageSql(String sql, int pageNo, int pageSize) {

		StringBuilder sb = new StringBuilder(sql);
		// 计算第一条记录的位置
		int offset = (pageNo - 1) * pageSize;
		sb.append(" limit " + offset + "," + pageSize);
		// 上面的Sql语句拼接之后大概是这个样子：
		// select * from (select u.*, rownum r from (select * from t_user) u where rownum < 31)
		// where r >= 16
		return sb.toString();
	}

	public String getOraclePageSql(String sql, int pageNo, int pageSize) {

		StringBuilder sb = new StringBuilder(sql);

		// 计算第一条记录的位置，Oracle分页是通过rownum进行的，而rownum是从1开始的
		int offset = (pageNo - 1) * pageSize + 1;
		sb.insert(0, "select u.*, rownum r from (").append(") u ");
		sb.insert(0, "select * from (").append(") where r >= ").append(offset).append(" and r < ").append(offset + pageSize);
		// 上面的Sql语句拼接之后大概是这个样子：
		// select * from (select u.*, rownum r from (select * from t_user) u where rownum < 31)
		// where r >= 16
		return sb.toString();
	}
}
