package com.fjhx.service.process.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.constants.FlowExplainConstant;
import com.fjhx.entity.process.ProcessInfo;
import com.fjhx.mapper.process.ProcessInfoMapper;
import com.fjhx.service.process.ProcessInfoService;
import com.fjhx.utils.Assert;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-08-12
 */
@Service
public class ProcessInfoServiceImpl extends ServiceImpl<ProcessInfoMapper, ProcessInfo> implements ProcessInfoService {

    @Override
    public List<ProcessInfo> getList(Map<String, String> condition) {

        String serviceName = condition.get("serviceName");

        LambdaQueryWrapper<ProcessInfo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(serviceName), ProcessInfo::getServiceName, serviceName);

        return list(wrapper);
    }

    @Override
    public Page<ProcessInfo> getPage(Map<String, String> condition) {

        String serviceName = condition.get("serviceName");
        String moduleName = condition.get("keyword");

        LambdaQueryWrapper<ProcessInfo> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(ObjectUtil.isNotEmpty(serviceName), ProcessInfo::getServiceName, serviceName);
        wrapper.eq(ObjectUtil.isNotEmpty(moduleName), ProcessInfo::getModuleName, moduleName);

        return page(createPage(condition), wrapper);
    }

    @Override
    public void add(ProcessInfo processInfo) {

        // 编码非空
        String code = processInfo.getCode();
        Assert.notEmpty(code, FlowExplainConstant.CODE_EMPTY);

        // 服务名称非空
        String serviceName = processInfo.getServiceName();
        Assert.notEmpty(serviceName, FlowExplainConstant.SERVICE_NAME_EMPTY);

        // 模块名称非空
        String moduleName = processInfo.getModuleName();
        Assert.notEmpty(moduleName, FlowExplainConstant.MODULE_NAME_EMPTY);

        // 验证编码唯一性
        Long count = count(ProcessInfo::getCode, processInfo.getCode());
        Assert.eqZero(count, FlowExplainConstant.PROCESS_EXIST);

        // 创建流程
        save(processInfo);

    }

}
