package org.jeecg.modules.adweb.site.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.adweb.common.util.SnowflakeIdUtil;
import org.jeecg.modules.adweb.site.entity.AdwebSitePermission;
import org.jeecg.modules.adweb.site.entity.AdwebSiteUserPermission;
import org.jeecg.modules.adweb.site.service.ISitePermissionService;
import org.jeecg.modules.adweb.site.service.ISiteUserPermissionService;
import org.springframework.stereotype.Service;

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

/**
 * @Description: 站点权限表
 * @Author: liang
 * @Date: 2022-08-09
 * @Version: V1.0
 */
@Service
@Slf4j
public class SiteUserPermissionServiceImpl implements ISiteUserPermissionService {

    @Resource
    private ISitePermissionService sitePermissionService;


    @Resource
    private SnowflakeIdUtil snowflakeIdUtil;

    @Override
    public List<String> getSiteCodeList(String uid) {
        List<String> codeList = new ArrayList<>();
        List<AdwebSiteUserPermission> AdwebSiteUserPermissionList = null;
        try {
            AdwebSiteUserPermissionList = this.list(new LambdaQueryWrapper<AdwebSiteUserPermission>()
                    .eq(AdwebSiteUserPermission::getUid, uid)
                    .eq(AdwebSiteUserPermission::getStatus, 1));
        } catch (Exception e) {
            log.error("通过uid获取站点权限失败", e);
        }
        if (CollectionUtils.isEmpty(AdwebSiteUserPermissionList)) {
            codeList.add("-1");
            return codeList;
        }
        List<String> permissionsList = AdwebSiteUserPermissionList.stream().map(AdwebSiteUserPermission::getPermissionCode).collect(Collectors.toList());

        try {
            codeList = sitePermissionService.list(new LambdaQueryWrapper<AdwebSitePermission>()
                            .in(AdwebSitePermission::getPermissionCode, permissionsList)
                            .eq(AdwebSitePermission::getStatus, 1))
                    .stream().map(AdwebSitePermission::getSiteCode).collect(Collectors.toList());
        } catch (Exception e) {
            log.error("通过站点权限获取站点code失败", e);
        }
        if (CollectionUtils.isEmpty(codeList)) {
            codeList.add("-1");
            return codeList;
        }
        return codeList;
    }

    @Override
    public List<String> getSiteCodeListByUids(List<String> list) {
        List<String> codeList = new ArrayList<>();
        List<AdwebSiteUserPermission> AdwebSiteUserPermissionList = null;
        try {
            AdwebSiteUserPermissionList = this.list(new LambdaQueryWrapper<AdwebSiteUserPermission>()
                    .in(AdwebSiteUserPermission::getUid, list)
                    .eq(AdwebSiteUserPermission::getStatus, 1));
        } catch (Exception e) {
            log.error("通过uid获取站点权限失败", e);
        }
        if (CollectionUtils.isEmpty(AdwebSiteUserPermissionList)) {
            codeList.add("-1");
            return codeList;
        }
        List<String> permissionsList = AdwebSiteUserPermissionList.stream().map(AdwebSiteUserPermission::getPermissionCode).collect(Collectors.toList());
        try {
            codeList = sitePermissionService.list(new LambdaQueryWrapper<AdwebSitePermission>()
                            .in(AdwebSitePermission::getPermissionCode, permissionsList)
                            .eq(AdwebSitePermission::getStatus, 1))
                    .stream().map(AdwebSitePermission::getSiteCode).collect(Collectors.toList());
        } catch (Exception e) {
            log.error("通过站点权限获取站点code失败", e);
        }
        if (CollectionUtils.isEmpty(codeList)) {
            codeList.add("-1");
            return codeList;
        }
        return codeList;
    }

    /**
     * 根据uid和siteCode向site_user_permission表中插入一条记录
     *
     * @return:
     * @Author: luxiaoxiao
     * @Date: 2021/4/22
     */
    @Override
    public void insertSiteUserPermission(String uid, String siteCode, String siteName) {

        List<AdwebSiteUserPermission> siteUserPermissionList = null;
        try {
            siteUserPermissionList = this.list(new LambdaQueryWrapper<AdwebSiteUserPermission>()
                    .eq(AdwebSiteUserPermission::getUid, uid)
                    .eq(AdwebSiteUserPermission::getStatus, 1));

            AdwebSiteUserPermission siteUserPermission = new AdwebSiteUserPermission();
            String permissionCode = "";
            if (CollectionUtils.isNotEmpty(siteUserPermissionList)) {
                siteUserPermission = siteUserPermissionList.get(0);
                permissionCode = siteUserPermission.getPermissionCode();
            } else {
                permissionCode = String.valueOf(snowflakeIdUtil.nextId());
                siteUserPermission.setUid(uid);
                siteUserPermission.setPermissionCode(permissionCode);
                siteUserPermission.setStatus(1);
                siteUserPermission.setCreateTime(new Date());
                this.save(siteUserPermission);
            }

            AdwebSitePermission sitePermission = new AdwebSitePermission();
            sitePermission.setSiteCode(siteCode);
            sitePermission.setSiteName(siteName);
            sitePermission.setPermissionCode(permissionCode);
            sitePermission.setStatus(1);
            sitePermission.setCreateTime(new Date());
            sitePermissionService.save(sitePermission);
        } catch (Exception e) {
            log.error("添加站点权限失败", e);
        }
    }

    /**
     * @param entityList
     * @param batchSize
     * @return
     */
    @Override
    public boolean saveBatch(Collection<AdwebSiteUserPermission> entityList, int batchSize) {
        return false;
    }

    /**
     * @param entityList
     * @param batchSize
     * @return
     */
    @Override
    public boolean saveOrUpdateBatch(Collection<AdwebSiteUserPermission> entityList, int batchSize) {
        return false;
    }

    /**
     * @param entityList
     * @param batchSize
     * @return
     */
    @Override
    public boolean updateBatchById(Collection<AdwebSiteUserPermission> entityList, int batchSize) {
        return false;
    }

    /**
     * @param entity
     * @return
     */
    @Override
    public boolean saveOrUpdate(AdwebSiteUserPermission entity) {
        return false;
    }

    /**
     * @param queryWrapper
     * @param throwEx
     * @return
     */
    @Override
    public AdwebSiteUserPermission getOne(Wrapper<AdwebSiteUserPermission> queryWrapper, boolean throwEx) {
        return null;
    }

    /**
     * @param queryWrapper
     * @param throwEx
     * @return
     */
    @Override
    public Optional<AdwebSiteUserPermission> getOneOpt(Wrapper<AdwebSiteUserPermission> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    /**
     * @param queryWrapper
     * @return
     */
    @Override
    public Map<String, Object> getMap(Wrapper<AdwebSiteUserPermission> queryWrapper) {
        return Map.of();
    }

    /**
     * @param queryWrapper
     * @param mapper
     * @param <V>
     * @return
     */
    @Override
    public <V> V getObj(Wrapper<AdwebSiteUserPermission> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    /**
     * @return
     */
    @Override
    public BaseMapper<AdwebSiteUserPermission> getBaseMapper() {
        return null;
    }

    /**
     * @return
     */
    @Override
    public Class<AdwebSiteUserPermission> getEntityClass() {
        return null;
    }
}
