package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.common.FileUploadUtil;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.*;

import org.springframework.web.bind.annotation.*;
import com.zcxk.entity.MenuEntity;
import com.zcxk.service.MenuService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 菜单页面控制器
 * @author WYY
 * @date 2020-10-26 16:15
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Value("${UMIS.sys_config_path}")
    private String baseDir;

    @ApiOperation(value = "分页查询菜单信息")
    @RequestMapping(value = "/selectPage", method = RequestMethod.POST)
    public AjaxMessage<Pagination<MenuEntity>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @ApiParam(value = "菜单信息", required = false) @RequestBody MenuEntity menuEntity) {
        IPage<MenuEntity> iPage = new Page<>(pageNum, pageSize);
        iPage = menuService.findPage(iPage, menuEntity);
        Pagination<MenuEntity> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

    @ApiOperation(value = "查询所有菜单")
    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    public AjaxMessage<List<MenuEntity>> allList(@ApiParam(value = "菜单信息", required = false) @RequestBody MenuEntity menuEntity) {
        List<MenuEntity> menuEntities = menuService.findList(menuEntity);
        return new AjaxMessage<>(ResultStatus.OK, menuEntities);
    }
    @ApiOperation(value = "查询权限标志")
    @RequestMapping(value = "/selectAuthCode", method = RequestMethod.POST)
    public AjaxMessage<List<String>> allList() {
        List<String> menuEntities = menuService.getAuthCode();
        return new AjaxMessage<>(ResultStatus.OK, menuEntities);
    }
    /**
     * 新增
     */
    @ApiOperation(value = "新增菜单信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxMessage<Integer> addMenu(@ApiParam(value = "菜单信息", required = true) @RequestBody MenuEntity menu) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            menu.setUpdateBy(currentUser.getUsername());
            menu.setCreateBy(currentUser.getUsername());
            menu.setDateCreate(new Date());
            menu.setDateUpdate(new Date());
        }
        if (menu.getParentMenuId() != null) {
            MenuEntity menuEntity = menuService.findMenuById(menu.getParentMenuId());
            if (menuEntity.getAppId() != null)
                menu.setAppId(menuEntity.getAppId());
        }
        menu.setStatus(1);
        int result = menuService.addMenu(menu) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 批量新增
     */
    @ApiOperation(value = "批量新增菜单信息")
    @RequestMapping(value = "/addBatch", method = RequestMethod.POST)
    public AjaxMessage<Integer> addMenus(@ApiParam(value = "菜单信息", required = true) @RequestBody ArrayList<MenuEntity> menus) {
        int result = menuService.saveBatch(menus) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "菜单图标")
    @RequestMapping(value = "addMenuImag", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<String> addUserPhoto(@ApiParam(value = "菜单图标", required = true) @RequestParam("avatarfile") MultipartFile file) {
        String avatar = "";
        if (!file.isEmpty()) {
            try {
                avatar = FileUploadUtil.uploadWeb(baseDir, file);
            } catch (IOException e) {
                return new AjaxMessage<>(ResultStatus.ERROR, e.getMessage());
            }
        }
        return new AjaxMessage<>(ResultStatus.OK, avatar);
    }

    /**
     * 按ID查询菜单
     */
    @ApiOperation(value = "按ID查询菜单")
    @GetMapping(value = "/findMenuById")
    public AjaxMessage<MenuEntity> findMenuById(@ApiParam(value = "菜单ID", required = true) @RequestParam Long id) {
        MenuEntity menu = menuService.findMenuById(id);
        return new AjaxMessage<>(ResultStatus.OK, menu);
    }

    /**
     * 修改保存菜单
     */
    @ApiOperation(value = "修改保存菜单")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public AjaxMessage<Integer> editMenu(@ApiParam(value = "菜单信息", required = true) @RequestBody MenuEntity menu) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            menu.setUpdateBy(currentUser.getUsername());
            menu.setDateUpdate(new Date());
        }
        int result = menuService.updateMenuById(menu) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除菜单")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public AjaxMessage<Integer> deleteByIds(@ApiParam(value = "菜单IDS", required = true) @RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }
        List<Long> longIds = new ArrayList<>();
        for (Long item : ids) {
            longIds.add(item);
        }
        List<MenuEntity> menuEntityList = menuService.findMenuByParentIds(longIds);
        if (menuEntityList.size() > 0) {
            return new AjaxMessage<>(ResultStatus.EXSIT_IS_PARENT_ERROR, 0);
        }

        int result = menuService.delMenuByIds(ids) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "单个删除菜单")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public AjaxMessage<Integer> deleteById(@ApiParam(value = "菜单ID", required = true) @RequestParam Long id) {
        if (id == null) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }

        List<Long> longIds = new ArrayList<>();
        longIds.add(id);
        List<MenuEntity> menuEntityList = menuService.findMenuByParentIds(longIds);
        if (menuEntityList.size() > 0) {
            return new AjaxMessage<>(ResultStatus.EXSIT_IS_PARENT_ERROR, 0);
        } else {
            int result = menuService.delMenuById(id) ? 1 : 0;
            return new AjaxMessage<>(ResultStatus.OK, result);
        }
    }
    /**
     * 按应用查询菜单信息(系统菜单-用户)
     */
    @ApiOperation(value = "查询应用菜单信息")
    @GetMapping(value = "/getUserMenuByUid")
    public Map<String, String>getUserMenuByUid(@ApiParam(value = "应用ID", required = true) @RequestParam String appId){
        appId=null;
        Map<String, String>urls=menuService.getUserMenuByUid(appId);
        return urls;
    }
}
