package com.iamberry.wechat.service.home;
import java.util.List;

import com.iamberry.wechat.service.mapper.AdminBannerMapper;
import com.iamberry.wechat.service.mapper.HomeMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.banner.ActionBannerImage;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.banner.AdminBannerService;
import com.iamberry.wechat.face.home.HomeService;
import com.iamberry.wechat.service.home.dao.HomeServiceDao;
/**
 * @author 何秀刚
 * Class Description: 首页Service接口
 * Create Date:2016年4月18日
 * Update Date:2016年4月18日
 */
@Service
public class HomeServiceImpl implements HomeService {

	@Autowired
	private AdminBannerMapper adminBannerMapper;

	@Autowired
	private HomeMemberMapper homeMemberMapper;
	
	@Override
	public List<ActionBannerImage> selectNowBannerByType() {
		// TODO Auto-generated method stub
		return adminBannerMapper.selectNowBannerByType(null);
	}

	@Override
	public Member selectMemberInfoByOpenId(String openid) {
		// TODO Auto-generated method stub
		return homeMemberMapper.selectMemberInfoByOpenId(openid);
	}

	@Override
	public Integer updateWechatMemberInfoByByOpenid(Member member) {
		// TODO Auto-generated method stub
		return homeMemberMapper.updateWechatMemberInfoByByOpenid(member);
	}

	@Override
	public Integer insertMemberInfo(Member member) {
		// TODO Auto-generated method stub
		try {
			return homeMemberMapper.insertMemberInfo(member);
		} catch (DuplicateKeyException e) {
			// 报错，触发数据库唯一约束错误，系统捕获，而后对获取数据重头再来
			member = homeMemberMapper.selectMemberInfoByOpenId(member.getUserOpenid());
			return 1;
		}
	}

	@Override
	public Integer updateUserResAndTypeByOpenid(Member member) {
		// TODO Auto-generated method stub
		return homeMemberMapper.updateUserResAndTypeByOpenid(member);
	}

	@Override
	public Integer updateMemberStatusByOpenId(String openid) {
		// TODO Auto-generated method stub
		return homeMemberMapper.updateMemberStatusByOpenId(openid);
	}


	@Override
	public Member selectMemberQrcodeUrlByOpenid(String openid) {
		// TODO Auto-generated method stub
		return homeMemberMapper.selectMemberQrcodeUrlByOpenid(openid);
	}


	@Override
	public Integer updateMemberQrcodeByOpenid(Member member) {
		// TODO Auto-generated method stub
		return homeMemberMapper.updateMemberQrcodeByOpenid(member);
	}

	public Integer selectUserIntegralByOpenid(String openid) {
		// TODO Auto-generated method stub
		return homeMemberMapper.selectUserIntegralByOpenid(openid);
	}


	@Override
	public Member selectUserInfoByPlaceId(Integer placeId) {
		// TODO Auto-generated method stub
		return homeMemberMapper.selectUserInfoByPlaceId(placeId);
	}
	
	/**
	 * 根据Openid，更新绑定状态
	 * @param member
	 * @return
	 */
	@Override
	public Integer updateMemberIsFlagByOpenid(Member member) {
		// TODO Auto-generated method stub
		return homeMemberMapper.updateMemberIsFlagByOpenid(member);
	}
}
