package com.yiqi.recharge.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.app.domain.AppUser;
import com.yiqi.app.service.IAppUserBillMstrService;
import com.yiqi.app.service.IAppUserService;
import com.yiqi.common.enums.*;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.OrderNoUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.Utils;
import com.yiqi.order.domain.OrderRecharge;
import com.yiqi.order.domain.dto.OrderRechargeCreateDTO;
import com.yiqi.order.domain.dto.RefundBaseDTO;
import com.yiqi.order.service.IOrderRechargeService;
import com.yiqi.recharge.domain.RechargeCardBatchRecord;
import com.yiqi.recharge.domain.RechargePasswordCard;
import com.yiqi.recharge.domain.dto.*;
import com.yiqi.recharge.domain.vo.RechargeBatchCardDetailListQueryVO;
import com.yiqi.recharge.domain.vo.RechargeBatchUseRecordListQueryVO;
import com.yiqi.recharge.mapper.RechargeCardBatchRecordMapper;
import com.yiqi.recharge.service.IRechargeCardBatchRecordService;
import com.yiqi.recharge.service.IRechargePasswordCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author xiaohei
 * @date 2024/1/6
 */
@Service
public class RechargeCardBatchRecordServiceImpl extends ServiceImpl<RechargeCardBatchRecordMapper, RechargeCardBatchRecord>
        implements IRechargeCardBatchRecordService {
    @Autowired
    private OrderNoUtils orderNoUtils;

    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IAppUserBillMstrService appUserBillMstrService;
    @Autowired
    private IRechargePasswordCardService rechargePasswordCardService;
    @Autowired
    private IOrderRechargeService orderRechargeService;

    @Override
    public List<RechargeBatchUseRecordListQueryVO> listCardUseRecordByBatchNo(RechargeBatchUseRecordListQueryDTO rechargeBatchUseRecordListQueryDTO) {
        return baseMapper.listCardUseRecordByBatchNo(rechargeBatchUseRecordListQueryDTO);
    }

    @Override
    public int countCardUseRecordByBatchNo(RechargeBatchUseRecordListQueryDTO rechargeBatchUseRecordListQueryDTO) {
        return baseMapper.countCardUseRecordByBatchNo(rechargeBatchUseRecordListQueryDTO);
    }

    @Override
    public List<RechargeBatchCardDetailListQueryVO> listCardDetailByBatchNo(RechargeBatchCardDetailListQueryDTO rechargeBatchCardDetailListQueryDTO) {
        return baseMapper.listCardDetailByBatchNo(rechargeBatchCardDetailListQueryDTO);
    }

    @Override
    public int countCardDetailByBatchNo(RechargeBatchCardDetailListQueryDTO rechargeBatchCardDetailListQueryDTO) {
        return baseMapper.countCardDetailByBatchNo(rechargeBatchCardDetailListQueryDTO);
    }

    @Override
    public void deleteCardByCardId(Long rechargeCardId) {
        RechargePasswordCard rechargePasswordCard = rechargePasswordCardService.getOne(new QueryWrapper<RechargePasswordCard>().lambda()
                .eq(RechargePasswordCard::getId, rechargeCardId)
                .eq(RechargePasswordCard::getDelFlag, StatusType.OK.getCode()), Boolean.FALSE);
        if (rechargePasswordCard == null){
            throw new ServiceException("未查询到该充值卡信息");
        }
        if (!StatusType.OK.getCode().equals(rechargePasswordCard.getRechargeStatus())){
            throw new ServiceException("该充值卡已使用，无法操作删除");
        }
        if (StrUtil.isNotEmpty(rechargePasswordCard.getOrderNo())){
            throw new ServiceException("该充值卡已生成订单，无法操作删除");
        }
        rechargePasswordCard.setDelFlag(StatusType.DELETED.getCode());
        rechargePasswordCardService.updateById(rechargePasswordCard);
    }

    @Override
    public void addOneCardByDirect(RechargeBatchDirectCardAddDTO rechargeBatchDirectCardAddDTO, RechargeCardBatchRecord rechargeCardBatchRecord) {
        AppUser appUser = appUserService.getOne(new QueryWrapper<AppUser>().lambda()
                .eq(AppUser::getPhoneNumber, rechargeBatchDirectCardAddDTO.getAppUserPhoneNumber()), Boolean.FALSE);
        if (appUser == null){
            throw new ServiceException("手机号不存在，请确认");
        }
        RechargePasswordCard rechargePasswordCard = new RechargePasswordCard();
        rechargePasswordCard.setBatchNo(rechargeCardBatchRecord.getBatchNo());
        rechargePasswordCard.setCardNo(orderNoUtils.getOrderNo());
        rechargePasswordCard.setActualAmount(rechargeBatchDirectCardAddDTO.getRechargeAmount());
        rechargePasswordCard.setPassword(Utils.getPassWordOne(8));
        rechargePasswordCard.setCreateType(RechargeCreateType.IMPORT.getCode());
        rechargePasswordCard.setRechargeAmount(rechargeBatchDirectCardAddDTO.getRechargeAmount());
        rechargePasswordCard.setGiveAmount(BigDecimal.ZERO);
        rechargePasswordCard.setGivePointAmount(0);
        rechargePasswordCard.setRechargeTime(DateUtils.getNowDate());
        rechargePasswordCard.setRechargeBalance(rechargePasswordCard.getRechargeAmount());
        rechargePasswordCard.setGiveBalance(rechargePasswordCard.getGiveAmount());
        rechargePasswordCard.setDelFlag(DeleteStatus.OK.getCode());
        rechargePasswordCard.setRechargeStatus(RechargeStatusType.HAS_USE.getCode());
        rechargePasswordCard.setRechargeUserId(appUser.getId());
        rechargePasswordCard.setIsSupportLife(SysBoolType.YES.getCode());
        rechargePasswordCard.setIsSupportGoods(SysBoolType.YES.getCode());
        rechargePasswordCard.setIsSupportCloth(SysBoolType.YES.getCode());
        rechargePasswordCard.setIsSupportCar(SysBoolType.YES.getCode());
        rechargePasswordCard.setCreateById(rechargeCardBatchRecord.getCreateById());
        rechargePasswordCard.setCreateBy(rechargeCardBatchRecord.getCreateBy());
        rechargePasswordCard.setCreateTime(DateUtils.getNowDate());
        rechargePasswordCard.setRechargeStoreId(rechargeBatchDirectCardAddDTO.getStoreOrgId());

        // 保存订单
        OrderRecharge orderRecharge = orderRechargeService.buildOrderRechargeByCardPassword(rechargePasswordCard, OrderRechargeCreateDTO.buildDirectChargingCardByOrder(rechargePasswordCard.getRechargeUserId()));
        // 保存消费日志
        appUserBillMstrService.consumeRecord(ConsumeOrderDTO.buildOrderRechargeData(orderRecharge));

        rechargePasswordCard.setOrderNo(orderRecharge.getOrderNo());
        rechargePasswordCardService.save(rechargePasswordCard);
    }

    @Override
    public void addOneCardByCommon(RechargeBatchCommonCardAddDTO rechargeBatchCommonCardAddDTO, RechargeCardBatchRecord rechargeCardBatchRecord) {
        RechargePasswordCard rechargePasswordCard = new RechargePasswordCard();
        rechargePasswordCard.setBatchNo(rechargeCardBatchRecord.getBatchNo());
        rechargePasswordCard.setCardNo(orderNoUtils.getOrderNo());
        rechargePasswordCard.setPassword(Utils.getPassWordOne(8));
        rechargePasswordCard.setCreateType(RechargeCreateType.IMPORT.getCode());
        rechargePasswordCard.setRechargeAmount(rechargeBatchCommonCardAddDTO.getRechargeAmount());
        rechargePasswordCard.setGiveAmount(rechargeBatchCommonCardAddDTO.getGiveAmount());
        rechargePasswordCard.setGivePointAmount(rechargeBatchCommonCardAddDTO.getGivePointAmount());
        rechargePasswordCard.setRechargeBalance(rechargePasswordCard.getRechargeAmount());
        rechargePasswordCard.setGiveBalance(rechargePasswordCard.getGiveAmount());
        rechargePasswordCard.setActualAmount(rechargeCardBatchRecord.getActualAmount());
        rechargePasswordCard.setDelFlag(DeleteStatus.OK.getCode());
        rechargePasswordCard.setRechargeStatus(RechargeStatusType.NOT_UES.getCode());
        rechargePasswordCard.setCreateById(SecurityUtils.getUserId());
        rechargePasswordCard.setCreateBy(SecurityUtils.getUsername());
        rechargePasswordCard.setCreateTime(DateUtils.getNowDate());
        rechargePasswordCard.setIsSupportLife(SysBoolType.YES.getCode());
        rechargePasswordCard.setIsSupportGoods(SysBoolType.YES.getCode());
        rechargePasswordCard.setIsSupportCloth(SysBoolType.YES.getCode());
        rechargePasswordCard.setIsSupportCar(SysBoolType.YES.getCode());

        rechargePasswordCardService.save(rechargePasswordCard);
    }
}
