package com.yonge.cooleshow.admin.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.ActivityPlanDto;
import com.yonge.cooleshow.biz.dal.dto.ActivityPlanRewardDto;
import com.yonge.cooleshow.biz.dal.dto.SaveOrUpdateRewardDto;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.common.enums.ActivityTypeEnum;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.yonge.cooleshow.biz.dal.vo.ActivityPlanVo;
import com.yonge.cooleshow.biz.dal.dto.search.ActivityPlanSearch;
import com.yonge.cooleshow.biz.dal.entity.ActivityPlan;
import com.yonge.cooleshow.biz.dal.service.ActivityPlanService;

import java.util.List;

@RestController
@RequestMapping("/activityPlan")
@Api(value = "活动计划表", tags = "活动计划表")
public class ActivityPlanController extends BaseController {

    @Autowired
    private ActivityPlanService activityPlanService;

    @Resource
    private SysUserFeignService sysUserFeignService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/detail')")
    public HttpResponseResult<ActivityPlanVo> detail(@PathVariable("id") Long id) {
        return succeed(activityPlanService.detail(id));
    }

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入activityPlanSearch")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/page')")
    public HttpResponseResult<PageInfo<ActivityPlanVo>> page(@RequestBody ActivityPlanSearch query) {
        IPage<ActivityPlanVo> pages = activityPlanService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "传入activityPlan")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/submit')")
    public HttpResponseResult<ActivityPlan> submit(@Valid @RequestBody ActivityPlanDto activityPlan) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        activityPlan.setUserId(user.getId());

        // 分享活动奖品校验
        if (ActivityTypeEnum.SHARE == activityPlan.getActivityType()
                && CollectionUtils.isEmpty(activityPlan.getRewardItems())) {
            return failed("分享活动奖品信息为空");
        }

        try {
            return HttpResponseResult.succeed(activityPlanService.submit(activityPlan));
        } catch (BizException e) {
            return HttpResponseResult.failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResponseResult.failed("变更失败");
        }
    }

    /**
     * 启用/停用
     */
    @GetMapping("/updateActivityState")
    @ApiOperation(value = "启用/停用")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/updateActivityState')")
    public HttpResponseResult updateActivityState(
            @ApiParam(value = "活动id", required = true) @RequestParam("activityId") Long activityId,
            @ApiParam(value = "活动状态 0 停用 1 启用", required = true) @RequestParam("activityState") Integer activityState) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return status(activityPlanService.updateActivityState(activityId, activityState, user.getId()));
    }

    /**
     * 草稿/正式
     */
    @GetMapping("/updateDraftFlag")
    @ApiOperation(value = "草稿/正式")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/updateDraftFlag')")
    public HttpResponseResult updateDraftFlag(
            @ApiParam(value = "活动id", required = true) @RequestParam("activityId") Long activityId,
            @ApiParam(value = "创建状态 0 正式 1 草稿 ", required = true) @RequestParam("draftFlag") Integer draftFlag) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return status(activityPlanService.updateDraftFlag(activityId, draftFlag, user.getId()));
    }

    /**
     * 修改奖品
     */
    @PostMapping("/saveOrUpdateReward")
    @ApiOperation(value = "修改奖品")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/saveOrUpdateReward')")
    public HttpResponseResult saveOrUpdateReward(@Validated @RequestBody SaveOrUpdateRewardDto saveOrUpdateRewardDto) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        try {
            return HttpResponseResult.succeed(activityPlanService.saveOrUpdateReward(saveOrUpdateRewardDto));
        } catch (BizException e) {
            return HttpResponseResult.failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResponseResult.failed("变更失败");
        }
    }
}