package com.template.controller;


import com.template.annotation.DESRespondSecret;
import com.template.api.SmartSubjectControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartIdentity;
import com.template.model.pojo.SmartSubject;
import com.template.model.pojo.SmartSubject;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartSubjectService;
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-03-07
 */
@RestController
//返回参数加密注解
@DESRespondSecret
public class SmartSubjectController implements SmartSubjectControllerAPI {

    @Autowired
    private SmartSubjectService smartSubjectService;

    /**
     * 新增科目
     *
     * @param smartApply    科目数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult insertSmartSubject(SmartSubject smartApply, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartSubject  data = smartSubjectService.querySubjectByName(smartApply.getName());
        if (data != null) {
            return CommonResult.fail("该科目已存在");
        }

        int result = smartSubjectService.insertSmartSubject(smartApply);

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

    /**
     * 更新科目
     *
     * @param sa            科目数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult updateSmartSubjectById(SmartSubject sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartSubject oldData = smartSubjectService.getSmartById(sa.getId());
        if (oldData == null) {
            return CommonResult.fail("科目数据无效，修改失败");
        }

        if (sa.getName() != null && !sa.getName().equals(oldData.getName())) {
            //查看是否存在相同身份的数据
            SmartSubject data = smartSubjectService.querySubjectByName(sa.getName());
            if (data != null) {
                return CommonResult.fail("该科目已存在");
            }
        }

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

    /**
     * 科目分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult queryPageSmartSubjects(int currentPage, int pageCount, String name) {
        PageUtils<SmartSubject> result = smartSubjectService.queryPageSmartSubjects(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }


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

        SmartSubject data = smartSubjectService.getSmartById(id);

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

        int result = smartSubjectService.deleteSmartSubjectById(id);

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

