package com.yiqi.admin.controller.app;

import com.yiqi.app.domain.AppBanner;
import com.yiqi.app.service.IAppBannerService;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.utils.poi.ExcelUtil;
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 xiaohei
 * @date 2023-08-08
 */
@RestController
@RequestMapping("/app/banner")
public class AppBannerController extends BaseController {
    @Autowired
    private IAppBannerService appBannerService;

    /**
     * 查询轮播图及启动页广告配置列表
     */
    @PreAuthorize("@ss.hasPermi('app:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppBanner appBanner) {
        startPage();
        List<AppBanner> list = appBannerService.selectAppBannerList(appBanner);
        return getDataTable(list);
    }

    /**
     * 导出轮播图及启动页广告配置列表
     */
    @PreAuthorize("@ss.hasPermi('app:banner:export')")
    @Log(title = "轮播图及启动页广告配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppBanner appBanner) {
        List<AppBanner> list = appBannerService.selectAppBannerList(appBanner);
        ExcelUtil<AppBanner> util = new ExcelUtil<AppBanner>(AppBanner. class);
        util.exportExcel(response, list, "轮播图及启动页广告配置数据");
    }

    /**
     * 获取轮播图及启动页广告配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(appBannerService.selectAppBannerById(id));
    }

    /**
     * 新增轮播图及启动页广告配置
     */
    @PreAuthorize("@ss.hasPermi('app:banner:add')")
    @Log(title = "轮播图及启动页广告配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppBanner appBanner) {
        appBanner.setCreateById(getLoginUser().getUserId());
        appBanner.setCreateBy(getUsername());
        return toAjax(appBannerService.insertAppBanner(appBanner));
    }

    /**
     * 修改轮播图及启动页广告配置
     */
    @PreAuthorize("@ss.hasPermi('app:banner:edit')")
    @Log(title = "轮播图及启动页广告配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppBanner appBanner) {
        appBanner.setUpdateById(getLoginUser().getUserId());
        appBanner.setUpdateBy(getUsername());
        return toAjax(appBannerService.updateAppBanner(appBanner));
    }

    /**
     * 删除轮播图及启动页广告配置
     */
    @PreAuthorize("@ss.hasPermi('app:banner:remove')")
    @Log(title = "轮播图及启动页广告配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(appBannerService.deleteAppBannerByIds(ids));
    }
}
