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

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.dao.entity.sys.role.SysRole;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRoleMenuKey;
import cn.superdesk.uniorder.dto.vo.sys.role.SysRoleVo;
import cn.superdesk.uniorder.dubbo.consumer.sys.role.SysRoleDubboConsumerService;
import cn.superdesk.uniorder.common.numerical.AnConvertBeanUtil;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "系统权限-角色(菜单权限)-管理", tags = {"系统权限-角色(菜单权限)-管理"})
@Slf4j
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {


	@Autowired
	private SysRoleDubboConsumerService sysRoleDubboConsumerService;

	/**
	 * 返回角色列表-分页
	 *
	 * @return
	 */
	@PostMapping("/findAllByPage")
	@ResponseBody
	public R<List<SysRole>> findAllByPage() {

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

		try {

			Map<String, Object> params = new HashMap<>();

			params.put("roleName", getString("roleName"));

			result = sysRoleDubboConsumerService.findAllPage(params, getPageNum(), getPageSize());

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage("系统查找失败！");

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

		return result;
	}


	/**
	 * 新增修改
	 *
	 * @param model
	 * @return
	 */
	@PostMapping("/saveOrUpdate")
	@ResponseBody
	public R<?> saveOrUpdate(@RequestBody SysRoleVo model) {

		R<?> result = new R<>();

		try {

			List<SysRoleMenuKey> ls = new ArrayList<SysRoleMenuKey>();

			if (!StringUtils.isEmpty(model.getMenus())) {
				String[] menus = model.getMenus().split(",");

				for (int i = 0; i < menus.length; i++) {
					SysRoleMenuKey key = new SysRoleMenuKey();
					key.setMenuId(Long.valueOf(menus[i]));
					ls.add(key);
				}
			}

			SysRole role = AnConvertBeanUtil.copyBean(model, SysRole.class);
			if (model.getId() == null) {
				//新增
				sysRoleDubboConsumerService.add(role, ls, getToken());
				result.setSuccess(true);

			} else {
				//修改
				sysRoleDubboConsumerService.edit(role, ls, getToken());
				result.setSuccess(true);
			}

		} catch (Exception e) {

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

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

	/**
	 * 删除
	 *
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	public R<?> delete(Long id) {

		R<?> result = new R<>();

		try {
			sysRoleDubboConsumerService.delete(id);

			result.setSuccess(true);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage("删除失败,该角色已经被用户使用！");

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

		return result;
	}
}
