package com.ruoyi.xq.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.xq.domain.ActivityOrder;
import com.ruoyi.xq.domain.Group;
import com.ruoyi.xq.domain.GroupOrder;
import com.ruoyi.xq.dto.app.group.CreateGroupOrderReq;
import com.ruoyi.xq.dto.app.pay.ConsumeResp;
import com.ruoyi.xq.dto.common.user.MinUser;
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.GroupOrderMapper;
import com.ruoyi.xq.service.GroupOrderService;
import com.ruoyi.xq.service.GroupService;
import com.ruoyi.xq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 入群订单Service业务层处理
 *
 * @author 77
 * @date 2024-05-15
 */
@Service
public class GroupOrderServiceImpl extends ServiceImpl<GroupOrderMapper,GroupOrder> implements GroupOrderService {

    @Autowired
    private GroupService groupService;
    @Autowired
    private UserService userService;

    @Override
    public GroupOrder getSuccessByGroupIdAndUserId(Long groupId, Long userId) {
        return this.getOne(Wrappers.lambdaQuery(GroupOrder.class).eq(GroupOrder::getGroupId, groupId)
                .eq(GroupOrder::getPayStatus, PayStatusEnum.PAY.getCode())
                .eq(GroupOrder::getUserId, userId).last("limit 1"));
    }

    @Override
    public GroupOrder createOrder(CreateGroupOrderReq req) {
        GroupOrder order = this.getSuccessByGroupIdAndUserId(req.getGroupId(), req.getUserId());
        if(order != null){
            throw new ServiceException("您已支付过，无法重复支付");
        }
        MinUser minUser = userService.getMinUserById(req.getUserId());
        if(minUser== null){
            throw new ServiceException("用户不存在");
        }
        Group group = groupService.getById(req.getGroupId());
        if(group == null){
            throw new ServiceException("相亲群不存在");
        }
        String orderNo = OrderNoUtil.gen(OrderTypeEnum.GROUP);
        GroupOrder add = new GroupOrder();
        add.setGroupId(group.getId());
        add.setGroupImage(group.getGroupImage());
        add.setGroupName(group.getGroupName());
        add.setCityName(group.getCityName());
        add.setCityCode(group.getCityCode());
        add.setJoinMobile(req.getMobile());
        add.setJoinPrice(group.getJoinPrice());
        add.setUserId(minUser.getId());
        add.setUsercode(minUser.getUsercode());
        add.setGender(minUser.getGender());
        add.setBody("入群费用");
        add.setOrderName("入群费用");
        add.setOrderNo(orderNo);
        add.setPayStatus(PayStatusEnum.READY_PAY.getCode());
        return add;
    }

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

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

}
