package cn.axzo.workflow.core.service.impl;

import cn.axzo.basics.common.BeanMapper;
import cn.axzo.workflow.common.model.response.bpmn.model.BpmnModelExtVO;
import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import cn.axzo.workflow.core.repository.entity.ExtAxReModel;
import cn.axzo.workflow.core.repository.mapper.ExtAxReModelMapper;
import cn.axzo.workflow.core.service.ExtAxReModelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static cn.axzo.workflow.core.common.code.BpmnModelRespCode.MODEL_ID_NOT_EXISTS;


/**
 * 模型扩展表操作服务实现
 *
 * @author wangli
 * @since 2023/11/18 15:09
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ExtAxAxReModelServiceImpl implements ExtAxReModelService {
    @Resource
    private ExtAxReModelMapper extAxReModelMapper;

    @Override
    public List<ExtAxReModel> listByModelIds(List<String> modelIds) {
        return extAxReModelMapper.selectList(new QueryWrapper<ExtAxReModel>().in("model_id", modelIds));
    }

    @Override
    public void create(String modeId) {
        ExtAxReModel extAxReModel = new ExtAxReModel();
        extAxReModel.setModelId(modeId);
        extAxReModelMapper.insert(extAxReModel);
    }

    public void update(String modelId, Integer status) {
        ExtAxReModel extAxReModel = new ExtAxReModel();
        extAxReModel.setStatus(status);
        extAxReModelMapper.update(extAxReModel, new QueryWrapper<ExtAxReModel>().eq("model_id", modelId));
    }

    @Override
    public void changeStatus(String modelId, Integer status) {
        ExtAxReModel reModel = extAxReModelMapper.selectOne(new QueryWrapper<ExtAxReModel>().eq("model_id", modelId));
        if (Objects.isNull(reModel)) {
            throw new WorkflowEngineException(MODEL_ID_NOT_EXISTS, reModel.getModelId());
        }
        reModel.setStatus(status);
        extAxReModelMapper.updateById(reModel);
    }

    @Override
    public BpmnModelExtVO getStatusByModelId(String modelId) {
        ExtAxReModel model = extAxReModelMapper.selectOne(new QueryWrapper<ExtAxReModel>().eq("model_id", modelId));
        return BeanMapper.copyBean(model, BpmnModelExtVO.class);
    }
}
