package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.hezhi.domain.TbBanner;
import com.ruoyi.hezhi.domain.dto.BannerDTO;
import com.ruoyi.hezhi.domain.vo.BannerVO;
import com.ruoyi.hezhi.mapper.TbBannerMapper;
import com.ruoyi.hezhi.service.ITbBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 轮播图Service业务层处理
 *
 * @author CC
 * @date 2024-03-12
 */
@Service
public class TbBannerServiceImpl implements ITbBannerService
{
    @Autowired
    private TbBannerMapper tbBannerMapper;

    /**
     * 查询轮播图
     *
     * @param bannerId 轮播图主键
     * @return 轮播图
     */
    @Override
    public TbBanner selectTbBannerByBannerId(String bannerId)
    {
        return tbBannerMapper.selectTbBannerByBannerId(bannerId);
    }

    /**
     * 查询轮播图列表
     *
     * @param tbBanner 轮播图
     * @return 轮播图
     */
    @Override
    public List<TbBanner> selectTbBannerList(TbBanner tbBanner)
    {
        return tbBannerMapper.selectTbBannerList(tbBanner);
    }

    /**
     * 新增轮播图
     *
     * @param tbBanner 轮播图
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int insertTbBanner(TbBanner tbBanner)
    {
        // checkSort(tbBanner.getBannerSort(), null,tbBanner.getType());
        tbBanner.setBannerId(IdUtils.fastSimpleUUID());
        tbBanner.setCreateBy(SecurityUtils.getUsername());
        tbBanner.setCreateTime(DateUtils.getNowDate());
        return tbBannerMapper.insertTbBanner(tbBanner);
    }

    /**
     * 校验排序
     * @param sort 排序
     * @param id 编号
     */
    public void checkSort(Long sort, String id, String type){
        int i = tbBannerMapper.checkSort(sort, id, type);
        if (i>0){
            throw new ServiceException("排序序号重复");
        }
    }

    /**
     * 修改轮播图
     *
     * @param tbBanner 轮播图
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateTbBanner(TbBanner tbBanner)
    {
        // checkSort(tbBanner.getBannerSort(), tbBanner.getBannerId(),tbBanner.getType());
        tbBanner.setUpdateTime(DateUtils.getNowDate());
        tbBanner.setUpdateBy(SecurityUtils.getUsername());
        return tbBannerMapper.updateTbBanner(tbBanner);
    }

    /**
     * 批量删除轮播图
     *
     * @param bannerIds 需要删除的轮播图主键
     * @return 结果
     */
    @Override
    public int deleteTbBannerByBannerIds(String[] bannerIds)
    {
        return tbBannerMapper.deleteTbBannerByBannerIds(bannerIds,SecurityUtils.getUsername());
    }

    /**
     * 删除轮播图信息
     *
     * @param bannerId 轮播图主键
     * @return 结果
     */
    @Override
    public int deleteTbBannerByBannerId(String bannerId)
    {
        return tbBannerMapper.deleteTbBannerByBannerId(bannerId,SecurityUtils.getUsername());
    }

    /**
     * xcx通用——查询轮播图列表
     *
     * @param banner 轮播图参数
     * @return 轮播图集合
     */
    @Override
    public List<BannerVO> selectBannerListForXcxCommon(BannerDTO banner) {
        return tbBannerMapper.selectBannerListForXcxCommon(banner);
    }
}
