/**
 * 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.toolkit.Wrappers;
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.core.tool.api.R;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.operation.pojo.dto.OperationSiteEmployeeDTO;
import org.springblade.modules.operation.pojo.entity.OperationSiteEmployeeEntity;
import org.springblade.modules.operation.pojo.entity.OperationSiteEntity;
import org.springblade.modules.operation.pojo.vo.EditSiteEmpAuthVo;
import org.springblade.modules.operation.pojo.vo.OperationSiteEmployeeVO;
import org.springblade.modules.operation.excel.OperationSiteEmployeeExcel;
import org.springblade.modules.operation.mapper.OperationSiteEmployeeMapper;
import org.springblade.modules.operation.pojo.vo.OperationSiteVO;
import org.springblade.modules.operation.service.IOperationSiteEmployeeService;
import org.springblade.modules.system.pojo.entity.RoleMenu;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * 服务商员工信息表 服务实现类
 *
 * @author lzp
 * @since 2024-06-18
 */
@Service
public class OperationSiteEmployeeServiceImpl extends ServiceImpl<OperationSiteEmployeeMapper, OperationSiteEmployeeEntity> implements IOperationSiteEmployeeService {
	@Autowired
	UserServiceImpl userService;
	@Autowired
	OperationSiteServiceImpl siteService;
	@Override
	public IPage<OperationSiteEmployeeVO> selectOperationSiteEmployeePage(IPage<OperationSiteEmployeeVO> page, OperationSiteEmployeeVO operationSiteEmployee) {
		BladeUser bladeUser = AuthUtil.getUser();
		User dquser = userService.getById(bladeUser.getUserId());
		if("3".equals(dquser.getDeptId())){
			OperationSiteEntity site = siteService.findSiteByUserId(bladeUser.getUserId());
			operationSiteEmployee.setSiteId(site.getId());
		}else if("5".equals(dquser.getDeptId())){
			String siteId = baseMapper.findSiteIdByUserId(bladeUser.getUserId());
			operationSiteEmployee.setSiteId(siteId);
		}
		return page.setRecords(baseMapper.selectOperationSiteEmployeePage(page, operationSiteEmployee));
	}

	@Override
	public OperationSiteEmployeeVO getSiteEmployee(String id) {
		return baseMapper.getSiteEmployee(id);
	}

	@Override
	public OperationSiteEmployeeEntity getEmployeByUserId(Long userId) {
		return baseMapper.findByuserId(userId);
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public R saveSiteEmp(OperationSiteEmployeeDTO dto) {
		BladeUser bladeUser = AuthUtil.getUser();
		User dquser = userService.getById(bladeUser.getUserId());
		if(!"3".equals(dquser.getDeptId()) && !"5".equals(dquser.getDeptId())){
			return R.fail(201,"当前账号无权添加服务工程师");
		}
		if(userService.userByAccount(bladeUser.getTenantId(), dto.getLoginName()) != null){
			return R.fail(201,"登录账号已存在");
		}
		OperationSiteEmployeeEntity emp = new OperationSiteEmployeeEntity();
		if("3".equals(dquser.getDeptId())){
			OperationSiteEntity site = siteService.findSiteByUserId(bladeUser.getUserId());
			emp.setSiteId(site.getId());
		}else if("5".equals(dquser.getDeptId())){
			String siteId = baseMapper.findSiteIdByUserId(bladeUser.getUserId());
			emp.setSiteId(siteId);
		}
		User user = new User();
		user.setStatus(dto.getIsDeleted());
		user.setUserType(1);
		user.setIsDeleted(dto.getIsDeleted());
		user.setName(dto.getName());
		user.setTenantId("000000");
		user.setRealName(dto.getName());
		user.setRoleId("1803307073501319170");
		if(dto.getRole()==1){
			user.setDeptId("6");
		}else{
			user.setDeptId("5");
		}
		user.setPostId("1803302359644688386");
		user.setAccount(dto.getLoginName());
		user.setPassword(dto.getPassword());
		boolean userck = userService.submit(user);
		if(!userck){
			return R.fail(201,"保存失败");
		}
		emp.setUserId(user.getId());
		emp.setName(dto.getName());
		emp.setMobile(dto.getMobile());
		emp.setRole(dto.getRole());
		emp.setIsDeleted(dto.getIsDeleted());
		emp.setYears(dto.getYears());
		emp.setHiredate(dto.getHiredate());
		emp.setCreateTime(new Date());
		return R.status(save(emp));
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateSiteEmp(OperationSiteEmployeeDTO dto) {
		BladeUser bladeUser = AuthUtil.getUser();
		OperationSiteEmployeeEntity emp =getById(dto.getId());
		if(userService.userByAccountUserId(bladeUser.getTenantId(), dto.getLoginName(),emp.getUserId()) != null){
			throw new ServiceException("登录账号已存在");
		}
		User user = userService.userByUserId(emp.getUserId());
		user.setAccount(dto.getLoginName());
		if(StringUtil.isNotBlank(dto.getPassword()) && !"password".equals(dto.getPassword())){
			user.setPassword(DigestUtil.encrypt(dto.getPassword()));
		}
		user.setStatus(dto.getIsDeleted());

		boolean userck = userService.updateById(user);
		if(!userck){
			return false;
		}
		emp.setRole(dto.getRole());
		emp.setIsDeleted(dto.getIsDeleted());
		emp.setName(dto.getName());
		emp.setMobile(dto.getMobile());
		emp.setYears(dto.getYears());
		emp.setHiredate(dto.getHiredate());
		return updateById(emp);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateEmpStatus(String id, String status) {
		OperationSiteEmployeeEntity emp = getBaseMapper().selectById(id);
		emp.setIsDeleted(Integer.valueOf(status));
		User us = userService.userByUserId(emp.getUserId());
		us.setStatus(Integer.valueOf(status));
		boolean chstatus = userService.updateById(us);
		if(!chstatus){
			throw new ServiceException("操作失败");
		}
		return baseMapper.updateById(emp)>0;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteSiteEmp(String id) {
		OperationSiteEmployeeEntity emp = getBaseMapper().selectById(id);
		emp.setIsDeleted(1);
	/*	User us = userService.userByUserId(emp.getUserId());
		us.setStatus(2);// 1.正常 2.其他
		us.setIsDeleted(1);*/
		userService.update(Wrappers.<User>update().lambda().set(User::getStatus, 2).set(User ::getIsDeleted,1).eq(User::getId, emp.getUserId()));
		return updateById(emp);
	}

	public long findEmpCount(String id){
		return  baseMapper.findEmpCount(id);
	}


	@Override
	public List<OperationSiteEmployeeExcel> exportOperationSiteEmployee(Wrapper<OperationSiteEmployeeEntity> queryWrapper) {
		List<OperationSiteEmployeeExcel> operationSiteEmployeeList = baseMapper.exportOperationSiteEmployee(queryWrapper);

		return operationSiteEmployeeList;
	}

	@Override
	public boolean editSiteEmpAuth(EditSiteEmpAuthVo authVo) {
		BladeUser bladeUser = AuthUtil.getUser();
		OperationSiteEmployeeEntity emp =getById(authVo.getId());
		emp.setProvinces(authVo.getProvinces());
		emp.setCity(authVo.getCity());
		emp.setArea(authVo.getArea());
		emp.setUpdateTime(new Date());
		return updateById(emp);
	}


}
