package com.jeeplus.pubmodules.luckyDraw.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.pubmodules.luckyDraw.service.LuckyDrawEventsService;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawEventsDTO;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@Api("抽奖-活动")
@RestController
@RequestMapping(value = "/luckyDraw/events")
public class LuckyDrawEventsController {

    @Resource
    private LuckyDrawEventsService luckyDrawEventsService;


    @ApiLog("活动列表")
    //@PreAuthorize("hasAuthority('lucky_draw:events:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<LuckyDrawEventsDTO>> data(LuckyDrawEventsDTO luckyDrawEventsDTO, Page<LuckyDrawEventsDTO> page) throws Exception {
        IPage<LuckyDrawEventsDTO> result = new Page<LuckyDrawEventsDTO>();
        result = luckyDrawEventsService.findList (page,luckyDrawEventsDTO);
        return ResponseEntity.ok (result);
    }

    @ApiLog("查询活动数据详情")
    //@PreAuthorize("hasAnyAuthority('lucky_draw:events:view','lucky_draw:events:add','lucky_draw:events:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        LuckyDrawEventsDTO luckyDrawEventsDTO = luckyDrawEventsService.queryById ( id );
        return ResponseEntity.ok (luckyDrawEventsDTO);
    }

    @ApiLog(value = "修改/新增活动数据", type = LogTypeEnum.SAVE)
    //@PreAuthorize("hasAnyAuthority('lucky_draw:events:add','lucky_draw:events:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody LuckyDrawEventsDTO luckyDrawEventsDTO) throws Exception {
        luckyDrawEventsService.saveForm(luckyDrawEventsDTO);
        return ResponseEntity.ok ("操作成功");
    }

    @ApiLog(value = "删除活动", type = LogTypeEnum.SAVE)
    //@PreAuthorize("hasAuthority('lucky_draw:events:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return luckyDrawEventsService.deleteByIds(ids);
    }

    @ApiLog("开启活动")
    //@PreAuthorize("hasAnyAuthority('lucky_draw:events:edit')")
    @GetMapping("openEvent")
    public ResponseEntity openEvent(@RequestParam("id") String id) {
        return luckyDrawEventsService.changeSwitchOpen(id);
    }

    @ApiLog("关闭活动")
    //@PreAuthorize("hasAnyAuthority('lucky_draw:events:edit')")
    @GetMapping("closeEvent")
    public ResponseEntity closeEvent(@RequestParam("id") String id) {
        return luckyDrawEventsService.changeSwitchClose(id);
    }

}
