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

import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.keepalive.dto.smartrpa.OcProcessCommitFilterDto;
import com.friday.operation.api.keepalive.dto.smartrpa.OcProcessReleaseDto;
import com.friday.operation.api.keepalive.dto.smartrpa.OcProcessReleaseFilterDto;
import com.friday.operation.api.tooldriver.dto.common.DriverBase;
import com.friday.operation.api.tooldriver.dto.rpa.CodePackageDto;
import com.friday.tooldriver.mapper.taieoc.OcProcessCommitMapper;
import com.friday.tooldriver.mapper.taieoc.OcProcessReleaseMapper;
import com.friday.tooldriver.po.taieoc.OcProcessCommitPo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class ProcessReleaseManage {
    @Autowired
    private OcProcessReleaseMapper processReleaseMapper;
    @Autowired
    private OcProcessCommitMapper ocProcessCommitMapper;

    public void deleteGroupByKey(String key) {
        List<OcProcessReleaseDto> ocProcessReleaseDtos = processReleaseMapper.selectProcessRelease(OcProcessReleaseFilterDto.builder().ocGroupId(Long.parseLong(key)).build());
        if (!CollectionUtils.isEmpty(ocProcessReleaseDtos)) {
            processReleaseMapper.deleteByProcessGroup(Long.parseLong(key));
        }
    }


    public OcProcessCommitPo obtainMaxVersion(String ocTenantKey, String processName, String fileType) {
        // 获取当前最大版本信息
        return ocProcessCommitMapper.selectMaxVersion(
                OcProcessCommitFilterDto.builder()
                        .processName(processName)
                        .fileType(fileType)
                        .ocTenantId(Long.parseLong(ocTenantKey))
                        .build()
        );
    }

    public void uploadPackage(String ocTenantKey, String version, CodePackageDto pkg) throws Exception {
        OcProcessCommitPo processCommitPo = OcProcessCommitPo.builder()
                .fileKey(pkg.getFilePath())
                .uploadType(pkg.getFileType())
                .processName(pkg.getName())
                .uploadVersion(version)
                .fileName(pkg.getFileName())
                .ocTenantId(Long.parseLong(ocTenantKey))
                .build();
        ocProcessCommitMapper.insert(processCommitPo);
    }

    public List<OcProcessReleaseDto> queryProcessReleaseInfo(String ocTenantKey, String processName, String version){
        return processReleaseMapper.selectProcessRelease(OcProcessReleaseFilterDto.builder()
                .ocTenantId(Long.parseLong(ocTenantKey))
                .processName(processName)
                .version(version)
                .build());
    }

    /**
     * 删除代码包
     */
    public void deletePackage(String ocTenantKey, String processName, String processVersion){
        ocProcessCommitMapper.deleteByProcessInfo(processName, Long.parseLong(ocTenantKey), processVersion);
    }

}
