package com.uas.platform.b2b.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.platform.b2b.model.Attach;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.FileUpload;
import com.uas.platform.b2b.service.AttachService;
import com.uas.platform.b2b.service.EnterpriseService;
import com.uas.platform.b2b.service.UserService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 企业请求
 * @author suntg
 * @date 2015年3月10日17:37:37
 */
@Controller
@RequestMapping(value = "/signin")
public class RegisterController {
	
	@Autowired
	private EnterpriseService enterpriseService;
	@Autowired
	private AttachService attachService;
	@Autowired
	private UserService userService;
	
	/**
	 * 执照号是否可用
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/codeEnable", method = RequestMethod.GET)
	public ResponseEntity<String> bussinessCodeEnable(String code) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/text; charset=utf-8");
		return new ResponseEntity<String>(enterpriseService.bussinessCodeEnable(code), 
				headers, HttpStatus.OK);
	}
	
	/**
	 * 注册新企业用户
	 */
	@RequestMapping("/register.action")
	public @ResponseBody
	ResponseEntity<ModelMap> register(String enterprise, FileUpload uploadItem) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json; charset=utf-8");
		ModelMap map = new ModelMap();
		Enterprise newEnterprise = FlexJsonUtils.fromJson(enterprise, Enterprise.class);//需要把字符串转成Object
		Attach attach = attachService.upload(uploadItem, "bussinessCodeImg", "客户营业执照复印件或照片");//先保存上传到文件
		if(attach == null){//文件过大
			map.put("error", "文件过大");
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.FORBIDDEN);
		} else {//上传成功
			newEnterprise.setEnBussinessCodeAttach(attach);
			Enterprise regEnterprise = enterpriseService.registerEnterprise(newEnterprise);
			if(regEnterprise == null) {//营业执照码验证
				map.put("error", "操作失败");
				return new ResponseEntity<ModelMap>(map, headers, HttpStatus.EXPECTATION_FAILED);
			}
			map.put("uu", regEnterprise.getUu());
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.OK);
		}
	}
	
	/**
	 * 处理审核结果
	 */
	@RequestMapping(value = "/audit", method = RequestMethod.POST)
	public @ResponseBody
	ResponseEntity<ModelMap> aduit(Long uu, String result, String reason) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json; charset=utf-8");
		ModelMap map = new ModelMap();
		System.out.println(uu);
		System.out.println(result);
		System.out.println(reason);
		String aduitReslt = enterpriseService.audit(uu, result, reason);
		map.put("aduit", aduitReslt);
		if(aduitReslt.equals("ALLOW") || aduitReslt.equals("NOTALLOW")) {
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.OK);
		} else {
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.EXPECTATION_FAILED);
		}
	}
	
	/**
	 * 激活码验证激活
	 */
	@RequestMapping(value = "/activate", method = RequestMethod.POST)
	public @ResponseBody
	ResponseEntity<ModelMap> activateCheck(Long uu, String checkcode) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json; charset=utf-8");
		ModelMap map = new ModelMap();
		String checkReslt = enterpriseService.activateCheck(uu, checkcode);
		map.put("result", checkReslt);
		if(checkReslt.equals("SUCCESS")) {//验证通过
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.OK);
		} else {//验证不通过
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.EXPECTATION_FAILED);
		}
	}
	
	/**
	 * 设置管理员账号密码
	 */
	@RequestMapping(value = "/setAdminPassword", method = RequestMethod.POST)
	public @ResponseBody
	ResponseEntity<ModelMap> setAdminPassword(Long enuu, String password) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json; charset=utf-8");
		ModelMap map = new ModelMap();
		String result = enterpriseService.setAdminPassword(enuu, password);
		map.put("result", result);
		if(result.equals("SUCCESS")) {//验证通过
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.OK);
		} else {//验证不通过
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.EXPECTATION_FAILED);
		}
	}
	
	/**
	 * 重新发送激活邮件
	 */
	@RequestMapping(value = "/setEmailAgain", method = RequestMethod.GET)
	public @ResponseBody
	ResponseEntity<ModelMap> setActivateEmailAgain(Long enuu) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json; charset=utf-8");
		ModelMap map = new ModelMap();
		String result = enterpriseService.sendActivateEmail(enuu);
		map.put("result", result);
		if(result.equals("SUCCESS")) {//验证通过
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.OK);
		} else {//验证不通过
			return new ResponseEntity<ModelMap>(map, headers, HttpStatus.EXPECTATION_FAILED);
		}
	}
	
	/**
	 * 手机号是否可用
	 * @param tel
	 * @return
	 */
	@RequestMapping(value = "/telEnable", method = RequestMethod.GET)
	public ResponseEntity<Boolean> telEnable(String tel) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/text; charset=utf-8");
		return new ResponseEntity<Boolean>(userService.isTelUseable(tel), 
				headers, HttpStatus.OK);
	}
	
	/**
	 * 邮箱地址是否可用
	 * @param email
	 * @return
	 */
	@RequestMapping(value = "/emailEnable", method = RequestMethod.GET)
	public ResponseEntity<Boolean> emailEnable(String email) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/text; charset=utf-8");
		return new ResponseEntity<Boolean>(userService.isEmailUseable(email), 
				headers, HttpStatus.OK);
	}

}
