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

import cn.axzo.basics.common.BeanMapper;
import cn.axzo.workflow.common.enums.ExtModelStateFieldEnum;
import cn.axzo.workflow.common.exception.WorkflowEngineException;
import cn.axzo.workflow.common.model.request.bpmn.print.PrintTemplateConfigUpsertDTO;
import cn.axzo.workflow.common.model.response.bpmn.model.BpmnModelExtVO;
import cn.axzo.workflow.common.model.response.print.PrintModelDTO;
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 org.springframework.util.StringUtils;

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

import static cn.axzo.workflow.common.code.BpmnModelRespCode.MODEL_NOT_EXISTS;


/**
 * 模型扩展表操作服务实现
 *
 * @author wangli
 * @since 2023/11/18 15:09
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ExtAxReModelServiceImpl 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, ExtModelStateFieldEnum field) {
        ExtAxReModel reModel = extAxReModelMapper.selectOne(new QueryWrapper<ExtAxReModel>().eq("model_id", modelId));
        if (Objects.isNull(reModel)) {
            throw new WorkflowEngineException(MODEL_NOT_EXISTS);
        }
        if (Objects.equals(field, ExtModelStateFieldEnum.status)) {
            reModel.setStatus(status);
        }
        if (Objects.equals(field, ExtModelStateFieldEnum.printStatus)) {
            reModel.setPrintStatus(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);
    }

    @Override
    public void printTemplateConfig(PrintTemplateConfigUpsertDTO dto) {
        ExtAxReModel model = extAxReModelMapper.selectOne(new QueryWrapper<ExtAxReModel>().eq("model_id", dto.getModelId()));
        if (Objects.isNull(model)) {
            throw new WorkflowEngineException(MODEL_NOT_EXISTS);
        }
        if (Objects.nonNull(dto.getPrintFileName())) {
            model.setPrintFileName(dto.getPrintFileName());
        }
        if (Objects.nonNull(dto.getPrintTemplateName())) {
            model.setPrintTemplateName(dto.getPrintTemplateName());
        }
        if (Objects.nonNull(dto.getPrintTemplateConfig())) {
            model.setPrintTemplateConfig(dto.getPrintTemplateConfig());
        }
        extAxReModelMapper.updateById(model);
    }

    @Override
    public Boolean hasPrintTemplateConfig(String modelId) {
        ExtAxReModel model = extAxReModelMapper.selectOne(new QueryWrapper<ExtAxReModel>().eq("model_id", modelId));
        return Objects.equals(1, model.getPrintStatus()) && StringUtils.hasText(model.getPrintTemplateConfig());
    }

    @Override
    public PrintModelDTO getPrintTemplateConfig(String modelId) {
        ExtAxReModel model = extAxReModelMapper.selectOne(new QueryWrapper<ExtAxReModel>().eq("model_id", modelId));
        if (Objects.isNull(model)) {
            throw new WorkflowEngineException(MODEL_NOT_EXISTS);
        }
        return PrintModelDTO.builder()
                .printFileName(model.getPrintFileName())
                .printTemplateName(model.getPrintTemplateName())
                .printTemplateConfig(model.getPrintTemplateConfig())
                .build();
    }
}
