package com.hollywood.applet.controller;
import java.time.LocalDateTime;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hollywood.applet.dto.Vip;
import com.hollywood.applet.dto.VipPayDto;
import com.hollywood.applet.service.TConfigService;
import com.hollywood.applet.service.TUserService;
import com.hollywood.applet.service.TVipConfigService;
import com.hollywood.applet.utils.LoginInfoUtil;
import com.hollywood.common.basic.ApiResult;
import com.hollywood.common.model.TConfig;
import com.hollywood.common.model.TUser;
import com.hollywood.common.model.TVipConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
*
* 会员设置 前端控制器
*
*
* @author xiaochen
* @since 2024-02-29
*/
@Api(tags = "会员设置")
@RestController
@RequestMapping("/tVipConfig")
public class TVipConfigController {
private final TVipConfigService vipConfigService;
@Autowired
private LoginInfoUtil loginInfoUtil;
@Autowired
private TUserService userService;
@Autowired
private TVipConfigService configService;
@Autowired
private TConfigService tConfigService;
@Autowired
public TVipConfigController(TVipConfigService vipConfigService) {
this.vipConfigService = vipConfigService;
}
/**
* 获取会员配置
*/
@ApiOperation(value = "获取会员配置")
@GetMapping(value = "/getById")
public ApiResult getById() {
return ApiResult.success(vipConfigService.getById(1));
}
/**
* 编辑会员配置
*/
@ApiOperation(value = "编辑会员配置")
@PostMapping(value = "/updateById")
public ApiResult updateById(@RequestBody TVipConfig vipConfig) {
vipConfig.setId(1L);
return ApiResult.success(vipConfigService.updateById(vipConfig));
}
@ApiOperation(value = "个人中心-会员信息")
@PostMapping(value = "/vipinfo")
public ApiResult vipinfo() {
Long userId = loginInfoUtil.getUserId();
TUser byId = userService.getById(userId);
TConfig byId1 = tConfigService.getById(2);
BigDecimal money= (byId.getHasPay() ==null ? BigDecimal.valueOf(0) : byId.getHasPay());
String vipJson;
TVipConfig one = configService.getOne(null);
if (byId.getVipType()==1){
vipJson=one.getPersonVipLevel();
}else if (byId.getVipType()==2){
vipJson=one.getEnterpriseVipLevel();
}else {
Vip vip =new Vip();
return ApiResult.success(vip);
}
JSONObject jsonObject = JSON.parseObject(vipJson);
// 从 JSON 中获取各个值并转换为 BigDecimal 类型
BigDecimal money1 = new BigDecimal(jsonObject.getString("money1"));
BigDecimal money2 = new BigDecimal(jsonObject.getString("money2"));
BigDecimal money3 = new BigDecimal(jsonObject.getString("money3"));
BigDecimal money4 = new BigDecimal(jsonObject.getString("money4"));
BigDecimal money5 = new BigDecimal(jsonObject.getString("money5"));
// 组合为 BigDecimal 数组
BigDecimal[] moneyArray = { money1, money2, money3, money4, money5 };
Integer level = 0;
BigDecimal needPay = null;
BigDecimal total = null;
for (int i = 0; i < moneyArray.length; i++) {
BigDecimal bigDecimal = moneyArray[i];
if (money.compareTo(bigDecimal)<0){
needPay= bigDecimal.subtract(money);
total = bigDecimal;
level = i;
break;
}
}
Vip vip = new Vip();
vip.setVipEndTime(byId.getEndTime()==null?null:byId.getEndTime());
vip.setVipType(byId.getVipType());
vip.setConfig(byId1);
vip.setNeedPayToUp(needPay);
vip.setLevel(level);
vip.setTotal(total);
return ApiResult.success(vip);
}
@ApiOperation(value = "购买会员操作")
@PostMapping(value = "/pay")
public ApiResult updateById(@RequestBody VipPayDto vipPayDto) throws Exception {
TVipConfig byId = vipConfigService.getById(1);
BigDecimal money = BigDecimal.ZERO;
switch (vipPayDto.getType()){
case 1: money =byId.getPersonalVipCost();break;
case 2: money = byId.getPersonalVipRenew();break;
case 3: money = byId.getEnterpriseVipCost();break;
case 4: money = byId.getEnterpriseVipRenew();break;
}
Long userId = loginInfoUtil.getUserId();
return vipConfigService.pay(userId,money,vipPayDto.getMonth(),vipPayDto.getPayType(),vipPayDto.getType());
}
// @ApiOperation(value = "查询还差多少升级下一会员")
// @PostMapping(value = "/viplevel")
// public ApiResult viplevel(){
// Long userId = loginInfoUtil.getUserId();
// TUser byId = userService.getById(userId);
//
//
// }
}