package com.template.controller;


import com.template.annotation.DESRespondSecret;
import com.template.api.SmartExamtypeControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartExamtype;
import com.template.model.pojo.SmartGrade;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartExamtypeService;
import com.template.services.SmartGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

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

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 考试类型表 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-03-12
 */
@RestController
//返回参数加密注解
@DESRespondSecret
public class SmartExamtypeController implements SmartExamtypeControllerAPI {

    @Autowired
    private SmartExamtypeService smartExamtypeService;
    @Autowired
    private SmartGradeService smartGradeService;

    /**
     * 新增考试类型
     *
     * @param smartApply    考试类型数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult insertSmartExamtype(SmartExamtype smartApply, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = smartExamtypeService.insertSmartExamtype(smartApply);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    /**
     * 更新考试类型
     *
     * @param sa            考试类型数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult updateSmartExamtypeById(SmartExamtype sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = smartExamtypeService.updateSmartExamtype(sa);
        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 考试类型分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult queryPageSmartExamtypes(int currentPage, int pageCount, String name, Integer gradeId) {
        PageUtils<SmartExamtype> result = smartExamtypeService.queryPageSmartExamtypes(currentPage, pageCount, name, gradeId);

        List<SmartGrade> grades = smartGradeService.list(null);
        if(grades != null && grades.size() > 0){
            for (SmartExamtype examtype : result.getList()) {
                Optional<SmartGrade> grade = grades.stream().filter(e -> e.getId().equals(examtype.getGradeId())).findFirst();
                if(grade != null && grade.isPresent()) {
                    examtype.setGrade(grade.get().getName());
                }
            }
        }

        return CommonResult.ok(result);
    }


    @Override
    @DESRespondSecret(validated = true)
    public CommonResult deleteSmartExamtypeById(int id) {

        SmartExamtype data = smartExamtypeService.getSmartById(id);

        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = smartExamtypeService.deleteSmartExamtypeById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }
}

