package com.app.admin.framework.system.permission.role.service.impl;

import cn.hutool.core.date.DateUtil;
import com.app.admin.framework.system.permission.role.dao.SysRoleDao;
import com.app.admin.framework.system.permission.role.model.SysRole;
import com.app.admin.framework.system.permission.role.service.SysRoleService;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.enums.common.StateEnum;
import com.app.framework.core.support.service.impl.CrudBaseServiceSupport;
import com.app.framework.core.support.view.grid.Paging;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色管理
 */
@Service
public class SysRoleServiceImpl extends CrudBaseServiceSupport<SysRoleDao, SysRole> implements SysRoleService {

    public List<SysRole> getPageList(SysRole view, Paging paging) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getOrganizationId, view.getOrganizationId());
        wrapper.eq(SysRole::getType, 1);
        if (StringUtils.isNotBlank(view.getName())) {
            wrapper.like(SysRole::getName, view.getName());
        }
        wrapper.orderByDesc(SysRole::getCreateTime);
        return this.pageList(wrapper, paging);
    }

    /**
     * 保存对象
     *
     * @param entity 对象
     */
    public boolean saveSysRole(SysRole entity) {
        entity.setType(1);
        if (entity.getState() == null) {
            entity.setState(StateEnum.NORMAL.getValue());
        }
        boolean resultCount;
        if (entity.getOid() == null) {
            entity.setCreateTime(DateUtil.date());
            resultCount = this.save(entity);
        } else {
            resultCount = this.updateById(entity);
        }
        return resultCount;
    }

    /**
     * 根据机构ID加载启用的角色列表
     *
     * @param organizationId 机构ID
     * @return
     */
    public List<SysRole> selectByEnable(Long organizationId) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getOrganizationId, organizationId);
        wrapper.eq(SysRole::getType, 1);
        wrapper.eq(SysRole::getState, StateEnum.NORMAL.getValue());
        return this.list(wrapper);
    }

    /**
     * 根据机构ID加载管理角色
     *
     * @param organizationId 机构ID
     * @return
     */
    public SysRole getOrgAdminRole(Long organizationId) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getOrganizationId, organizationId);
        wrapper.eq(SysRole::getType, 0);
        return this.getOne(wrapper);
    }

    /**
     * 根据机构ID保存管理角色
     *
     * @param organizationId 机构ID
     * @return
     */
    public SysRole saveOrgAdminRole(Long organizationId, LoginUser loginUser) {
        SysRole role = this.getOrgAdminRole(organizationId);
        if (role == null) {
            role = new SysRole();
            role.setOrganizationId(organizationId);
            role.setType(0);
            role.setState(StateEnum.NORMAL.getValue());
            role.setName("机构管理员");
            role.setCreateUserId(loginUser.getId());
            this.save(role);
        }
        return role;
    }
}