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

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.bst.ad.domain.AdVerifyDTO;
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.bst.ad.service.AdConverter;
import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.device.domain.enums.DeviceStatus;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
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) {
        // 默认设置为商户广告
        if (ad.getBelong() == null || ad.getBelong().equals("")) {
            ad.setBelong(AdBelong.STORE.getCode());
        }
        // 商户广告存在上限
        if (ad.getBelong().equals(AdBelong.STORE.getCode())) {
            AreaVO area = areaService.selectAreaById(ad.getAreaId());
            ad.setStoreId(area.getUserId());

            // 条件查询广告
            AdQuery query = new AdQuery();
            query.setAreaId(ad.getAreaId());
            query.setDeleted(false);
            List<AdVO> adVOList = adMapper.selectAdList(query);
            ServiceUtil.assertion(adVOList.size() > 1, "当前运营区存在广告已达上限");
        }
        ad.setCreateTime(DateUtils.getNowDate());
        return adMapper.insertAd(ad);
    }

    /**
     * 修改广告
     *
     * @param ad 广告
     * @return 结果
     */
    @Override
    public int updateAd(Ad ad) {
        if (ad.getAreaId() != null) {
            AdVO oldAd = adMapper.selectAdByAdId(ad.getAdId());
            ServiceUtil.assertion(oldAd == null, "广告不存在");
            if (ad.getAreaId() != null) {
                int count = adMapper.countByAreaId(ad.getAreaId(), ad.getAdId());
                ServiceUtil.assertion(count > 1, "该运营区下广告已达上限");
            }
        }
        AreaVO area = areaService.selectAreaById(ad.getAreaId());
        if (area != null && area.getUserId() != null) {
            ad.setStoreId(area.getUserId());
        }
        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 int logicalDel(List<Long> adIds) {
        return adMapper.logicalDel(adIds);
    }

    @Override
    public int verify(AdVerifyDTO dto) {
        // 查询广告
        AdVO ad = this.selectAdByAdId(dto.getId());
        ServiceUtil.assertion(ad == null, "当前广告信息不存在", dto.getId());
        ServiceUtil.assertion(!AdVerifyStatus.canVerify().contains(ad.getAuditStatus()), "ID为%s的广告当前不允许审核", dto.getId());
        boolean pass = dto.getPass() != null && dto.getPass();
        // 更新广告状态
        Ad data = new Ad();
        data.setAuditStatus(pass ? AdVerifyStatus.PASSED.getCode() : AdVerifyStatus.REJECTED.getCode());
        data.setVerifyBy(SecurityUtils.getUserId());
        data.setVerifyEndTime(LocalDateTime.now());
        data.setVerifyRemark(dto.getVerifyRemark());
        AdQuery query = new AdQuery();
        query.setAdId(dto.getId());
        query.setStatusList(AdVerifyStatus.canVerify());
        int rows = adMapper.updateByQuery(data, query);
        ServiceUtil.assertion(rows != 1, "ID为%s的广告审核失败", dto.getId());
        return rows;
    }

    @Override
    public int enable(Long adId) {
        AdVO ad = this.selectAdByAdId(adId);
        ServiceUtil.assertion(ad == null, "当前广告信息不存在", adId);
        ServiceUtil.assertion(!AdStatus.canEnable().contains(ad.getStatus()), "广告%s当前状态不允许启用", adId);
        // 更新广告状态
        return 1;
    }

    @Override
    public int disable(Long adId) {
        AdVO ad = this.selectAdByAdId(adId);
        ServiceUtil.assertion(ad == null, "当前广告信息不存在", adId);
        ServiceUtil.assertion(!AdStatus.canDisable().contains(ad.getStatus()), "广告%s当前状态不允许启用", adId);
        // 更新广告状态
        return 1;
    }

}
