package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.domain.ModelTestTask;
import com.ruoyi.mapper.ModelTestTaskMapper;
import com.ruoyi.service.StrategyModelTestTask;
import com.ruoyi.web.response.ModelTestTaskViewResponse;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StrategyModelTestTaskPending implements StrategyModelTestTask, InitializingBean {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private StrategyModelTestTaskContext strategyModelTestTaskContext;

    @Autowired
    private ModelTestTaskMapper modelTestTaskMapper;

    @Autowired
    private StrategyModelTestTaskNew strategyModelTestTaskNew;

    @Override
    public List<ModelTestTaskViewResponse> doView(ModelTestTask modelTestTask) {
        List<ModelTestTaskViewResponse> list = strategyModelTestTaskNew.getTestScheme(modelTestTask.getId());
        return list;
    }

    @Override
    public void doStartTest(ModelTestTask modelTestTask) {
        throw new ServiceException("不能开始一个进行中的任务", HttpStatus.ERROR);
    }

    @Override
    public void doConfirmTest(ModelTestTask modelTestTask) {
        // 进行中的任务, 点击确认后, 状态改为待签字
        modelTestTaskMapper.update(new ModelTestTask(),
                new LambdaUpdateWrapper<ModelTestTask>()
                        .set(ModelTestTask::getTaskStatus, ModelTestTask.TASK_STATUS_SIGNED)
                        .eq(ModelTestTask::getId, modelTestTask.getId()));
    }

    @Override
    public void doSubmitTest(ModelTestTask modelTestTask, List<String> imagesUrl) {
        throw new ServiceException("不能提交一个进行中的任务", HttpStatus.ERROR);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        strategyModelTestTaskContext.putResource(ModelTestTask.TASK_STATUS_PENDING, applicationContext.getBean(this.getClass()));
    }
}