package com.xiaoniu.system.platformsettings.notice.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.system.util.MarkdownUtils;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.utils.bean.BeanUtils;
import com.xiaoniu.common.core.utils.html.HtmlUtils;
import com.xiaoniu.common.security.utils.SecurityUtils;
import com.xiaoniu.system.platformsettings.notice.domain.TbNotice;
import com.xiaoniu.system.platformsettings.notice.dto.AddNoticeDTO;
import com.xiaoniu.system.platformsettings.notice.dto.EditNoticeDTO;
import com.xiaoniu.system.platformsettings.notice.dto.NoticeListQueryDTO;
import com.xiaoniu.system.platformsettings.notice.mapper.TbNoticeMapper;
import com.xiaoniu.system.platformsettings.notice.service.ITbNoticeService;
import com.xiaoniu.system.platformsettings.notice.vo.NoticeDetailsVO;
import com.xiaoniu.system.platformsettings.notice.vo.NoticeListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 活动公告 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-11 11:30:35
 */
@Service
public class TbNoticeServiceImpl extends ServiceImpl<TbNoticeMapper, TbNotice> implements ITbNoticeService {

    @Override
    public RowsData<NoticeListVO> noticeList(NoticeListQueryDTO dto) {
        try (Page<Object> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize())) {
            LambdaQueryWrapper<TbNotice> wrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotEmpty(dto.getType())) {
                wrapper.eq(TbNotice::getType, dto.getType());
            }
            if (StringUtils.isNotEmpty(dto.getNoticeTitle())) {
                wrapper.like(TbNotice::getNoticeTitle, dto.getNoticeTitle());
            }
            if (StringUtils.isNotEmpty(dto.getBeginTime())) {
                wrapper.gt(TbNotice::getCreateTime, dto.getBeginTime() + " 00:00:01");
            }
            if (StringUtils.isNotEmpty(dto.getEndTime())) {
                wrapper.lt(TbNotice::getCreateTime, dto.getEndTime() + " 23:59:59");
            }
            if (StringUtils.isNotNull(dto.getStatus()) && !dto.getStatus().equals(Constants.ZERO)) {
                wrapper.eq(TbNotice::getStatus, dto.getStatus());
            }
            wrapper.eq(TbNotice::getDelFlag, Constants.ZERO);
            wrapper.orderByDesc(TbNotice::getCreateTime);
            List<NoticeListVO> list = baseMapper.selectVoList(wrapper, NoticeListVO.class);
            if (!CollectionUtils.isEmpty(list)) {
                for (NoticeListVO noticeListVO : list) {
                    String noticeContent = noticeListVO.getNoticeContent();
                    if (StringUtils.isNotEmpty(noticeContent)) {
                        String text = HtmlUtils.HtmlToText(MarkdownUtils.markdownToHtmlExtensions(noticeContent));
                        noticeListVO.setNoticeContent(text);
                    }
                }

            }
            return new RowsData<>(list, page.getTotal(), dto.getPageNum());
        }
    }

    @Override
    public void addNotice(AddNoticeDTO dto) {
        TbNotice tbNotice = new TbNotice();
        BeanUtils.copyProperties(dto, tbNotice);
        tbNotice.setNoticeType(Constants.ONE);
        try {
            tbNotice.setCreateBy(SecurityUtils.getUsername());
        } catch (Exception e) {
            log.error("获取当前登陆用户失败{}" + e.getMessage());
        }
        tbNotice.setCreateTime(new Date());
        if (!CollectionUtils.isEmpty(dto.getTargetUsers())) {
            tbNotice.setTargetUsers(String.join(",", dto.getTargetUsers()));
        }
        baseMapper.insert(tbNotice);
    }

    @Override
    public void editNotice(EditNoticeDTO dto) {
        TbNotice tbNotice = baseMapper.selectById(dto.getId());
        if (StringUtils.isNull(tbNotice)) {
            throw new ServiceException("当前公告不存在");
        }
        BeanUtils.copyProperties(dto, tbNotice);
        try {
            tbNotice.setUpdateBy(SecurityUtils.getUsername());
        } catch (Exception e) {
            log.error("获取当前登陆用户失败{}" + e.getMessage());
        }
        if (!CollectionUtils.isEmpty(dto.getTargetUsers())) {
            tbNotice.setTargetUsers(String.join(",", dto.getTargetUsers()));
        }
        tbNotice.setUpdateTime(new Date());
        baseMapper.updateById(tbNotice);
    }

    @Override
    public NoticeDetailsVO details(Long id) {
        TbNotice tbNotice = baseMapper.selectById(id);
        if (StringUtils.isNull(tbNotice)) {
            throw new ServiceException("当前公告不存在");
        }
        if (Constants.TWO.equals(tbNotice.getDelFlag())) {
            throw new ServiceException("当前公告已删除");
        }
        NoticeDetailsVO detailsVO = new NoticeDetailsVO();
        BeanUtils.copyProperties(tbNotice, detailsVO);
        return detailsVO;
    }

    @Override
    public void delNotice(Long id) {
        TbNotice tbNotice = baseMapper.selectById(id);
        if (StringUtils.isNull(tbNotice)) {
            throw new ServiceException("当前公告不存在");
        }
        tbNotice.setDelFlag(Constants.TWO);
        try {
            tbNotice.setUpdateBy(SecurityUtils.getUsername());
        } catch (Exception e) {
            log.error("获取当前登陆用户失败{}" + e.getMessage());
        }
        tbNotice.setUpdateTime(new Date());
        baseMapper.updateById(tbNotice);
    }

    @Override
    public void changeState(Long id) {
        TbNotice tbNotice = baseMapper.selectById(id);
        if (StringUtils.isNull(tbNotice)) {
            throw new ServiceException("当前公告不存在");
        }
        if (Constants.ONE.equals(tbNotice.getStatus())) {
            tbNotice.setStatus(Constants.TWO);
        } else {
            tbNotice.setStatus(Constants.ONE);
        }
        baseMapper.updateById(tbNotice);
    }
}
