/**
 * 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.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.operation.pojo.entity.PlatformEmployeeEntity;
import org.springblade.modules.operation.pojo.vo.EmpAuthVo;
import org.springblade.modules.operation.pojo.vo.PlatformEmployeeVO;
import org.springblade.modules.operation.excel.PlatformEmployeeExcel;
import org.springblade.modules.operation.mapper.PlatformEmployeeMapper;
import org.springblade.modules.operation.service.IPlatformEmployeeService;
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 org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 平台员工权限表 服务实现类
 *
 * @author BladeX
 * @since 2024-06-27
 */
@Service
public class PlatformEmployeeServiceImpl extends ServiceImpl<PlatformEmployeeMapper, PlatformEmployeeEntity> implements IPlatformEmployeeService {

	@Override
	public PlatformEmployeeEntity empDetail(String userId) {
		LambdaQueryWrapper<PlatformEmployeeEntity> queryWrapper=new LambdaQueryWrapper<>();
		queryWrapper.eq(PlatformEmployeeEntity::getUserId,userId);
		PlatformEmployeeEntity employeeEntity=getOne(queryWrapper);
		if(employeeEntity==null){
			return new PlatformEmployeeEntity();
		}else {
			return employeeEntity;
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean editeEmpAuth(PlatformEmployeeEntity employeeEntity) {
		BladeUser user = AuthUtil.getUser();
		LambdaQueryWrapper<PlatformEmployeeEntity> queryWrapper=new LambdaQueryWrapper<>();
		queryWrapper.eq(PlatformEmployeeEntity::getUserId,employeeEntity.getUserId());
		PlatformEmployeeEntity oldEmployData=getOne(queryWrapper);
		if(oldEmployData==null){
			employeeEntity.setCreateTime(new Date());
			employeeEntity.setCreateName(user.getNickName());
			return saveOrUpdate(employeeEntity);
		}else{
			oldEmployData.setProvinces(employeeEntity.getProvinces());
			oldEmployData.setCity(employeeEntity.getCity());
			oldEmployData.setArea(employeeEntity.getArea());
			oldEmployData.setSiteId(employeeEntity.getSiteId());
			oldEmployData.setEntryId(employeeEntity.getEntryId());
			oldEmployData.setPartnerName(employeeEntity.getPartnerName());
			return updateById(oldEmployData);
		}

	}

	@Override
	public EmpAuthVo findPlatEmpAuthByUserId(Long userId){
		return  baseMapper.findPlatEmpAuthByUserId(userId);
	}

	public List<String> findEmpUserId(String roleId,String province,String city,String siteId){
		return  baseMapper.findEmpUserId(roleId, province, city, siteId);
	}
	public List<String> getFitUserEmpId(String roleId,String province,String city,String siteId,String ifOrder){
		return  baseMapper.getFitUserEmpId(roleId, province, city, siteId,ifOrder);
	}
}
