package com.ruoyi.web.app;

import com.ruoyi.bst.ad.domain.AdQuery;
import com.ruoyi.bst.ad.domain.AdVO;
import com.ruoyi.bst.ad.domain.enums.AdBelong;
import com.ruoyi.bst.ad.domain.enums.AdStatus;
import com.ruoyi.bst.ad.domain.enums.AdVerifyStatus;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.ad.service.AdService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/ad")
public class AppAdController extends BaseController {

    @Autowired
    private AdService adService;

    @Autowired
    private UserService userService;

    @ApiOperation("获取广告")
    @GetMapping
    @Anonymous
    public AjaxResult getAd(AdQuery query) {
        query.setAuditStatus(AdVerifyStatus.PASSED.getCode());
        query.setStatus(AdStatus.ENABLE.getCode());
        List<AdVO> list = new ArrayList<>();

        // 查询所属运营区的广告
        UserVO user = userService.selectUserById(getUserId());
        if (user.getAreaId() != null) {
            query.setAreaId(user.getAreaId());
            query.setBelong(AdBelong.STORE.getCode());
            List<AdVO> areaAdVOList = adService.selectAdList(query);
            if (areaAdVOList != null) {
                list.addAll(areaAdVOList);
            }
        }

        // 查询管理员发布的广告
        query.setAreaId(null);
        query.setBelong(AdBelong.ADMIN.getCode());
        List<AdVO> adminAdVOList = adService.selectAdList(query);
        if (adminAdVOList != null) {
            list.addAll(adminAdVOList);
        }

        return success(list);
    }

}
