package com.ruoyi.xq.service.impl;

import cn.hutool.core.util.NumberUtil;
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.User;
import com.ruoyi.xq.domain.WxTransOrder;
import com.ruoyi.xq.dto.admin.account.WxTransOrderAdminVo;
import com.ruoyi.xq.dto.app.pay.ConsumeResp;
import com.ruoyi.xq.enums.account.WxTransLogCateEnum;
import com.ruoyi.xq.enums.common.OrderTypeEnum;
import com.ruoyi.xq.enums.common.SystemConfigEnum;
import com.ruoyi.xq.enums.common.TraceIdEnum;
import com.ruoyi.xq.enums.pay.PayStatusEnum;
import com.ruoyi.xq.enums.account.WxTransSourceEnum;
import com.ruoyi.xq.enums.pay.PlatformTypeEnum;
import com.ruoyi.xq.manager.OrderNoUtil;
import com.ruoyi.xq.manager.SystemConfigManager;
import com.ruoyi.xq.manager.TraceIdManager;
import com.ruoyi.xq.mapper.WxTransOrderMapper;
import com.ruoyi.xq.service.UserExtendService;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.service.WxTransOrderService;
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-03-20
 */
@Service
public class WxTransOrderServiceImpl extends ServiceImpl<WxTransOrderMapper,WxTransOrder> implements WxTransOrderService {

    @Autowired
    private SystemConfigManager systemConfigManager;
    @Autowired
    private UserService userService;
    @Autowired
    private UserExtendService userExtendService;

    @Override
    public WxTransOrder createWxTransOrder(Long userId, Integer wxTransNum) {
        BigDecimal price = systemConfigManager.getSystemConfigOfBigDecimal(SystemConfigEnum.WX_TRANS_PRICE);
        String orderNo = OrderNoUtil.gen(OrderTypeEnum.WX_TRANS);
        String traceId = TraceIdManager.gen(TraceIdEnum.WX_TRANS);
        User user = userService.getById(userId);
        WxTransOrder order = new WxTransOrder();
        order.setUserId(user.getId());
        order.setUsercode(user.getUsercode());
        order.setBody("微信交换订单");
        order.setWxNum(wxTransNum);
        order.setTraceId(traceId);
        order.setWxPrice(NumberUtil.mul(price,wxTransNum));
        order.setOrderName("微信交换订单");
        order.setOrderNo(orderNo);
        order.setPayStatus(PayStatusEnum.READY_PAY.getCode());
        order.setSource(WxTransSourceEnum.ORDER.getCode());
        this.save(order);
        return order;
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ConsumeResp doSuccess(String orderNo, Map<String, String> params, String appId, PlatformTypeEnum payTypeEnum){
        WxTransOrder wxTransOrder = this.getByOrderNo(orderNo);
        if(wxTransOrder == null){
            ConsumeResp resp = new ConsumeResp();
            resp.setSuccess(false);
            return resp;
        }
        String traceId = TraceIdManager.gen(TraceIdEnum.VIP);
        boolean success = this.update(Wrappers.lambdaUpdate(WxTransOrder.class)
                .eq(WxTransOrder::getId, wxTransOrder.getId())
                .eq(WxTransOrder::getPayStatus, PayStatusEnum.READY_PAY.getCode())
                .set(WxTransOrder::getPayStatus, PayStatusEnum.PAY.getCode())
                .set(WxTransOrder::getTraceId, traceId)
                .set(WxTransOrder::getReturnContent, JSON.toJSONString(params))
                .set(WxTransOrder::getAppid,appId)
                .set(WxTransOrder::getPlatformType, payTypeEnum.getCode()));
        if(!success){
            ConsumeResp resp = new ConsumeResp();
            resp.setSuccess(false);
            return resp;
        }
        userExtendService.incsWxTrans(wxTransOrder.getUserId(),wxTransOrder.getWxNum(), WxTransLogCateEnum.ORDER,wxTransOrder.getTraceId(),
                "购买一次微信交换");
        ConsumeResp resp = new ConsumeResp();
        resp.setPrice(wxTransOrder.getWxPrice());
        resp.setUserId(wxTransOrder.getUserId());
        resp.setTraceId(traceId);
        resp.setSuccess(true);
        return resp;

    }

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