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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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.mapper.SiteUserPermissionMapper;
import org.jeecg.modules.adweb.site.service.ISitePermissionService;
import org.jeecg.modules.adweb.site.service.ISiteUserPermissionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 站点权限表
 * @Author: liang
 * @Date: 2022-08-09
 * @Version: V1.0
 */
@Service
@Slf4j
public class SiteUserPermissionServiceImpl
        extends ServiceImpl<SiteUserPermissionMapper, AdwebSiteUserPermission>
        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 {
            LambdaQueryWrapper<AdwebSiteUserPermission> queryWrapper = new LambdaQueryWrapper<>();

            queryWrapper
                    .eq(AdwebSiteUserPermission::getUid, uid)
                    .eq(AdwebSiteUserPermission::getStatus, 1);

            siteUserPermissionList = this.list(queryWrapper);

            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);
        }
    }
}
