package cn.iocoder.yudao.module.member.service.user;

import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberCardConsumptionPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardChangeRecordDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardConsumptionDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberCardChangeRecordMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberCardConsumptionMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberCardMapper;
import cn.iocoder.yudao.module.system.enums.common.BooleanEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

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


/**
 * 会员卡消费 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class MemberCardConsumptionServiceImpl implements MemberCardConsumptionService {

    @Resource
    private MemberCardMapper memberCardMapper;
    @Resource
    private MemberCardConsumptionMapper memberCardConsumptionMapper;

    @Resource
    private MemberCardChangeRecordMapper memberCardChangeRecordMapper;

    @Override
    public Long createMemberCardConsumption(MemberCardConsumptionDO createReqVO) {
        createReqVO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        createReqVO.setCreatedTime(new Date());
        createReqVO.setOrderNum("X" + RandomUtil.randomNumbers(15));
        memberCardConsumptionMapper.insert(createReqVO);

        MemberCardDO memberCard = memberCardMapper.selectOne(new LambdaQueryWrapperX<MemberCardDO>()
                .eq(MemberCardDO::getMemberCardNumber, createReqVO.getCardNumber()));



        //创建变动记录
        MemberCardChangeRecordDO cardConsumptionRecord = new MemberCardChangeRecordDO();
        cardConsumptionRecord.setOrderNum(createReqVO.getOrderNum());
        cardConsumptionRecord.setMemberUserId(memberCard.getMemberUserId());
        cardConsumptionRecord.setMemberUserName(memberCard.getMemberUserName());
        cardConsumptionRecord.setMemberCardId(memberCard.getId());
        cardConsumptionRecord.setCardName(memberCard.getMemberCardName());
        cardConsumptionRecord.setCardNumber(memberCard.getMemberCardNumber());
        cardConsumptionRecord.setBeforeChangeAmount(memberCard.getBalanceAmount());
        BigDecimal afterChangeAmount = memberCard.getBalanceAmount().subtract(createReqVO.getAmount());
        cardConsumptionRecord.setAfterChangeAmount(afterChangeAmount);
        cardConsumptionRecord.setAmount(createReqVO.getAmount());
        cardConsumptionRecord.setType(3);  //消费
        cardConsumptionRecord.setCreatedTime(new Date());
        cardConsumptionRecord.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        cardConsumptionRecord.setOrgType(memberCard.getOrgType());
        memberCardChangeRecordMapper.insert(cardConsumptionRecord);

        memberCard.setBalanceAmount(afterChangeAmount);
        memberCardMapper.updateById(memberCard);

        return createReqVO.getId();
    }

    @Override
    public MemberCardConsumptionDO getCardConsumption(Long id) {
        return memberCardConsumptionMapper.selectById(id);
    }

    @Override
    public void cancel(MemberCardConsumptionDO updateReqVO) {
        updateReqVO.setCancelTime(new Date());
        updateReqVO.setStatus(BooleanEnum.FALSE.getValue());
        memberCardConsumptionMapper.updateById(updateReqVO);

        MemberCardChangeRecordDO cardChangeRecord = memberCardChangeRecordMapper.selectOne(new LambdaQueryWrapperX<MemberCardChangeRecordDO>()
                .eq(MemberCardChangeRecordDO::getOrderNum, updateReqVO.getOrderNum()));

        if (cardChangeRecord != null){
//            //删除消费记录
//            memberCardChangeRecordMapper.delete(new LambdaQueryWrapperX<MemberCardChangeRecordDO>()
//                    .eq(MemberCardChangeRecordDO::getOrderNum,updateReqVO.getOrderNum()));

            BigDecimal amount = cardChangeRecord.getAmount();

            MemberCardDO memberCard = memberCardMapper.selectById(cardChangeRecord.getMemberCardId());
            BigDecimal balanceAmount = memberCard.getBalanceAmount();
            BigDecimal afterChangeAmount = memberCard.getBalanceAmount().add(amount);
            //创建变动记录
            MemberCardChangeRecordDO cardConsumptionRecord = new MemberCardChangeRecordDO();
            cardConsumptionRecord.setMemberUserId(memberCard.getMemberUserId());
            cardConsumptionRecord.setMemberUserName(memberCard.getMemberUserName());
            cardConsumptionRecord.setMemberCardId(memberCard.getId());
            cardConsumptionRecord.setCardName(memberCard.getMemberCardName());
            cardConsumptionRecord.setCardNumber(memberCard.getMemberCardNumber());
            cardConsumptionRecord.setBeforeChangeAmount(balanceAmount);
            cardConsumptionRecord.setAfterChangeAmount(afterChangeAmount);
            cardConsumptionRecord.setAmount(amount);
            cardConsumptionRecord.setType(2);  //退款
            cardConsumptionRecord.setCreatedTime(new Date());
            cardConsumptionRecord.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            cardConsumptionRecord.setOrgType(memberCard.getOrgType());
            memberCardChangeRecordMapper.insert(cardConsumptionRecord);

            memberCard.setBalanceAmount(afterChangeAmount);
            memberCardMapper.updateById(memberCard);

        }

    }

    @Override
    public PageResult<MemberCardConsumptionDO> getCardConsumptionPage(MemberCardConsumptionPageReqVO pageReqVO) {
        return memberCardConsumptionMapper.selectPage(pageReqVO);
    }
}