package com.template.controller;


import com.template.api.AlumniCategoryNewsControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.AlumniCategoryImage;
import com.template.model.pojo.AlumniCategoryNews;
import com.template.model.request.insertCategoryRequest;
import com.template.model.request.updateCategoryRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.AlumniCategoryNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

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

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-12-02
 */
@RestController
public class AlumniCategoryNewsController implements AlumniCategoryNewsControllerAPI {
    @Autowired
    private AlumniCategoryNewsService alumniCategoryNewsService;

    @Override
    public CommonResult insertCategoryNews(insertCategoryRequest icr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        AlumniCategoryNews existData = alumniCategoryNewsService.queryDataByName(icr.getName());
        if(existData != null){
            return CommonResult.fail("该分类名称已存在，无法新增");
        }

        AlumniCategoryNews ac = new AlumniCategoryNews();
        ac.setName(icr.getName());

        int result = alumniCategoryNewsService.insertAlumniCategoryNews(ac);
        if (result <= 0) {
            return CommonResult.fail("新增失败");
        }
        return CommonResult.ok("新增成功");
    }

    @Override
    public CommonResult updateCategoryNews(updateCategoryRequest icr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        AlumniCategoryNews existData = alumniCategoryNewsService.queryDataByName(icr.getName());
        if(existData != null && existData.getId().intValue() != icr.getId().intValue()){
            return CommonResult.fail("该分类名称已存在，无法更新");
        }

        AlumniCategoryNews oldData = alumniCategoryNewsService.queryDataById(icr.getId());
        if (oldData == null) {
            return CommonResult.fail("该数据已失效，更新失败");
        }

        oldData.setName(icr.getName());

        int result = alumniCategoryNewsService.updateAlumniCategoryNews(oldData);
        if (result <= 0) {
            return CommonResult.fail("更新失败");
        }

        return CommonResult.ok("更新成功");
    }

    @Override
    public CommonResult deleteCategoryNews(int id) {

        AlumniCategoryNews oldData = alumniCategoryNewsService.queryDataById(id);
        if (oldData == null) {
            return CommonResult.fail("该数据已失效，删除失败");
        }

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

    @Override
    public CommonResult queryCategoryNewsPage(int currentPage, int pageCount, String startTime, String endTime) {
        PageUtils<AlumniCategoryNews> result = alumniCategoryNewsService.queryPageList(currentPage, pageCount, startTime, endTime);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryCategoryNewss() {
        List<AlumniCategoryNews> result = alumniCategoryNewsService.queryDatas();
        return CommonResult.ok(result);
    }
}

