package cd.casic.ci.process.engine.worker.target;


import cd.casic.ci.process.common.WorkAtom;
import cd.casic.ci.process.engine.runContext.TaskRunContext;
import cd.casic.ci.process.engine.worker.base.BaseWorker;
import cd.casic.ci.process.constant.PipelineTargetTypeConstant;
import cd.casic.ci.process.process.service.target.TargetVersionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 *  目标处理worker
 *
 * */
@Slf4j
@WorkAtom(taskType = PipelineTargetTypeConstant.SOURCE_CODE)
public class TargetSourceCodeWorker extends BaseWorker {
    @Resource
    private TargetVersionService targetVersionService;
    @Override
    public void execute(TaskRunContext context) {
        // 暂无获取文件方式 todo 先注释掉
//        String filePath = "";
//        Map<String, Object> localVariables = context.getLocalVariables();
//        PipBaseElement taskContextDef = context.getContextDef();
//        if (taskContextDef instanceof PipTask pipTask){
//            // 查询并下载目标文件
//            String pipelineId = pipTask.getPipelineId();
//            //根据流水线id查询流水线信息
//            PipPipeline pipeline = (PipPipeline) getContextManager().getContext(pipelineId).getContextDef();
//            //根据目标id查询目标信息
//            if (StringUtils.isEmpty(pipeline.getTargetVersionId())){
//                throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标文件不存在");
//            }
//
//            TargetVersion targetVersion = targetVersionService.getById(pipeline.getTargetVersionId());
//            filePath = targetVersion.getFilePath();
//            File file = new File(filePath);
//            if (!file.exists() || !file.canRead()) {
//                log.error("目标文件不存在或不可读");
//                localVariables.put("statusCode", "-1");
//                append(context,"目标文件不存在或不可读");
//                toBadEnding();
//            }
//            // 上传文件
//            ResourceFindResp resourceById = getResourceManagerService().findResourceById(pipeline.getResourceId());
//            PipResourceMachine resourceMachine = resourceById.getResourceMachine();
//            append(context,"开始文件上传");
//            try {
//                SftpUploadUtil.uploadFileViaSftp(resourceMachine.getMachineHost(),Integer.valueOf(resourceMachine.getSshPort()),resourceMachine.getUsername(), CryptogramUtil.doDecrypt(resourceMachine.getPassword()),null,file.getAbsolutePath(),"/home/casic/706/ai_test_527",file.getName());
//            } catch (SftpUploadUtil.SftpUploadException e) {
//                log.error("文件上传失败",e);
//                toBadEnding();
//            }
//            append(context,"文件上传至"+resourceMachine.getMachineHost()+"/home/casic/706/ai_test_527");
//        }
    }
}
