package com.template.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.api.AlumniNewsControllerAPI;
import com.template.common.utils.TimeExchange;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.*;
import com.template.model.request.insertAlumniNewsRequest;
import com.template.model.request.updateAlumniNewsRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.NewsPageVo;
import com.template.services.AlumniNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-12-02
 */
@RestController
public class AlumniNewsController implements AlumniNewsControllerAPI {

    @Autowired
    private AlumniNewsService alumniNewsService;

    @Override
    public CommonResult insertAlumniNews(insertAlumniNewsRequest ianr, BindingResult bindingResult) {
        if (ianr == null) {
            return CommonResult.fail("参数不能为空");
        }

        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        AlumniNews an = new AlumniNews();
        an.setTheme(ianr.getTheme());
        an.setContent(ianr.getContent());
        an.setCategoryId(ianr.getCategoryId());
        an.setCategoryName(ianr.getCategoryName());
        an.setUserId(ianr.getUserId());
        an.setUserName(ianr.getUserName());

        int result = alumniNewsService.insertAlumniNews(an);
        if (result <= 0) {
            return CommonResult.fail("新闻发布失败");
        }

        return CommonResult.ok("新闻发布成功");
    }

    @Override
    public CommonResult updateAlumniNews(updateAlumniNewsRequest uanr, BindingResult bindingResult) {
        if (uanr == null) {
            return CommonResult.fail("参数不能为空");
        }

        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        AlumniNews an = alumniNewsService.queryDataById(uanr.getId());
        an.setTheme(uanr.getTheme());
        an.setContent(uanr.getContent());
        an.setCategoryId(uanr.getCategoryId());
        an.setCategoryName(uanr.getCategoryName());
        an.setUserId(uanr.getUserId());
        an.setUserName(uanr.getUserName());

        int result = alumniNewsService.updateAlumniNews(an);
        if (result <= 0) {
            return CommonResult.fail("新闻编辑失败");
        }

        return CommonResult.ok("新闻编辑成功");
    }

    @Override
    public CommonResult deleteAlumniNews(int id) {
        AlumniNews oldData = alumniNewsService.queryDataById(id);
        if (oldData == null) {
            return CommonResult.fail("该数据已失效，删除失败");
        }

        int result = alumniNewsService.deleteDataById(id);
        if (result <= 0) {
            return CommonResult.fail("删除失败");
        }
        return CommonResult.ok("删除成功");
    }

    @Override
    public CommonResult queryAlumniNewsPage(int currentPage, int pageCount, String userName, String theme, String startTime, String endTime) {
        PageUtils<AlumniNews> result = alumniNewsService.queryPageList(currentPage, pageCount, theme, userName, startTime, endTime);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryPageByCategoryId(int currentPage, int pageCount, Integer categoryId) {
        PageUtils<AlumniNews> result = alumniNewsService.queryPageByCategoryId(currentPage, pageCount, categoryId);

        IPage<NewsPageVo> accountData = new Page<>();
        PageUtils<NewsPageVo> finallResult = new PageUtils<NewsPageVo>(accountData);
        List<NewsPageVo> datas = new ArrayList<>();
        for (AlumniNews ac : result.getList()) {
            NewsPageVo data = new NewsPageVo();
            data.setId(ac.getId());
            data.setTheme(ac.getTheme());
            data.setContent(ac.getContent());
            data.setCreateTime(TimeExchange.chineseDateTime(ac.getCreateTime() ));
            datas.add(data);
        }
        finallResult.setList(datas);
        finallResult.setTotalPage(result.getTotalPage());
        finallResult.setPageSize(result.getPageSize());
        finallResult.setCurrentPage(result.getCurrentPage());
        finallResult.setTotalCount(result.getTotalCount());

        return CommonResult.ok(result);
    }
}

