package com.ruoyi.web.controller.chenyanlogin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.controller.chenyanlogin.entity.NationUnity;
import com.ruoyi.web.controller.chenyanlogin.mapper.NationUnityMapper;
import com.ruoyi.web.controller.chenyanlogin.service.INationUnityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.domain.dto.mediaPropaganda.MediaPropagandaAddRequest;
import com.ruoyi.web.domain.dto.mediaPropaganda.MediaPropagandaEditRequest;
import com.ruoyi.web.domain.dto.mediaPropaganda.MediaPropagandaQueryRequest;
import com.ruoyi.web.domain.entity.MediaPropaganda;
import com.ruoyi.web.domain.vo.MediaPropagandaVO;
import org.bouncycastle.math.raw.Nat;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 媒体宣传内容收集表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-23
 */
@Service
public class NationUnityServiceImpl extends ServiceImpl<NationUnityMapper, NationUnity> implements INationUnityService {

    @Override
    public Integer addMediaPropaganda(MediaPropagandaAddRequest mediaPropagandaAddRequest) {
        return null;
    }

    @Override
    public boolean deleteMediaPropaganda(String ids) {
        if (StrUtil.isBlank(ids)) {
            throw new ServiceException("ID不能为空或ID异常");
        }

        try {
            // 分割ID字符串为列表
            List<Long> idList = Arrays.stream(ids.split(","))
                    .map(String::trim)
                    .filter(StrUtil::isNotBlank)
                    .map(Long::parseLong)
                    .collect(Collectors.toList());

            if (idList.isEmpty()) {
                throw new ServiceException("ID格式异常");
            }

            // 构建删除条件
            QueryWrapper<NationUnity> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", idList);

            // 执行删除（返回是否删除成功）
            return remove(queryWrapper);
        } catch (NumberFormatException e) {
            log.error("删除媒体宣传内容失败，ID格式错误", e);
            throw new ServiceException("ID格式错误");
        } catch (Exception e) {
            log.error("删除媒体宣传内容失败", e);
            throw new ServiceException("删除媒体宣传内容失败: " + e.getMessage());
        }
    }

    @Override
    public void editMediaPropaganda(MediaPropagandaEditRequest mediaPropagandaEditRequest) {

    }

    @Override
    public MediaPropagandaVO getMediaPropagandaById(int id) {
        return null;
    }

    @Override
    public Page<MediaPropagandaVO> getListMediaPropagandaByPage(MediaPropagandaQueryRequest mediaPropagandaQueryRequest) {
        if (mediaPropagandaQueryRequest == null) {
            throw new ServiceException("查询参数为空");
        }

        try {
            // 1. 构建查询条件
            QueryWrapper<NationUnity> queryWrapper = getQueryWrapper(mediaPropagandaQueryRequest);

            // 2. 执行分页查询
            Page<NationUnity> page = new Page<>(mediaPropagandaQueryRequest.getPageNum(), mediaPropagandaQueryRequest.getPageSize());
            Page<NationUnity> mediaPropagandaPage = this.page(page, queryWrapper);

            // 3. 转换为VO
            Page<MediaPropagandaVO> voPage = new Page<>();
            BeanUtil.copyProperties(mediaPropagandaPage, voPage);

            List<MediaPropagandaVO> voList = mediaPropagandaPage.getRecords().stream()
                    .map(this::getMediaPropagandaVO)
                    .collect(Collectors.toList());
            voPage.setRecords(voList);

            return voPage;
        } catch (Exception e) {
            log.error("分页查询媒体宣传内容失败", e);
            throw new ServiceException("分页查询媒体宣传内容失败: " + e.getMessage());
        }
    }
    @Override
    public MediaPropagandaVO getMediaPropagandaVO(NationUnity mediaPropaganda) {
        if (mediaPropaganda == null) {
            return null;
        }

        try {
            MediaPropagandaVO mediaPropagandaVO = new MediaPropagandaVO();
            BeanUtil.copyProperties(mediaPropaganda, mediaPropagandaVO);
            return mediaPropagandaVO;
        } catch (Exception e) {
            log.error("转换媒体宣传内容VO失败", e);
            throw new ServiceException("转换媒体宣传内容VO失败: " + e.getMessage());
        }
    }
    public QueryWrapper<NationUnity> getQueryWrapper(MediaPropagandaQueryRequest mediaPropagandaQueryRequest) {
        QueryWrapper<NationUnity> queryWrapper = new QueryWrapper<>();
        Date extractStartTime = mediaPropagandaQueryRequest.getExtractStartTime();
        Date extractEndTime = mediaPropagandaQueryRequest.getExtractEndTime();
        Date publishStartTime = mediaPropagandaQueryRequest.getPublishStartTime();
        Date publishEndTime = mediaPropagandaQueryRequest.getPublishEndTime();
        String keyword = mediaPropagandaQueryRequest.getKeyword();
        String sortField = mediaPropagandaQueryRequest.getSortField();
        String sortOrder = mediaPropagandaQueryRequest.getSortOrder();

        // 关键词搜索（标题或内容）
        if (StrUtil.isNotBlank(mediaPropagandaQueryRequest.getKeyword())) {
            queryWrapper.and(wrapper -> wrapper
                    .like("title", keyword)
                    .or()
                    .like("content", keyword)
            );
        }

        // 摘录日期范围查询
        if (ObjectUtil.isNotNull(mediaPropagandaQueryRequest.getExtractStartTime())) {
            queryWrapper.ge("extract_date", extractStartTime);
        }
        if (ObjectUtil.isNotNull(mediaPropagandaQueryRequest.getExtractEndTime())) {
            queryWrapper.le("extract_date", extractEndTime);
        }

        // 发布日期范围查询
        if (ObjectUtil.isNotNull(mediaPropagandaQueryRequest.getPublishStartTime())) {
            queryWrapper.ge("publish_date", publishStartTime);
        }
        if (ObjectUtil.isNotNull(mediaPropagandaQueryRequest.getPublishEndTime())) {
            queryWrapper.le("publish_date", publishEndTime);
        }

        // 排序
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), "ascend".equals(sortOrder), sortField);


        return queryWrapper;
    }
}
