package com.ruoyi.clock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.domain.Shop;
import com.ruoyi.clock.exception.AdExceptionEnum;
import com.ruoyi.clock.exception.ShopExceptionEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.clock.domain.bo.AdBo;
import com.ruoyi.clock.domain.vo.AdVo;
import com.ruoyi.clock.domain.Ad;
import com.ruoyi.clock.mapper.AdMapper;
import com.ruoyi.clock.service.IAdService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 广告Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class AdServiceImpl implements IAdService {

    private final AdMapper baseMapper;

    /**
     * 查询广告分页
     *
     * @param bo 广告
     * @return 广告
     */
    @Override
    public TableDataInfo<AdVo> queryPageList(AdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ad> lqw = buildQueryWrapper(bo);
        Page<AdVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询广告列表
     *
     * @param bo 广告
     * @return 广告
     */
    @Override
    public List<AdVo> queryList(AdBo bo) {
        LambdaQueryWrapper<Ad> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ad> buildQueryWrapper(AdBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ad> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), Ad::getImage, bo.getImage());
        lqw.like(StringUtils.isNotBlank(bo.getOutsideAddress()), Ad::getOutsideAddress, bo.getOutsideAddress());
        lqw.eq(bo.getOrderIndex() != null, Ad::getOrderIndex, bo.getOrderIndex());
        lqw.eq(bo.getStatus() != null, Ad::getStatus, bo.getStatus());
        lqw.orderByAsc(Ad::getOrderIndex);
        return lqw;
    }

    /**
     * 查询广告
     *
     * @param adId 广告主键
     * @return 广告
     */
    @Override
    public AdVo queryById(Long adId){
        return baseMapper.selectVoById(adId);
    }

    @Override
    public AdVo queryByLocationCode(String locationCode) {
        LambdaQueryWrapper<Ad> lqw = Wrappers.lambdaQuery();
        lqw.eq(Ad::getLocationCode, locationCode);
        lqw.eq(Ad::getStatus, 0);
        lqw.last(" limit 1");
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 详情广告
     *
     * @param adId 广告主键
     * @return 广告
     */
    @Override
    public Ad loadById(Long adId, Boolean tw){
        Ad info = this.baseMapper.selectById(adId);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(AdExceptionEnum.AD_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增广告
     *
     * @param bo 广告
     * @return 结果
     */
    @Override
    public Boolean insertByBo(AdBo bo) {
        validEntityBeforeSave(bo);
        Ad add = BeanUtil.toBean(bo, Ad.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAdId(add.getAdId());
        }
        return flag;
    }

    /**
     * 修改广告
     *
     * @param bo 广告
     * @return 结果
     */
    @Override
    public Boolean updateByBo(AdBo bo) {
        validEntityBeforeSave(bo);
        Ad ad = baseMapper.selectById(bo.getAdId());
        Ad update = BeanCopyUtils.copy(bo, ad);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param bo 实体类数据
     */
    private void validEntityBeforeSave(AdBo bo){
        //TODO 做一些数据校验,如唯一约束
        Ad ad = getAdByLocationCode(bo.getLocationCode());
        if(ObjectUtil.isNotNull(ad)&& !ad.getAdId().equals(bo.getAdId())){
            throw new ServiceException(AdExceptionEnum.AD_LOCATION_CODE_REPEAT);
        }
    }

    public Ad getAdByLocationCode(String locationCode) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Ad>().eq(Ad::getLocationCode,locationCode)
            .last("limit 1"));
    }

    /**
     * 批量删除广告
     *
     * @param ids 需要删除的广告主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
