package com.zzsmart.qomo.kn.cost.manage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostResultValue;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostResultValueMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostResultValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: app_scene_cost_result_value
 * @Author: jeecg-boot
 * @Date: 2024-07-22
 * @Version: V1.0
 */
@Service
public class AppSceneCostResultValueServiceImpl extends ServiceImpl<AppSceneCostResultValueMapper, AppSceneCostResultValue> implements IAppSceneCostResultValueService {
    @Autowired
    private AppSceneCostResultValueMapper appSceneCostResultValueMapper;

    /**
     * 根据顶级物料编码查询满足条件的结果值
     *
     * @param topMaterialCode
     * @param taskType
     * @param flowInstanceId
     * @return
     */
    @Override
    public List<AppSceneCostResultValue> getResultValueByTopMaterialNo(String topMaterialCode, String taskType, String flowInstanceId) {
        List<AppSceneCostResultValue> result = new ArrayList<>();
        QueryWrapper<AppSceneCostResultValue> queryWrapper = new QueryWrapper<>();
        if (topMaterialCode != null && !topMaterialCode.equals("") && taskType != null && !taskType.equals("") && flowInstanceId != null && !flowInstanceId.equals("")) {
            //查询某个流程节点（即执行任务）的结果值
            queryWrapper.eq("belong_top_material_no", topMaterialCode).eq("task_type", taskType).eq("flow_instance_id", flowInstanceId);
            result = appSceneCostResultValueMapper.selectList(queryWrapper);
        } else if (topMaterialCode != null && !topMaterialCode.equals("") && flowInstanceId != null && !flowInstanceId.equals("") && taskType == null) {
            //查询BOM物料所有的树节点信息
            queryWrapper.eq("belong_top_material_no", topMaterialCode).eq("flow_instance_id", flowInstanceId).isNull("task_type");
            result = appSceneCostResultValueMapper.selectList(queryWrapper);
        }
        return result;
    }

    /**
     * 查询根物料的某次成本计算流程下的所有结果值
     *
     * @param topMaterialCode
     * @param flowInstanceId
     * @return
     */
    @Override
    public List<AppSceneCostResultValue> getAllResultValueByTopMaterialNo(String topMaterialCode, String flowInstanceId) {
        List<AppSceneCostResultValue> result = new ArrayList<>();
        QueryWrapper<AppSceneCostResultValue> queryWrapper = new QueryWrapper<>();
        if (topMaterialCode != null && !topMaterialCode.equals("") && flowInstanceId != null && !flowInstanceId.equals("")) {
            //查询某个流程节点（即执行任务）的结果值
            queryWrapper.eq("belong_top_material_no", topMaterialCode).eq("flow_instance_id", flowInstanceId);
            result = appSceneCostResultValueMapper.selectList(queryWrapper);
        }
        return result;
    }
}
