package co.yixiang.yshop.module.shop.controller.admin.popup;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.shop.controller.admin.popup.vo.*;
import co.yixiang.yshop.module.shop.dal.dataobject.popup.PopupDO;
import co.yixiang.yshop.module.shop.convert.popup.PopupConvert;
import co.yixiang.yshop.module.shop.service.popup.PopupService;

@Tag(name = "管理后台 - 弹窗")
@RestController
@RequestMapping("/shop/popup")
@Validated
public class PopupController {

    @Resource
    private PopupService popupService;

    @PostMapping("/create")
    @Operation(summary = "创建弹窗")
    @PreAuthorize("@ss.hasPermission('shop:popup:create')")
    public CommonResult<Long> createPopup(@Valid @RequestBody PopupCreateReqVO createReqVO) {
        return success(popupService.createPopup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新弹窗")
    @PreAuthorize("@ss.hasPermission('shop:popup:update')")
    public CommonResult<Boolean> updatePopup(@Valid @RequestBody PopupUpdateReqVO updateReqVO) {
        popupService.updatePopup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除弹窗")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:popup:delete')")
    public CommonResult<Boolean> deletePopup(@RequestParam("id") Long id) {
        popupService.deletePopup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得弹窗")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:popup:get')")
    public CommonResult<PopupRespVO> getPopup(@RequestParam("id") Long id) {
        PopupDO popup = popupService.getPopup(id);
        return success(PopupConvert.INSTANCE.convert(popup));
    }

    @GetMapping("/list")
    @Operation(summary = "获得弹窗列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:popup:list')")
    public CommonResult<List<PopupRespVO>> getPopupList(@RequestParam("ids") Collection<Long> ids) {
        List<PopupDO> list = popupService.getPopupList(ids);
        return success(PopupConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得弹窗分页")
    @PreAuthorize("@ss.hasPermission('shop:popup:query')")
    public CommonResult<PageResult<PopupRespVO>> getPopupPage(@Valid PopupPageReqVO pageVO) {
        PageResult<PopupDO> pageResult = popupService.getPopupPage(pageVO);
        return success(PopupConvert.INSTANCE.convertPage(pageResult));
    }

}
