package com.dcs.hnyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcs.common.enums.GeneralStatus;
import com.dcs.hnyz.domain.Equipment;
import com.dcs.hnyz.domain.Flow;
import com.dcs.hnyz.mapper.FlowMapper;
import com.dcs.hnyz.service.IEquipmentService;
import com.dcs.hnyz.service.IFlowService;
import org.springframework.stereotype.Service;

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

/**
 * 流程Service业务层处理
 * 
 * @author hmy
 * @date 2025-03-08
 */
@Service
public class FlowServiceImpl implements IFlowService
{
    @Resource
    private FlowMapper flowMapper;
    @Resource
    private IEquipmentService equipmentService;

    /**
     * 查询流程
     * 
     * @param flowId 流程主键
     * @return 流程
     */
    @Override
    public Flow selectFlowByFlowId(Long flowId)
    {
        return flowMapper.selectById(flowId);
    }

    /**
     * 查询流程列表
     * 
     * @param flow 流程
     * @return 流程
     */
    @Override
    public List<Flow> selectFlowList(Flow flow)
    {
        return flowMapper.selectFlowList(flow);
    }

    /**
     * 新增流程
     * 
     * @param flow 流程
     * @return 结果
     */
    @Override
    public int insertFlow(Flow flow)
    {
        return flowMapper.insert(flow);
    }

    /**
     * 修改流程
     * 
     * @param flow 流程
     * @return 结果
     */
    @Override
    public int updateFlow(Flow flow)
    {
        return flowMapper.updateById(flow);
    }

    /**
     * 批量删除流程
     * 
     * @param flowIds 需要删除的流程主键
     * @return 结果
     */
    @Override
    public int deleteFlowByFlowIds(Long[] flowIds)
    {
        return flowMapper.deleteBatchIds(Arrays.asList(flowIds));
    }

    /**
     * 删除流程信息
     * 
     * @param flowId 流程主键
     * @return 结果
     */
    @Override
    public int deleteFlowByFlowId(Long flowId)
    {
        return flowMapper.deleteById(flowId);
    }

    public String selectFlowNameByFlowId(Long flowId) {
        LambdaQueryWrapper<Flow> wrapper=new LambdaQueryWrapper<>();
        //根据flowId查找flowName
        wrapper.eq(Flow::getFlowId,flowId)
                .select(Flow::getFlowName);
        Flow flow = flowMapper.selectOne(wrapper);
        return flow != null ? flow.getFlowName() : null;
    }

    /**
     * 通过设备id获取流程列表
     * @param equipmentId
     * @return
     */
    @Override
    public List<Flow> getFlowList(Long equipmentId,String flowType) {
        LambdaQueryWrapper<Flow> wrapper=new LambdaQueryWrapper<>();
        if(flowType!=null){
            wrapper.eq(Flow::getFlowType,flowType);
        }
        if(equipmentId==null){//不传设备id时默认查全部
            return flowMapper.selectList(wrapper);
        }
        Equipment e = equipmentService.selectEquipmentByEquipmentId(equipmentId);
        String flowIds = e.getFlowIds();//获取流程ids
        List<Flow> list=new ArrayList<>();
        for (String s : flowIds.split(",")) {
            Flow flow = flowMapper.selectById(Long.valueOf(s));
            list.add(flow);
        }
        return list;
    }
}
