package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.dto.WebNewsDto;
import com.hc.gpbusiness.mapper.WebNewsMapper;
import com.hc.gpbusiness.mapper.WebNewsTypeMapper;
import com.hc.gpbusiness.model.WebNews;
import com.hc.gpbusiness.model.WebNewsType;
import com.hc.gpbusiness.service.WebNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 新闻表
 * (WebNews)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:10
 */

@Service("newsService")
public class WebNewsServiceImpl implements WebNewsService {

    private final WebNewsMapper newsMapper;

    private final WebNewsTypeMapper newsTypeMapper;

    public WebNewsServiceImpl(WebNewsMapper newsMapper,WebNewsTypeMapper newsTypeMapper) {
        this.newsMapper = newsMapper;
        this.newsTypeMapper = newsTypeMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param newsId 主键
     * @return 实例对象
     */
    @Override
    public WebNews queryById(Integer newsId) {
        return this.newsMapper.selectByPrimaryKey(newsId);
    }

    @Override
    public WebNewsDto queryModelById(Integer newsId) {
        return copy(this.newsMapper.selectByPrimaryKey(newsId));
    }

    /**
     * 新增数据
     *
     * @param news 实例对象
     * @return 实例对象
     */
    @Override
    public WebNews insert(WebNews news) {
        news.setCreateTime(System.currentTimeMillis());
        this.newsMapper.insert(news);
        return news;
    }

    /**
     * 修改数据
     *
     * @param news 实例对象
     * @return 实例对象
     */
    @Override
    public WebNews update(WebNews news) {
        news.setUpdateTime(System.currentTimeMillis());
        this.newsMapper.updateByPrimaryKeySelective(news);
        return this.queryById(news.getNewsId());
    }

    /**
     * 通过主键删除数据
     *
     * @param newsId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer newsId) {
        return this.newsMapper.deleteByPrimaryKey(newsId) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param page     页码
     * @param size     查询条数
     * @param moduleId 板块id
     * @return 对象列表
     */
//    @Override
//    public PageInfo queryAllByLimit(int page, int size, Integer moduleId) {
//        PageHelper.startPage(page, size);
//        List<WebNews> news = this.newsMapper.queryNewsByModuleId(moduleId);
//        List<WebNewsDto> resultList = copyList(news);
//        return new PageInfo(resultList);
//    }
//
//    public PageInfo queryAllByLimit1(int page, int size, Integer moduleId) {
//
//        List<WebNews> news = this.newsMapper.queryNewsByModuleId(moduleId);
//        List<WebNewsDto> resultList = copyList(news);
//        PageHelper.startPage(page, size).doSelectPage(resultList);
//        PageInfo info = new PageInfo(resultList);
//        return info;
//    }

    @Override
    public PageInfo queryAllByLimit(int page, int size, Integer moduleId) {
        PageInfo info = PageHelper.startPage(page, size).setOrderBy("flag desc,create_time desc").doSelectPageInfo(() -> this.newsMapper.queryNewsByModuleId(moduleId));
        info.setList(copyList(info.getList()));
        return info;
    }

    public List<WebNewsDto> copyList(List<WebNews> records) {
        List<WebNewsDto> WebNewsDtoList = new ArrayList<>();
        for (WebNews record : records) {
            WebNewsDtoList.add(copy(record));
        }
        return WebNewsDtoList;
    }

    public WebNewsDto copy(WebNews entity) {
        WebNewsDto newsDto = new WebNewsDto();
        BeanUtils.copyProperties(entity, newsDto);

        WebNewsType type = newsTypeMapper.selectByPrimaryKey(entity.getTypeId());
        if (type != null) {
            newsDto.setTypeName(type.getTypeName());
        }

        return newsDto;
    }
}