package com.sysu.admin.support.system.menu;

import com.google.common.collect.Maps;
import com.sysu.admin.support.base.BaseVo;
import com.sysu.admin.support.base.ServiceContext;
import com.xiesx.fastboot.base.pagination.PaginationVo;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import com.xiesx.fastboot.utils.CopyUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @title MenuController
 * @description
 * @author XIE
 * @date 2020年4月24日下午10:04:02
 */
@Validated
@Controller
@RequestMapping(value = "menu/")
public class SMenuController extends ServiceContext {

    @Autowired
    SMenuRepository mMenuRepository;

    @Autowired
    SMenuService mMenuService;

    /**
     * 页面
     *
     * @return
     */
    @RequestMapping(value = "index")
    @RequiresPermissions("menu:insert")
    public String index() {
        return "system/menu/index";
    }

    /**
     * 添加编辑页面
     * @return
     */
    @RequestMapping(value = "form")
    @RequiresPermissions("menu:view")
    public String form(BaseVo baseVo, Model model) {
        SMenu sMenu = mMenuRepository.getOne(baseVo.getId());
        SMenu parent = mMenuRepository.getOne(sMenu.getParent());
        if (ObjectUtils.isEmpty(sMenu)) {
            sMenu = new SMenu();
        }
        model.addAttribute("json", sMenu.toJSONString());
        if(sMenu.getParent() > -1) {
            model.addAttribute("parentName", parent.getName());
        }
        return "system/menu/form";
    }

    /**
     * 添加子节点
     * @return
     */
    @RequiresPermissions("menu:insert")
    @RequestMapping(value = "addChildForm")
    public String addChildForm(BaseVo baseVo, Model model) {
        SMenu sMenu = mMenuRepository.getOne(baseVo.getId());
        List<SMenu> childList = mMenuRepository.findByParent(baseVo.getId());
        model.addAttribute("parent", sMenu);
        model.addAttribute("sort", childList.size() + 1);
        return "system/menu/addChildForm";
    }

    /**
     * 添加根菜单
     * @return
     */
    @RequiresPermissions("menu:insert")
    @RequestMapping(value = "addRoot")
    public String addRoot(Model model) {
        List<SMenu> childList = mMenuRepository.findByParent(-1L);
        model.addAttribute("sort", childList.size() + 1);
        return "system/menu/addRoot";
    }

    /**
     * 添加编辑页面
     * @return
     */
    @RequiresPermissions("menu:update")
    @RequestMapping(value = "submit")
    @ResponseBody
    public BaseResult submit(SMenu req) {
        req.setIsMenu(req.getIsMenu() == null ? 0 : 1);
        if(req.getId() == null){
            mMenuRepository.insertOrUpdate(req);
        }else{
            SMenu sMenu = mMenuRepository.findOne(req.getId());
            BeanUtils.copyProperties(req, sMenu, CopyUtils.ignoreNullNames(req));
            mMenuRepository.insertOrUpdate(sMenu);
        }
        return R.succ();
    }

    /**
     * 分页列表
     *
     * @param base
     * @param page
     * @return
     */
    @ResponseBody
    @RequiresPermissions("menu:view")
    @RequestMapping(value = "list")
    public Map<String, Object> list(BaseVo base, PaginationVo page) {
        List<SMenuResBO.MenuBO> menus = mMenuService.getMenuList();
        // 构造分页
        Map<String, Object> maps = Maps.newHashMap();
        maps.put("code", 0);
        maps.put("msg", "操作成功");
        maps.put("count", menus.size());
        maps.put("data", menus);
        return maps;
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    @RequiresPermissions("menu:delete")
    public BaseResult delete(Long id) {
        Integer row = mMenuRepository.delete(id);
        return (row >= 1) ? R.succ() : R.fail();
    }
}
