package com.friday.tooldriver.service.rpa.taieoc;

import com.alibaba.fastjson.JSON;
import com.friday.base.database.annotation.ListPage;
import com.friday.base.common.response.DubboResult;
import com.friday.mystery.coresecurity.constant.SecurityConstant;
import com.friday.operation.api.keepalive.constant.taieoc.TableName;
import com.friday.operation.api.keepalive.dto.taieoc.OcTenantDetailDto;
import com.friday.operation.api.keepalive.dto.taieoc.OcTenantDictDto;
import com.friday.operation.api.keepalive.dto.taieoc.OcTenantFilterDto;
import com.friday.operation.api.keepalive.dto.taieoc.OcTenantPageDto;
import com.friday.operation.api.keepalive.service.taieoc.ITenantManage;
import com.friday.operation.api.tooldriver.constant.SupplierEnum;
import com.friday.operation.api.keepalive.constant.taieoc.TaieOcConstants;
import com.friday.tooldriver.constants.TaieOcManageCode;
import com.friday.tooldriver.mapper.taieoc.OcGroupMapper;
import com.friday.tooldriver.mapper.taieoc.OcMapper;
import com.friday.tooldriver.mapper.taieoc.OcTenantMapper;
import com.friday.tooldriver.po.taieoc.OcInfoPo;
import com.friday.tooldriver.po.taieoc.OcTenantPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TenantManageImpl implements ITenantManage {

    @Autowired
    private OcMapper ocMapper;
    @Autowired
    private OcTenantMapper ocTenantMapper;

    @Autowired
    private OcGroupMapper ocGroupMapper;
    @Override
    @ListPage(tableName = TableName.OC_TENANT_PAGE_NAME)
    public DubboResult<ArrayList<OcTenantPageDto>> queryTenantPage(OcTenantFilterDto filterDto) {
        DubboResult<ArrayList<OcTenantPageDto>> result;
        List<OcTenantPo> tenantPos = ocTenantMapper.selectOcTenant(filterDto);
        ArrayList<OcTenantPageDto> ocTenantPageDtoList = tenantPos.stream().map(e -> {
            OcTenantPageDto pageDto = OcTenantPo.createPage(e);
            pageDto.setRelateGroupNum(ocGroupMapper.selectGroupCountByTenant(e.getId()));
            return pageDto;
        }).collect(Collectors.toCollection(ArrayList::new));
        result = DubboResult.newSuccess(ocTenantPageDtoList);
        result.setMessage(TaieOcManageCode.SUCCESS.getMessage());
        return result;
    }

    @Override
    public DubboResult<ArrayList<OcTenantDetailDto>> queryTenantDetail(OcTenantFilterDto filterDto) {
        try{
            List<OcTenantPo> tenantPos = ocTenantMapper.selectOcTenant(filterDto);
            ArrayList<OcTenantDetailDto> ocTenantDetailDtoArrayList = tenantPos.stream().map(e -> {
                OcTenantDetailDto detailDto = OcTenantDetailDto.builder()
                        .ocTenantId(e.getId())
                        .ocTenant(e.getOcTenant())
                        .ocTenantUser(e.getTenantUser())
                        .ocTenantPassword(e.getTenantPassword())
                        .minLicenseNum(e.getMinLicenseNum())
                        .extInfo(JSON.parseObject(e.getExtInfo()))
                        .build();
                OcInfoPo ocInfoPo = ocMapper.selectByPrimaryKey(e.getOcInfoId());
                detailDto.setOcInfo(ocInfoPo.getOcInfo());
                detailDto.setOcInfoId(e.getOcInfoId());
                detailDto.setOcIp(ocInfoPo.getOcIp());
                detailDto.setSupplier(ocInfoPo.getSupplier());
                detailDto.setSupplierEn(ocInfoPo.getSupplierEn());
                return detailDto;
            }).collect(Collectors.toCollection(ArrayList::new));
            return DubboResult.newSuccess(ocTenantDetailDtoArrayList);
        }catch (Exception e){
            log.error(TaieOcManageCode.FAIL.toString(), e);
            return DubboResult.newFailure(TaieOcManageCode.FAIL.getCode(), TaieOcManageCode.FAIL.getMessage());
        }
    }

    @Override
    public DubboResult<OcTenantDetailDto> queryPioneerTenantDetail(String supplier, String userTenant) {
        try{
            List<OcInfoPo> ocInfo = ocMapper.selectOcInfo(OcTenantFilterDto.ocTenantFilterBuilder()
                    .ocInfo(TaieOcConstants.PIONEER_CONSOLE)
                    .tenant(userTenant)
                    .supplierCH(StringUtils.isEmpty(supplier) ? SupplierEnum.RPASupplierEnum.UIPATH.getSupplierName() : supplier)
                    .build());
            if(CollectionUtils.isEmpty(ocInfo)){
                log.info(TaieOcManageCode.OC_NOT_EXIST.toString());
                return DubboResult.newFailure(TaieOcManageCode.OC_NOT_EXIST.getCode(), TaieOcManageCode.OC_NOT_EXIST.getMessage());
            }
            List<OcTenantPo> tenantPos = ocTenantMapper.selectOcTenant(OcTenantFilterDto.ocTenantFilterBuilder()
                    .tenant(TaieOcConstants.PIONEER_TENANT)
                    .ocInfoId(ocInfo.get(0).getId())
                    .build());
            if(CollectionUtils.isEmpty(tenantPos)){
                log.info(TaieOcManageCode.TENANT_NOT_EXIST.toString());
                return DubboResult.newFailure(TaieOcManageCode.TENANT_NOT_EXIST.getCode(), TaieOcManageCode.TENANT_NOT_EXIST.getMessage());
            }
            return DubboResult.newSuccess(OcTenantDetailDto.builder().ocTenantId(tenantPos.get(0).getId())
                    .ocTenant(tenantPos.get(0).getOcTenant())
                    .minLicenseNum(tenantPos.get(0).getMinLicenseNum())
                    .tenantKey(String.valueOf(tenantPos.get(0).getId()))
                    .ocInfo(ocInfo.get(0).getOcInfo())
                    .supplier(ocInfo.get(0).getSupplier())
                    .supplierEn(ocInfo.get(0).getSupplierEn())
                    .build());
        }catch (Exception e){
            log.error(TaieOcManageCode.INTERNAL_ERROR.toString(),e);
            return DubboResult.newFailure(TaieOcManageCode.INTERNAL_ERROR.getCode(), TaieOcManageCode.INTERNAL_ERROR.getMessage());
        }

    }

    @Override
    public DubboResult<ArrayList<OcTenantDictDto>> queryOcTenantDropList(String supplierEn, String ocInfo, String userTenant) {
        List<OcTenantPo> tenantPos = ocTenantMapper.selectOcTenant(OcTenantFilterDto.ocTenantFilterBuilder()
                .ocInfo(StringUtils.isNotEmpty(ocInfo) ? ocInfo : null)
                .supplierEn(supplierEn)
                //公用 官网的厂商中控信息
                .tenant(SecurityConstant.portalDefaultTenant)
                .build());
        ArrayList<OcTenantDictDto> dictDtoList = tenantPos.stream().map(OcTenantPo::createDict).collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OcTenantDictDto::getOcTenant))), ArrayList::new));
        return DubboResult.newSuccess(dictDtoList);
    }
}
