package com.zcloud.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.entity.Result;
import com.zcloud.service.system.FHlogService;
import com.zcloud.service.system.RouteService;
import com.zcloud.util.Jurisdiction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 说明：菜单管理处理类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Controller
@RequestMapping("/route")
public class RouteController extends BaseController {

    @Autowired
    private RouteService routeService;
    @Autowired
    private FHlogService FHLOG;

    /**
     * @Description: 菜单列表
     * @Author: dearLin
     * @Date: 2023/6/9/009 9:28
     * @Param: [java.lang.String] [ROUTE_ID]
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/treeAll")
    @ResponseBody
    public Result routeTreeAll() throws Exception {
        Result result = new Result();
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ROUTE_ID", "0");
        List<PageData> menuList = routeService.treeAll(pd);
        result.put("menuList", menuList);
        return result;
    }


    /**
     * @Description: 菜单新增
     * @Author: dearLin
     * @Date: 2023/6/9/009 10:19
     * @Param: [java.util.Map<java.lang.String, java.lang.String>] [data]
     * @Return: com.zcloud.entity.R
     */
    @RequestMapping(value = "/add")
//	// @RequiresPermissions("route:add")
    @ResponseBody
    public Result routeAdd() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ROUTE_ID", String.valueOf(Integer.parseInt(routeService.findMaxId(pd).get("MID").toString()) + 1));
        pd.put("ROUTE_ICON", "");                                                    //默认无路由图标
        routeService.add(pd);                                                //新增路由
        FHLOG.save(Jurisdiction.getUsername(), "新增路由:" + ((JSONObject) JSONObject.parse(pd.getString("META"))).getString("title"));    //记录日志
        return Result.ok();
    }


    /**
     * @Description: 菜单修改
     * @Author: dearLin
     * @Date: 2023/6/9/009 10:19
     * @Param: [java.util.Map<java.lang.String, java.lang.String>] [data]
     * @Return: com.zcloud.entity.R
     */
    @RequestMapping(value = "/edit")
//	// @RequiresPermissions("route:edit")
    @ResponseBody
    public Result routeEdit() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();
        routeService.edit(pd);
        FHLOG.save(Jurisdiction.getUsername(), "修改路由:" + ((JSONObject) JSONObject.parse(pd.getString("META"))).getString("title"));                //记录日志
        return Result.ok();
    }


    @RequestMapping(value = "/delete")
//	// @RequiresPermissions("route:del")
    @ResponseBody
    public Result routeDelete() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();
        String ROUTE_ID = pd.getString("ROUTE_ID");
        if (routeService.listSubRouteByParentId(ROUTE_ID).size() > 0) {//判断是否有子路由，是：不允许删除
            return Result.error("请先删除子路由");
        } else {
            routeService.delete(ROUTE_ID);
            FHLOG.save(Jurisdiction.getUsername(), "删除的路由ID为:" + ROUTE_ID);                //记录日志
        }
        return Result.ok();
    }

    /**
     * 新增菜单
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/icon")
//    // @RequiresPermissions("route:edit")
    @ResponseBody
    public Object icon() throws Exception {
        Result result = new Result();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData route = routeService.findById(pd);
        JSONObject metaJson = JSONObject.parseObject(route.getString("META"));
        metaJson.put("icon", pd.getString("ICON"));
        route.put("META",  metaJson.toJSONString());
        routeService.edit(route);
        result.put("result", errInfo);
        return result;
    }

}
