package com.yihu.jw.base.endpoint.menu;

import com.yihu.jw.base.service.menu.BaseMenuManageService;
import com.yihu.jw.base.service.role.RoleMenuService;
import com.yihu.jw.base.util.ErrorCodeUtil;
import com.yihu.jw.entity.base.menu.BaseLinkDictDO;
import com.yihu.jw.entity.base.menu.BaseMenuDictDO;
import com.yihu.jw.entity.base.menu.BaseMenuShowDO;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.base.BaseRequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yeshijie on 2018/9/26.
 */
@RestController
@RequestMapping(value = BaseRequestMapping.MenuDict.PREFIX)
@Api(value = "首页菜单管理", description = "首页菜单管理", tags = {"基础服务 - 首页菜单管理"})
public class BaseMenuManageEndpoint extends EnvelopRestEndpoint {


    @Autowired
    private BaseMenuManageService menuService;
    @PostMapping(value = BaseRequestMapping.MenuDict.deleteMenuDict)
    @ApiOperation(value = "删除菜单")
    public Envelop deleteMenuDict(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            menuService.deletMenuDict(id);
            return success("删除成功");
        }catch (Exception e){
            return failedException(e);
        }
    }

    @PostMapping(value = BaseRequestMapping.MenuDict.createMenuDict)
    @ApiOperation(value = "新建或修改")
    public Envelop create (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) {
        try {
            BaseMenuDictDO baseMenuDictDO = menuService.createOrUpdateMenu(jsonData);
            return success(baseMenuDictDO);
        } catch (Exception e) {
            return failedException(e);
        }
    }
    @PostMapping(value = BaseRequestMapping.MenuDict.findOneMenuDict)
    @ApiOperation(value = "根据id查询链接单条")
    public Envelop findOneMenuDict(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            return success(menuService.findOneMenuDict(id));
        }catch (Exception e){
            return failedException(e);
        }
    }
    @PostMapping(value = BaseRequestMapping.MenuDict.MOVE_DOWN)
    @ApiOperation(value = "下移")
    public Envelop moveDown(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            BaseMenuDictDO baseMenuDictDO = menuService.downMenu(id);
            return success(baseMenuDictDO);
        }catch (Exception e){
            return failedException(e);
        }

    }

    @PostMapping(value = BaseRequestMapping.MenuDict.MOVE_UP)
    @ApiOperation(value = "上移")
    public Envelop moveUp(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            BaseMenuDictDO baseMenuDictDO = menuService.upMenu(id);
            return success(baseMenuDictDO);
        }catch (Exception e){
            return failedException(e);
        }
    }

    @PostMapping(value = BaseRequestMapping.MenuDict.STATUS)
    @ApiOperation(value = "生效/失效")
    public Envelop status(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id,
            @ApiParam(name = "status", value = "1生效，0失效", required = true)
            @RequestParam(value = "status") Integer status) {
        try {
            menuService.updateStatus(id, status);
            return success("修改成功");
        }catch (Exception e){
            return failedException(e);
        }
    }
    @GetMapping(value = BaseRequestMapping.MenuDict.LIST)
    @ApiOperation(value = "查询菜单字典列表")
    public Envelop findMenuDictByKey(
            @ApiParam(name = "parentId", value = "parentId", required = false)
            @RequestParam(value = "parentId",required = false) String parentId,
            @ApiParam(name = "name", value = "name", required = false)
            @RequestParam(value = "name",required = false) String name,
            @ApiParam(name = "status", value = "status", required = false)
            @RequestParam(value = "status",required = false) Integer status) {
        try {
            return success(menuService.findMenuDictByKey(parentId, name,status));
        }catch (Exception e){
            return failedException(e);
        }
    }



    @PostMapping(value = BaseRequestMapping.LinkDict.createLinkDict)
    @ApiOperation(value = "新建或修改")
    public Envelop createLink (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) {
        try {
            BaseLinkDictDO baseLinkDictDO = menuService.createOrUpdateLink(jsonData);
            return success(baseLinkDictDO);
        } catch (Exception e) {
            return failedException(e);
        }
    }

    @PostMapping(value = BaseRequestMapping.LinkDict.MOVELinkDOWN)
    @ApiOperation(value = "下移")
    public Envelop moveDownDict(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            BaseLinkDictDO baseLinkDictDO = menuService.downlink(id);
            return success(baseLinkDictDO);
        }catch (Exception e){
            return failedException(e);
        }

    }

    @PostMapping(value = BaseRequestMapping.LinkDict.MOVELinkUP)
    @ApiOperation(value = "上移")
    public Envelop moveLink(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            BaseLinkDictDO baseLinkDictDO = menuService.upLink(id);
            return success(baseLinkDictDO);
        }catch (Exception e){
            return failedException(e);
        }
    }

    @PostMapping(value = BaseRequestMapping.LinkDict.linkStatus)
    @ApiOperation(value = "生效/失效")
    public Envelop statusLink(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id,
            @ApiParam(name = "status", value = "1生效，0失效", required = true)
            @RequestParam(value = "status") String status) {
        try {
            menuService.updateLinkStatus(id, status);
            return success("修改成功");
        }catch (Exception e){
            return failedException(e);
        }
    }
    @PostMapping(value = BaseRequestMapping.LinkDict.findOneLink)
    @ApiOperation(value = "根据id查询链接单条")
    public Envelop findOneLink(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            return success(menuService.findOneLinkDict(id));
        }catch (Exception e){
            return failedException(e);
        }
    }
    @PostMapping(value = BaseRequestMapping.LinkDict.deleteLink)
    @ApiOperation(value = "删除链接")
    public Envelop deleteLink(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            menuService.updateLinkDel(id);
            return success("删除成功");
        }catch (Exception e){
            return failedException(e);
        }
    }
    @PostMapping(value = BaseRequestMapping.LinkDict.linkShow)
    @ApiOperation(value = "生效/失效")
    public Envelop isShow(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id,
            @ApiParam(name = "status", value = "1展示0不展示", required = true)
            @RequestParam(value = "status") String status) {
        try {
            menuService.updateLinkShow(id, status);
            return success("修改成功");
        }catch (Exception e){
            return failedException(e);
        }
    }
    @GetMapping(value = BaseRequestMapping.LinkDict.findLinkDictALL)
    @ApiOperation(value = "查询友情链接字典列表")
    public Envelop findLinkDictByKey(
            @ApiParam(name = "name", value = "name", required = false)
            @RequestParam(value = "name",required = false) String name,
            @ApiParam(name = "status", value = "status", required = false)
            @RequestParam(value = "status",required = false) String status) {
        try {
            return success(menuService.findLinkDict(name,status));
        }catch (Exception e){
            return failedException(e);
        }
    }
    @PostMapping(value = BaseRequestMapping.MenuShow.menuShowstatus)
    @ApiOperation(value = "移除")
    public Envelop removeMenu(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            menuService.removeMenu(id);
            return success("修改成功");
        }catch (Exception e){
            return failedException(e);
        }
    }


    @PostMapping(value = BaseRequestMapping.MenuShow.createMenuShow)
    @ApiOperation(value = "新建或修首页菜单")
    public Envelop createMenuShow (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) {
        try {
            menuService.saveMenuShow(jsonData);
            return success("操作成功");
        } catch (Exception e) {
            return failedException(e);
        }
    }

    @PostMapping(value = BaseRequestMapping.MenuShow.MOVEMenuShowDOWN)
    @ApiOperation(value = "下移")
    public Envelop moveDownShow(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            BaseMenuShowDO  baseMenuShowDO= menuService.downMenuShow(id);
            return success(baseMenuShowDO);
        }catch (Exception e){
            return failedException(e);
        }

    }

    @PostMapping(value = BaseRequestMapping.MenuShow.MOVEMenuShowUP)
    @ApiOperation(value = "上移")
    public Envelop moveUpShow(
            @ApiParam(name = "id", value = "id", required = true)
            @RequestParam(value = "id") String id) {
        try {
            BaseMenuShowDO baseMenuShowDO = menuService.upMenuShow(id);
            return success(baseMenuShowDO);
        }catch (Exception e){
            return failedException(e);
        }
    }


    @GetMapping(value = BaseRequestMapping.MenuShow.findMenuShow)
    @ApiOperation(value = "查询首页列表")
    public Envelop findMenuShowByKey(
            @ApiParam(name = "parentId", value = "parentId", required = false)
            @RequestParam(value = "parentId",required = false) String parentId,
            @ApiParam(name = "name", value = "name", required = false)
            @RequestParam(value = "name",required = false) String name) {
        try {
            return success(menuService.findMenuShow());
        }catch (Exception e){
            return failedException(e);
        }
    }


    @GetMapping(value = BaseRequestMapping.MenuShow.findMenuShowDict)
    @ApiOperation(value = "查询首页菜单词典列表")
    public Envelop findMenuShowDict(@ApiParam(name = "parentId", value = "parentId", required = false)
                                        @RequestParam(value = "parentId",required = false) String parentId,
                                    @ApiParam(name = "name", value = "name", required = false)
                                        @RequestParam(value = "name",required = false) String name) {
        try {
            return success(menuService.findMenuDictParent(parentId,name));
        }catch (Exception e){
            return failedException(e);
        }
    }
}
