/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.enterprise.entity.Staff;
import org.springblade.estate.entity.ServerUser;
import org.springblade.estate.service.IUserAccessPermissionsService;
import org.springblade.estate.vo.ServerUserVO;
import org.springblade.estate.mapper.ServerUserMapper;
import org.springblade.estate.service.IServerUserService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.List;

/**
 * 服务人员 服务实现类
 *
 * @author BladeX
 * @since 2022-01-04
 */
@Service
public class ServerUserServiceImpl extends BaseServiceImpl<ServerUserMapper, ServerUser> implements IServerUserService {

	@Autowired
	private IUserAccessPermissionsService userAccessPermissionsService;

	@Override
	public IPage<ServerUserVO> selectServerUserPage(IPage<ServerUserVO> page, ServerUserVO serverUser) {
		return page.setRecords(baseMapper.selectServerUserPage(page, serverUser));
	}

	@Override
	public Integer serverUserStat() {
		BladeUser bladeUser = AuthUtil.getUser();
		QueryWrapper<ServerUser> queryWrapper = Condition.getQueryWrapper(new HashMap<>(), ServerUser.class);
		if(!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)){  //超管
			return this.count(queryWrapper.lambda().eq(ServerUser::getTenantId, bladeUser.getTenantId()));
		}else{
			return this.count(queryWrapper);
		}
	}

	@Override
	public R deleteServerUser(List<Long> idList) {
		for(Long id : idList){
			//删除人脸
			userAccessPermissionsService.deleteFace(id.toString(), CommonConstant.UserFlag.SERVER_USER_FLAG.getValue());
			removeById(id);
		}
		return R.status(true);
	}

}
