package com.ylx.point.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.R;
import com.ylx.point.domain.PointActivity;
import com.ylx.point.domain.PointUserLog;
import com.ylx.point.domain.dto.CompleteTaskDTO;
import com.ylx.point.domain.dto.UserPointActivityPageDTO;
import com.ylx.point.domain.dto.UserPointPageDTO;
import com.ylx.point.domain.vo.UserPointActivityVo;
import com.ylx.point.domain.vo.UserPointInfoVO;
import com.ylx.point.domain.vo.UserPointLogVO;
import com.ylx.point.service.IPointAccountService;
import com.ylx.point.service.IPointActivityService;
import com.ylx.point.service.IPointUserActivityTaskCompletionService;
import com.ylx.point.service.IPointUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@PreAuthorize("@customerAuth.isCustomer()")
@RestController
@RequestMapping("/user/point")
@Api(tags = {"用户积分"})
public class UserPointController extends BaseController {

    @Resource
    private IPointUserLogService pointUserLogService;
    @Resource
    private IPointActivityService pointActivityService;
    @Resource
    private IPointUserActivityTaskCompletionService pointUserActivityTaskCompletionService;
    @Resource
    private IPointAccountService pointAccountService;

    @ApiOperation("获取当前用户的积分信息")
    @GetMapping
    public R<UserPointInfoVO> getUserPointInfo(@RequestParam String cityCode) {
        UserPointInfoVO vo = this.pointUserLogService.getUserPointInfo(cityCode);
        return R.ok(vo);
    }

    @ApiOperation("获取当前用户的积分信息分页数据")
    @PostMapping("/page")
    public R<Page<UserPointLogVO>> page(Page<PointUserLog> page, @RequestBody UserPointPageDTO dto) {
        Page<UserPointLogVO> pageData = pointUserLogService.getUserPointLogList(page, dto);
        return R.ok(pageData);
    }

    @ApiOperation("获取当前用户活动任务分页数据")
    @PostMapping("/activity/page")
    public R<Page<UserPointActivityVo>> activityPage(Page<PointActivity> page, @RequestBody UserPointActivityPageDTO dto) {
        Page<UserPointActivityVo> pageData = pointActivityService.getUserPointActivityList(page, dto);
        return R.ok(pageData);
    }

    @ApiOperation("获取当前用户做任务赚积分集合")
    @GetMapping("/activity/list")
    public R<List<UserPointActivityVo>> activityList() {
        List<UserPointActivityVo> list = pointActivityService.activityList();
        return R.ok(list);
    }

    @ApiOperation("用户点击“去完成”接口")
    @PostMapping("/complete")
    public R completeTask(@Validated @RequestBody CompleteTaskDTO dto) {
        this.pointUserActivityTaskCompletionService.executeTask(dto.getTaskId());
        return R.ok();
    }

    @ApiOperation("用户领取积分")
    @PostMapping("/draw")
    public R draw() {
        this.pointAccountService.draw();
        return R.ok();
    }

}
