package com.template.controller;


import com.template.api.SmartMenuControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartMenu;
import com.template.model.pojo.SmartSchool;
import com.template.model.request.insertMenuRequest;
import com.template.model.request.updateMenuRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

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

/**
 * <p>
 * 应用管理 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-01-16
 */
@RestController
public class SmartMenuController implements SmartMenuControllerAPI {

    @Autowired
    private SmartMenuService smartMenuService;

    @Override
    public CommonResult insertSmartMenu(insertMenuRequest imr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        //去重判断
        SmartMenu existSm = smartMenuService.querySmartMenuByName(imr.getName(),imr.getRoute());
        if(existSm != null){
            return CommonResult.fail("系统中存在重复菜单，添加失败");
        }

        SmartMenu sm = new SmartMenu();
        sm.setName(imr.getName());
        sm.setRoute(imr.getRoute());

        int result = smartMenuService.insertSmartMenu(sm);

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

    @Override
    public CommonResult updateSmartMenuById(updateMenuRequest umr, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if(umr.getId() == null){
            return CommonResult.fail("数据ID不能为空");
        }

        SmartMenu oldData = smartMenuService.getSmartById(umr.getId());
        if(oldData == null){
            return CommonResult.fail("菜单基本信息无效，修改失败");
        }

        //去重判断
        SmartMenu existSm = smartMenuService.querySmartMenuByName(umr.getName(),umr.getRoute());
        if(existSm != null && !existSm.getId().equals(umr.getId())){
            return CommonResult.fail("系统中存在重复菜单，修改失败");
        }

        oldData.setName(umr.getName() == null ? oldData.getName() : umr.getName());
        oldData.setRoute(umr.getRoute() == null ? oldData.getRoute() : umr.getRoute());

        int result = smartMenuService.updateSmartMenu(oldData);
        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    @Override
    public CommonResult queryPageSmartMenus(int currentPage, int pageCount, String name) {
        PageUtils<SmartMenu> result = smartMenuService.queryPageSmartMenus(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult deleteSmartMenuById(int id) {
        SmartMenu data = smartMenuService.getSmartById(id);

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

        int result = smartMenuService.deleteSmartMenuById(id);

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

}

