package com.vctgo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.DateUtils;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.core.web.domain.TenantEntity;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.SysTenant;
import com.vctgo.system.domain.SysUserTenant;
import com.vctgo.system.mapper.SysUserTenantMapper;
import com.vctgo.system.service.ISysUserTenantService;
import com.vctgo.system.util.ConstantUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 集团用户可管理园Service业务层处理
 *
 * @author caesar
 * @date 2023-10-26
 */
@Slf4j
@Service
public class SysUserTenantServiceImpl extends ServiceImpl<SysUserTenantMapper, SysUserTenant> implements ISysUserTenantService {
    @Autowired
    private SysUserTenantMapper sysUserTenantMapper;

    /**
     * 查询集团用户可管理园
     *
     * @param id 集团用户可管理园主键
     * @return 集团用户可管理园
     */
    @Override
    public SysUserTenant selectSysUserTenantById(Long id) {
        return sysUserTenantMapper.selectById(id);
    }

    /**
     * 查询集团用户可管理园列表-分页
     *
     * @param sysUserTenant 集团用户可管理园
     * @return 集团用户可管理园
     */
//    @Override
    public IPage<SysUserTenant> selectSysUserTenantPage(SysUserTenant sysUserTenant) {
        Page mpPage = new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L)
                , Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L));
        return sysUserTenantMapper.selectSysUserTenantList(mpPage, sysUserTenant);
    }

    /**
     * 查询集团用户可管理园列表
     *
     * @param sysUserTenant 集团用户可管理园
     * @return 集团用户可管理园
     */
    @Override
    public List<SysUserTenant> selectSysUserTenantList(SysUserTenant sysUserTenant) {
        List<SysUserTenant> sysTenantList = sysUserTenantMapper.selectSysUserTenantList(sysUserTenant);
        return sysTenantList.stream().filter(sysTenant -> "0".equals(sysTenant.getStatus()) && ("1".equals(sysTenant.getLongTerm()) || (Objects.nonNull(sysTenant.getTenantEndTime()) && DateUtils.getNowDate().before(sysTenant.getTenantEndTime())))).collect(Collectors.toList());
    }

    /**
     * 新增集团用户可管理园
     *
     * @param sysUserTenant 集团用户可管理园
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysUserTenant(SysUserTenant sysUserTenant) {
        sysUserTenant.setCreateById(SecurityUtils.getUserId());
        return sysUserTenantMapper.insert(sysUserTenant);
    }

    @Transactional
    @Override
    public int insertSysUserTenantList(List<SysUserTenant> sysUserTenantList) {
        List<SysUserTenant> sysUserTenantTmpList = new ArrayList<>();
        if (!sysUserTenantList.isEmpty()) {
            LambdaQueryWrapper<SysUserTenant> sysUserTenantWrapper = new LambdaQueryWrapper<>();
            sysUserTenantWrapper.eq(SysUserTenant::getIsDeleted, "0");
            sysUserTenantWrapper.eq(SysUserTenant::getUserId, sysUserTenantList.get(0).getUserId());
            sysUserTenantTmpList = sysUserTenantMapper.selectList(sysUserTenantWrapper);
        }
        List<Long> sysUserTenantIdFromDB = sysUserTenantTmpList.stream().map(SysUserTenant::getTenantId).collect(Collectors.toList());
        List<Long> sysUserTenantIdFromFront = sysUserTenantList.stream().map(SysUserTenant::getTenantId).collect(Collectors.toList());
        List<Long> sysUserTenantTmpIdList = new ArrayList<>();
        Collections.copy(sysUserTenantIdFromDB, sysUserTenantTmpIdList);
        sysUserTenantTmpIdList.retainAll(sysUserTenantIdFromFront);

        List<SysUserTenant> sysUserTenantListTmp = new ArrayList<>();
        // 应该删除的
        sysUserTenantTmpList.stream().filter(sysUserTenant -> !sysUserTenantTmpIdList.contains(sysUserTenant.getTenantId())).forEach(sysUserTenant -> {
            sysUserTenant.setUpdateById(SecurityUtils.getUserId());
            sysUserTenant.setUpdateTime(new Date());
            sysUserTenant.setIsDeleted("1");
            sysUserTenantListTmp.add(sysUserTenant);
        });

        // 应该新增的
        sysUserTenantList.stream().filter(sysUserTenant -> !sysUserTenantTmpIdList.contains(sysUserTenant.getTenantId())).forEach(sysUserTenant -> {
            sysUserTenant.setCreateById(SecurityUtils.getUserId());
            sysUserTenantListTmp.add(sysUserTenant);
        });

        return saveOrUpdateBatch(sysUserTenantListTmp) ? 1 : 0;
    }

    /**
     * 修改集团用户可管理园
     *
     * @param sysUserTenant 集团用户可管理园
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysUserTenant(SysUserTenant sysUserTenant) {
        return sysUserTenantMapper.updateById(sysUserTenant);
    }

    /**
     * 逻辑删除集团用户可管理园
     *
     * @param ids 需要删除的集团用户可管理园主键
     * @return 结果
     */
    @Transactional
    @Override
    public int logicalDeleteSysUserTenantByIds(Long[] ids) {
        SysUserTenant sysUserTenant = new SysUserTenant();
        sysUserTenant.setUpdateById(SecurityUtils.getUserId());
        sysUserTenant.setUpdateBy(SecurityUtils.getUsername());
        sysUserTenant.setUpdateTime(new Date());
        sysUserTenant.setIsDeleted(ConstantUtils.IS_DELETED_1);
        return sysUserTenantMapper.logicalDeleteSysUserTenantByIds(ids, sysUserTenant);
    }

    /**
     * 批量删除集团用户可管理园
     *
     * @param ids 需要删除的集团用户可管理园主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysUserTenantByIds(Long[] ids) {
        return sysUserTenantMapper.deleteSysUserTenantByIds(ids);
    }

    /**
     * 删除集团用户可管理园信息
     *
     * @param id 集团用户可管理园主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysUserTenantById(Long id) {
        return sysUserTenantMapper.deleteById(id);
    }
}
