package com.ruoyi.xq.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.xq.domain.AuthOrder;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.dto.admin.authOrder.AuthOrderAdminVo;
import com.ruoyi.xq.dto.app.pay.ConsumeResp;
import com.ruoyi.xq.enums.common.OrderTypeEnum;
import com.ruoyi.xq.enums.common.TraceIdEnum;
import com.ruoyi.xq.enums.pay.PayStatusEnum;
import com.ruoyi.xq.enums.pay.PlatformTypeEnum;
import com.ruoyi.xq.manager.OrderNoUtil;
import com.ruoyi.xq.manager.TraceIdManager;
import com.ruoyi.xq.mapper.AuthOrderMapper;
import com.ruoyi.xq.service.AuthOrderService;
import com.ruoyi.xq.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 实名认证订单Service业务层处理
 *
 * @author 77
 * @date 2024-04-27
 */
@RequiredArgsConstructor
@Service
public class AuthOrderServiceImpl extends ServiceImpl<AuthOrderMapper,AuthOrder> implements AuthOrderService {

    @Autowired
    private UserService userService;

    @Override
    public Page<AuthOrderAdminVo> pageAdmin(PageQuery pageQuery, AuthOrderAdminVo bo) {
        return baseMapper.pageAdmin(pageQuery.build(), bo);
    }

    @Override
    public AuthOrder createVipOrder(Long userId) {
        String orderNo = OrderNoUtil.gen(OrderTypeEnum.AUTH_ORDER);
        User user = userService.getById(userId);
        AuthOrder authOrder = new AuthOrder();
        authOrder.setUserId(user.getId());
        authOrder.setUsercode(user.getUsercode());
        authOrder.setOrderName("实名认证订单");
        authOrder.setBody("实名认证订单");
        authOrder.setPrice(BigDecimal.valueOf(10));
        authOrder.setOrderNo(orderNo);
        authOrder.setPayStatus(PayStatusEnum.READY_PAY.getCode());
        this.save(authOrder);
        return authOrder;
    }

    @Override
    public AuthOrder getByOrderNo(String orderNo){
        return this.getOne(Wrappers.lambdaQuery(AuthOrder.class).eq(AuthOrder::getOrderNo, orderNo).last("limit 1"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ConsumeResp doSuccess(String orderNo, Map<String, String> params, String appId, PlatformTypeEnum payTypeEnum){
        AuthOrder authOrder = this.getByOrderNo(orderNo);
        if(authOrder == null){
            ConsumeResp resp = new ConsumeResp();
            resp.setSuccess(false);
            return resp;
        }
        String traceId = TraceIdManager.gen(TraceIdEnum.VIP);
        boolean success = this.update(Wrappers.lambdaUpdate(AuthOrder.class)
                .eq(AuthOrder::getId, authOrder.getId())
                .eq(AuthOrder::getPayStatus, PayStatusEnum.READY_PAY.getCode())
                .set(AuthOrder::getPayStatus, PayStatusEnum.PAY.getCode())
                .set(AuthOrder::getTraceId, traceId)
                .set(AuthOrder::getReturnContent, JSON.toJSONString(params))
                .set(AuthOrder::getAppid,appId)
                .set(AuthOrder::getPlatformType, payTypeEnum.getCode()));
        if(!success){
            ConsumeResp resp = new ConsumeResp();
            resp.setSuccess(false);
            return resp;
        }
        ConsumeResp resp = new ConsumeResp();
        resp.setPrice(authOrder.getPrice());
        resp.setUserId(authOrder.getUserId());
        resp.setTraceId(traceId);
        resp.setSuccess(true);
        return resp;

    }
}
