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

import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardChangeRecordDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberCardChangeRecordMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberCardMapper;
import cn.iocoder.yudao.module.member.service.user.MemberCardService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_ERROR;


/**
 * 会员用户的 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberUserApiImpl implements MemberUserApi {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberCardService memberCardService;
    @Resource
    private MemberCardMapper memberCardMapper;

    @Resource
    private MemberCardChangeRecordMapper memberCardChangeRecordMapper;

    @Override
    public MemberUserRespDTO getUser(Long id) {
        MemberUserDO user = userService.getUser(id);
        return MemberUserConvert.INSTANCE.convert2(user);
    }

    @Override
    public List<MemberUserRespDTO> getUserList(Collection<Long> ids) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserList(ids));
    }

    @Override
    public List<MemberUserRespDTO> getUserListByNickname(String nickname) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserListByNickname(nickname));
    }

    @Override
    public MemberUserRespDTO getUserByMobile(String mobile) {
        return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile));
    }

    @Override
    public void handleMemberCardPay(String memberCardNumber, BigDecimal payAmount) {
        MemberCardDO dbMemberCard = memberCardMapper.selectOne(new LambdaQueryWrapperX<MemberCardDO>()
                .eq(MemberCardDO::getMemberCardNumber, memberCardNumber));


        //进行扣减
        BigDecimal balanceAmount = dbMemberCard.getBalanceAmount();

        //支付金额如果大于会员卡余额
        if (payAmount.compareTo(balanceAmount) > 0) {
            throw exception(COMMON_ERROR);
        }

        BigDecimal afterAmount = balanceAmount.subtract(payAmount);
        dbMemberCard.setBalanceAmount(afterAmount);
        memberCardMapper.updateById(dbMemberCard);

        //创建变动记录
        MemberCardChangeRecordDO cardConsumptionRecord = new MemberCardChangeRecordDO();
        cardConsumptionRecord.setOrderNum("X" + RandomUtil.randomNumbers(15));
        cardConsumptionRecord.setMemberUserId(dbMemberCard.getMemberUserId());
        cardConsumptionRecord.setMemberUserName(dbMemberCard.getMemberUserName());
        cardConsumptionRecord.setMemberCardId(dbMemberCard.getId());
        cardConsumptionRecord.setCardName(dbMemberCard.getMemberCardName());
        cardConsumptionRecord.setCardNumber(dbMemberCard.getMemberCardNumber());
        cardConsumptionRecord.setBeforeChangeAmount(balanceAmount);
        cardConsumptionRecord.setAfterChangeAmount(afterAmount);
        cardConsumptionRecord.setAmount(payAmount);
        cardConsumptionRecord.setType(3);  //消费
        cardConsumptionRecord.setCreatedTime(new Date());
        cardConsumptionRecord.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        cardConsumptionRecord.setOrgType(dbMemberCard.getOrgType());
        memberCardChangeRecordMapper.insert(cardConsumptionRecord);
    }

}
