package cn.iocoder.yudao.module.member.controller.app.card;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.billing.vo.BillingRespMoneyVO;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.CardRespVO;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.RechargeVO;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.RobotReqVO;
import cn.iocoder.yudao.module.member.controller.app.card.vo.AppCardMonthVO;
import cn.iocoder.yudao.module.member.controller.app.card.vo.AppCardPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.card.CardDO;
import cn.iocoder.yudao.module.member.service.billing.BillingService;
import cn.iocoder.yudao.module.member.service.card.CardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.time.LocalDate;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

@Tag(name = "用户 APP - 余额变动明细")
@RestController
@RequestMapping("/member/card")
@Validated
public class AppCardController {

    @Resource
    private CardService cardService;
    @Resource
    private BillingService billingService;

    //   写一个示例，将一条数据插入redis
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @GetMapping("/page")
    @Operation(summary = "获得余额变动明细分页")
    public CommonResult<PageResult<CardRespVO>> getCardPage(@Valid AppCardPageReqVO pageReqVO) {
        PageResult<CardDO> pageResult = cardService.getCardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CardRespVO.class));
    }



    @PutMapping("/recharge")
    @Operation(summary = "充值")
    public CommonResult<Boolean> recharge(BigDecimal money,BigDecimal giftMoney) {
        return success(true);

    }

    @GetMapping("/getMoney")
    @Operation(summary = "获取余额")
    public CommonResult<BigDecimal> getMoney(Long carteenId) {
        return success(cardService.getMoney(carteenId));
    }

    @GetMapping("/month")
    @Operation(summary = "获取本月支出或充值金额")
    @Parameter(name = "flag", description = "0-支出，1-充值", required = true, example = "1")
    public CommonResult<AppCardMonthVO> getMonthMoney(Long userId, String flag, String time) {
        return success(cardService.getMonthMoney(userId, flag, time));
    }



    @PostMapping("/weighingAddInfo")
    @Operation(summary = "称重信息入口")
    public void weighingAddInfo(@RequestBody RobotReqVO reqVO) {
//        生成一个redis对象并且插入数据
        redisTemplate.opsForValue().set(reqVO.getIp(), reqVO);
    }

    @GetMapping("/weighingGetInfo")
    @Operation(summary = "获取称重相关信息")
    public CommonResult<Object> weighingGetInfo(@RequestParam String ip) {
        return success(redisTemplate.opsForValue().get(ip));
    }

    @GetMapping("/plateGetInfo")
    @Operation(summary = "获取盘子信息")
    public CommonResult<Object> plateGetInfo(@RequestParam String ip) {
        return success(redisTemplate.opsForValue().get(ip));
    }

    @PostMapping("/plateAddInfo")
    @Operation(summary = "盘子信息入口")
    public void plateAddInfo(@RequestBody RobotReqVO reqVO) {
        redisTemplate.opsForValue().set(reqVO.getIp(), reqVO);
    }

    @PutMapping("/rechargeByAdmin")
    @Operation(summary = "管理充值")
    public CommonResult<Boolean> rechargeByAdmin(@RequestBody RechargeVO vo) {
//        BillingRespMoneyVO haveWithoutMoney = billingService.getHaveWithoutMoney(SecurityFrameworkUtils.getLoginUserId());
//        if (haveWithoutMoney.getWithoutMoney().compareTo(BigDecimal.ZERO)>0){
//            throw exception(BILLING_NOT_COMPLETE);
//        }

        //限制每月27号及以后不能充值
        LocalDate today = LocalDate.now();

        // 获取今天是几号
        int dayOfMonth = today.getDayOfMonth();

        // 判断是否是27号及以后
        if (dayOfMonth >= 27) {
            throw exception(TIME_NOT_SATISFY);
        }

        cardService.rechargeByAdmin(vo);
        return success(true);
    }
}