package com.willalp.flow.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.flow.domain.HsFlowConditionModel;
import com.willalp.flow.mapper.HsFlowConditionModelMapper;
import com.willalp.flow.service.IHsFlowConditionModelService;
import org.springframework.stereotype.Service;

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

/**
 * 流程条件组件Service业务层处理
 *
 * @author willalp
 * @date 2022-10-14
 */
@Service
public class HsFlowConditionModelServiceImpl extends ServiceImpl<HsFlowConditionModelMapper, HsFlowConditionModel> implements IHsFlowConditionModelService {
    @Resource
    private HsFlowConditionModelMapper hsFlowConditionModelMapper;

    /**
     * 查询流程条件组件列表
     *
     * @param hsFlowConditionModel 流程条件组件
     * @return 流程条件组件
     */
    @Override
    public List<HsFlowConditionModel> selectHsFlowConditionModelList(HsFlowConditionModel hsFlowConditionModel) {
        return hsFlowConditionModelMapper.selectHsFlowConditionModelList(hsFlowConditionModel);
    }

    @Override
    public boolean executeConditionJudgment(HsFlowConditionModel fcm, Object obj) {
        if (null != fcm) {
            if (null != fcm.getConditionType() && null != fcm.getConditionChar()) {
                if ("1".equals(fcm.getConditionType())) {
                    try {
                        Double aDouble = Double.valueOf(fcm.getThresholdValue());
                        Double objDbl = Double.parseDouble(obj.toString());
                        switch (fcm.getConditionChar()) {
                            case ">":
                                return objDbl > aDouble;
                            case "<":
                                return objDbl < aDouble;
                            case ">=":
                                return objDbl >= aDouble;
                            case "<=":
                                return objDbl <= aDouble;
                            case "!=":
                                return !objDbl.equals(aDouble);
                            case "==":
                                return objDbl.equals(aDouble);
                            default:
                                throw new BaseException("条件组件配置错误");
                        }
                    } catch (Exception e) {
                        throw new BaseException("条件组件配置错误");
                    }
                } else {
                    return StringUtils.equals(String.valueOf(obj), fcm.getThresholdValue());
                }
            }
        }
        return false;
    }
}
