/**
 * 
 */
package org.springblade.resource;

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

import org.springblade.modules.system.entity.DataPermission;
import org.springblade.modules.system.service.normal.DataPermissionService;
import org.springblade.modules.system.service.normal.DataPermissionUserService;
import org.springblade.system.client.DataPermClient;
import org.springblade.system.client.dto.DataPermDto;
import org.springblade.system.client.model.DataPermModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;

import springfox.documentation.annotations.ApiIgnore;

/**
 * @author wookvn
 *
 */
@RestController
@ApiIgnore
public class DataPermResource implements DataPermClient {

	@Autowired
	private DataPermissionService dataPermissionService;
	
	@Autowired
	private DataPermissionUserService dataPermissionUserService;
	
	@Override
	public DataPermModel getById(Integer id) {
		DataPermission dataPermission = dataPermissionService.getById(id);
		if(dataPermission != null) {
			DataPermModel dataPermModel = new DataPermModel();
			dataPermModel.setDataPermId(id);
			dataPermModel.setDataPermName(dataPermission.getPermName());
			dataPermModel.setDataPermDto(JSON.parseObject(dataPermission.getPermJson(), DataPermDto.class));
			return dataPermModel;
		}
		return null;
	}

	@Override
	public List<DataPermModel> listByIds(List<Integer> ids) {
		List<DataPermission> entityList = dataPermissionService.listByIds(ids);
		List<DataPermModel> dataList = new ArrayList<>();
		if(entityList != null && !entityList.isEmpty()) {
			entityList.forEach(item -> {
				DataPermModel dataPermModel = new DataPermModel();
				dataPermModel.setDataPermId(item.getId());
				dataPermModel.setDataPermName(item.getPermName());
				dataPermModel.setDataPermDto(JSON.parseObject(item.getPermJson(), DataPermDto.class));
				dataList.add(dataPermModel);
			});
		}
		return dataList;
	}

	@Override
	public Boolean checkSuperPermission(Long userId) {
		long count = dataPermissionUserService.checkSuperPermission(userId);
		return count >= 1;
	}

}
