/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.workflow.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codvision.commoncore.utils.Preconditions;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.QueryPageUtil;
import com.codvision.workflow.entity.WorkFlowExpression;
import com.codvision.workflow.mapper.WorkFlowExpressionMapper;
import com.codvision.workflow.service.WorkFlowExpressionService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author hxl
 */
@Service
public class WorkFlowExpressionServiceImpl extends ServiceImpl<WorkFlowExpressionMapper, WorkFlowExpression> implements WorkFlowExpressionService {

    @Override
    public IPage<WorkFlowExpression> getPageResult(QueryPage<Map<String, Object>> queryPage) {
        return getBaseMapper().getPageResult(queryPage.getPage(), QueryPageUtil.queryMap(queryPage));
    }

    @Override
    public void addExpression(WorkFlowExpression expression) {
        WorkFlowExpression workFlowExpression = this.getOne(Wrappers.<WorkFlowExpression>lambdaQuery().eq(WorkFlowExpression::getName, expression.getName()));
        Preconditions.checkArgument(workFlowExpression == null,"名称已存在");
        this.save(expression);
    }

    @Override
    public void updateExpression(WorkFlowExpression expression) {
        Preconditions.checkArgument(expression.getId() != null,30002);

        WorkFlowExpression workFlowExpression = this.getById(expression.getId());
        Preconditions.checkArgument(workFlowExpression != null,30001);

        if (!workFlowExpression.getName().equals(expression.getName())) {
            workFlowExpression = this.getOne(Wrappers.<WorkFlowExpression>lambdaQuery().eq(WorkFlowExpression::getName, expression.getName()));
            Preconditions.checkArgument(workFlowExpression == null,"名称已存在");
        }

        this.updateById(expression);
    }

    @Override
    public void removeExpression(Integer id) {
        Preconditions.checkArgument(id != null,30002);

        WorkFlowExpression workFlowExpression = this.getById(id);
        Preconditions.checkArgument(workFlowExpression != null,30001);
        Preconditions.checkArgument(!workFlowExpression.getIsSystem(),"系统内置数据禁止删除");
        this.removeById(id);
    }
}
