package com.contract.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.contract.entity.AppBanner;
import com.contract.entity.resp.MessageResult;
import com.contract.enums.MessageCode;
import com.contract.service.AppBannerService;
import com.contract.util.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app-banner")
public class AppBannerController {

    @Autowired
    private AppBannerService appBannerService;

    /**
     * 分页查询
     */
    @PostMapping("/page-query")
    public MessageResult pageQuery(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String title,
            HttpServletRequest request) {
        String lang = RequestUtils.getLangFromHeader(request);
        Page<AppBanner> page = appBannerService.pageQuery(pageNum, pageSize, status, title);
        return MessageResult.successLang(lang, page);
    }

    /**
     * 查询详情
     */
    @GetMapping("/detail")
    public MessageResult detail(@RequestParam Long id, HttpServletRequest request) {
        String lang = RequestUtils.getLangFromHeader(request);
        AppBanner banner = appBannerService.getById(id);
        if (banner == null) {
            return MessageResult.failLang(lang, MessageCode.BANNER_NOT_FOUND);
        }
        return MessageResult.successLang(lang, banner);
    }

    /**
     * 查询App端生效的Banner列表
     */
    @GetMapping("/active-list")
    public MessageResult activeList(HttpServletRequest request) {
        String lang = RequestUtils.getLangFromHeader(request);
        return MessageResult.successLang(lang, appBannerService.findActiveBanners());
    }
}
