package com.zcloud.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcloud.common.utils.*;
import com.zcloud.datasource.annotation.DataSource;
import com.zcloud.modules.sys.dao.SysUserDao;
import com.zcloud.modules.sys.entity.SysUserEntity;
import com.zcloud.modules.sys.service.SysUserRoleService;
import com.zcloud.modules.sys.service.SysUserService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
	@Autowired
	private SysUserRoleService sysUserRoleService;

	@Override
	public SysUserEntity queryByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	public List<SysUserEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectList(queryWrapper);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<Map<String,Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()),Integer.parseInt(params.get("limit").toString()));
		params.put("isDelete",0);
		IPage<Map<String,Object>> ipage = baseMapper.listPage(page,params);
		return new PageUtils(ipage);
	}

	@Override
	public SysUserEntity queryById(String userId) {
		return baseMapper.queryById(userId);
	}

	@Override
	public List<String> queryAllPerms(String userId) {
		return baseMapper.queryAllPerms(userId);
	}

	@Override
	public List<String> queryAllMenuId(String userId) {
		return baseMapper.queryAllMenuId(userId);
	}

	@Override
	@DataSource("slave1")
	public SysUserEntity queryByUserName(String username) {
		return baseMapper.queryByUserName(username);
	}

	@Override
	@Transactional
	public void saveUser(SysUserEntity user) {
		user.setStatus(1);
		user.setType("1");
		//sha256加密
		user.setPassword(new Sha256Hash("666666", user.getUsername()).toHex());
		this.save(user);
		List<String> roleIdList = new ArrayList<>();
		roleIdList.add(user.getRoleId());
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), roleIdList);
	}

	@Override
	@Transactional
	public void update(SysUserEntity user) {
		SysUserEntity sysUserEntity = baseMapper.selectById(user.getUserId());
		if (!sysUserEntity.getUsername().equals(user.getUsername())) {
			user.setPassword(new Sha256Hash("666666", user.getUsername()).toHex());
		}

		this.updateById(user);

		//保存用户与角色关系
		if (user.getRoleId() != null) {
			List<String> roleIdList = new ArrayList<>();
			roleIdList.add(user.getRoleId());
			sysUserRoleService.saveOrUpdate(user.getUserId(), roleIdList);
		}
	}

	@Override
	public void deleteBatch(String[] userId) {
		baseMapper.deleteBatch(userId);
	}

	@Override
	public boolean updatePassword(String userId, String password, String newPassword) {
		SysUserEntity userEntity = new SysUserEntity();
		userEntity.setPassword(newPassword);
		return this.update(userEntity,
				new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
	}

	@Override
	public List<SysUserEntity> listAll(Map<String, Object> params) {
		params.put("isDelete",0);
		return baseMapper.listAll(params);
	}

	@Override
	public R updateNickName(SysUserEntity param, MultipartFile file) {
		SysUserService sysUserService = ((SysUserService)SpringContextUtils.getBean("sysUserService"));
		if (file != null) {
			String  ffile = DateUtil.getDays();
			String fileName = Tools.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			FileUploadUtil.sshSftp(file, fileName, "/uploadFiles/" + ffile);

		}

		sysUserService.updateUserById(param);
		return R.ok().put("userInfo",sysUserService.queryUserById(param));
	}
	@Override
	@DataSource("master")
	public void updateUserById(SysUserEntity param){
		updateById(param);
	}
	@Override
	@DataSource("slave1")
	public SysUserEntity queryUserById(SysUserEntity param){
		return baseMapper.queryById(param.getUserId());
	}

	@Override
	public List<SysUserEntity> listByCustomerinfoId(String customerinfoId) {
		LambdaQueryWrapper<SysUserEntity> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SysUserEntity::getCustomerinfoId,customerinfoId);
		wrapper.eq(SysUserEntity::getIsDelete,0);
		return baseMapper.selectList(wrapper);
	}

}
