package com.energy.manage.service.service.system.impl;


import com.alibaba.fastjson.JSONObject;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.base.Page;
import com.energy.manage.common.po.system.SysRolePO;
import com.energy.manage.common.po.system.SysRolePermissionPO;
import com.energy.manage.common.po.system.SysUserRolePO;
import com.energy.manage.common.reponse.RespMsg;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.domain.dto.system.RoleUpdateStateDto;
import com.energy.manage.service.mappers.system.SysRoleMapper;
import com.energy.manage.service.mappers.system.SysRolePermissionMapper;
import com.energy.manage.service.mappers.system.SysUserRoleMapper;
import com.energy.manage.service.service.system.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRolePO> implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Autowired
	private SysUserRoleMapper userRoleMapper;

	@Autowired
	private SysRolePermissionMapper rolePermissionMapper;

	@Override
	public SysRolePO selectOne(SysRolePO rolePO) {
		return sysRoleMapper.selectOne(rolePO);
	}

	@Override
	public boolean addRole(SysRolePO rolePO) {
		// 设定自定义来源
		rolePO.setSource(1);
		return sysRoleMapper.insert(rolePO) > 0;
	}

	@Override
	public ResultResp deleteRole(SysRolePO rolePO) {
		SysUserRolePO po = new SysUserRolePO();
		po.setRoleId(rolePO.getId());
		int count = userRoleMapper.selectCount(po);
		if (count > 0) {
			return ResultResp.FAIL("该角色尚存在绑定人员，不可删除");
		}
		SysRolePermissionPO rpPo = new SysRolePermissionPO();
		rpPo.setRoleId(rolePO.getId());
		rolePermissionMapper.delete(rpPo);
		boolean flag = sysRoleMapper.delete(rolePO) > 0;
		if (flag) {
			return ResultResp.SUCCESS();
		}
		return ResultResp.FAIL();
	}

	@Override
	public boolean updateRoleAvailable(RoleUpdateStateDto updateStateDto) {
		SysRolePO sysRolePO = new SysRolePO();
		sysRolePO.setRoleAvailable(updateStateDto.getRoleAvailable());
		Example queryExample = new Example(SysRolePO.class);
		Example.Criteria criteria = queryExample.createCriteria();
		criteria.andEqualTo("id", updateStateDto.getRoleId());
		return sysRoleMapper.updateByExampleSelective(sysRolePO,queryExample) >= 0;
	}

	@Override
	public boolean updateRole(SysRolePO rolePO) {
		return sysRoleMapper.updateRoleSelective(rolePO) >= 0;
	}

	@Override
	public Page<SysRolePO> getRoleListByName(Integer pageNum, Integer pageSize, String roleName,Integer roleAvailable) {
		PageHelper.startPage(pageNum, pageSize);
		List<SysRolePO> list = sysRoleMapper.getRoleListByName(roleName,roleAvailable);
		Long total = CollectionUtils.isEmpty(list) ? 0L : new PageInfo<>(list).getTotal();
		return Page.build(pageNum, pageSize, total, list);
	}

	/**
	 * 通过userID 获取角色信息
	 * @param userId
	 * @return
	 */
	@Override
	public SysRolePO getSysRole(Integer userId){
		return sysRoleMapper.selectRoleByUserId(userId);
	}



}
