package com.ruoyi.web.service.impl;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.domain.dto.cloudBroadcasting.CloudBroadcastingQueryRequest;
import com.ruoyi.web.domain.entity.CloudBroadcasting;
import com.ruoyi.web.domain.vo.CloudBroadcastingVO;
import com.ruoyi.web.mapper.CloudBroadcastingMapper;
import com.ruoyi.web.service.CloudBroadcastingService;
import org.springframework.stereotype.Service;

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

/**
 *
 */
@Service
public class CloudBroadcastingServiceImpl extends ServiceImpl<CloudBroadcastingMapper, CloudBroadcasting>
        implements CloudBroadcastingService {
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public CloudBroadcastingVO getCloudBroadcastingById(int id) {
        if (id <= 0 || ObjectUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空或id异常");
        }
        CloudBroadcasting cloudBroadcasting = this.getById(id);
        return getCloudBroadcastingVO(cloudBroadcasting);
    }

    /**
     * 获取云广播包装类
     *
     * @param cloudBroadcasting
     * @return
     */
    @Override
    public CloudBroadcastingVO getCloudBroadcastingVO(CloudBroadcasting cloudBroadcasting) {
        if (ObjectUtil.isEmpty(cloudBroadcasting)) {
            throw new ServiceException("请求参数不存在");
        }
        CloudBroadcastingVO cloudBroadcastingVO = new CloudBroadcastingVO();
        BeanUtils.copyProperties(cloudBroadcasting, cloudBroadcastingVO);
        return cloudBroadcastingVO;
    }

    /**
     * 分页获取云广播列表
     *
     * @param cloudBroadcastingQueryRequest
     * @return
     */
    @Override
    public Page<CloudBroadcastingVO> getListCloudBroadcastingByPage(CloudBroadcastingQueryRequest cloudBroadcastingQueryRequest) {
        long current = cloudBroadcastingQueryRequest.getPageNum();
        long size = cloudBroadcastingQueryRequest.getPageSize();
        Page<CloudBroadcasting> page = this.page(new Page<>(current, size),
                getQueryWrapper(cloudBroadcastingQueryRequest));
        Page<CloudBroadcastingVO> cloudBroadcastingVOPage = new Page<>();
        BeanUtils.copyProperties(page, cloudBroadcastingVOPage, "records");
        List<CloudBroadcastingVO> voList = page.getRecords().stream()
                .map(cloudBroadcasting -> {
                    CloudBroadcastingVO vo = new CloudBroadcastingVO();
                    BeanUtils.copyProperties(cloudBroadcasting, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        cloudBroadcastingVOPage.setRecords(voList);
        return cloudBroadcastingVOPage;
    }

    /**
     * 获取查询条件
     *
     * @param cloudBroadcastingQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<CloudBroadcasting> getQueryWrapper(CloudBroadcastingQueryRequest cloudBroadcastingQueryRequest) {
        QueryWrapper<CloudBroadcasting> queryWrapper = new QueryWrapper<>();
        if (cloudBroadcastingQueryRequest == null) {
            return queryWrapper;
        }

        // 从对象中取值
        Integer id = cloudBroadcastingQueryRequest.getId();
        Date startTime = cloudBroadcastingQueryRequest.getStartTime();
        Date endTime = cloudBroadcastingQueryRequest.getEndTime();
        String sortField = cloudBroadcastingQueryRequest.getSortField();
        String sortOrder = cloudBroadcastingQueryRequest.getSortOrder();


        queryWrapper.eq(ObjectUtil.isNotEmpty(id), "id", id);

        // 日期范围查询（优先判断范围）
        if (startTime != null && endTime != null) {
            queryWrapper.between("date", startTime, endTime);
        } else if (startTime != null) {
            queryWrapper.ge("date", startTime); // >= 开始日期
        } else if (endTime != null) {
            queryWrapper.le("date", endTime);   // <= 结束日期
        }

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




