package com.zd.laboratory.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zd.common.core.enums.HardwareTypeEnum;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.laboratory.config.HardwareFunctionStatusConfig;
import com.zd.laboratory.domain.LabHardware;
import com.zd.laboratory.domain.LabRiskPlanAbnormalGroup;
import com.zd.laboratory.domain.LabRiskPlanAbnormalLog;
import com.zd.laboratory.domain.vo.LabRiskPlanAbnormalGroupVO;
import com.zd.laboratory.mapper.LabHardwareMapper;
import com.zd.laboratory.mapper.LabRiskPlanAbnormalGroupMapper;
import com.zd.laboratory.mapper.LabRiskPlanAbnormalLogMapper;
import com.zd.laboratory.service.ILabExitLineService;
import com.zd.laboratory.service.ILabRiskPlanAbnormalGroupService;
import com.zd.system.api.laboratory.domain.SensorFunctionStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 查询风险异常组Service业务层处理
 *
 * @author cyl
 * @date 2021-10-09
 */
@Service
public class LabRiskPlanAbnormalGroupServiceImpl implements ILabRiskPlanAbnormalGroupService {
    @Autowired
    private LabRiskPlanAbnormalGroupMapper labRiskPlanAbnormalGroupMapper;

    @Autowired
    HardwareFunctionStatusConfig hardwareFunctionStatusConfig;
    @Autowired
    private LabHardwareMapper labHardwareMapper;
    @Autowired
    private LabRiskPlanAbnormalLogMapper labRiskPlanAbnormalLogMapper;

    @Autowired
    private ILabExitLineService labExitLineService;


    @Override
//    @DataScope(deptAlias = "t")
    public List <LabRiskPlanAbnormalGroupVO> selectLabRiskPlanAbnormalGroupList(LabRiskPlanAbnormalGroup labRiskPlanAbnormalGroup) {
        List <LabRiskPlanAbnormalGroupVO> groupList = labRiskPlanAbnormalGroupMapper.selectLabRiskPlanAbnormalGroupListByDept(labRiskPlanAbnormalGroup);

        Optional.ofNullable(groupList).filter(a->a.size()>0).orElseGet(Collections::emptyList).stream().forEach(a->{
            LabRiskPlanAbnormalLog labRiskPlanAbnormalLog = new LabRiskPlanAbnormalLog();
            labRiskPlanAbnormalLog.setGroupId(a.getId());
            List<LabRiskPlanAbnormalLog> abnormalLogList = labRiskPlanAbnormalLogMapper.selectLabRiskPlanAbnormalLogOrderByList(labRiskPlanAbnormalLog);
            StringBuffer sensorStr = new StringBuffer();
            Optional.ofNullable(abnormalLogList).orElseGet(Collections::emptyList).stream().filter(b->b.getRiskStatus()==1).forEach(b->{
                JSONArray sensorList = JSONObject.parseArray(b.getSensorJson());
                for(int x=0;x<sensorList.size();x++){
                    JSONObject jsonObj=sensorList.getJSONObject(x);
                    sensorStr.append(jsonObj.get("describe")+":"+jsonObj.get("value")+""+jsonObj.get("unit")+"  ");
                    if(StringUtils.isNotNull(jsonObj.get("funNum")) && (jsonObj.get("funNum")+"").equals("huoyan")){
                        if(!a.getClosePlan()){
                            a.setClosePlan(true);
                        }
                    }
                }
            });
            a.setSensorStr(sensorStr.toString());

            //根据实验室id和设备类型查询硬件列表
            LabHardware labHardware = new LabHardware();
            labHardware.setSubjectId(a.getSubjectId());
            labHardware.setType(HardwareTypeEnum.SWITCH);
            List<LabHardware> hardwareList = labHardwareMapper.selectLabHardwareListBySubject(labHardware);
            a.setHardwareList(hardwareList);

            //todo 这里查看预案下是不是有疏散线路，返回true和false
            try{
                labExitLineService.getRoute(a.getSubjectId(),0);
                a.setWhetherRoute(true);
            }catch (Exception e){
                a.setWhetherRoute(false);
            }

        });
        return groupList;
    }


    /**
     * 查询实验室当前预警测点功能
     *
     * @return
     * @param subId
     */
    @Override
    public List<SensorFunctionStatus> queryWranFunNumBySubId(Long subId) {
        List<String> strings = labRiskPlanAbnormalGroupMapper.queryWranFunNumBySubId(subId);
        final Map<String, SensorFunctionStatus> mapFunNum = hardwareFunctionStatusConfig.getMapFunNum();

        return Optional.ofNullable(strings)
                .filter(a -> a.size() > 0)
                .orElseGet(Collections::emptyList)
                .stream()
                .distinct()
                .map(a -> mapFunNum.get(a))
                .collect(Collectors.toList());
    }
}
