package com.jw.app.common.action;

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

import com.gaowj.business.SystemOpeBusiness;
import com.gaowj.business.action.BasicAction;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.business.util.Encipher;
import com.gaowj.business.util.PropUtil;
import com.gaowj.business.util.RequestUtils;
import com.gaowj.business.util.Status;
import com.gaowj.service.client.utils.PbeEncrypt;
import com.jw.app.business.UserBusiness;
import com.sun.xml.internal.messaging.saaj.util.Base64;

import net.sf.json.JSONObject;

public class ssoLoginActAction extends BasicAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6870137449795271497L;
	private JSONObject jsonObject = new JSONObject();

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
    public String login() throws BusinessException{
    	UserBusiness business = BusinessManager.getBusiness(UserBusiness.class);
    	SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		System.out.println("验证开始......");
		if(entity.get("EMID") != null){
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("EMID", entity.get("EMID"));
			List<Map<String, Object>> list = systemOpeBusiness.listLoginServiceUserInfo(map1);
			if(list.size() > 0){
				if(entity.get("TYPE") != null){
					if(entity.get("TYPE").toString().equals("0")){
						//手机 user_id验证  USER 是USER_ID  password 是手机
						if(entity.get("USER") != null && entity.get("PASSWORD") != null){
							String userid = (String) entity.get("USER");
							String mobile = (String) entity.get("PASSWORD");
							if(("1").equals(list.get(0).get("PUSH_ENCRYPT"))){
								userid = Base64.base64Decode((String) entity.get("USER"));
								mobile = Base64.base64Decode((String) entity.get("PASSWORD"));
							}else if(("2").equals(list.get(0).get("PUSH_ENCRYPT"))){
								try {
									userid = PbeEncrypt.decrypt(entity.get("USER").toString(), entity.get("EMID").toString());
									mobile = PbeEncrypt.decrypt(entity.get("PASSWORD").toString(), entity.get("EMID").toString());
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							Map<String, Object> query = new HashMap<String, Object>();
							query.put("USER_ID", userid);
							query.put("MOBILE", mobile);
							System.out.println("输出：用户ID="+userid);
							int ret = business.listSsoUser(query);
							if(ret > 0){
								System.out.println("输出:" + ret + " , 验证成功");
								jsonObject.putAll(Status.getStatusSuccessMessage("验证成功"));
							}else{
								System.out.println("输出:" + ret + " , 验证失败");
								jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
							}
						}else{
							jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
							System.out.println("输出：参数错误....");
						}
						System.out.println("结束验证......");
					}else if (entity.get("TYPE").toString().equals("1")){
						//手机 密码验证 user是mobile password 是password
						//String decodeflag = PropUtil.getValue("decodeflag","quartz_service","0");
						if(entity.get("PASSWORD") != null && entity.get("USER") != null){
							String mobile = (String) entity.get("USER");
							String password = (String) entity.get("PASSWORD");
							if(list.get(0).get("PUSH_ENCRYPT").equals("1")){
								mobile = Base64.base64Decode((String) entity.get("USER"));
								password = Base64.base64Decode((String) entity.get("PASSWORD"));
							}else if(list.get(0).get("PUSH_ENCRYPT").equals("2")){
								try {
									mobile = PbeEncrypt.decrypt(entity.get("USER").toString(), entity.get("EMID").toString());
									password = PbeEncrypt.decrypt(entity.get("PASSWORD").toString(), entity.get("EMID").toString());
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							System.out.println("密码： "+ password);
							Map<String, Object> query = new HashMap<String, Object>();
							query.put("MOBILE", mobile);
							System.out.println("输出：用户手机=" + mobile);
							Map<String, Object> map = business.listMobileUser(query);
							if(null == map){
								jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
							}else{
								String userpass = Encipher.DecodePasswd((String)map.get("USER_PWD"));
								if(password.equals(userpass)){
									System.out.println("输出:密码正确，验证成功");
									jsonObject.putAll(Status.getStatusSuccessMessage("验证成功"));
								}else{
									System.out.println("输出:密码错误，验证失败");
									jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
								}
							}
						}else{
							jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
							System.out.println("输出：参数错误....");
						}
						System.out.println("结束验证......");
					}else if (entity.get("TYPE").toString().equals("2")){
						//账号 密码验证 USER是id_number password是password
						if(entity.get("PASSWORD") != null && entity.get("USER") != null){
							String idnumber = (String) entity.get("USER");
							String password = (String) entity.get("PASSWORD");
							if(("1").equals(list.get(0).get("PUSH_ENCRYPT"))){
								idnumber = Base64.base64Decode((String) entity.get("USER"));
								password = Base64.base64Decode((String) entity.get("PASSWORD"));
							}else if(("2").equals(list.get(0).get("PUSH_ENCRYPT"))){
								try {
									idnumber = PbeEncrypt.decrypt(entity.get("USER").toString(), entity.get("EMID").toString());
									password = PbeEncrypt.decrypt(entity.get("PASSWORD").toString(), entity.get("EMID").toString());
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							System.out.println("密码： "+ password);
							Map<String, Object> query = new HashMap<String, Object>();
							query.put("ID_NUMBER", idnumber);
							System.out.println("输出：用户账号=" + idnumber);
							Map<String, Object> map = business.listIdNumberUser(query);
							if(null == map){
								jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
							}else{
								String userpass = Encipher.DecodePasswd((String)map.get("USER_PWD"));
								if(password.equals(userpass)){
									System.out.println("输出:密码正确，验证成功");
									jsonObject.putAll(Status.getStatusSuccessMessage("验证成功"));
								}else{
									System.out.println("输出:密码错误，验证失败");
									jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
								}
							}
						}else{
							jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
							System.out.println("输出：参数错误....");
						}
						System.out.println("结束验证......");
					}
				}else{
					System.out.println("输出:type值错误....");
					jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
					System.out.println("结束验证......");
				}
			}else{
				System.out.println("输出：系统id不正确....");
				jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
				System.out.println("结束验证......");
			}
		}else{
			System.out.println("输出：系统字段缺失....");
			jsonObject.putAll(Status.getStatusErrorMessage("验证失败"));
			System.out.println("结束验证......");
		}
		
		return "json";
    }
}

