package com.template.controller;


import com.template.api.AlumniCategoryControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.AlumniCategory;
import com.template.model.pojo.AlumniUser;
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.AlumniCategoryService;
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;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-11-20
 */
@RestController
public class AlumniCategoryController implements AlumniCategoryControllerAPI {

    @Autowired
    private AlumniCategoryService alumniCategoryService;

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

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

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

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

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

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

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

        oldData.setName(icr.getName());

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

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

    @Override
    public CommonResult deleteCategory(int id) {

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

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

    @Override
    public CommonResult queryCategoryPage(int currentPage, int pageCount, String name, Integer userId, String startTime, String endTime) {
        PageUtils<AlumniCategory> result = alumniCategoryService.queryPageList( currentPage, pageCount, name, userId, startTime, endTime);

        return CommonResult.ok(result);
    }
}

