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

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

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.SysUserBusinessDao;
import com.dxhy.admin.sys.entity.SysUserBusinessEntity;
import com.dxhy.admin.sys.service.SysUserBusinessService;
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("sysUserBusinessService")
@Slf4j
public class SysUserBusinessServiceImpl extends ServiceImpl<SysUserBusinessDao, SysUserBusinessEntity>
    implements SysUserBusinessService {

    @Override
    public List<SysUserBusinessEntity> queryUserId(Long userId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectList(new QueryWrapper<SysUserBusinessEntity>().eq("user_id", userId));
    }

    @Override
    public void saveOrUpdate(Long userId, List<SysUserBusinessEntity> businessList) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        if (businessList != null) {
            Aops.getSelf(this).insertBusiness(userId, businessList);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void insertBusiness(Long userId, List<SysUserBusinessEntity> businessList) {
        List<SysUserBusinessEntity> list = new ArrayList<>();
        // 删除 用户与业务单元关联关系
        this.remove(new QueryWrapper<SysUserBusinessEntity>().eq("user_id", userId));

        // 保存用户与业务单元关联关系
        for (SysUserBusinessEntity sysUserBusinessEntity : businessList) {
            sysUserBusinessEntity.setUserId(userId);
            list.add(sysUserBusinessEntity);
        }

        this.saveOrUpdateBatch(list);
    }

    @Override
    public void updateBusinessId(String businessCode, String businessName, Long businessId) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).updateBusiness(businessCode, businessName, businessId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateBusiness(String businessCode, String businessName, Long businessId) {
        baseMapper.updateBusinessId(businessCode, businessName, businessId);
    }

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

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

    @Override
    public List<Long> queryBusinessIdList(Long userId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);

        return baseMapper.queryBusinessIdList(userId);
    }

}
