package com.hzs.member.account.provider;

import com.hzs.common.core.domain.R;
import com.hzs.common.domain.member.account.CuMemberAccount;
import com.hzs.common.domain.member.account.CuMemberTrade;
import com.hzs.common.domain.member.ext.CuMemberAccountExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.member.account.IMemberAccountServiceApi;
import com.hzs.member.account.service.ICuMemberAccountService;
import com.hzs.member.base.service.ICuMemberBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

@Slf4j
@DubboService
public class MemberAccountServiceProvider implements IMemberAccountServiceApi {

    @Autowired
    private ICuMemberAccountService cuMemberAccountService;
    @Autowired
    private ICuMemberBusinessService iCuMemberBusinessService;

    @Override
    public R<List<CuMemberAccountExt>> queryMemberAccountByCondition(CuMemberAccount cuMemberAccount) {
        return R.ok(cuMemberAccountService.queryMemberAccountByCondition(cuMemberAccount));
    }

    @Override
    public R<List<CuMemberAccount>> queryCuMemberAccountByPkMembers(List<Long> pkMemberList) {
        return R.ok(cuMemberAccountService.queryCuMemberAccountByPkMembers(pkMemberList));
    }

    @Override
    public R orderChangeProduct(SaOrder saOrder, SaOrder updateOrder, Long userId, Object obj) {
        try {
            iCuMemberBusinessService.orderChangeProduct(saOrder, updateOrder, userId, obj);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @Override
    public R orderReturnMember(List<Long> pkMemberList, Long userId) {
        try {
            iCuMemberBusinessService.orderReturnMember(pkMemberList, userId);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @Override
    public R insertMemberTrade(SaOrder saOrder, CuMemberAccountExt cuMemberAccount) {
        try {
            iCuMemberBusinessService.insertMemberTrade(saOrder, cuMemberAccount);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @Override
    public R handleOrderPolicyActivity(SaOrderExt saOrderExt, BigDecimal policyCurrency) {
        try {
            if (iCuMemberBusinessService.handleOrderPolicy(saOrderExt, policyCurrency)) {
                return R.ok();
            }
            return R.fail("订单赠送政策币失败");
        } catch (Exception e) {
            log.error("订单赠送政策币异常，{}", e.getMessage(), e);
            return R.fail("订单赠送政策币异常，" + e.getMessage());
        }
    }

    @Override
    public R backOrderPolicyActivity(SaOrderExt saOrderExt, CuMemberTrade cuMemberTrade) {
        try {
            if (iCuMemberBusinessService.backOrderPolicy(saOrderExt, cuMemberTrade)) {
                return R.ok();
            }
            return R.fail("撤单回退政策币失败");
        } catch (Exception e) {
            log.error("撤单回退政策币异常，{}", e.getMessage(), e);
            return R.fail("撤单回退政策币异常，" + e.getMessage());
        }
    }

}
