package com.sy.coinage.sys.controller;

import com.alibaba.fastjson2.JSON;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.IdReq;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.core.vo.OptVO;
import com.sy.coinage.sys.model.Menu;
import com.sy.coinage.sys.req.MenuReq;
import com.sy.coinage.sys.service.IMenuService;
import com.sy.coinage.sys.vo.MenuListVO;
import com.sy.coinage.sys.vo.MenuPermissionVO;
import com.sy.coinage.sys.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜单Controller
 */
@Api(value = "菜单接口", tags = "012_菜单接口")
@RestController
@RequestMapping(value = "/menu")
public class MenuController extends BaseController {

    @Autowired
    private IMenuService menuService;

    /**
     * 菜单树形列表
     */
    @ApiOperation(value = "菜单树形列表")
    @GetMapping(value = "/treeList")
    public List<MenuListVO> treeList() {
        List<MenuListVO> data = this.menuService.treeList();
        return data;
    }

    /**
     * 菜单新增
     */
    @Operation(value = "新增菜单")
    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    public Object add(@RequestBody @Validated({Create.class}) MenuReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        Menu model = new Menu();
        BeanUtils.copyProperties(req, model);
        model.setStatus(Constants.STATUS_USABLE);
        if (model.getHideMenu() == null) {
            model.setHideMenu(false);
            this.addPermission(model, req);
        }
        if (model.getPid() == null) {
            model.setPid(0);
        }
        this.menuService.save(model);
        return "";
    }

    /**
     * 菜单修改
     */
    @ApiOperation(value = "修改菜单")
    @Operation(value = "修改菜单")
    @PostMapping("/update")
    public Object update(@RequestBody @Validated({Update.class}) MenuReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        Menu model = this.menuService.getById(req.getId());
        BeanUtils.copyProperties(req, model);
        this.addPermission(model, req);
        this.menuService.updateById(model);
        return "";
    }

    /**
     * 菜单详情
     */
    @ApiOperation(value = "菜单详情")
    @PostMapping("/get")
    public MenuVO get(@RequestBody @Valid IdReq req) {
        MenuVO data = null;
        Menu model = this.menuService.getById(req.getId());
        data = new MenuVO();
        BeanUtils.copyProperties(model, data);
        data.setPermissionList(JSON.parseArray(model.getPermission(), MenuPermissionVO.class));
        return data;
    }

    /**
     * 修改菜单状态
     */
    @ApiOperation(value = "修改菜单状态")
    @Operation(value = "修改菜单状态")
    @PostMapping("/status")
    public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        Menu model = this.menuService.getById(req.getId());
        if (model.getStatus() != null && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
            model.setStatus(Constants.STATUS_USABLE);
        } else {
            model.setStatus(Constants.STATUS_FORBIDDEN);
        }
        this.menuService.updateById(model);
        return model.getStatus();
    }

    /**
     * 菜单下拉选项树,用于配置角色菜单
     */
    @ApiOperation(value = "菜单树形选项")
    @GetMapping("/treeOpt")
    public List<OptVO> treeOpt() {
        List<OptVO> data = this.menuService.treeOpt();
        return data;
    }

    private void addPermission(Menu model, MenuReq req) {
        List<MenuPermissionVO> permissionList = null;
        if (req.getPermissionList() != null && req.getPermissionList().size() > 0) {
            permissionList = req.getPermissionList().stream().filter(item -> StringUtils.isNotBlank(item.getKey()) && StringUtils.isNotBlank(item.getLabel()))
                    .collect(Collectors.toList());
        }
        if (permissionList != null) {
            model.setPermission(JSON.toJSONString(permissionList));
        }
    }

}
