package com.ruoyi.bst.ad.service.impl;

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

import com.ruoyi.bst.ad.enums.AdBlong;
import com.ruoyi.bst.ad.service.AdConverter;
import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.OrderVO;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bst.ad.mapper.AdMapper;
import com.ruoyi.bst.ad.domain.Ad;
import com.ruoyi.bst.ad.domain.AdVO;
import com.ruoyi.bst.ad.domain.AdQuery;
import com.ruoyi.bst.ad.service.AdService;

/**
 * 广告Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@Service
public class AdServiceImpl implements AdService
{
    @Autowired
    private AdMapper adMapper;
    @Autowired
    private AdConverter adConverter;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private UserService userService;

    /**
     * 查询广告
     *
     * @param adId 广告主键
     * @return 广告
     */
    @Override
    public AdVO selectAdByAdId(Long adId)
    {
        return adMapper.selectAdByAdId(adId);
    }

    /**
     * 查询广告列表
     *
     * @param ad 广告
     * @return 广告
     */
    @Override
    public List<AdVO> selectAdList(AdQuery ad)
    {

        return adMapper.selectAdList(ad);
    }

    /**
     * 新增广告
     *
     * @param ad 广告
     * @return 结果
     */
    @Override
    public int insertAd(Ad ad)
    {
        ad.setCreateTime(DateUtils.getNowDate());
        if (SecurityUtils.isSysAdmin()){
            ad.setBelong(AdBlong.ADMIN.getCode());
        }else {
            ad.setBelong(AdBlong.STORE.getCode());
        }
        return adMapper.insertAd(ad);
    }

    /**
     * 修改广告
     *
     * @param ad 广告
     * @return 结果
     */
    @Override
    public int updateAd(Ad ad)
    {
        ad.setUpdateTime(DateUtils.getNowDate());
        return adMapper.updateAd(ad);
    }

    /**
     * 批量删除广告
     *
     * @param adIds 需要删除的广告主键
     * @return 结果
     */
    @Override
    public int deleteAdByAdIds(List<Long> adIds)
    {
        return adMapper.deleteAdByAdIds(adIds);
    }

    /**
     * 删除广告信息
     *
     * @param adId 广告主键
     * @return 结果
     */
    @Override
    public int deleteAdByAdId(Long adId)
    {
        return adMapper.deleteAdByAdId(adId);
    }

    @Override
    public AdVO selectOne(AdQuery query) {
        return adMapper.selectOne(query);
    }

    @Override
    public List<AdVO> toVOList() {
        // 管理员可以查看所有的广告
        if (SecurityUtils.isAdmin()){
            return adMapper.selectAdList(new AdQuery());
        }
        // 查询对应商户的广告
        List<AdVO> voList = new ArrayList<>();
        UserVO user = userService.selectUserById(SecurityUtils.getUserId());
        if (user != null) {
            if (user.getAreaId() != null) {
                AdVO vo = adMapper.selectOne(adConverter.toAdStoreVO());
                voList.add(vo);
            }
        }
        return voList;
    }

    @Override
    public List<AdVO> toAppVOList() {
        // 查询管理员发布的广告
        List<AdVO> voList = new ArrayList<>();
        List<AdVO> adminVOList = adMapper.selectAdList(adConverter.toAdAdminVO());

        // 查询对应商户的广告
        UserVO user = userService.selectUserById(SecurityUtils.getUserId());
        if (user != null) {
            if (user.getAreaId() != null) {
                AdVO vo = adMapper.selectOne(adConverter.toAdStoreVO());
                voList.add(vo);
            }
        }
        voList.addAll(adminVOList);
        return voList;
    }
}
