package com.crunii.micro.service.sysmgr.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.domain.entity.main.TsmRole;
import com.crunii.micro.domain.repo.main.TsmRoleRepo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.RoleResp;
import com.crunii.micro.service.sysmgr.dto.sysmgr.RoleC;
import com.crunii.micro.service.sysmgr.dto.sysmgr.RoleListRspVo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.RoleReq;
import com.crunii.micro.service.sysmgr.dto.sysmgr.RoleU;
import com.crunii.micro.service.sysmgr.jdbc.RoleJdbcDao;
import com.crunii.micro.service.sysmgr.service.RoleService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 田平 create 2019年2月19日下午2:57:27
 */
@Service
public class RoleServiceImpl implements RoleService {

	private final TsmRoleRepo tsmRoleRepo;
	private final RoleJdbcDao roleJdbcDao;

	public RoleServiceImpl(TsmRoleRepo tsmRoleRepo, RoleJdbcDao roleJdbcDao) {
		this.tsmRoleRepo = tsmRoleRepo;
		this.roleJdbcDao = roleJdbcDao;
	}

	@Override
	public PageRsp<RoleResp> pageRole(PageReq<RoleReq> req){
		return roleJdbcDao.pageRole(req);
	}

	@Override
	public List<TsmRole> selectRoleList(RoleReq req){
		return roleJdbcDao.selectRoleList(req);
	}

	@Override
	public void updateRole(RoleU roleU) {
		roleJdbcDao.updateRole(roleU);
	}

	@Override
	public void createRole(RoleC roleC) {
		roleJdbcDao.createRole(roleC);
	}

	/**
	 * 获取角色列表
	 * @return
	 */
	@Override
	public List<RoleListRspVo> getRoleList(String roleName) {
		List<RoleListRspVo> list=new ArrayList<>();
		List<TsmRole> allRole;
		if (StringUtils.isEmpty(roleName)){
			allRole=tsmRoleRepo.findAllRole();
		}else {
			allRole=tsmRoleRepo.findRoleByRoleName(roleName);
		}
		for (TsmRole tsmRole : allRole) {
			RoleListRspVo roleListRspVo=new RoleListRspVo();
			roleListRspVo.setId((long)tsmRole.getRoleId());
			roleListRspVo.setName(tsmRole.getRoleName());
			list.add(roleListRspVo);
		}
		return list;
	}

}
