package cn.superdesk.uniorder.controller.sys.role;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.vo.sys.role.ViewMenuTree;
import cn.superdesk.uniorder.dto.vo.sys.role.ViewRoleMenu;
import cn.superdesk.uniorder.dubbo.consumer.sys.role.SysMenuDubboConsumerService;
import cn.superdesk.uniorder.dto.utils.ConvertTreeUtils;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/5/20 上午10:52
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/20 上午10:52
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "系统权限-菜单-非管理", tags = {"系统权限-菜单-非管理"})
@Slf4j
@RestController
@RequestMapping("/menu")
public class SysMenuController extends BaseController {

	@Autowired
	private SysMenuDubboConsumerService sysMenuDubboConsumerService;

	/**
	 * 返回菜单(树结构,不传id则返回完整树结构)
	 *
	 * @return
	 */
	@PostMapping("/getTree")
	@ResponseBody
	public R<List<ViewMenuTree>> getTree() {

		R<List<ViewMenuTree>> result = new R<>();

		try {
			Long id = getLong("id");

			List<ViewMenuTree> resultData = sysMenuDubboConsumerService.getTreeById(getLong("id"));

			//树形转化
			List<ViewMenuTree> nodeList = new ArrayList<>();

			for (ViewMenuTree node : resultData) {

				if (node.getPid() == id) {
					node = ConvertTreeUtils.buildTreeMenu(resultData, node);
					nodeList.add(node);
				}
			}

			result.setData(nodeList);
			result.setSuccess(true);

		} catch (Exception e) {


			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 返回用户菜单(树结构)
	 *
	 * @return
	 */
	@PostMapping("/getTreeByUser")
	@ResponseBody
	public R<List<ViewMenuTree>> getTreeByUser() {

		R<List<ViewMenuTree>> result = new R<>();

		try {

			Date beginDate = DateUtil.date();


			List<ViewMenuTree> ls = sysMenuDubboConsumerService.getTreeByUser(getToken());

			// 树形转化
			List<ViewMenuTree> nodeList = new ArrayList<>();
			for (ViewMenuTree node : ls) {

				if (node.getPid() == null) {
					node = ConvertTreeUtils.buildTreeMenu(ls, node);
					nodeList.add(node);
				}
			}

			Date endDate = DateUtil.date();

//			log.info("\n\n\n\nMenu Start Time {}\nMenu   End Time {}\nMenu processing {}ms\n\n\n\n", beginD.getTime(), endD.getTime(), (endD.getTime()-beginD.getTime()));


			result.setData(nodeList);
			result.setSuccess(true);

		} catch (Exception e) {

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
		}

		return result;
	}

	/**
	 * 返回角色菜单(树结构)
	 *
	 * @return
	 */
	@PostMapping("/getTreeByRole")
	@ResponseBody
	public R<List<ViewMenuTree>> getTreeByRoleId() {

		R<List<ViewMenuTree>> result = new R<>();

		try {
			List<ViewMenuTree> ls = sysMenuDubboConsumerService.getTreeByRoleId(getLong("id"));

			//树形转化
			List<ViewMenuTree> nodeList = new ArrayList<>();

			for (ViewMenuTree node : ls) {

				if (node.getPid() == null) {
					node = ConvertTreeUtils.buildTreeMenu(ls, node);
					nodeList.add(node);
				}
			}

			result.setData(nodeList);
			result.setSuccess(true);

		} catch (Exception e) {

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);

		}

		return result;
	}

	/**
	 * 返回菜单按钮权限-用户
	 *
	 * @return
	 */
	@PostMapping("/getDataByUser")
	@ResponseBody
	public R<List<ViewRoleMenu>> getDataByUser() {

		R<List<ViewRoleMenu>> result = new R<>();

		try {
			List<ViewRoleMenu> tree = sysMenuDubboConsumerService.getDataByUser(getToken());

			result.setData(tree);
			result.setSuccess(true);

		} catch (Exception e) {

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
		}

		return result;
	}
}
