package com.cab.service;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cab.dao.CabThirdPartyMapper;
import com.cab.dao.UserMapper;
import com.cab.model.CabThirdParty;
import com.cab.model.User;
import com.cku.core.ZAErrorCode;
import com.cku.core.ZAException;
import com.cku.dao.CkuDvUserMapper;
import com.cku.dao.CkuUserMapper;
import com.cku.model.CkuDvUserModel;
import com.cku.util.Debugger;
import com.cku.util.StringUtils;
import com.sys.dao.AreaMapper;
import com.sys.dao.CityMapper;
import com.sys.dao.ProvinceMapper;
import com.sys.service.TokenService;

@Service("cabMyBoundService")
public class CabMyBoundServiceImpl {
	
	@Autowired
	public CabThirdPartyMapper cabThirdPartyMapper;
	
	@Autowired
	public UserMapper userMapper;
	
	@Autowired
	public ValidationCodeServiceImpl validationCodeService;
	
	@Autowired
	public CkuUserMapper ckuUserMapper;
	
	@Autowired
	public CkuDvUserMapper ckuDvUserMapper;
	
	@Autowired
	public ProvinceMapper provinceMapper;
	
	@Autowired
	public CityMapper cityMapper;
	
	@Autowired
	public AreaMapper areaMapper;
	/**
	 * 
	 * @Description：查询我的账号绑定
	 * @author: zhuoHeng
	 * @version: 2016年4月13日 下午4:42:28
	 */
	public CabThirdParty getMyBoundMessage(Integer userId){
		User user = userMapper.selectByPrimaryId(userId);
		CabThirdParty cabThirdParty = cabThirdPartyMapper.selectByUserId(userId);
		if(cabThirdParty != null){
			cabThirdParty.setId(null);
			cabThirdParty.setCabUserId(null);
			cabThirdParty.setCreateTime(null);
			cabThirdParty.setUpdateTime(null);
		}else {
			cabThirdParty = new CabThirdParty();
			cabThirdParty.setWx("");
			cabThirdParty.setQq("");
			cabThirdParty.setSina("");
			cabThirdParty.setTcb("");
			cabThirdParty.setCkuUserId(null);
		}
		cabThirdParty.setPhone(user.getPhone());
		return cabThirdParty;
	}
	
	/**
	 * 
	 * @Description：根据宠爱王国用户id增加第三方绑定
	 * @author: zhuoHeng
	 * @version: 2016年4月16日 下午12:28:56
	 * @throws ZAException 
	 */
	public void bindAccount(CabThirdParty record,String flag,String value) throws ZAException{
		CabThirdParty cabThirdParty = cabThirdPartyMapper.selectByOpenId(flag,value);
		if("cku".equals(flag)){
			Debugger.doAssert(cabThirdParty==null, ZAErrorCode.ZA_ERC_PHONE_REPEAT, "该CKU账号已被绑定");
			record.setCkuUserId(value);
		} else if("wx".equals(flag)){
			Debugger.doAssert(cabThirdParty==null, ZAErrorCode.ZA_ERC_WC_REPEAT, "该微信号已被绑定");
			record.setWx(value);
		} else if("qq".equals(flag)){
			Debugger.doAssert(cabThirdParty==null, ZAErrorCode.ZA_ERC_QQ_REPEAT, "该QQ号已被绑定");
			record.setQq(value);
		}else if("sina".equals(flag)){
			Debugger.doAssert(cabThirdParty==null, ZAErrorCode.ZA_ERC_SINA_REPEAT, "该新浪账号已被绑定");
			record.setSina(value);
		}else if("tcb".equals(flag)){
			Debugger.doAssert(cabThirdParty==null, ZAErrorCode.ZA_ERC_TCB_REPEAT, "该淘宠宝账号已被绑定");
			record.setTcb(value);
		}
		cabThirdPartyMapper.insertThirdParty(record);
	}
	
	/**
	 * 
	 * @Description：根据宠爱王国用户id解除第三方绑定
	 * @author: zhuoHeng
	 * @version: 2016年4月16日 下午2:50:13
	 */
	public void unbindAccount(CabThirdParty record,String flag){
		if("cku".equals(flag)){
			record.setCkuUserId("");
		} else if("wx".equals(flag)){
			record.setWx("");
		} else if("qq".equals(flag)){
			record.setQq("");
		}else if("sina".equals(flag)){
			record.setSina("");
		}else if("tcb".equals(flag)){
			record.setTcb("");
		}
		cabThirdPartyMapper.insertThirdParty(record);
	}
	/**
	 * 
	 * @Description：账号手机号绑定
	 * @author: zhuoHeng
	 * @version: 2016年4月18日 上午10:06:01
	 */
	public void bindPhone(User user,String validation_code) throws ZAException{
		//验证手机验证码
		validationCodeService.verifyCodeAndUse(user.getPhone(), validation_code);
		//验证手机号码
		Debugger.doAssert(StringUtils.isMobile(user.getPhone()), ZAErrorCode.ZA_ERC_INVALID_PARAMETER, "非法手机号码");
		User u = userMapper.selectByUserPhone(user.getPhone());
		//重复绑定验证
		Debugger.doAssert(u==null, ZAErrorCode.ZA_ERC_PHONE_REPEAT, "该手机号已被绑定");
		userMapper.updateByPrimaryKeySelective(user);
	}
	/**
	 * 微信登录
	 * @throws Exception 
	 */
	public User addWechatLogin(String type,String ip,String openId,String name,String avatar) throws Exception{
		User user = null;
		CabThirdParty ctp = cabThirdPartyMapper.selectByOpenId(type,openId);
		//已经存在该账号的数据
		if(ctp!=null){
			user = userMapper.selectByPrimaryId(ctp.getCabUserId());
			String dvUserId = "-1";
			String ckuUserId = "";
			//根据cab用户id查询出第三方表中对应的CKU账号id
			CabThirdParty cabThirdParty = cabThirdPartyMapper.selectByUserId(ctp.getCabUserId());
			if(cabThirdParty != null){
				dvUserId = cabThirdParty.getCkuUserId();
			}
			//根据CKU账号id查询出CKU账号信息
			CkuDvUserModel ckuDvUserModel = ckuDvUserMapper.getckuMemberByUserId(dvUserId);
			if(ckuDvUserModel != null){
				ckuUserId = ckuDvUserModel.getCkuId();
			}
			user.setVipNum(ckuUserId);
		}else{
			user = new User();
			user.setUsername("");
			user.setPassword("");
			user.setPhone("");
			user.setRegistIp(ip);
			user.setName(name);
			user.setAvatar(avatar);
			//注册宠爱王国用户
			userMapper.insertSelective(user);
			ctp = new CabThirdParty();
			ctp.setCabUserId(new Long(user.getId()).intValue());
			if("wx".equals(type)){
				ctp.setWx(openId);
			}else if("qq".equals(type)){
				ctp.setQq(openId);
			}else if("sina".equals(type)){
				ctp.setSina(openId);
			}
			ctp.setCreateTime(new Date());
			cabThirdPartyMapper.insertSelective(ctp);
		}
		//设置用户返回信息
		String token = TokenService.instance().registerToken(user.getId());
		String provinceName = "";
		if(user.getProvinceId()!=null){
			provinceName = provinceMapper.getProvince(user.getProvinceId().toString());
		}
		String cityName = "";
		if(user.getCityId()!=null){
			cityName = cityMapper.getCity(user.getCityId().toString());
		}
		String areaName = "";
		if(user.getAreaId()!=null){
			areaName = areaMapper.getArea(user.getAreaId().toString());
		}
		//控制省市区展示
		if(provinceName==null){
			user.setAddAess("");
		}else if(cityName==null){
			user.setAddAess(provinceName);
		}else if(areaName==null){
			user.setAddAess(provinceName+" "+cityName);
		}else {
			user.setAddAess(provinceName+" "+cityName+" "+areaName);
		}
		user.setToken(token);
		user.setPassword(null);
		user.setRegistIp(null);
		user.setUsername(null);
		user.setId(null);
		user.setCityId(null);
		user.setAreaId(null);
		user.setProvinceId(null);
		user.setPayPassWord(null);
		user.setCodeTime(null);
		user.setRandomCode(null);
		user.setCardId(null);
		user.setThreeLoginType(type);
		user.setAge("0");
		return user;
	}
	
}
