package com.fr.tx.common.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fr.tx.common.dao.CommonDao;
import com.fr.tx.common.dao.base.BaseDao;
import com.fr.tx.common.entity.ResourceEntity;
import com.fr.tx.common.entity.RoleResourceEntity;
import com.fr.tx.common.entity.UserEntity;
import com.fr.tx.common.util.MD5;

public class CommonDaoImpl extends BaseDao implements CommonDao{

	@Override
	public String searchShowSn(String input) {
		// 定义返回值
		String sn = "";
		try{
			makeSession();
			// 查询显示用编号
			sn = session.selectOne("Common.searchShowSn", input);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
        return sn;
	}

	@Override
	public String searchNextSn(String input) {
		// 定义返回值
		String sn = "";
		try{
			makeSession();
			sn = session.selectOne("Common.searchNextSn", input);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
        return sn;
	}

	@Override
	public List<ResourceEntity> searchResourceAll() {
		// 定义返回值
        List<ResourceEntity> en = null;
        try{
			makeSession();
			en = session.selectList("Common.searchResourceAll");
        }catch(Exception e){
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
        return en;
	}

	@Override
	public List<RoleResourceEntity> searchResourceCheck(String roleSn) {
		// 定义返回值
        List<RoleResourceEntity> en =null;
        try{
			makeSession();
			en = session.selectList("Common.searchResourceCheck",roleSn);
        }catch(Exception e){
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
        return en;
	}

	@Override
	public void deleteAllResourceByRoleSn(String roleSn) {
		try{
			makeSession();
			session.delete("Common.deleteAllResourceByRoleSn", roleSn);
			session.commit();
		}catch(Exception e){
			session.rollback();
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
   	}

	@Override
	public void insertResourceByRoleSn(List<RoleResourceEntity> en) {
		try{
			makeSession();
			session.insert("Common.insertResourceByRoleSn", en);
			session.commit();
		}catch(Exception e){
			session.rollback();
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
	}
	
	@Override
	public List<String> searchTypeOneByOrgSn(String roleSn) {
		// 定义返回值
        List<String> en = null;
        try{
        	makeSession();
        	en = session.selectList("Common.searchTypeOneByOrgSn",roleSn);
        }catch(Exception e){
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
        return en;
	}

	@Override
	public UserEntity loginSys(UserEntity con) {
		// 登录查询
		// password->MD5
		con.setPassword(MD5.GetMD5Code(con.getPassword()));
		UserEntity result = new UserEntity();
		List<UserEntity> en = null;
		try{
			makeSession();
			en = session.selectList("Common.loginSys",con);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
		if(en!=null && en.size()>0){
			result = en.get(0);
			// 查询成功
			result.setLoginFlg(true);
		}
		return result;
	}

	@Override
	public String getNoSn(String type,String date) {
		// 获取当前日期时间
		
		Map<String,String> con = new HashMap<String,String>();
		con.put("type",type);
		con.put("date", date);
		// 现根据type查询到最新的一条记录
		String sn = "";
		try{
			makeSession();
			sn = session.selectOne("Common.getNoSn",con);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			// 用完要close
			close();
		}
		//如果没有取到数据则为当天的00001单号
		if(sn==null){
			sn =date+"00001";
		}else{
			// MAX+1
			sn = getMaxPlusOne(date,sn);
		}
		return type+sn;
	}
	
	@Override
	public void updateNoSn(String saleSn) {
		// 根据type 和日期检索 如果有则更新no，否则插入一条新的
		Map<String,String> con = new HashMap<String,String>();
		if(saleSn.length()>=15){
			con.put("type",saleSn.substring(0, 2));
			con.put("date",saleSn.substring(2, 10));
			con.put("sn",saleSn.substring(10, 15));
			// 定义返回值
			String sn = "";
			try{
				makeSession();
				sn = session.selectOne("Common.getNoSn",con);
				// insert
				if(sn==null){
					session.insert("Common.insertNoSn",con);
				// update
				}else{
					session.update("Common.updateNoSn",con);
				}
				session.commit();
	        }catch (Exception e) {
	        	e.printStackTrace();
			}finally {
				close();
			}
		}
	}
	
	private String getMaxPlusOne(String date,String input){
		// 2018071200001=>2018071200002
		String output = date;
		String newno = String.format("%05d", (Integer.parseInt(input)+1));
		return output+newno;
	}


}
