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.common.domain.member.account.CuMemberTrade;
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;

/**
 * @Description:
 * @Author: ljc
 * @Time: 2023/3/8 18:28
 * @Classname: IMemberTradeServiceProvider
 * @Package_name: com.hzs.member.account.provider
 */
@Slf4j
@DubboService
public class IMemberTradeServiceProvider implements IMemberTradeServiceApi {

    @Autowired
    ICuMemberTradeService memberTradeService;

    @Autowired
    ICuMemberRechargeService memberRechargeService;

    @Override
    public R saveMemberOnlineTradeAndAccount(String rechargeCode) {
        try {
            return R.ok(memberTradeService.saveMemberTradeAndAccountByRechargeCode(rechargeCode));
        } catch (Exception e) {
            log.error("在线充值插入交易流水失败，充值单号：{} ", rechargeCode, e);
            return R.fail("提交扣费失败！");
        }

    }

    @Override
    public R<?> updateOnlineRechargeByRechargeCode(String rechargeCode, EApproveOperation eApproveOperation, Long pkMember) {
        try {
            return R.ok(memberRechargeService.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 = memberRechargeService.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("查询预充值信息失败！");
        }

    }

    /**
     * 查询余额不足的用户信息
     *
     * @param businessCommissionDTO 业务参数
     * @return List<MemberBalanceDTO>
     */
    @Override
    public List<MemberBalanceDTO> selectBalanceNotEnoughMember(BusinessCommissionDTO businessCommissionDTO) {
        return memberTradeService.selectBalanceNotEnoughMember(businessCommissionDTO);
    }

    /**
     * 业务办理扣费提交
     *
     * @param businessCommissionDTO 业务办理入参
     * @return Integer
     */
    @Override
    public R<Boolean> businessCommissionSubmit(BusinessCommissionDTO businessCommissionDTO) {
        return R.ok(memberTradeService.businessCommissionSubmit(businessCommissionDTO));
    }

    @Override
    public R<List<CuMemberTrade>> querySourceCodeLastTrade(Long pkMember, String sourceCode, Integer tradeClassify) {
        return R.ok(memberTradeService.querySourceCodeLastTrade(pkMember, sourceCode, tradeClassify));
    }

}
