package com.iamberry.wechat.service.member.dao.imp;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.iamberry.wechat.core.entity.admin.FullReduction;
import com.iamberry.wechat.core.entity.coupon.CouponItem;
import com.iamberry.wechat.service.mapper.CouponItemMapper;
import com.iamberry.wechat.service.mapper.CouponTypeMapper;
import com.iamberry.wechat.tools.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.iamberry.wechat.core.entity.Page;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.service.mapper.MemberMapper;
import com.iamberry.wechat.service.member.dao.MemberDao;

/**
 * 会员信息 DAO 实现
 * 2016年4月21日
 * @author Administrator
 *
 */
@Repository
public class MemberDaoImpl  implements MemberDao {
	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private CouponItemMapper couponItemMapper;
	@Autowired
	private CouponTypeMapper couponTypeMapper;

	/**
	 * 根据userid得到会员信息
	 * @param userId
	 * @return
	 */
	@Override
	public Member getMemberByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return memberMapper.getMemberByUserId(userId);
	}
	
	/**
	 * 根据 openid 查询一个member
	 * 2016年4月28日
	 * @author 穆再兴
	 * @return
	 */
	public Member getMemberByUserOpenId(String openid){
		return memberMapper.getMemberByUserOpenId(openid);
	}
	@Override
	public Integer getCount() {
		// TODO Auto-generated method stub
		return memberMapper.getCount();
	}

	@Override
	public List<Member> getMemberByPage(Page page) {
		// TODO Auto-generated method stub
		return memberMapper.getMemberByPage(page);
	}

	/**
	 * 根据 userid 修改会员信息
	 * 2016年4月21日
	 * @author 穆再兴
	 * @param userId
	 * @return
	 */
	@Override
	public Integer updateMemberByUserId(Member member){
		return memberMapper.updateMemberByUserId(member);
	}
	@Override
	public Integer updateUserUseIntegral(Member member) {
		// TODO Auto-generated method stub
		return memberMapper.updateUserUseIntegral(member);
	}
	@Override
	public List<Member> selectUserIntegralByWechatProxyId(Page page) {
		// TODO Auto-generated method stub
		return memberMapper.selectUserIntegralByWechatProxyId(page);
	}
	@Override
	public Member selectUserIdentityAndQrcodeByOpenId(String openid) {
		// TODO Auto-generated method stub
		return memberMapper.selectUserIdentityAndQrcodeByOpenId(openid);
	}

	@Override
	public Member selectUserResAndIdSByOpenId(String openid) {
		// TODO Auto-generated method stub
		return memberMapper.selectUserResAndIdSByOpenId(openid);
	}
	
	/**
	 * 根据 openid 修改用户可用金额
	 * 2016年5月4日
	 * @param member
	 * @return
	 */
	@Override
	public Integer updateUserIncomeByOpenId(Member member){
		return memberMapper.updateUserIncomeByOpenId(member);
	}

	@Override
	public Integer updateUserFlagByOpenId(String openId) {
		// TODO Auto-generated method stub
		return memberMapper.updateUserFlagByOpenId(openId);
	}
	
	/**
	 * 根据userid获取当前用户的“下线人数”和“已购买人数”
	 * 2016年5月11日
	 * @param openId
	 * @return
	 */
	@Override
	public List<Integer> selectDownCountByUserId(Integer userId){
		return memberMapper.selectDownCountByUserId(userId);
	}

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

	@Override
	public Integer addFullReduction(String openId) {

		Calendar calendar = Calendar.getInstance();
		calendar.set(2019,2,16);
		calendar.set(Calendar.HOUR_OF_DAY,23);
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,58);

		Integer flag = 0;
		//创建优惠券
		CouponItem couponItem = new CouponItem();
		String uuidStr = StrUtils.getUUID();
		couponItem.setCouponItemId(uuidStr);
		couponItem.setCouponItemUseropenid(openId);
		couponItem.setCouponId(20000);
		couponItem.setCouponReceiveDate(new Date());
		couponItem.setCouponUseEndDate(calendar.getTime());
		couponItem.setCouponUseStatus(1);
		couponItem.setCouponItemRemark("38节活动优惠券");
		flag = couponItemMapper.insertCouponItem(couponItem);
		if(flag < 1){
			throw new RuntimeException("创建优惠券失败");
		}
		flag = memberMapper.addFullReduction(openId);
		if(flag < 1){
			throw new RuntimeException("创建优惠权益失败");
		}
		return flag;
	}

	@Override
	public FullReduction getFullReduction(String openId) {
		return memberMapper.getFullReduction(openId);
	}
}
