package com.liang.controller;

import com.liang.common.base.BaseResult;
import com.liang.entity.SysPermissionInfo;
import com.liang.service.SysDictInfoService;
import com.liang.service.SysPermissionInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.liang.common.idgen.IdGenerate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (SysPermissionInfo)表控制层
 *
 * @author makejava
 * @since 2023-02-19 21:37:38
 */
@Controller
@RequestMapping("/permission")
public class SysPermissionInfoController {
    /**
     * 服务对象
     */
    @Resource
    private SysPermissionInfoService sysPermissionInfoService;

    @Resource
    private SysDictInfoService sysDictInfoService;

    /**
     * 分页查询
     *
     * @param sysPermissionInfo 筛选条件
     * @param pageRequest       分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<SysPermissionInfo>> queryByPage(SysPermissionInfo sysPermissionInfo, PageRequest pageRequest) {
        return ResponseEntity.ok(this.sysPermissionInfoService.queryByPage(sysPermissionInfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SysPermissionInfo> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.sysPermissionInfoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysPermissionInfo 实体
     * @return 新增结果
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult doAdd(SysPermissionInfo sysPermissionInfo) {
        sysPermissionInfo.setPermissionId(IdGenerate.nextId());
        if (sysPermissionInfo.getParentId() == null)
            sysPermissionInfo.setParentId("");
        if (sysPermissionInfo.getHref() == null)
            sysPermissionInfo.setHref("");
        if (sysPermissionInfo.getTarget() == null)
            sysPermissionInfo.setTarget("");
        if (sysPermissionInfo.getIcon() == null)
            sysPermissionInfo.setIcon("");
        if (sysPermissionInfo.getDescript() == null)
            sysPermissionInfo.setDescript("");

        sysPermissionInfo.setCreateUser("admin");

        if (sysPermissionInfo.getParentId() == "") {
            sysPermissionInfo.setParentId("0");
        }
        int num = this.sysPermissionInfoService.insert(sysPermissionInfo);
        if (num > 0) {
            return BaseResult.success("新增菜单成功!");
        } else {
            return BaseResult.failure("新增菜单失败!");
        }
    }

    /**
     * 新增菜单
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "permissioninfo/add";
    }

    /**
     * 修改菜单
     *
     * @return
     */
    @RequestMapping(value = "/update/{permissionId}", method = RequestMethod.GET)
    public String update(Model model,
                         @PathVariable String permissionId) {
        SysPermissionInfo permissionInfo = sysPermissionInfoService.queryById(permissionId);
        model.addAttribute("permissionInfo", permissionInfo);
        // 获取父级菜单名字
        SysPermissionInfo parentInfo = sysPermissionInfoService.queryById(permissionInfo.getParentId());

        model.addAttribute("parentName", parentInfo == null ? "" : parentInfo.getName());

        // 获取下拉栏枚举值
        List<Map<String, String>> baseDictList = sysDictInfoService.queryByCode("permission_target");
        model.addAttribute("baseDictList", baseDictList);

        return "permissioninfo/update";
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult doUpdate(SysPermissionInfo sysPermissionInfo) {

        int num = sysPermissionInfoService.update(sysPermissionInfo);
        if (num > 0) {
            return BaseResult.success("修改菜单成功!");
        } else {
            return BaseResult.failure("修改菜单失败!");
        }
    }

    /**
     * 逻辑删除一个系统菜单
     *
     * @return
     */
    @RequestMapping(value = "/delete/{permissionId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult delete(@PathVariable String permissionId) {
        boolean flag = sysPermissionInfoService.deleteById(permissionId);
        if (flag) {
            return BaseResult.success("删除成功!");
        } else {
            return BaseResult.failure("删除失败!");
        }
    }

    /**
     * 查询页面初始化
     *
     * @return
     */
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init() {
        return "permissioninfo/list";
    }

    /**
     * 获取菜单列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list() {
        List<SysPermissionInfo> list = sysPermissionInfoService.list();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", list.size());
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 获取菜单列表
     *
     * @return
     */
    @RequestMapping(value = "/listPermission", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult listPermission() {
        List<Map<String, Object>> result = sysPermissionInfoService.listPermission();
        return BaseResult.success("获取菜单列表成功!", result);
    }

    /**
     * 禁用一个系统菜单
     *
     * @return
     */
    @RequestMapping(value = "/forbid/{permissionId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult forbid(@PathVariable String permissionId) {
        int num = sysPermissionInfoService.forbid(permissionId);
        if (num > 0) {
            return BaseResult.success("禁用成功!");
        } else {
            return BaseResult.failure("禁用失败!");
        }
    }


    /**
     * 启用一个系统菜单
     *
     * @return
     */
    @RequestMapping(value = "/enable/{permissionId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult enable(@PathVariable String permissionId) {
        int num = sysPermissionInfoService.enable(permissionId);
        if (num > 0) {
            return BaseResult.success("启用成功!");
        } else {
            return BaseResult.failure("启用失败!");
        }
    }

}

