package com.hzs.sale.ticket.provider;

import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.service.RedisService;
import com.hzs.sale.order.param.OrderParam;
import com.hzs.sale.ticket.ITicketServiceApi;
import com.hzs.sale.ticket.service.ISaTicketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

/**
 * 购票对外dubbo服务
 */
@Slf4j
@DubboService
public class TicketServiceProvider implements ITicketServiceApi {

    @Autowired
    private RedisService redisService;
    @Autowired
    private ISaTicketService iSaTicketService;

    @Override
    public R<BigDecimal> getTicketPayOrder(Long pkMember, String orderCode) {
        try {
            OrderParam orderParam = redisService.getCacheObject(CacheConstants.TEMP_ORDER + pkMember + orderCode);
            return R.ok(orderParam.getPrice().multiply(new BigDecimal(orderParam.getTicketParamList().size())));
        } catch (Exception e) {
            log.error("查询购票在线支付信息失败. pkMember: {}, orderCode: {}", pkMember, orderCode, e);
            return R.fail("订单已失效");
        }
    }

    @Override
    public R<?> paySaTicketOnlineOrderCode(Long pkMember, String orderCode) {
        try {
            iSaTicketService.paySaTicketOnlineOrderCode(pkMember, orderCode);
            return R.ok();
        } catch (Exception e) {
            log.error("在线支付回调处理购票异常. pkMember: {}, orderCode: {}", pkMember, orderCode, e);
            return R.fail("购票回调处理异常:" + e.getMessage());
        }
    }

}
