package com.template.controller;


import com.template.api.AlumniCategoryImageControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.AlumniCategory;
import com.template.model.pojo.AlumniCategoryImage;
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.model.vo.ListDataVO;
import com.template.services.AlumniCategoryImageService;
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-11-25
 */
@RestController
public class AlumniCategoryImageController implements AlumniCategoryImageControllerAPI {

    @Autowired
    private AlumniCategoryImageService alumniCategoryImageService;

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

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

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

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

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

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

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

        oldData.setName(icr.getName());

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

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

    @Override
    public CommonResult deleteCategoryImage(int id) {

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

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

    @Override
    public CommonResult queryCategoryImagePage(int currentPage, int pageCount, String startTime, String endTime) {
        PageUtils<AlumniCategoryImage> result = alumniCategoryImageService.queryPageList(currentPage, pageCount, startTime, endTime);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryCategoryImages() {
        List<AlumniCategoryImage> result = alumniCategoryImageService.queryDatas();
        return CommonResult.ok(result);
    }
}

