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.module.member.controller.admin.card.vo.CardRespVO;
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.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 static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

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

    @Resource
    private CardService cardService;

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

    //@PostMapping("/create")
    //@Operation(summary = "创建余额变动明细")
    //@PreAuthorize("@ss.hasPermission('member:card:create')")
    //public CommonResult<Long> createCard(@Valid @RequestBody CardSaveReqVO createReqVO) {
    //    return success(cardService.createCard(createReqVO));
    //}
    //
    //@PutMapping("/update")
    //@Operation(summary = "更新余额变动明细")
    //@PreAuthorize("@ss.hasPermission('member:card:update')")
    //public CommonResult<Boolean> updateCard(@Valid @RequestBody CardSaveReqVO updateReqVO) {
    //    cardService.updateCard(updateReqVO);
    //    return success(true);
    //}
    //
    //@DeleteMapping("/delete")
    //@Operation(summary = "删除余额变动明细")
    //@Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('member:card:delete')")
    //public CommonResult<Boolean> deleteCard(@RequestParam("id") Long id) {
    //    cardService.deleteCard(id);
    //    return success(true);
    //}
    //
    //@GetMapping("/get")
    //@Operation(summary = "获得余额变动明细")
    //@Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('member:card:query')")
    //public CommonResult<CardRespVO> getCard(@RequestParam("id") Long id) {
    //    CardDO card = cardService.getCard(id);
    //    return success(BeanUtils.toBean(card, CardRespVO.class));
    //}

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

    //@GetMapping("/export-excel")
    //@Operation(summary = "导出余额变动明细 Excel")
    //@PreAuthorize("@ss.hasPermission('member:card:export')")
    //@OperateLog(type = EXPORT)
    //public void exportCardExcel(@Valid CardPageReqVO pageReqVO,
    //          HttpServletResponse response) throws IOException {
    //    pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    //    List<CardDO> list = cardService.getCardPage(pageReqVO).getList();
    //    // 导出 Excel
    //    ExcelUtils.write(response, "余额变动明细.xls", "数据", CardRespVO.class,
    //                    BeanUtils.toBean(list, CardRespVO.class));
    //}

    @PutMapping("/recharge")
    @Operation(summary = "充值")
    //@PreAuthorize("@ss.hasPermission('member:card:update')")
    public CommonResult<Boolean> recharge(BigDecimal money,BigDecimal giftMoney) {
        //BigDecimal bigDecimal = money.divide(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
        return success(cardService.recharge(money,CardDO.ADD,giftMoney));

    }

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

    @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) {
//        生成一个redis对象并且插入数据
//        redisTemplate.opsForValue().get(ip);
        return success(redisTemplate.opsForValue().get(ip));
    }

    @GetMapping("/plateGetInfo")
    @Operation(summary = "获取盘子信息")
    public CommonResult<Object> plateGetInfo(@RequestParam String ip) {
//        生成一个redis对象并且插入数据
        return success(redisTemplate.opsForValue().get(ip));
    }

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

}