package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.elasticsearch.UserGrowthLog;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.service.IUserGrowthLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/16
 */
@RestController
@RequestMapping("/userGrowthLog")
@Api(value = "用户成长值记录控制器", tags = "用户成长值记录控制器", description = "用户成长值记录控制器")
public class UserGrowthLogController {
    @Autowired
    private IUserGrowthLogService iUserGrowthLogService;

    @GetMapping("/logs")
    @ApiOperation(value = "查询成长值记录")
    public Page<UserGrowthLog> query(@ApiParam(value = "页数", required = true) @RequestParam Integer page,
                                     @ApiParam(value = "每页数量", required = true) @RequestParam Integer size,
                                     @ApiParam(value = "密文手机号", required = true) @RequestParam String secretPhone) {
        return iUserGrowthLogService.queryBySecretPhone(secretPhone, page, size);
    }

    @GetMapping("/lastLog")
    @ApiOperation(value = "获取最后记录")
    public ResultObject<UserGrowthLog> getLastLog(@ApiParam(value = "密文手机号", required = true) @RequestParam String secretPhone) {
        UserGrowthLog log = iUserGrowthLogService.getLastLog(secretPhone);
        if(log == null) {
            return ResultObject.success("没有记录",null);
        } else {
            return ResultObject.success(log);
        }
    }

    @GetMapping("/balance")
    @ApiOperation(value = "获取积分余额")
    public ResultObject<Integer> getBalance(@ApiParam(value = "密文手机号", required = true) @RequestParam String secretPhone) {
        return ResultObject.success(iUserGrowthLogService.getPointBalance(secretPhone));
    }

    @PostMapping("/log")
    @ApiOperation(value = "添加积分记录")
    public ResultObject<Object> addLog(@ApiParam(value = "积分记录对象", required = true) @RequestBody UserGrowthLog log) {
        if(iUserGrowthLogService.addLog(log)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }
}
