package com.fkzy.warn.service.impl;

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.fkzy.warn.common.constants.EntityConstants;
import com.fkzy.warn.common.request.RequestParameter;
import com.fkzy.warn.common.response.ApiResponse;
import com.fkzy.warn.mapper.NewsManageMapper;
import com.fkzy.warn.model.NewsManage;
import com.fkzy.warn.service.NewsManageService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangjing
 * @date 2023/10/16 18:17
 * @description
 */
@Service
public class NewsManageServiceImpl extends ServiceImpl<NewsManageMapper, NewsManage> implements
    NewsManageService {


    @Override
    public ApiResponse saveModel(NewsManage entity, String ticket) {
        save(entity);
        return new ApiResponse();
    }

    @Override
    public ApiResponse updateModel(NewsManage entity,String ticket) {
        this.updateById(entity);
        return new ApiResponse();
    }

    @Override
    public ApiResponse deleteModel(List<String> ids) {
        NewsManage entity = new NewsManage();
        entity.setIsDel(EntityConstants.DEL);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("id", ids);
        update(entity, wrapper);
        return new ApiResponse();
    }

    @Override
    public Page<NewsManage> queryPage(RequestParameter parameter) {
        NewsManage entity = parameter.getParameter().toJavaObject(NewsManage.class);
        Page<NewsManage> page = new Page<NewsManage>(parameter.getCurrent(), parameter.getSize());
        page.setSearchCount(true);
        page.setOptimizeCountSql(true);
        QueryWrapper<NewsManage> eWrapper = new QueryWrapper<NewsManage>(entity);
        Page<NewsManage> result = this.page(page, eWrapper);
        eWrapper.eq("is_del", EntityConstants.NORMAL)
            .orderByDesc("status")
            .orderByDesc("create_time");
        return result;
    }

    @Override
    public ApiResponse publish(NewsManage entity, String ticket) {
        ApiResponse response = new ApiResponse();
        updateById(entity);
        return response;
    }
}
