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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.harmonic.pojo.po.PmsMonitorPO;
import com.njcn.prepare.harmonic.constant.PqsPrepareExceptionEnum;
import com.njcn.prepare.harmonic.mapper.mysql.area.PmsMonitorPOMapper;
import com.njcn.prepare.harmonic.service.mysql.area.PmsMonitorPOService;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.user.api.DeptFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

import static com.njcn.system.pojo.constant.DicDataConstant.NAME_KEY;

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

    private final RedisUtil redisUtil;

    private final PmsMonitorPOMapper pmsMonitorPOMapper;

    private final DeptFeignClient deptFeignClient;

    /**
     * @Description: 在运监测点集合
     * @Param: [orgid]
     * @return: java.lang.Integer
     * @Author: clam
     * @Date: 2022/11/25
     */
    @Override
    public List<Monitor> qurymeasurementRunPoints(String orgid) {
        LambdaQueryWrapper<Monitor> monitorPOLambdaQueryWrapper = new LambdaQueryWrapper ( );
        /*获取当前部门及子部门*/
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(orgid).getData();
        monitorPOLambdaQueryWrapper.in (Monitor::getOrgId, deptIds);
        String state = redisUtil.getStringByKey (NAME_KEY+
                StrUtil.DASHED + DicDataTypeEnum.LINE_STATE.getCode ( )+
                StrUtil.DASHED+ DicDataEnum.RUN.getCode ( ));
        if(StringUtils.isEmpty (state)){
            throw  new BusinessException (PqsPrepareExceptionEnum.REDIS_NULL_EXCEPTION);
        }
        monitorPOLambdaQueryWrapper.eq (Monitor::getMonitorState, state);
        List<Monitor> pmsMonitorPOS = pmsMonitorPOMapper.selectList (monitorPOLambdaQueryWrapper);
        return pmsMonitorPOS;
    }
}
