package com.dxhy.admin.sys.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.dxhy.admin.sys.entity.SysCompanyEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.admin.sys.dao.SysUserOrgDao;
import com.dxhy.admin.sys.entity.SysDeptEntity;
import com.dxhy.admin.sys.entity.SysUserOrgEntity;
import com.dxhy.admin.sys.service.SysUserOrgService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户与角色对应关系
 *
 * @author jiaohongyang
 */
@Service("sysUserOrgService")
@Slf4j
public class SysUserOrgServiceImpl extends ServiceImpl<SysUserOrgDao, SysUserOrgEntity> implements SysUserOrgService {
    @Override
    public void saveOrUpdate(Long userId, List<SysUserOrgEntity> orgList) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).insertUserOrg(userId, orgList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertUserOrg(Long userId, List<SysUserOrgEntity> orgList) {
        List<SysUserOrgEntity> userOrgList = new ArrayList<>();
        // 先删除用户与角色关系
        this.remove(new QueryWrapper<SysUserOrgEntity>().eq("userid", userId));

        if (orgList == null || orgList.size() == 0) {
            return;
        }
        Set<SysUserOrgEntity> orgSet = new HashSet<>(orgList);
        // 保存用户与角色关系
        for (SysUserOrgEntity sysUserOrgEntity : orgSet) {
            sysUserOrgEntity.setUserid(userId);
            userOrgList.add(sysUserOrgEntity);
        }
        this.saveOrUpdateBatch(userOrgList);
    }

    @Override
    public List<SysUserOrgEntity> queryOrgIdList(Long userId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.queryOrgIdList(userId);
    }

    @Override
    public List<SysDeptEntity> queryOrgCodes(Long userId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.queryOrgCodes(userId);
    }

    @Override
    public void updateDeptId(String taxno, String taxname, Long deptId) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).updateDept(taxno, taxname, deptId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateDept(String taxno, String taxname, Long deptId) {
        baseMapper.updateDeptId(taxno, taxname, deptId);
    }

    @Override
    public void delete(Long userId) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteUserOrg(userId);
    }

    @Override
    public List<SysCompanyEntity> getOrgsByUserId(Long userId) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);

        return baseMapper.getOrgsByUserId(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteUserOrg(Long userId) {
        baseMapper.delete(new QueryWrapper<SysUserOrgEntity>().eq("userid", userId));
    }
}
