package com.xmomen.module.wb.controller;

import java.security.Principal;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.xmomen.framework.exception.BusinessException;
import com.xmomen.module.base.entity.CdMember;
import com.xmomen.module.base.model.CreateMember;
import com.xmomen.module.base.service.MemberService;
import com.xmomen.module.member.model.MemberAddressCreate;
import com.xmomen.module.wb.model.PcMember;
import com.xmomen.module.wb.model.PcMemberInfo;

@RestController
public class CommonMemberController {

	@Autowired
	MemberService memberService;
	
	/**
	 * 普通用户注册
	 */
	@RequestMapping(value = "/member/register", method = RequestMethod.POST)
	public CdMember register(@RequestBody @Valid PcMember createPcMember) {
		CreateMember createMember = new CreateMember();
		createMember.setName(createPcMember.getName());
		createMember.setPhoneNumber(createPcMember.getPhoneNumber());
		createMember.setPassword(createPcMember.getPassword());
		createMember.setMemberAddressList(new ArrayList<MemberAddressCreate>());
		createMember.setEmail(createPcMember.getEmail());
		
		CdMember memberQuery = new CdMember();
		memberQuery.setPhoneNumber(createPcMember.getPhoneNumber());
		CdMember cdMember = memberService.findMember(memberQuery);
		if(cdMember == null) {
			cdMember = memberService.createMember(createMember);
		} else if(StringUtils.isEmpty(cdMember.getPassword())) {
			memberService.updatePassword(cdMember.getId(), createPcMember.getPassword(), "");
		} else {
			throw new IllegalArgumentException("用户已被注册");
		}
		return cdMember;
	}
	
	@RequestMapping(value = "/member/login", method = RequestMethod.POST)
    public PcMemberInfo login(HttpServletRequest request, Model model) throws Exception{
        if(SecurityUtils.getSubject().isAuthenticated()){
            String phoneNumber = (String) SecurityUtils.getSubject().getPrincipal();
            // 可能是
            CdMember query = new CdMember();
            query.setPhoneNumber(phoneNumber);
            CdMember cdMember = memberService.findMember(query);
            if(cdMember == null) {
                throw new BusinessException("用户名不存在");
            } else {
            	PcMemberInfo pcMemberInfo = new PcMemberInfo();
            	pcMemberInfo.setMemberId(cdMember.getId());
            	pcMemberInfo.setPhoneNumber(cdMember.getPhoneNumber());
            	//pcMemberInfo.setEmail(cdMember.getEmail());
            	pcMemberInfo.setName(cdMember.getName());
            	return pcMemberInfo;
            }
        }
        String exceptionClassName = (String)request.getAttribute("shiroLoginFailure");
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名不存在";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        } else {
            error = "系统错误";
        }
        throw new BusinessException(error);
    }
	
	@RequestMapping(value = "/member/logout")
    public boolean logout(HttpServletRequest request) throws Exception{
		if(SecurityUtils.getSubject().isAuthenticated()){
			SecurityUtils.getSubject().logout();
		}
		return true;
    }
}

@@ -39,40 +39,8 @@
 r;%0A%0A
-import java.security.Principal;%0A
 impo
@@ -63,16 +63,16 @@
 ayList;%0A
+
 %0Aimport 
@@ -334,32 +334,89 @@
 countException;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.sprin
@@ -462,16 +462,108 @@
 owired;%0A
+import org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0A
 import o
@@ -1047,16 +1047,85 @@
 ervice;%0A
+import com.xmomen.module.core.web.filter.PcFormAuthenticationFilter;%0A
 import c
@@ -1284,16 +1284,16 @@
 troller%0A
-
 public c
@@ -1323,16 +1323,104 @@
 ller %7B%0A%0A
+%09private static Logger logger = LoggerFactory.getLogger(CommonMemberController.class);%0A%0A
 %09@Autowi
@@ -4018,15 +4018,22 @@
 lic 
-boolean
+ResponseEntity
  log
@@ -4067,32 +4067,51 @@
 est)
- throws Exception
+%7B%0A%09%09String message = %22SUCCESS%22;%0A%09%09try 
 %7B%0A%09%09
+%09
-if(
 Secu
@@ -4137,85 +4137,161 @@
 t().
-isAuthenticated())%7B%0A%09%09%09SecurityUtils.getSubject().logout();%0A%09%09%7D%0A%09%09return true
+logout();%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09logger.error(%22%E7%99%BB%E5%87%BA%E5%A4%B1%E8%B4%A5%22, e);%0A%09%09%09message = %22FAILURE%22;%0A%09%09%7D%0A%09%09return new ResponseEntity(message, HttpStatus.UNAUTHORIZED)
 ;%0A  

优化logout API