package com.ruoyi.web.controller.ss;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ss.ad.domain.AdQuery;
import com.ruoyi.ss.ad.domain.AdVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.ad.domain.Ad;
import com.ruoyi.ss.ad.service.IAdService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 广告Controller
 *
 * @author ruoyi
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/ss/ad")
public class AdController extends BaseController
{
    @Autowired
    private IAdService adService;

    /**
     * 查询广告列表
     */
    @PreAuthorize("@ss.hasPermi('ss:ad:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdQuery ad)
    {
        startPage();
        List<AdVO> list = adService.selectAdList(ad);
        return getDataTable(list);
    }

    /**
     * 导出广告列表
     */
    @PreAuthorize("@ss.hasPermi('ss:ad:export')")
    @Log(title = "广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdQuery ad)
    {
        List<AdVO> list = adService.selectAdList(ad);
        ExcelUtil<AdVO> util = new ExcelUtil<AdVO>(AdVO.class);
        util.exportExcel(response, list, "广告数据");
    }

    /**
     * 获取广告详细信息
     */
    @PreAuthorize("@ss.hasPermi('ss:ad:query')")
    @GetMapping(value = "/{adId}")
    public AjaxResult getInfo(@PathVariable("adId") Long adId)
    {
        return success(adService.selectAdByAdId(adId));
    }

    /**
     * 新增广告
     */
    @PreAuthorize("@ss.hasPermi('ss:ad:add')")
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ad ad)
    {
        return toAjax(adService.insertAd(ad));
    }

    /**
     * 修改广告
     */
    @PreAuthorize("@ss.hasPermi('ss:ad:edit')")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ad ad)
    {
        return toAjax(adService.updateAd(ad));
    }

    /**
     * 删除广告
     */
    @PreAuthorize("@ss.hasPermi('ss:ad:remove')")
    @Log(title = "广告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{adIds}")
    public AjaxResult remove(@PathVariable Long[] adIds)
    {
        return toAjax(adService.logicDel(Arrays.asList(adIds)));
    }
}
