package com.yx.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.yx.exam.core.api.dto.PagingReqDTO;
import com.yx.exam.core.utils.StringUtils;
import com.yx.exam.modules.gonggao.entity.TGonggao;
import com.yx.exam.modules.gonggao.mapper.TGonggaoMapper;
import com.yx.exam.modules.gonggao.service.TGonggaoService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author chenhaodong
 * @since 2023-03-03
 */
@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;
    }
}
