package com.bc.exam.modules.gonggao.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bc.exam.core.api.dto.PagingReqDTO;
import com.bc.exam.core.utils.StringUtils;
import com.bc.exam.modules.gonggao.entity.TGonggao;
import com.bc.exam.modules.gonggao.mapper.TGonggaoMapper;
import com.bc.exam.modules.gonggao.service.TGonggaoService;
import org.springframework.stereotype.Service;

/**
 * @Description 描述:公告表 服务实现类
 * @Author A贾宇婷034244310
 * @Date 20260615
 */
@Service
public class TGonggaoServiceImpl extends ServiceImpl<TGonggaoMapper, TGonggao> implements TGonggaoService {

    @Override
    public IPage<TGonggao> paging(PagingReqDTO<TGonggao> reqDTO) {
        //创建分页对象
        IPage<TGonggao> query = new Page<>(reqDTO.getCurrent(), reqDTO.getSize());

        //查询条件
        QueryWrapper<TGonggao> wrapper = new QueryWrapper<>();
        TGonggao params = reqDTO.getParams();
        if(!StringUtils.isBlank(params.getId())){
            wrapper.like(!StringUtils.isBlank(params.getId()),"id", params.getId());
        }
        if(!StringUtils.isBlank(params.getTitle())){
            wrapper.like(!StringUtils.isBlank(params.getTitle()),"title", params.getTitle());
        }
        if(!StringUtils.isBlank(params.getText())){
            wrapper.like(!StringUtils.isBlank(params.getText()),"text", params.getText());
        }
        if(null!=params.getCreateTime()){
            wrapper.apply("create_time >= {0} ",params.getCreateTime());
        }
        if(null!=params.getUpdateTime()){
            wrapper.apply("create_time <= {0} ",params.getUpdateTime());
        }

        //获得数据
        IPage<TGonggao> page = this.page(query, wrapper);
        //转换结果
        IPage<TGonggao> pageData = JSON.parseObject(JSON.toJSONString(page), new TypeReference<Page<TGonggao>>(){});

        return pageData;
    }
}
