package com.zhongzheng.controller.activity;

import cn.hutool.core.convert.Convert;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.activity.bo.ActivityConfigurationAddBo;
import com.zhongzheng.modules.activity.bo.ActivityConfigurationEditBo;
import com.zhongzheng.modules.activity.bo.ActivityConfigurationQueryBo;
import com.zhongzheng.modules.activity.bo.ActivityConfigurationUseAddBo;
import com.zhongzheng.modules.activity.service.IActivityConfigurationService;
import com.zhongzheng.modules.activity.service.IActivityConfigurationUseService;
import com.zhongzheng.modules.activity.vo.ActivityConfigurationVo;
import com.zhongzheng.modules.course.bo.CourseQueryBo;
import com.zhongzheng.modules.course.service.ICourseService;
import com.zhongzheng.modules.course.vo.CourseConfigurationVo;
import com.zhongzheng.modules.order.bo.CouponQueryBo;
import com.zhongzheng.modules.order.service.ICouponService;
import com.zhongzheng.modules.order.vo.CouponVo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 活动配置Controller
 * 
 * @author change
 * @date 2021-05-17
 */
@Api(value = "活动配置控制器", tags = {"活动配置管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/configuration")
public class ActivityConfigurationController extends BaseController {

    private final IActivityConfigurationService iActivityConfigurationService;

    private final IActivityConfigurationUseService activityConfigurationUseServiceImpl;

    private final ICouponService couponService;

    private final WxTokenService WxTokenService;

    /**
     * 查询活动配置列表
     */
    @ApiOperation("查询活动配置列表")
    @GetMapping("/list")
    public TableDataInfo<ActivityConfigurationVo> list(ActivityConfigurationQueryBo bo) {
        startPage();
        ClientLoginUser loginUser = WxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(Convert.toInt(loginUser.getUser().getUserId()));
        List<ActivityConfigurationVo> list = iActivityConfigurationService.queryIssueList(bo);
        return getDataTable(list);
    }

    /**
     * 查询活动配置列表
     */
    @ApiOperation("查询课程所做的活动")
    @GetMapping("/listConfig")
    public TableDataInfo<ActivityConfigurationVo> listConfiguration(CourseQueryBo bo) {
        startPage();
        List<ActivityConfigurationVo> list = iActivityConfigurationService.listConfiguration(bo);
        return getDataTable(list);
    }


    /**
     * 领取活动
     */
    @ApiOperation("領取活動發送請求")
    @PostMapping("/receive")
    public AjaxResult<Void> receive(ActivityConfigurationUseAddBo bo) {
        ClientLoginUser loginUser = WxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(activityConfigurationUseServiceImpl.receive(bo) ? 1 : 0);
    }


    /**
     * 查詢擁有券
     */
    @ApiOperation("查詢用户擁有券")
    @GetMapping("/listCoupon")
    public TableDataInfo<CouponVo> listCoupon(CouponQueryBo bo) {
        startPage();
        ClientLoginUser loginUser = WxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<CouponVo> list = couponService.queryIssueListCoupon(bo);
        return getDataTable(list);
    }


}
