package com.fdkankan.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.CommonStatus;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.response.Result;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.manage.entity.News;
import com.fdkankan.manage.mapper.INewsMapper;
import com.fdkankan.manage.service.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage.vo.BaseParamVO;
import com.fdkankan.manage.vo.NewsDisplayParamVO;
import com.fdkankan.manage.vo.NewsPublicParamVO;
import com.fdkankan.manage.vo.NewsPutTopParamVO;
import com.fdkankan.manage.vo.PageNewsParamVO;
import com.fdkankan.manage.vo.PageNewsVO;
import com.fdkankan.manage.vo.SaveNewsParamVO;
import com.fdkankan.manage.vo.UpdateNewsParamVO;
import java.util.Calendar;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-06-06
 */
@Service
public class NewsServiceImpl extends ServiceImpl<INewsMapper, News> implements INewsService {

    @Override
    public Page<PageNewsVO> pageNews(PageNewsParamVO param) {
        Page<PageNewsVO> pageNewsVOPage =
            this.baseMapper.pageNews(new Page<>(param.getPageNum(), param.getPageSize()), param);
        return pageNewsVOPage;
    }

    @Override
    public ResultData display(Long userId, NewsDisplayParamVO param) {
        this.update(new LambdaUpdateWrapper<News>()
            .set(News::getDisplay, param.getDisplay())
            .set(News::getUpdateTime, Calendar.getInstance().getTime())
            .set(News::getUpdaterId, userId)
            .eq(News::getId, param.getId()));
        return ResultData.ok();
    }

    @Override
    public ResultData putTop(Long userId, NewsPutTopParamVO param) {

        LambdaUpdateWrapper<News> updateWrapper = new LambdaUpdateWrapper<News>()
            .set(News::getIsTop, param.getIsTop())
            .set(News::getUpdateTime, Calendar.getInstance().getTime())
            .set(News::getUpdaterId, userId)
            .eq(News::getId, param.getId());
        if(CommonStatus.YES.code() == param.getIsTop().byteValue()){
            updateWrapper.set(News::getTopTime, Calendar.getInstance().getTime());
        }
        this.update(updateWrapper);

        return ResultData.ok();
    }

    @Override
    public ResultData publicNews(Long userId, NewsPublicParamVO param) {
        LambdaUpdateWrapper<News> updateWrapper = new LambdaUpdateWrapper<News>()
            .set(News::getIsPublic, param.getIsPublic())
            .set(News::getUpdateTime, Calendar.getInstance().getTime())
            .set(News::getUpdaterId, userId)
            .eq(News::getId, param.getId());
        if(CommonStatus.YES.code() == param.getIsPublic().byteValue()){
            updateWrapper.set(News::getPublicTime, Calendar.getInstance().getTime());
        }
        this.update(updateWrapper);

        return ResultData.ok();
    }

    @Override
    public ResultData delete(Long userId, BaseParamVO param) {
        this.update(new LambdaUpdateWrapper<News>()
            .set(News::getTbStatus, TbStatus.DELETE.code())
            .set(News::getUpdateTime, Calendar.getInstance().getTime())
            .set(News::getUpdaterId, userId)
            .eq(News::getId, param.getId()));
        return ResultData.ok();
    }

    @Override
    public News getNewsDetail(BaseParamVO param) {
        return this.getById(param.getId());
    }

    @Override
    public ResultData addNews(Long userId, SaveNewsParamVO param) {
        News news = BeanUtil.copyProperties(param, News.class);
        news.setCreaterId(userId);
        news.setPublicTime(Calendar.getInstance().getTime());
        this.save(news);
        return ResultData.ok();
    }

    @Override
    public ResultData updateNews(Long userId, UpdateNewsParamVO param) {
        News news = BeanUtil.copyProperties(param, News.class);
        news.setUpdateTime(Calendar.getInstance().getTime());
        news.setUpdaterId(userId);
        this.updateById(news);
        return ResultData.ok();
    }
}
