package com.hzs.member.account.provider;

import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApproveOperation;
import com.hzs.common.core.enums.EApproveRechargeStatus;
import com.hzs.common.domain.member.account.CuMemberRecharge;
import com.hzs.member.account.IMemberTradeServiceApi;
import com.hzs.member.account.dto.BusinessCommissionDTO;
import com.hzs.member.account.dto.MemberBalanceDTO;
import com.hzs.member.account.service.ICuMemberRechargeService;
import com.hzs.member.account.service.ICuMemberTradeService;
import com.hzs.member.account.vo.CuMemberRechargeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
@DubboService
public class IMemberTradeServiceProvider implements IMemberTradeServiceApi {

    @Autowired
    private ICuMemberTradeService iCuMemberTradeService;
    @Autowired
    private ICuMemberRechargeService iCuMemberRechargeService;

    @Override
    public R<?> updateOnlineRechargeByRechargeCode(String rechargeCode, EApproveOperation eApproveOperation, Long pkMember) {
        try {
            return R.ok(iCuMemberRechargeService.updateOnlineRechargeByRechargeCode(rechargeCode, eApproveOperation, pkMember));
        } catch (Exception e) {
            log.error("更新充值信息失败，充值单号：{} ", rechargeCode, e);
            return R.fail("充值回调处理异常:" + e.getMessage());
        }
    }

    @Override
    public R<CuMemberRecharge> selectMemberRechargeByCodeAndPkMember(String rechargeCode, Long pkMember) {
        try {
            CuMemberRechargeVO memberRechargeVO = CuMemberRechargeVO.builder().rechargeCode(rechargeCode).pkMember(pkMember).build();
            CuMemberRecharge cuMemberRecharge = iCuMemberRechargeService.selectMemberRechargeByCodeAndPkMember(memberRechargeVO);
            if (null == cuMemberRecharge) {
                return R.fail("充值信息不存在！");
            }
            Integer approveState = cuMemberRecharge.getApproveState();
            if (approveState.equals(EApproveRechargeStatus.AGREE.getValue())) {
                return R.fail("该单号已充值！");
            }
            if (approveState.equals(EApproveRechargeStatus.REJECT.getValue())) {
                return R.fail("该单号充值失败过！");
            }
            return R.ok(cuMemberRecharge);
        } catch (Exception e) {
            log.error("查询预充值信息失败，充值单号：{} ", rechargeCode, e);
            return R.fail("查询预充值信息失败！");
        }
    }

    @Override
    public List<MemberBalanceDTO> selectBalanceNotEnoughMember(BusinessCommissionDTO businessCommissionDTO) {
        return iCuMemberTradeService.selectBalanceNotEnoughMember(businessCommissionDTO);
    }

    @Override
    public R<Boolean> businessCommissionSubmit(BusinessCommissionDTO businessCommissionDTO) {
        return R.ok(iCuMemberTradeService.businessCommissionSubmit(businessCommissionDTO));
    }

}
