package com.ruoyi.web.bst;

import com.ruoyi.bst.ad.domain.Ad;
import com.ruoyi.bst.ad.domain.AdQuery;
import com.ruoyi.bst.ad.domain.AdVO;
import com.ruoyi.bst.ad.enums.AdBlong;
import com.ruoyi.bst.ad.service.AdConverter;
import com.ruoyi.bst.ad.service.AdService;
import com.ruoyi.bst.ad.service.AdValidator;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.user.service.UserService;
import com.ruoyi.system.user.service.UserValidator;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 广告Controller
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/bst/ad")
public class AdController extends BaseController
{
    @Autowired
    private AdService adService;
    @Autowired
    private AdConverter adConverter;
    @Autowired
    private AdValidator adValidator;
    @Autowired
    private AreaService areaService;

    /**
     * 查询广告列表
     */
    @PreAuthorize("@ss.hasPermi('bst:ad:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        query.setDeleted(false);
        return getDataTable(adService.selectAdList(query));
    }

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

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

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

    /**
     * 修改广告
     */
    @PreAuthorize("@ss.hasPermi('bst:ad:edit')")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ad ad)
    {
        if (!adValidator.canEdit(ad.getAdId())){
            return AjaxResult.error("您没有权限修改id为" + ad.getAdId() + "的广告信息");
        }
        return toAjax(adService.updateAd(ad));
    }

    /**
     * 删除广告
     */
    @PreAuthorize("@ss.hasPermi('bst:ad:remove')")
    @Log(title = "广告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{adIds}")
    public AjaxResult remove(@PathVariable List<Long> adIds)
    {
        if (!adValidator.canDeleteAll(adIds)) {
            return AjaxResult.error("您没有权限删除ID为" + adIds + "的广告信息");
        }
        return toAjax(adService.logicalDel(adIds));
    }
}
