package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bst.ad.domain.enums.AdBelong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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.domain.AdVerifyDTO;
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.core.validate.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 广告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, bizIdName = "arg0", bizType = LogBizType.AD)
    @PostMapping
    public AjaxResult add(@Validated(ValidGroup.Create.class) @RequestBody Ad ad)
    {
        if (isSysAdmin()) {
            ad.setAuditStatus("2");
        } else {
            ad.setAuditStatus("1");
            ad.setBelong(AdBelong.STORE.getCode());
        }
        ad = adConverter.toPoByCreate(ad);
        return toAjax(adService.insertAd(ad));
    }


    /**
     * 修改广告
     */
    @PreAuthorize("@ss.hasPermi('bst:ad:edit')")
    @Log(title = "修改广告", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.AD)
    @PutMapping
    public AjaxResult edit(@Validated(ValidGroup.Update.class)@RequestBody Ad ad)
    {
        if (!adValidator.canEdit(ad.getAdId())){
            return AjaxResult.error("您没有权限修改id为" + ad.getAdId() + "的广告信息");
        }
        if (isSysAdmin()) {
            ad.setAuditStatus("2");
        } else {
            ad.setAuditStatus("1");
        }
        adConverter.toPoByUpdate(ad);
        return toAjax(adService.updateAd(ad));
    }

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

    @PreAuthorize("@ss.hasPermi('bst:ad:edit')")
    @Log(title = "广告审核", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.AD)
    @ApiOperation("广告审核")
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody @Validated AdVerifyDTO dto) {
       if (!isSysAdmin()){
           return AjaxResult.error("您没有权限处理id为" + dto.getId() + "的广告添加申请");
       }
        return toAjax(adService.verify(dto));
    }

    @PreAuthorize("@ss.hasPermi('bst:ad:enable')")
    @Log(title = "广告启用", businessType = BusinessType.OTHER, bizIdName = "arg0", bizType = LogBizType.AD)
    @ApiOperation("广告启用")
    @PutMapping("/enable")
    public AjaxResult enableAd(@RequestBody Long id) {
        if (!adValidator.canEdit(id)){
            return error("您无权启用id为"+ id +"的广告");
        }
        int count = adService.enable(id);
        if (count == 0) {
            return error("启用失败");
        }
        return toAjax(count);
    }

    @PreAuthorize("@ss.hasPermi('bst:ad:disable')")
    @Log(title = "广告禁用", businessType = BusinessType.OTHER, bizIdName = "arg0", bizType = LogBizType.AD)
    @ApiOperation("广告禁用")
    @PutMapping("/disable")
    public AjaxResult disableAd(@RequestBody Long id) {
        if (!adValidator.canEdit(id)){
            return error("您无权禁用id为"+ id +"的广告");
        }
        int count = adService.disable(id);
        if (count == 0) {
            return error("禁用失败");
        }
        return toAjax(count);
    }

}
