package com.fs.live.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.fs.common.utils.DateUtils;
import com.fs.huifuPay.domain.HuiFuQueryOrderResult;
import com.fs.huifuPay.sdk.opps.core.request.V2TradePaymentScanpayQueryRequest;
import com.fs.huifuPay.service.HuiFuService;
import com.fs.live.domain.LiveOrderPayment;
import com.fs.live.mapper.LiveOrderPaymentMapper;
import com.fs.live.service.ILiveOrderPaymentService;
import com.fs.live.service.ILiveOrderService;
import com.fs.live.vo.LiveOrderPaymentVo;
import com.fs.store.domain.FsStoreProduct;
import com.fs.store.mapper.FsStoreProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 支付明细Service业务层处理
 *
 * @author fs
 * @date 2025-08-07
 */
@Service
@Slf4j
public class LiveOrderPaymentServiceImpl implements ILiveOrderPaymentService {
    @Autowired
    private LiveOrderPaymentMapper baseMapper;

    /**
     * 查询支付明细
     *
     * @param paymentId 支付明细主键
     * @return 支付明细
     */
    @Override
    public LiveOrderPayment selectLiveOrderPaymentByPaymentId(Long paymentId)
    {
        return baseMapper.selectLiveOrderPaymentByPaymentId(paymentId);
    }

    /**
     * 查询支付明细列表
     *
     * @param liveOrderPayment 支付明细
     * @return 支付明细
     */
    @Override
    public List<LiveOrderPayment> selectLiveOrderPaymentList(LiveOrderPayment liveOrderPayment)
    {
        return baseMapper.selectLiveOrderPaymentList(liveOrderPayment);
    }

    @Autowired
    private FsStoreProductMapper fsStoreProductMapper;
    @Override
    public List<LiveOrderPaymentVo> selectLiveOrderPaymentVoList(LiveOrderPaymentVo liveOrderPayment) {
        Map<Long, FsStoreProduct> productMap = fsStoreProductMapper.selectAllProductName();
        List<LiveOrderPaymentVo> liveOrderPaymentVos = baseMapper.selectLiveOrderPaymentVoList(liveOrderPayment);

        for (LiveOrderPaymentVo vo : liveOrderPaymentVos) {
            if(ObjectUtil.isNotNull(vo.getProductId())){
                FsStoreProduct fsStoreProduct = productMap.get(vo.getProductId());
                if(fsStoreProduct != null){
                    vo.setPackageTitle(fsStoreProduct.getProductName());
                }
            }
        }
        return liveOrderPaymentVos;
    }

    @Override
    public LiveOrderPaymentVo selectLiveOrderPaymentByPaymentIdNew(Long paymentId) {
        return baseMapper.selectLiveOrderPaymentByPaymentIdNew(paymentId);
    }

    @Override
    public List<LiveOrderPayment> selectLiveOrderPaymentByOrderId(Long id) {
        return baseMapper.selectLiveOrderPaymentByOrderId(id);
    }

    @Autowired
    private HuiFuService huiFuService;
    @Autowired
    private ILiveOrderService liveOrderService;
    @Override
    public boolean queryHf(LiveOrderPayment liveOrderPayment) {
        V2TradePaymentScanpayQueryRequest request = new V2TradePaymentScanpayQueryRequest();
        request.setOrgReqDate(new SimpleDateFormat("yyyyMMdd").format(liveOrderPayment.getCreateTime()));
        request.setOrgHfSeqId(liveOrderPayment.getTradeNo());
        HuiFuQueryOrderResult huiFuQueryOrderResult;
        try {
            huiFuQueryOrderResult = huiFuService.queryOrder(request);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("汇付返回"+huiFuQueryOrderResult);
        if ("00000000".equals(huiFuQueryOrderResult.getResp_code())) {
            if (huiFuQueryOrderResult.getTrans_stat().equals("S")) {
                // 如果查询支付成功 更新订单状态
                String bankTrxId = huiFuQueryOrderResult.getOut_trans_id();
                String bankOrderId = huiFuQueryOrderResult.getParty_order_id();
                liveOrderService.payConfirm(1, Long.valueOf(liveOrderPayment.getBusinessId()), liveOrderPayment.getPayCode(),
                        liveOrderPayment.getTradeNo(),bankTrxId, bankOrderId);
                return true;
            } else if(huiFuQueryOrderResult.getTrans_stat().equals("F")){
                liveOrderPayment.setStatus(-2);
                this.updateLiveOrderPayment(liveOrderPayment);
                return false;
            }
        }
        return false;
    }

    /**
     * 新增支付明细
     *
     * @param liveOrderPayment 支付明细
     * @return 结果
     */
    @Override
    public int insertLiveOrderPayment(LiveOrderPayment liveOrderPayment)
    {
        liveOrderPayment.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertLiveOrderPayment(liveOrderPayment);
    }

    /**
     * 修改支付明细
     *
     * @param liveOrderPayment 支付明细
     * @return 结果
     */
    @Override
    public int updateLiveOrderPayment(LiveOrderPayment liveOrderPayment)
    {
        return baseMapper.updateLiveOrderPayment(liveOrderPayment);
    }

    /**
     * 批量删除支付明细
     *
     * @param paymentIds 需要删除的支付明细主键
     * @return 结果
     */
    @Override
    public int deleteLiveOrderPaymentByPaymentIds(Long[] paymentIds)
    {
        return baseMapper.deleteLiveOrderPaymentByPaymentIds(paymentIds);
    }

    /**
     * 删除支付明细信息
     *
     * @param paymentId 支付明细主键
     * @return 结果
     */
    @Override
    public int deleteLiveOrderPaymentByPaymentId(Long paymentId)
    {
        return baseMapper.deleteLiveOrderPaymentByPaymentId(paymentId);
    }


}
