package com.njcn.prepare.harmonic.service.mysql.Impl.area;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.device.biz.pojo.dto.DeptGetDeviceDTO;
import com.njcn.device.biz.pojo.dto.LineDevGetDTO;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.harmonic.pojo.po.PmsTerminalPO;
import com.njcn.prepare.harmonic.mapper.mysql.area.PmsTerminalPOMapper;
import com.njcn.prepare.harmonic.service.mysql.area.PmsTerminalPOService;
import com.njcn.user.api.DeptFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/11/29  15:53【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class PmsTerminalPOServiceImpl extends ServiceImpl<PmsTerminalPOMapper, PmsTerminalPO> implements PmsTerminalPOService {


    private final PmsTerminalPOMapper pmsTerminalPOMapper;
    private final DeptFeignClient deptFeignClient;
    private final CommTerminalGeneralClient commTerminalGeneralClient;

    /**
     * @Description: 监测终端集合
     * @Param: [orgid]
     * @return: java.util.List<com.njcn.prepare.harmonic.pojo.po.PmsTerminalPO>
     * @Author: clam
     * @Date: 2022/11/25
     */
    @Override
    public List<PmsTerminalPO> queryOnlineMeasurementPoints(String orgid) {
        List<PmsTerminalPO> list = new ArrayList<>();
        LambdaQueryWrapper<PmsTerminalPO> wrapper = new LambdaQueryWrapper();
        /*获取当前部门及子部门*/
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(orgid).getData();

        wrapper.in(PmsTerminalPO::getOrgId, deptIds);
        list = pmsTerminalPOMapper.selectList(wrapper);

        return list;
    }

    @Override
    public List<LineDevGetDTO> getRunTerminalAndLine(String orgId) {
        DeptGetLineParam deptGetLineParam=new DeptGetLineParam();
        deptGetLineParam.setDeptId(orgId);
        List<DeptGetDeviceDTO> data = commTerminalGeneralClient.deptGetDeviceAndMonitor(deptGetLineParam).getData();
        return data.stream().flatMap(x -> x.getDeviceList().stream()).distinct().collect(Collectors.toList());
    }
}
