package com.neusoft.smart.pos.member.service;

import com.neusoft.smart.pos.dto.member.request.MemberCardChangeRequest;
import com.neusoft.smart.pos.dto.member.response.DetailedMemberResponse;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.member.constants.MemberCapitalChangeType;
import com.neusoft.smart.pos.member.constants.MemberErrorCode;
import com.neusoft.smart.pos.member.persistent.domain.*;
import com.neusoft.smart.pos.member.persistent.mapper.MemberCardMapper;
import com.neusoft.smart.pos.member.persistent.mapper.MemberMapper;
import com.neusoft.smart.pos.member.persistent.mapper.MemberWithCardMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

@Service
public class MemberCard4CanteenService extends MemberCardService {
	
	@Autowired
	private MemberWithCardMapper memberWithCardMapper;
	
	@Autowired
	private MemberCardMapper memberCardMapper;
	
	@Autowired
	private Member4CanteenService member4CanteenService;
	
	@Autowired
	private MemberCapitalChangeService memberCapitalChangeService;
	
	@Autowired
	MemberMapper memberMapper;
	
	public void validateByCardNum(String cardNum) {
		MemberWithCardExample memberWithCardExample = new MemberWithCardExample();
		memberWithCardExample.createCriteria().andCardNumEqualTo(cardNum);
		MemberWithCard memberWithCard = memberWithCardMapper.selectByExample4Canteen(memberWithCardExample).stream().filter(card -> card.getCardActive().equals(1)).findAny().orElse(null);
		if (null != memberWithCard) {
			throw new BusinessCommonException(MemberErrorCode.MEMBER_CARD_NUM_DUPLICATE);
		}
	}
	
	@Override
	public String changeCard(MemberCardChangeRequest memberCardChangeRequest) {
		if (StringUtils.isEmpty(memberCardChangeRequest.getCardNum()) || StringUtils.isEmpty(memberCardChangeRequest.getCardHidCode())) {
			throw new BusinessCommonException(MemberErrorCode.CANTEEN_CARD_NOT_NULL);
		}
		
		Member member = memberMapper.selectByPrimaryKey(memberCardChangeRequest.getMemberId());
		if (null == member) {
			throw new BusinessCommonException(MemberErrorCode.MEMBER_CARD_NOT_EXIST);
		}
		
		if (!member.getIsActive().equals(1)) {
			throw new BusinessCommonException(MemberErrorCode.CANTEEN_CARD_CHANGE_ERROR);
		}

		MemberWithCard memberWithCard = memberWithCardMapper.selectByMemberId4Canteen(memberCardChangeRequest.getMemberId(), null, true);
		
		MemberCard newCard = new MemberCard();
		MemberCard oldCard = memberCardMapper.selectByPrimaryKey(memberWithCard.getCardId());
		BeanUtils.copyProperties(oldCard, newCard, "cardId", "version", "createdBy", "updatedTime", "updatedBy");
		newCard.setCardHidCode(memberCardChangeRequest.getCardHidCode());
		newCard.setCardNum(memberCardChangeRequest.getCardNum());
		newCard.setCreatedBy(RequestHeaderUtil.getUserId() + "");
		memberCardMapper.insertSelective(newCard);
		oldCard.setIsActive(0);
		memberCardMapper.updateByPrimaryKeySelective(oldCard);
		return null;
	}
	
	/**
	 * 清空会员卡余额
	 *
	 * @param cardHidCode
	 */
	@Transactional
	public void emptyBalance(String cardHidCode) {
		DetailedMemberResponse detailedMemberResponse = member4CanteenService.getDetailByHidCode(cardHidCode);
		if (null == detailedMemberResponse) {
			throw new BusinessCommonException(MemberErrorCode.MEMBER_CARD_NOT_EXIST);
		}
		MemberCard card = memberCardMapper.selectByPrimaryKey(detailedMemberResponse.getCardId());
		BigDecimal originalBalance = card.getCardBalance();
		card.setCardBalance(new BigDecimal(0));
		card.setCardPoint(0);
		memberCardMapper.updateByPrimaryKeySelective(card);
		
		//存下变更表
		MemberCapitalChange memberCapitalChange = new MemberCapitalChange();
		memberCapitalChange.setMemberCardId(card.getCardId());
		memberCapitalChange.setChangeOrderId(UUID.randomUUID().toString());//随个uuid占位
		memberCapitalChange.setAmountChange(originalBalance);//修改后减去修改前,就是差额,负的就是扣减
		memberCapitalChange.setAmountBefore(originalBalance);
		memberCapitalChange.setAmountAfter(new BigDecimal("0.00"));
		memberCapitalChange.setContent("清卡");
		memberCapitalChange.setChangeTime(new Date());
		memberCapitalChange.setChangeType(MemberCapitalChangeType.EMPTY.getCode());
		memberCapitalChangeService.changeLog(memberCapitalChange);
	}
}
