package com.yc.ship.module.miniapplet.controller.admin.advertlog;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.miniapplet.controller.admin.advertlog.vo.AdvertLogPageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.advertlog.vo.AdvertLogRespVO;
import com.yc.ship.module.miniapplet.controller.admin.advertlog.vo.AdvertLogSaveReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.advertlog.AdvertLogDO;
import com.yc.ship.module.miniapplet.service.advertlog.AdvertLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 广告操作日志管理")
@RestController
@RequestMapping("/applet/advert-log")
@Validated
public class AdvertLogController {

    @Resource
    private AdvertLogService advertLogService;

    @PostMapping("/create")
    @Operation(summary = "创建广告操作日志管理")
    @PreAuthorize("@ss.hasPermission('applet:advert-log:create')")
    public CommonResult<Long> createAdvertLog(@Valid @RequestBody AdvertLogSaveReqVO createReqVO) {
        return success(advertLogService.createAdvertLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告操作日志管理")
    @PreAuthorize("@ss.hasPermission('applet:advert-log:update')")
    public CommonResult<Boolean> updateAdvertLog(@Valid @RequestBody AdvertLogSaveReqVO updateReqVO) {
        advertLogService.updateAdvertLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告操作日志管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('applet:advert-log:delete')")
    public CommonResult<Boolean> deleteAdvertLog(@RequestParam("id") Long id) {
        advertLogService.deleteAdvertLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告操作日志管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('applet:advert-log:query')")
    public CommonResult<AdvertLogRespVO> getAdvertLog(@RequestParam("id") Long id) {
        AdvertLogDO advertLog = advertLogService.getAdvertLog(id);
        return success(BeanUtils.toBean(advertLog, AdvertLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告操作日志管理分页")
    @PreAuthorize("@ss.hasPermission('applet:advert-log:query')")
    public CommonResult<PageResult<AdvertLogRespVO>> getAdvertLogPage(@Valid AdvertLogPageReqVO pageReqVO) {
        PageResult<AdvertLogDO> pageResult = advertLogService.getAdvertLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdvertLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告操作日志管理 Excel")
    @PreAuthorize("@ss.hasPermission('applet:advert-log:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportAdvertLogExcel(@Valid AdvertLogPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdvertLogDO> list = advertLogService.getAdvertLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告操作日志管理.xls", "数据", AdvertLogRespVO.class,
                BeanUtils.toBean(list, AdvertLogRespVO.class));
    }

}
