package cn.exploring.engine.server.db.service;

import cn.exploring.engine.server.db.dao.AdminAdminMapper;
import cn.exploring.engine.server.db.domain.AdminAdmin;
import cn.exploring.engine.server.db.domain.AdminAdmin.Column;
import cn.exploring.engine.server.db.domain.AdminAdminExample;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class AdminAdminService {
    public static final int ORGANIZE_ADMINISTRATOR_ROLE_ID = 999;  // 组织管理员角色ID
    public static final int SCHOOL_ADMINISTRATOR_ROLE_ID = 1000;  // 管理管理员角色ID

    private final Column[] result = new Column[]{Column.id, Column.username, Column.avatar, Column.roleIds, Column.organizeIds};
    @Resource
    private AdminAdminMapper adminMapper;

    public List<AdminAdmin> findAdmin(String username) {
        AdminAdminExample example = new AdminAdminExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    public AdminAdmin findAdmin(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public List<AdminAdmin> querySelective(String username, Integer page, Integer limit, String sort, String order) {
        AdminAdminExample example = new AdminAdminExample();
        AdminAdminExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public int updateById(AdminAdmin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(AdminAdmin admin) {
        admin.setAddTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.insertSelective(admin);
    }

    public AdminAdmin findById(Integer id) {
        return adminMapper.selectByPrimaryKeySelective(id, result);
    }

    /**
     * 某角色是否可删除，如果已经被分配，则不可删除
     * @return
     */
    public boolean canRoleDeleted(Integer roleId) {
        boolean result = true;

        AdminAdminExample example = new AdminAdminExample();
        AdminAdminExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<AdminAdmin> list = adminMapper.selectByExample(example);

        if (list != null && list.size() > 0) {
            for (AdminAdmin adminAdmin : list) {
                Integer[] roleIds = adminAdmin.getRoleIds();
                if (roleIds != null && roleIds.length > 0) {
                    if (Arrays.asList(roleIds).contains(roleId)) {
                        result = false;
                        break;
                    }
                }
            }
        }

        return result;
    }
}
