package com.ruoyi.web.modules.content.controller;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.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.web.modules.content.domain.BizHomeBanner;
import com.ruoyi.web.modules.content.dto.BannerSortDTO;
import com.ruoyi.web.modules.content.service.IBannerService;
import com.ruoyi.web.modules.content.vo.BannerListVO;

/**
 * 平台端首页 Banner 管理
 */
@RestController
@RequestMapping("/agri/banner")
public class BannerController extends BaseController
{
    @Autowired
    private IBannerService bannerService;

    @PreAuthorize("@ss.hasPermi('agri:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizHomeBanner query)
    {
        startPage();
        List<BannerListVO> list = bannerService.selectBannerList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('agri:banner:list')")
    @GetMapping("/{bannerId}")
    public AjaxResult getInfo(@PathVariable("bannerId") Long bannerId)
    {
        return success(bannerService.selectBannerById(bannerId));
    }

    @PreAuthorize("@ss.hasPermi('agri:banner:add')")
    @Log(title = "首页Banner", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizHomeBanner banner)
    {
        banner.setCreateBy(getUsername());
        Map<String, Object> data = bannerService.insertBanner(banner);
        AjaxResult result = success();
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:banner:edit')")
    @Log(title = "首页Banner", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizHomeBanner banner)
    {
        banner.setUpdateBy(getUsername());
        Map<String, Object> data = bannerService.updateBanner(banner);
        AjaxResult result = success();
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:banner:edit')")
    @Log(title = "首页Banner排序", businessType = BusinessType.UPDATE)
    @PutMapping("/sortNo")
    public AjaxResult updateSortNo(@RequestBody BannerSortDTO dto)
    {
        bannerService.updateSortNo(dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:banner:remove')")
    @Log(title = "首页Banner", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerIds}")
    public AjaxResult remove(@PathVariable("bannerIds") Long[] bannerIds)
    {
        String msg = bannerService.deleteBannerByIds(bannerIds, getUsername());
        return success(msg);
    }
}
