package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dto.req.OrderReq;
import com.yonge.cooleshow.biz.dal.entity.VipCardRecord;
import com.yonge.cooleshow.biz.dal.enums.PeriodEnum;
import com.yonge.cooleshow.biz.dal.service.StudentService;
import com.yonge.cooleshow.biz.dal.service.VipCardRecordService;
import com.yonge.cooleshow.biz.dal.vo.StudentVo;
import com.yonge.cooleshow.biz.dal.vo.UserOrderDetailVo;
import com.yonge.cooleshow.biz.dal.vo.res.OrderCreateRes;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yonge.cooleshow.biz.dal.entity.MemberPriceSettings;
import com.yonge.cooleshow.biz.dal.vo.MemberPriceSettingsVo;
import com.yonge.cooleshow.biz.dal.dto.search.MemberPriceSettingsSearch;
import com.yonge.cooleshow.biz.dal.dao.MemberPriceSettingsDao;
import com.yonge.cooleshow.biz.dal.service.MemberPriceSettingsService;

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


@Service
public class MemberPriceSettingsServiceImpl extends ServiceImpl<MemberPriceSettingsDao, MemberPriceSettings> implements MemberPriceSettingsService {
	private final static Logger log = LoggerFactory.getLogger(MemberPriceSettingsServiceImpl.class);
	@Autowired
	private StudentService studentService;
	@Autowired
	private VipCardRecordService vipCardRecordService;

	@Override
	public MemberPriceSettingsVo detail(Long id) {
		return baseMapper.detail(id);
	}

	@Override
	public IPage<MemberPriceSettingsVo> selectPage(IPage<MemberPriceSettingsVo> page, MemberPriceSettingsSearch query){
		return page.setRecords(baseMapper.selectPage(page, query));
	}

	@Override
	public List<MemberPriceSettingsVo> selectList(MemberPriceSettingsSearch query) {
		return baseMapper.selectList(query);
	}


	@Override
	public HttpResponseResult<OrderCreateRes> orderCreate(OrderReq.OrderReqInfo orderReqInfo) {
		MemberPriceSettingsVo detail = detail(Long.parseLong(orderReqInfo.getBizContent().toString()));
		if (null == detail) {
			return HttpResponseResult.failed("未找到会员卡信息");
		}
		//插入会员卡信息到会员卡记录表
		OrderCreateRes orderCreateRes = new OrderCreateRes();
		orderCreateRes.setRes(true);
		orderCreateRes.setBizId(detail.getId());
		orderCreateRes.setBizContent("会员卡购买-" + detail.getPeriod().getMsg());
		orderCreateRes.setGoodNum(1);
		orderCreateRes.setOriginalPrice(detail.getOriginalPrice());
		orderCreateRes.setExpectPrice(detail.getSalePrice());
		return HttpResponseResult.succeed(orderCreateRes);
	}


	@Override
	public void orderSuccess(UserOrderDetailVo orderDetailVo) {
		MemberPriceSettingsVo detail = detail(orderDetailVo.getBizId());
		if (null == detail) {
			return;
		}
		StudentVo studentVo = studentService.detail(orderDetailVo.getUserId());
		//修改用户会员时长
		Date membershipEndTime = studentVo.getMembershipEndTime();
		Calendar now = Calendar.getInstance();

		VipCardRecord vipCardRecord = new VipCardRecord();
		vipCardRecord.setUserId(orderDetailVo.getUserId());
		vipCardRecord.setOrderId(orderDetailVo.getOrderId());
		vipCardRecord.setVipCardId(detail.getId());

		if (null == membershipEndTime || membershipEndTime.before(new Date())) {
			//没有会员、会员已过期
			studentVo.setMembershipStartTime(now.getTime());
		} else {
			now.setTime(studentVo.getMembershipEndTime());
		}
		vipCardRecord.setStartTime(now.getTime());

		if(PeriodEnum.DAY.equals(detail.getPeriod())){
			now.add(Calendar.DAY_OF_MONTH, 1);
		}else if (PeriodEnum.MONTH.equals(detail.getPeriod())) {
			now.add(Calendar.MONTH, 1);
		} else if (PeriodEnum.QUARTERLY.equals(detail.getPeriod())) {
			now.add(Calendar.MONTH, 3);
		} else if (PeriodEnum.YEAR_HALF.equals(detail.getPeriod())) {
			now.add(Calendar.MONTH, 6);
		} else if (PeriodEnum.YEAR.equals(detail.getPeriod())) {
			now.add(Calendar.YEAR, 1);
		}

		vipCardRecord.setEndTime(now.getTime());
		studentVo.setMembershipEndTime(now.getTime());
		studentVo.setMemberRankSettingId(1l);
		studentVo.setUpdateTime(new Date());
		studentService.updateById(studentVo);
		//会员购买记录入库
		vipCardRecordService.save(vipCardRecord);
	}

}