package com.yinet.web.controller.yyqh;

import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.domain.AjaxResult;
import com.yinet.common.core.domain.Ztree;
import com.yinet.yyqh.domain.YyqhGuidance;
import com.yinet.yyqh.service.YyqhGuidanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 办事指南信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/yyqh/guidance")
public class YyqhGuidanceAdminController extends BaseController {
    static String prefix = "yyqh/admin/";

    @Autowired
    private YyqhGuidanceService guidanceService;

    @GetMapping()
    public String guidance() {
        return prefix + "/list";
    }

    /**
     * 获取办事指南列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public List<YyqhGuidance> list() {
        List<YyqhGuidance> guidanceList = guidanceService.selectGuidanceList();
        return guidanceList;
    }

    /**
     * 新增办事指南
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap) {
        YyqhGuidance guidance = null;
        if (0L != id) {
            guidance = guidanceService.selectGuidanceById(id);
        } else {
            guidance = new YyqhGuidance();
            guidance.setYyqhGuidanceId(0L);
            guidance.setGuidanceName("根目录");
        }
        mmap.addAttribute("guidanceNode", guidance);
        return prefix + "/add";
    }

    /**
     * 新增保存办事指南
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated YyqhGuidance guidance) {
        int i = guidanceService.insertGuidance(guidance);
        return toAjax(i);
    }

    /**
     * 编辑办事指南
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.addAttribute("guidanceNode", guidanceService.selectGuidanceById(id));
        return prefix + "/edit";
    }

    /**
     * 编辑保存办事事项
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated YyqhGuidance guidance) {
        if (guidance.getParentId().equals(0L) && guidance.getGuidanceType().equals("2")) {
            return AjaxResult.warn("不允许在根目录添加事项");
        }
        if (guidance.getYyqhGuidanceId().equals(guidance.getParentId())) {
            return AjaxResult.warn("不允许把自己设置为上级分类");
        }
        if (guidanceService.selectCountGuidanceByParentId(guidance.getYyqhGuidanceId()) > 0 && !guidance.getGuidanceType().equals("1")) {
            return AjaxResult.warn("存在子项,不允许更改事项类型");
        }
        int i = guidanceService.updateGuidance(guidance);
        return toAjax(i);
    }

    /**
     * 删除菜单
     */
    @GetMapping("/remove/{guidanceId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("guidanceId") Long guidanceId) {
        if (guidanceService.selectCountGuidanceByParentId(guidanceId) > 0) {
            return AjaxResult.warn("存在子项,不允许删除");
        }
        return toAjax(guidanceService.deleteGuidanceById(guidanceId));
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/guidanceTreeData")
    @ResponseBody
    public List<Ztree> guidanceTreeData() {
        List<Ztree> ztrees = guidanceService.guidanceTreeData();
        return ztrees;
    }

    /**
     * 选择办事指南树
     */
    @GetMapping("/selectGuidanceTree/{id}")
    public String selectGuidanceTree(@PathVariable("id") Long id, ModelMap mmap) {
        YyqhGuidance guidance = null;
        if (0L != id) {
            guidance = guidanceService.selectGuidanceById(id);
        } else {
            guidance = new YyqhGuidance();
            guidance.setYyqhGuidanceId(0L);
            guidance.setGuidanceName("根目录");
        }
        mmap.put("guidance", guidance);
        return prefix + "/tree";
    }
}