package com.maintain.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maintain.business.domain.SysPayLog;
import com.maintain.business.domain.bo.SysPayLogBo;
import com.maintain.business.domain.vo.SysPayLogVo;
import com.maintain.business.mapper.SysPayLogMapper;
import com.maintain.business.service.ISysPayLogService;
import com.maintain.common.core.domain.PageQuery;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.common.helper.LoginHelper;
import com.maintain.common.pay.PayResult;
import com.maintain.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 银联支付日志记录Service业务层处理
 *
 * @author liushuai
 * @date 2025-11-10
 */
@RequiredArgsConstructor
@Service
public class SysPayLogServiceImpl implements ISysPayLogService {

    private final SysPayLogMapper baseMapper;

    /**
     * 查询银联支付日志记录
     */
    @Override
    public SysPayLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询银联支付日志记录列表
     */
    @Override
    public TableDataInfo<SysPayLogVo> queryPageList(SysPayLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPayLog> lqw = buildQueryWrapper(bo);
        Page<SysPayLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询银联支付日志记录列表
     */
    @Override
    public List<SysPayLogVo> queryList(SysPayLogBo bo) {
        LambdaQueryWrapper<SysPayLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysPayLog> buildQueryWrapper(SysPayLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPayLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSettlementMaintainId() != null, SysPayLog::getSettlementMaintainId, bo.getSettlementMaintainId());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthorization()), SysPayLog::getAuthorization, bo.getAuthorization());
        lqw.eq(StringUtils.isNotBlank(bo.getOperParam()), SysPayLog::getOperParam, bo.getOperParam());
        lqw.eq(StringUtils.isNotBlank(bo.getJsonResult()), SysPayLog::getJsonResult, bo.getJsonResult());
        lqw.eq(bo.getStatus() != null, SysPayLog::getStatus, bo.getStatus());
        lqw.eq(bo.getOperTime() != null, SysPayLog::getOperTime, bo.getOperTime());
        return lqw;
    }

    /**
     * 新增银联支付日志记录
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, timeout = 30)
    public PayResult insertByBo(Long id, PayResult payResult) {
        // 保存支付日志
        SysPayLog sysPayLog = new SysPayLog();
        sysPayLog.setSettlementMaintainId(id);
        sysPayLog.setAuthorization(payResult.getAuthorization());
        sysPayLog.setOperParam(payResult.getRequestParams());
        sysPayLog.setJsonResult(payResult.getResponse());
        sysPayLog.setStatus(payResult.getPayResponse().getErrCode());
        sysPayLog.setOperTime(new Date());
        sysPayLog.setOperUser(LoginHelper.getUserId());
        baseMapper.insert(sysPayLog);
        return payResult;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysPayLog entity){
        //TODO 做一些数据校验,如唯一约束
    }
}
