package com.hotent.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.hotent.base.aop.OpenApiProcessor;
import com.hotent.base.cache.annotation.Cacheable;
import com.hotent.base.constants.CacheKeyConst;
import com.hotent.base.context.BaseContext;
import com.hotent.base.entity.URIEntity;
import com.hotent.base.enums.ResponseErrorEnums;
import com.hotent.base.exception.BaseException;
import com.hotent.base.feign.BpmModelFeignService;
import com.hotent.base.feign.BpmRuntimeFeignService;
import com.hotent.base.feign.FormFeignService;
import com.hotent.base.feign.UCFeignService;
import com.hotent.base.handler.MultiTenantHandler;
import com.hotent.base.handler.MultiTenantIgnoreResult;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.*;
import com.hotent.base.util.AppUtil;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.JsonUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.portal.model.PortalBizSystem;
import com.hotent.portal.model.PortalBizSystemAuth;
import com.hotent.portal.model.PortalBizSystemOrgAuth;
import com.hotent.portal.model.PortalUrlPermission;
import com.hotent.portal.persistence.dao.PortalBizSystemAuthDao;
import com.hotent.portal.persistence.dao.PortalBizSystemDao;
import com.hotent.portal.persistence.dao.PortalBizSystemOrgAuthDao;
import com.hotent.portal.persistence.dao.PortalUrlPermissionDao;
import com.hotent.portal.persistence.manager.PortalBizSystemAuthManager;
import com.hotent.portal.persistence.manager.PortalMsgSendLogManager;
import com.hotent.portal.service.PortalBizSystemManager;
import com.hotent.portal.service.PortalBizSystemOrgAuthManager;
import com.hotent.portal.service.PortalUrlPermissionManager;
import com.hotent.sys.persistence.manager.SysPropertiesManager;
import com.hotent.uc.api.impl.model.Org;
import com.hotent.uc.api.impl.model.UserFacade;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 *  服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author cola
 * @since 2021-08-25
 */
@Service
public class PortalBizSystemOrgAuthManagerImpl extends BaseManagerImpl<PortalBizSystemOrgAuthDao, PortalBizSystemOrgAuth> implements PortalBizSystemOrgAuthManager {
    @Autowired
    UCFeignService uCFeignService;

    @Autowired
    PortalBizSystemOrgAuthDao portalBizSystemOrgAuthDao;


    @Override
    public List<PortalBizSystemOrgAuth> getListByCode(String code,String tenantId,String demId) {
        LambdaQueryWrapper<PortalBizSystemOrgAuth> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PortalBizSystemOrgAuth::getSysCode,code);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<String> getListByCodeAsId(String code,String type, String tenantId, String demId) {
        LambdaQueryWrapper<PortalBizSystemOrgAuth> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PortalBizSystemOrgAuth::getSysCode,code);
        queryWrapper.eq(PortalBizSystemOrgAuth::getDataType,type);
        List<PortalBizSystemOrgAuth> resultList=this.baseMapper.selectList(queryWrapper);

        List<String> idsList=new ArrayList<>();
        idsList=resultList.stream().map(item->item.getOrgId()).collect(Collectors.toList());
        return idsList;
    }

    @Override
    public void saveEntity(String sysCode, String orgIdList,String type,String tenantId,String demId) {
        List<String> idList= Arrays.asList(orgIdList.split(","));
        IUser user = ContextUtil.getCurrentUser();
        LambdaQueryWrapper<PortalBizSystemOrgAuth> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PortalBizSystemOrgAuth::getSysCode,sysCode);
        queryWrapper.eq(PortalBizSystemOrgAuth::getDataType,type);
        this.remove(queryWrapper);
        //添加记录
        for(String id:idList){
//            LambdaQueryWrapper<PortalBizSystemOrgAuth> queryWrapper=new LambdaQueryWrapper<>();
//            queryWrapper.eq(PortalBizSystemOrgAuth::getSysCode,sysCode);
//            queryWrapper.eq(PortalBizSystemOrgAuth::getDataType,type);
//            queryWrapper.eq(PortalBizSystemOrgAuth::getOrgId,id);
//            this.remove(queryWrapper);

            PortalBizSystemOrgAuth entity=new PortalBizSystemOrgAuth();
            entity.setCreateBy(user.getAccount());
            entity.setDemId(demId);
            entity.setOrgId(id);
            if(type.equals("0")){
                ObjectNode org=uCFeignService.getOrgByIdOrCode(id);
                if(org!=null){

                    entity.setOrgType(org.get("orgType").asText()+"");
                }

            }
            entity.setSysCode(sysCode);
            entity.setDataType(type);
            this.save(entity);
        }

    }

    @Override
    public void deleteEntity(String sysCode, String orgIdList, String type) {
        List<String> idList= Arrays.asList(orgIdList.split(","));
        //清理记录
        LambdaQueryWrapper<PortalBizSystemOrgAuth> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PortalBizSystemOrgAuth::getSysCode,sysCode);
        queryWrapper.eq(PortalBizSystemOrgAuth::getDataType,type);
        queryWrapper.in(PortalBizSystemOrgAuth::getOrgId,idList);
        this.remove(queryWrapper);
    }

    @Override
    public PageList<Org> queryData(String sysCode,String type, QueryFilter<Org> queryFilter) {
        IPage<Org> page =  portalBizSystemOrgAuthDao.getOrgAuthSystem(convert2IPage(queryFilter.getPageBean()), convert2Wrapper(queryFilter, currentModelClass()), sysCode,type);
        return new PageList<>(page);
    }

    @Override
    public PageList<UserFacade> queryUserData(String sysCode, String type, QueryFilter<UserFacade> queryFilter) {
        IPage<UserFacade> page =  portalBizSystemOrgAuthDao.getOrgAuthSystemUser(convert2IPage(queryFilter.getPageBean()), convert2Wrapper(queryFilter, currentModelClass()), sysCode,type);
        return new PageList<>(page);
    }

}

