package com.uas.account.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.alibaba.druid.util.StringUtils;
import com.uas.account.core.Page;
import com.uas.account.entity.ChangeAdminDetail;

@Repository
public class ChangeAdminDaoImpl extends BaseDao implements ChangeAdminDao {

	/**
	 * 将数据存入mange中ac$changeadmin表中
	 */
	@Override
	public ChangeAdminDetail save(ChangeAdminDetail changeAdminDetail) {
		int count = count("from ac$changeadmin where ch_bussinesscode=?", changeAdminDetail.getBusinessCode());
		changeAdminDetail.setId(generateId("AC$CHANGEADMIN_SEQ"));
		if (count == 0) {
			jdbcTemplate.update(
					"insert into ac$changeadmin(ch_id,ch_enname,ch_bussinesscode,ch_bussinesscodeimg,ch_adminname,ch_admintel,ch_adminemail,ch_auditstatus,ch_date,ch_appid,ch_method) values (?,?,?,?,?,?,?,?,?,?,?)",
					changeAdminDetail.getId(), changeAdminDetail.getEnName().trim(),
					changeAdminDetail.getBusinessCode().trim(), changeAdminDetail.getBusinessCodeImage(),
					changeAdminDetail.getAdminName().trim(), changeAdminDetail.getAdminTel().trim(),
					changeAdminDetail.getAdminEmail().trim(), changeAdminDetail.getAuditStatus(),
					changeAdminDetail.getEnDate(), changeAdminDetail.getAppId(), changeAdminDetail.getMethod());
		} else {
			jdbcTemplate.update(
					"update ac$changeadmin set ch_enname=?,ch_bussinesscodeimg=?,ch_adminname=?,ch_admintel=?,ch_adminemail=?,ch_auditstatus=?,ch_auditresult=?,ch_date=?,ch_appid=?,ch_method=? where ch_bussinesscode=?",
					changeAdminDetail.getEnName().trim(), changeAdminDetail.getBusinessCodeImage(),
					changeAdminDetail.getAdminName().trim(), changeAdminDetail.getAdminTel().trim(),
					changeAdminDetail.getAdminEmail().trim(), changeAdminDetail.getAuditStatus(),
					changeAdminDetail.getAuditResult(), changeAdminDetail.getEnDate(), changeAdminDetail.getAppId(),
					changeAdminDetail.getMethod(), changeAdminDetail.getBusinessCode().trim());
		}
		return changeAdminDetail;
	}

	private RowMapper<ChangeAdminDetail> getRowMapper() {
		return new RowMapper<ChangeAdminDetail>() {
			@Override
			public ChangeAdminDetail mapRow(ResultSet rs, int rowNum) throws SQLException {
				ChangeAdminDetail detail = new ChangeAdminDetail();
				detail.setAdminName(rs.getString("CH_ADMINNAME"));
				detail.setAdminEmail(rs.getString("CH_ADMINEMAIL"));
				detail.setAdminTel(rs.getString("CH_ADMINTEL"));
				detail.setEnName(rs.getString("CH_ENNAME"));
				detail.setBusinessCode(rs.getString("CH_BUSSINESSCODE"));
				detail.setBusinessCodeImage(rs.getString("CH_BUSSINESSCODEIMG"));
				detail.setAuditStatus(rs.getInt("CH_AUDITSTATUS"));
				detail.setAuditResult(rs.getString("CH_AUDITRESULT"));
				detail.setAppId(rs.getString("CH_APPID"));
				Date date = rs.getDate("ch_date");
				if (date != null)
					detail.setEnDate(date);
				detail.setMethod(rs.getString("ch_method"));
				return detail;
			}

		};
	}

	/**
	 * 查询ac$changeadmin所有信息
	 */
	@Override
	public Page<ChangeAdminDetail> findAll(Integer pageNumber, Integer pageSize, String name, String businessCode) {
		StringBuffer countSql = new StringBuffer("select count(1) from ac$changeadmin where 1=1");
		StringBuffer querySql = new StringBuffer(
				"select * from (select t.*,rownum r from (select * from ac$changeadmin where 1=1");
		Object[] args = new Object[] {};
		int total = count(countSql.toString(), args);
		querySql.append(" order by ch_id) t where rownum <= ?) where r >= ?");
		args = Arrays.copyOf(args, args.length + 2);
		args[args.length - 2] = pageNumber * pageSize;
		args[args.length - 1] = (pageNumber - 1) * pageSize + 1;
		List<ChangeAdminDetail> content = null;
		try {
			content = jdbcTemplate.query(querySql.toString(), getRowMapper(), args);
		} catch (EmptyResultDataAccessException e) {

		}
		return new Page<ChangeAdminDetail>(pageNumber, pageSize, content, total);
	}

	/**
	 * 按auditStatus分类查找信息
	 */
	@Override
	public Page<ChangeAdminDetail> findAll(Integer pageNumber, Integer pageSize, Integer auditStatus, String name,
			String businessCode, String userName) {
		StringBuffer countSql = new StringBuffer("select count(1) from ac$changeadmin where 1=1");
		StringBuffer querySql = new StringBuffer(
				"select * from (select t.*,rownum r from (select * from ac$changeadmin where 1=1");
		Object[] args = new Object[] {};
		if (null != auditStatus) {
			countSql.append(" and CH_AUDITSTATUS=?");
			querySql.append(" and CH_AUDITSTATUS=?");
			args = Arrays.copyOf(args, args.length + 1);
			args[args.length - 1] = auditStatus;
		}
		if (!StringUtils.isEmpty(businessCode)) {
			countSql.append("and upper(ch_bussinesscode) = upper('").append(businessCode).append("')");
			querySql.append("and upper(ch_bussinesscode) = upper('").append(businessCode).append("')");
		}
		if (!StringUtils.isEmpty(name)) {
			countSql.append("and upper(ch_enname) like upper('%").append(name).append("%')");
			querySql.append("and upper(ch_enname) like upper('%").append(name).append("%')");
		}
		if (!StringUtils.isEmpty(userName)) {
			countSql.append("and upper(ch_adminname) = upper('").append(userName).append("')");
			querySql.append("and upper(ch_adminname) = upper('").append(userName).append("')");
		}
		int total = count(countSql.toString(), args);

		querySql.append(" order by ch_id) t where rownum <= ?) where r >= ?");
		args = Arrays.copyOf(args, args.length + 2);
		args[args.length - 2] = pageNumber * pageSize;
		args[args.length - 1] = (pageNumber - 1) * pageSize + 1;
		List<ChangeAdminDetail> content = null;
		try {
			content = jdbcTemplate.query(querySql.toString(), getRowMapper(), args);
		} catch (EmptyResultDataAccessException e) {

		}
		return new Page<ChangeAdminDetail>(pageNumber, pageSize, content, total);
	}

	@Override
	public ChangeAdminDetail findByBusinessCode(String businessCode) {
		try {
			return jdbcTemplate.queryForObject("select * from ac$changeadmin where ch_bussinessCode=?", getRowMapper(),
					businessCode);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
}
