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.ActivityRewardDto;
import com.yonge.cooleshow.biz.dal.dto.search.ActivityRewardChangeStockSearch;
import com.yonge.cooleshow.biz.dal.entity.ActivityRewardChangeStock;
import com.yonge.cooleshow.biz.dal.vo.ActivityRewardChangeStockVo;
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.RewardTypeEnum;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;

import com.yonge.toolset.base.util.StringUtil;
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.web.bind.annotation.*;

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

import com.yonge.cooleshow.biz.dal.vo.ActivityRewardVo;
import com.yonge.cooleshow.biz.dal.dto.search.ActivityRewardSearch;
import com.yonge.cooleshow.biz.dal.entity.ActivityReward;
import com.yonge.cooleshow.biz.dal.service.ActivityRewardService;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/activityReward")
@Api(value = "活动奖品表", tags = "活动奖品表")
public class ActivityRewardController extends BaseController {

    @Autowired
    private ActivityRewardService activityRewardService;
    @Resource
    private SysUserFeignService sysUserFeignService;

    /**
     * 通过奖品id查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "通过奖品id查询单条", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('activityReward/detail')")
    public HttpResponseResult<ActivityRewardVo> detail(@PathVariable("id") Long id) {
        return succeed(activityRewardService.detail(id));
    }

    /**
     * 通过活动id查询单条(弃用)
     */
    @GetMapping("/detailByActivityId/{id}")
    @ApiOperation(value = "通过活动id查询单条(弃用)", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('activityReward/detailByActivityId')")
    public HttpResponseResult<ActivityRewardVo> detailByActivityId(@PathVariable("id") Long id) {
        return succeed(activityRewardService.detailByActivityId(id));
    }

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

    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "传入activityRewardSearch")
    @PreAuthorize("@pcs.hasPermissions('activityReward/list')")
    public HttpResponseResult<List<ActivityRewardVo>> list(@RequestBody ActivityRewardSearch query) {
        List<ActivityRewardVo> list = activityRewardService.selectAll(query);
        return succeed(list);
    }

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

        // 校验请求参数
        if (RewardTypeEnum.COUPON == activityReward.getRewardType()
                && Optional.ofNullable(activityReward.getCouponId()).orElse(0L) <= 0) {
            return failed("无效的优惠券ID");
        }

        return status(activityRewardService.submit(activityReward, user));
    }

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

    /**
     * 调整库存
     */
    @PostMapping("/updateStock")
    @ApiOperation(value = "调整库存")
    @PreAuthorize("@pcs.hasPermissions('activityReward/updateStock')")
    public HttpResponseResult updateStock(@RequestBody ActivityRewardChangeStock activityRewardChangeStock) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        activityRewardChangeStock.setCreateBy(user.getId());
        activityRewardChangeStock.setSource("ADMIN");
        try {
            activityRewardService.updateStock(activityRewardChangeStock);
            return HttpResponseResult.succeed();
        } catch (BizException e) {
            return HttpResponseResult.failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResponseResult.failed("变更失败");
        }
    }

    /**
     * 库存变更记录查询分页
     */
    @PostMapping("/selectChangeStockPage")
    @ApiOperation(value = "库存变更记录查询分页", notes = "传入activityRewardChangeStockSearch")
    @PreAuthorize("@pcs.hasPermissions('activityReward/selectChangeStockPage')")
    public HttpResponseResult<PageInfo<ActivityRewardChangeStockVo>> selectChangeStockPage(@RequestBody ActivityRewardChangeStockSearch query) {
        query.setSource("ADMIN");
        IPage<ActivityRewardChangeStockVo> pages = activityRewardService.selectChangeStockPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

}