package cn.iocoder.yudao.module.bpm.service.business;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.bpm.controller.admin.business.vo.ModelConfigPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.business.vo.ModelConfigSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.business.ModelConfigDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.business.ModelConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.constant.BpmConstant.BPM_PROCESS_KEY_FORMAT;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.MODEL_CONFIG_NOT_EXISTS;

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/15  14:47
 * @Description:
 * @Version: 4.0
 */

@Service
@Validated
public class ModelConfigServiceImpl implements ModelConfigService {

    @Resource
    private ModelConfigMapper modelConfigMapper;

    @Override
    public Long createModelConfig(ModelConfigSaveReqVO createReqVO) {
        // 插入
        ModelConfigDO modelConfig = BeanUtils.toBean(createReqVO, ModelConfigDO.class);
        modelConfigMapper.insert(modelConfig);
        // 返回
        return modelConfig.getId();
    }

    @Override
    public void updateModelConfig(ModelConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateModelConfigExists(updateReqVO.getId());
        // 更新
        ModelConfigDO updateObj = BeanUtils.toBean(updateReqVO, ModelConfigDO.class);
        modelConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteModelConfig(Long id) {
        // 校验存在
        validateModelConfigExists(id);
        // 删除
        modelConfigMapper.deleteById(id);
    }

    private void validateModelConfigExists(Long id) {
        if (modelConfigMapper.selectById(id) == null) {
            throw exception(MODEL_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public ModelConfigDO getModelConfig(Long id) {
        return modelConfigMapper.selectById(id);
    }

    @Override
    public PageResult<ModelConfigDO> getModelConfigPage(ModelConfigPageReqVO pageReqVO) {
        return modelConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteModelConfigByModelId(String modelId) {
        modelConfigMapper.delete(new LambdaQueryWrapperX<ModelConfigDO>().eq(ModelConfigDO::getModelId, modelId));
    }

    @Override
    @TenantIgnore
    public Integer getModelStatus(String modelKey) {
        ModelConfigDO modelConfigDO = modelConfigMapper.selectOne(new LambdaQueryWrapperX<ModelConfigDO>()
                .eq(ModelConfigDO::getModelKey, modelKey)
                .eqIfPresent(ModelConfigDO::getTenantId, TenantContextHolder.getTenantId())
                .eq(ModelConfigDO::getDeleted, 0));
        if (null == modelConfigDO) {
            return 1;
        }
        return modelConfigDO.getStatus();
    }
}
