package com.xiaoniu.system.platformsettings.advertis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoniu.common.core.constant.Constants;
import com.xiaoniu.common.core.exception.ServiceException;
import com.xiaoniu.common.core.resdata.RowsData;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.utils.bean.BeanUtils;
import com.xiaoniu.common.security.utils.SecurityUtils;
import com.xiaoniu.system.platformsettings.advertis.domain.TbAdvertisInfo;
import com.xiaoniu.system.platformsettings.advertis.dto.AddAdvertisDTO;
import com.xiaoniu.system.platformsettings.advertis.dto.AdvertisListQueryDTO;
import com.xiaoniu.system.platformsettings.advertis.dto.EditAdvertisDTO;
import com.xiaoniu.system.platformsettings.advertis.mapper.TbAdvertisInfoMapper;
import com.xiaoniu.system.platformsettings.advertis.service.ITbAdvertisInfoService;
import com.xiaoniu.system.platformsettings.advertis.vo.AdvertisDetailsVO;
import com.xiaoniu.system.platformsettings.advertis.vo.AdvertisListVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 广告信息表 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-10 01:59:09
 */
@Service
public class TbAdvertisInfoServiceImpl extends ServiceImpl<TbAdvertisInfoMapper, TbAdvertisInfo> implements ITbAdvertisInfoService {

    @Override
    public RowsData<AdvertisListVO> advertisList(AdvertisListQueryDTO dto) {
        try (Page<Object> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize())) {
            LambdaQueryWrapper<TbAdvertisInfo> wrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotEmpty(dto.getAdvertisAddress())) {
                wrapper.eq(TbAdvertisInfo::getAdvertisAddress, dto.getAdvertisAddress());
            }
            if (StringUtils.isNotNull(dto.getStatus()) && !Constants.ZERO.equals(dto.getStatus())) {
                wrapper.eq(TbAdvertisInfo::getStatus, dto.getStatus());
            }
            if (StringUtils.isNotEmpty(dto.getBeginTime())) {
                wrapper.gt(TbAdvertisInfo::getCreateTime, dto.getBeginTime() + " 00:00:01");
            }
            if (StringUtils.isNotEmpty(dto.getEndTime())) {
                wrapper.lt(TbAdvertisInfo::getCreateTime, dto.getEndTime() + " 23:59:59");
            }
            wrapper.eq(TbAdvertisInfo::getDelFlag, Constants.ZERO);
            wrapper.orderByAsc(TbAdvertisInfo::getSort);
            wrapper.orderByDesc(TbAdvertisInfo::getCreateTime);
            List<AdvertisListVO> list = baseMapper.selectVoList(wrapper, AdvertisListVO.class);
            return new RowsData<>(list, page.getTotal(), dto.getPageNum());
        }
    }

    @Override
    public void addAdvertis(AddAdvertisDTO dto) {
        TbAdvertisInfo tbAdvertisInfo = new TbAdvertisInfo();
        BeanUtils.copyProperties(dto, tbAdvertisInfo);
        try {
            tbAdvertisInfo.setCreateBy(SecurityUtils.getUserId());
        } catch (Exception e) {
            log.error("获取当前登陆人id失败");
        }
        tbAdvertisInfo.setCreateTime(new Date());
        baseMapper.insert(tbAdvertisInfo);
    }

    @Override
    public void editAdvertis(EditAdvertisDTO dto) {
        TbAdvertisInfo tbAdvertisInfo = baseMapper.selectById(dto.getId());
        if (StringUtils.isNull(tbAdvertisInfo)) {
            throw new ServiceException("当前广告位不存在");
        }
        if (Constants.TWO.equals(tbAdvertisInfo.getDelFlag())) {
            throw new ServiceException("当前广告位已删除");
        }
        BeanUtils.copyProperties(dto, tbAdvertisInfo);
        try {
            tbAdvertisInfo.setUpdateBy(SecurityUtils.getUserId());
        } catch (Exception e) {
            log.error("获取当前登陆人id失败");
        }
        tbAdvertisInfo.setUpdateTime(new Date());
        baseMapper.updateById(tbAdvertisInfo);
    }

    @Override
    public AdvertisDetailsVO details(Long id) {
        if (StringUtils.isNull(id)) {
            throw new ServiceException("id不能为空");
        }
        return baseMapper.selectVoById(id, AdvertisDetailsVO.class);
    }

    @Override
    public void delAdvertis(Long id) {
        if (StringUtils.isNull(id)) {
            throw new ServiceException("id不能为空");
        }
        TbAdvertisInfo tbAdvertisInfo = baseMapper.selectById(id);
        if (StringUtils.isNull(tbAdvertisInfo)) {
            throw new ServiceException("当前广告位不存在");
        }
        tbAdvertisInfo.setDelFlag(Constants.TWO);
        try {
            tbAdvertisInfo.setUpdateBy(SecurityUtils.getUserId());
        } catch (Exception e) {
            log.error("获取当前登陆人id失败");
        }
        tbAdvertisInfo.setUpdateTime(new Date());
        baseMapper.updateById(tbAdvertisInfo);
    }
}
