package cn.superdesk.uniorder.dubbo.consumer.sys.role;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
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.ViewRole;
import cn.superdesk.uniorder.dubbo.facade.sys.role.DubboSysRoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Component
public class SysRoleDubboConsumerService {

	@Reference(version = "1.0.0", timeout = 30000, retries = -1)
	private DubboSysRoleService dubboSysRoleService;

	public ViewRole findById(Long id) {

		return dubboSysRoleService.findById(id);
	}

	public R<List<SysRole>> findAllPage(Map<String, Object> params, Integer pageNo, Integer pageSize) {
		return dubboSysRoleService.findAllPage(params, pageNo, pageSize);
	}

	public SysRole add(SysRole sysRole, List<SysRoleMenuKey> sysRoleMenuKeyList, String token) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		sysRole.setCreateTime(DateUtil.date());
		sysRole.setCreateUserId(loginSysUser.getSysUserId());

		sysRole.setUpdateTime(DateUtil.date());
		sysRole.setUpdateUserId(loginSysUser.getSysUserId());

		return dubboSysRoleService.add(sysRole, sysRoleMenuKeyList);
	}

	public void edit(SysRole sysRole, List<SysRoleMenuKey> sysRoleMenuKeyList, String token) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		sysRole.setUpdateTime(DateUtil.date());
		sysRole.setUpdateUserId(loginSysUser.getSysUserId());

		dubboSysRoleService.edit(sysRole, sysRoleMenuKeyList);
	}

	public void delete(Long roleId) {
		dubboSysRoleService.delete(roleId);
	}
}
