package com.yonge.cooleshow.teacher.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.queryInfo.UserRewardQueryInfo;
import com.yonge.cooleshow.biz.dal.service.ActivityUserRewardService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.vo.CheckVo;
import com.yonge.cooleshow.biz.dal.vo.activity.UserRewardWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.cooleshow.teacher.io.request.ActivityVo;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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


@Api(tags = "活动接口")
@RestController
@RequestMapping("${app-config.url.teacher:}/activity")
public class ActivityController extends BaseController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private ActivityUserRewardService activityUserRewardService;

    @ApiOperation(value = "检查是否存在未领奖")
    @GetMapping("/checkReceiveReward")
    public HttpResponseResult<CheckVo> checkReceiveReward(){
        Boolean b = activityUserRewardService.checkReceiveReward(sysUserService.getUserId(), ClientEnum.TEACHER);
        YesOrNoEnum check = b?YesOrNoEnum.YES:YesOrNoEnum.NO;
        CheckVo checkVo = new CheckVo();
        checkVo.setCheck(check);

        return succeed(checkVo);
    }


    @ApiOperation(value = "查看领奖列表")
    @PostMapping("/receiveRewardList")
    public HttpResponseResult<PageInfo<ActivityVo.ReceiveRewardList>> receiveRewardList(@RequestBody ActivityVo.ReceiveRewardQuery query){
        query.setClient(ClientEnum.TEACHER);
        query.setReceive(YesOrNoEnum.NO);
        query.setUserId(sysUserService.getUserId());

        IPage<UserRewardWrapper> iPage = activityUserRewardService.receiveRewardList(PageUtil.getPage(query), UserRewardQueryInfo.from(query.jsonString()));

        // 数据转换
        List<ActivityVo.ReceiveRewardList> pageInfos = JSON.parseArray(JSON.toJSONString(iPage.getRecords()),
                                                                       ActivityVo.ReceiveRewardList.class);

        return succeed(PageUtil.getPageInfo(iPage,pageInfos));
    }


    @ApiOperation(value = "领取奖品")
    @PostMapping("/receiveReward/{receiveRewardId}")
    public HttpResponseResult<CheckVo> receiveReward(@PathVariable Long receiveRewardId){
        return status(activityUserRewardService.receiveReward(sysUserService.getUserId(), receiveRewardId));
    }
}

