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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.refund.vo.RefundPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.refund.vo.RefundSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.refund.vo.AppRefundSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.refund.RefundDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.refund.IntegralRefundMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import cn.iocoder.yudao.module.member.enums.RefundStatusEnum;
import cn.iocoder.yudao.module.member.service.card.CardService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.REFUND_NOT_EXISTS;

/**
 * 退款审核 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class RefundServiceImpl implements RefundService {

    @Resource
    private IntegralRefundMapper refundMapper;
    @Resource
    private CardService cardService;
    @Resource
    private MemberUserMapper userMapper;

    @Override
    public Long createRefund(RefundSaveReqVO createReqVO) {
        // 插入
        RefundDO refund = BeanUtils.toBean(createReqVO, RefundDO.class);
        refundMapper.insert(refund);
        // 返回
        return refund.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRefund(RefundSaveReqVO updateReqVO) {
        // 校验存在
        validateRefundExists(updateReqVO.getId());
        // 更新
        RefundDO updateObj = BeanUtils.toBean(updateReqVO, RefundDO.class);
        refundMapper.updateById(updateObj);
        if(RefundStatusEnum.SUCCESS.getCode().equals(updateReqVO.getStatus())){
            cardService.refund(updateReqVO.getUserId(),updateReqVO.getMoney(),updateReqVO.getOrderId());
        }
    }

    @Override
    public void deleteRefund(Long id) {
        // 校验存在
        validateRefundExists(id);
        // 删除
        refundMapper.deleteById(id);
    }

    private void validateRefundExists(Long id) {
        if (refundMapper.selectById(id) == null) {
            throw exception(REFUND_NOT_EXISTS);
        }
    }

    @Override
    public RefundDO getRefund(Long id) {
        return refundMapper.selectById(id);
    }

    @Override
    public PageResult<RefundDO> getRefundPage(RefundPageReqVO pageReqVO) {
        return refundMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createAppRefund(AppRefundSaveReqVO createReqVO) {
        // 插入
        RefundDO refund = BeanUtils.toBean(createReqVO, RefundDO.class);
        MemberUserDO memberUserDO = userMapper.selectById(refund.getUserId());
        refund.setMobile(memberUserDO.getMobile());
        refundMapper.insert(refund);
        // 返回
        return refund.getId();
    }

    @Override
    public String getRefundStatus(Long orderId) {
        RefundDO refundDO = refundMapper.selectOne(Wrappers.<RefundDO>lambdaQuery()
                .eq(RefundDO::getOrderId, orderId).last("limit 1"));
        if(ObjectUtil.isNotEmpty(refundDO)){
            return refundDO.getStatus();
        }
        return null;
    }
}