package com.buy507.mall.portal.controller;

import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.mapper.OmsOrderMapper;
import com.buy507.mall.mapper.PmsProductMapper;
import com.buy507.mall.mapper.UmsMemberMapper;
import com.buy507.mall.model.OmsOrder;
import com.buy507.mall.model.UmsMember;
import com.buy507.mall.portal.service.OmsPortalOrderService;
import com.buy507.mall.portal.service.UmsMemberService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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



/**
 * ClassName:VipCenterController
 * Description:
 *
 * @Author axin
 * @Create 2025/2/19 11:09
 * @Version 1.0
 */

@Controller
@Api(tags = "VipCenterController", description = "会员中心")
@RequestMapping("/vipCenter")
public class VipCenterController {
    @Autowired
    private UmsMemberMapper memberMapper;

    @Autowired
    private UmsMemberService memberService;



    @Autowired
    private OmsOrderMapper orderMapper;

    //店长升级金额
    @Value("${storeManagerAmount}")
    private BigDecimal storeManagerAmount;

    //代理升级金额
    @Value("${agentAmount}")
    private BigDecimal agentAmount;

    //市代理升级金额
    @Value("${cityAgentAmount}")
    private BigDecimal cityAgentAmount;

    @ApiOperation("查询当前登录人信息")
    @RequestMapping("/getCurrentUserInfo")
    public CommonResult getCurrentUserInfo(){
        //获取当前登录人的信息
        UmsMember member = memberMapper.selectByPrimaryKey(memberService.getCurrentMember().getId());

        // 获取当前用户的会员等级ID
        Long memberLevelId = member.getMemberLevelId();

        String memberLevelName = "普通用户";
        if (memberLevelId == 1) {
            memberLevelName = "普通用户";
        } else if (memberLevelId == 2) {
            memberLevelName = "VIP会员";
        } else if (memberLevelId == 3) {
            memberLevelName = "店长";
        } else if (memberLevelId == 4) {
            memberLevelName = "代理";
        } else if (memberLevelId == 5) {
            memberLevelName = "市代理";
        }

        // 返回当前用户的会员信息
        return CommonResult.success("当前登录用户: " + member.getUsername() + ", 会员等级: " + memberLevelName);
    }

    @ApiOperation("创建会员中心订单")
    @RequestMapping( value = "/createVipOrder", method = RequestMethod.POST)
    public CommonResult createVipOrder(){
        //获取当前登录人的信息
        UmsMember member = memberMapper.selectByPrimaryKey(memberService.getCurrentMember().getId());

        // 获取当前用户的会员等级ID
        Long memberLevelId = member.getMemberLevelId();

        //创建返回值
        Map<String, Object> result = new HashMap<>();

        //生成订单
        OmsOrder order = new OmsOrder();
        // 根据会员等级ID动态设置订单金额
            // 店长
        if (memberLevelId == 3) {
            order.setTotalAmount(storeManagerAmount);
            order.setPayAmount(storeManagerAmount);
            // 代理
        } else if (memberLevelId == 4) {
            order.setTotalAmount(agentAmount);
            order.setPayAmount(agentAmount);
            // 市代理
        } else if (memberLevelId == 5) {
            order.setTotalAmount(cityAgentAmount);
            order.setPayAmount(cityAgentAmount);
        }
        order.setMemberId(member.getId());
        order.setCreateTime(new Date());
        order.setModifyTime(new Date());

        // 支付方式，0为未支付
        order.setPayType(0);
        // 订单状态，0为待付款
        order.setStatus(0);
        // 虚拟订单
        order.setOrderType(3);
        // 升级订单，无需实际收货人信息
        order.setReceiverName("会员升级");

        // 保存订单
        orderMapper.insert(order);

        // 更新返回结果
        result.put("orderId", order.getId());

        return CommonResult.success(result);

    }




}
