/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.operation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.operation.mapper.OperationSiteEmployeeMapper;
import org.springblade.modules.operation.pojo.entity.OperationSiteEntity;
import org.springblade.modules.operation.pojo.entity.OperationSiteRoleEntity;
import org.springblade.modules.operation.pojo.vo.OperationSiteRoleVO;
import org.springblade.modules.operation.excel.OperationSiteRoleExcel;
import org.springblade.modules.operation.mapper.OperationSiteRoleMapper;
import org.springblade.modules.operation.service.IOperationSiteRoleService;
import org.springblade.modules.system.pojo.entity.User;
import org.springblade.modules.system.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import java.util.List;

/**
 * 服务商员工角色 服务实现类
 *
 * @author lzp
 * @since 2024-06-19
 */
@Service
public class OperationSiteRoleServiceImpl extends ServiceImpl<OperationSiteRoleMapper, OperationSiteRoleEntity> implements IOperationSiteRoleService {
	@Autowired
	UserServiceImpl userService;
	@Autowired
	OperationSiteServiceImpl siteService;
	@Autowired
	OperationSiteEmployeeMapper employeeMapper;
	@Override
	public IPage<OperationSiteRoleVO> selectOperationSiteRolePage(IPage<OperationSiteRoleVO> page, OperationSiteRoleVO operationSiteRole) {
		BladeUser user = AuthUtil.getUser();
		if("3".equals(user.getDeptId())){
			OperationSiteEntity site = siteService.findSiteByUserId(user.getUserId());
			operationSiteRole.setSiteId(site.getId());
		}else if("5".equals(user.getDeptId())){
			String siteId = employeeMapper.findSiteIdByUserId(user.getUserId());
			operationSiteRole.setSiteId(siteId);
		}else{
			return null;
		}

		return page.setRecords(baseMapper.selectOperationSiteRolePage(page, operationSiteRole));
	}


	@Override
	public List<OperationSiteRoleExcel> exportOperationSiteRole(Wrapper<OperationSiteRoleEntity> queryWrapper) {
		List<OperationSiteRoleExcel> operationSiteRoleList = baseMapper.exportOperationSiteRole(queryWrapper);
		//operationSiteRoleList.forEach(operationSiteRole -> {
		//	operationSiteRole.setTypeName(DictCache.getValue(DictEnum.YES_NO, OperationSiteRole.getType()));
		//});
		return operationSiteRoleList;
	}

	@Override
	public List<OperationSiteRoleEntity> roleList(BladeUser bladeUser) {
		User dquser = userService.getById(bladeUser.getUserId());
		String siteId="";
		if("3".equals(dquser.getDeptId())){
			OperationSiteEntity site = siteService.findSiteByUserId(bladeUser.getUserId());
			siteId=site.getId();
		}else if("5".equals(dquser.getDeptId())){
			 siteId = employeeMapper.findSiteIdByUserId(bladeUser.getUserId());
		}
		return baseMapper.roleList(siteId);
	}

	@Override
	public boolean deletaStatus(String id) {
		OperationSiteRoleEntity role = baseMapper.selectById(id);
		role.setIsDeleted(1);
		return updateById(role);
	}

	@Override
	public boolean saveRole(OperationSiteRoleEntity operationSiteRole) {
		BladeUser user = AuthUtil.getUser();
		User dquser = userService.getById(user.getUserId());
		if("3".equals(dquser.getDeptId())){
			OperationSiteEntity site = siteService.findSiteByUserId(user.getUserId());
			operationSiteRole.setSiteId(site.getId());
		}else if("5".equals(dquser.getDeptId())){
			String siteId = employeeMapper.findSiteIdByUserId(user.getUserId());
			operationSiteRole.setSiteId(siteId);
		}else{
			throw new ServiceException("当前账号不可操作");
		}
		operationSiteRole.setIsDeleted(0);
		return save(operationSiteRole);
	}

}
