package com.dk.mdm.service.core;

import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.core.RoleRightConvert;
import com.dk.mdm.mapper.core.RoleRightMapper;
import com.dk.mdm.model.pojo.core.RoleRight;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.vo.core.RoleRightVO;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class RoleRightService extends BaseService<RoleRight> {

	@Override
	public BaseMapper<RoleRight> getRepository() {
		return roleRightMapper;
	}

	@Autowired
	private RoleRightMapper roleRightMapper;

	@Autowired
	private RoleRightConvert roleRightConvert;

	/**
	 * @desc   : 获取角色权限
	 * @author : 夏常明
	 * @date   : 2023/1/13 11:48
	 */
	public ResponseResultVO<List<Map<String, Object>>> getRoleRight(Map<String, Object> param) {
		// 获取系统基础数据
		List<Map<String, Object>> list = roleRightMapper.getRoleRight(param);
		return ResponseResultUtil.success(list);
	}

	/**
	 * @desc   : 保存角色权限
	 * @author : 夏常明
	 * @date   : 2023/1/13 15:12
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> saveRoleRight(List<RoleRightVO> param ) {

		// 删除角色权限
		roleRightMapper.deleteRoleRight(param.get(0).getRoleId(),param.get(0).getFtyId());
		List<RoleRight> roleRights = new ArrayList<RoleRight>();
		if(param.size()>0)
		{
			// 转化实体
			for (int i=0;i<param.size();i++)
			{
				if(param.get(i).getFunUuid().length()>0)
				{
					RoleRight roleRight =roleRightConvert.convertToPo(param.get(i));
					roleRights.add(roleRight);
				}
			}
			if(roleRights.size()>0)
			{
				long result=roleRightMapper.insertBatch(roleRights);
				Object  s=roleRightMapper.userFunByRole(roleRights.get(0).getRoleId());
				if(result>0)
				{
					return ResponseResultUtil.success(roleRights) ;
				}
				else
				{
					return  ResponseResultUtil.error(ResponseCodeEnum.INSERT_FAIL);
				}
			}

		}

		return ResponseResultUtil.success(roleRights) ;
	}

}
