package com.njcn.csdevice.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.csdevice.mapper.PqsDeviceUnitMapper;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.service.CsCommTerminalService;
import com.njcn.csdevice.service.CsDeviceUserPOService;
import com.njcn.csdevice.service.CsEquipmentDeliveryService;
import com.njcn.csdevice.service.CsLinePOService;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.constant.UserType;
import com.njcn.user.pojo.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @Author: cdf
 * @CreateTime: 2025-12-23
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class CsCommTerminalServiceImpl implements CsCommTerminalService {

    private final PqsDeviceUnitMapper pqsDeviceUnitMapper;
    private final CsDeviceUserPOService csDeviceUserPOService;
    private final CsLinePOService csLinePOService;
    private final UserFeignClient userFeignClient;
    private final CsEquipmentDeliveryService csEquipmentDeliveryService;


    @Override
    public List<String> getLineIdsByUser(String userId) {
        List<String> result = new ArrayList<>();
        List<String> devIds = commGetDevIds(userId);
        if (CollUtil.isEmpty(devIds)) {
            return result;
        }
        List<CsLinePO> poList = csLinePOService.lambdaQuery().select(CsLinePO::getLineId).in(CsLinePO::getDeviceId, devIds)
                .eq(CsLinePO::getStatus, DataStateEnum.ENABLE.getCode()).list();
        if (CollUtil.isNotEmpty(poList)) {
            result = poList.stream().map(CsLinePO::getLineId).distinct().collect(Collectors.toList());
        }
        return result;
    }

    @Override
    public List<String> getPqUserIdsByUser(String userId) {
        List<String> result = new ArrayList<>();
        List<String> devIds = commGetDevIds(userId);
        if (CollUtil.isEmpty(devIds)) {
            return result;
        }
        List<CsLinePO> poList = csLinePOService.lambdaQuery().select(CsLinePO::getLineId, CsLinePO::getMonitorUser).in(CsLinePO::getDeviceId, devIds)
                .eq(CsLinePO::getStatus, DataStateEnum.ENABLE.getCode()).list();
        if (CollUtil.isNotEmpty(poList)) {
            result = poList.stream().map(CsLinePO::getMonitorUser).distinct().collect(Collectors.toList());
        }
        return result;
    }

    @Override
    public PqsDeviceUnit lineUnitDetail(String lineId) {
        PqsDeviceUnit pqsDeviceUnit = pqsDeviceUnitMapper.selectById(lineId);
        if(Objects.isNull(pqsDeviceUnit)){
            pqsDeviceUnit = new PqsDeviceUnit();
        }
        return pqsDeviceUnit;
    }


    @Override
    public List<String> commGetDevIds(String userId) {
        UserVO userVO = userFeignClient.getUserById(userId).getData();
        List<String> devIds;
        if (userVO.getType().equals(UserType.SUPER_ADMINISTRATOR) || userVO.getType().equals(UserType.ADMINISTRATOR)) {
            devIds = csEquipmentDeliveryService.getAll().stream().map(CsEquipmentDeliveryPO::getId).collect(Collectors.toList());
        } else {
            List<CsDeviceUserPO> devList = csDeviceUserPOService.lambdaQuery().select(CsDeviceUserPO::getDeviceId)
                    .and(w -> w.eq(CsDeviceUserPO::getPrimaryUserId, userId).or().eq(CsDeviceUserPO::getSubUserId, userId))
                    .eq(CsDeviceUserPO::getStatus, DataStateEnum.ENABLE.getCode()).list();
            devIds = devList.stream().map(CsDeviceUserPO::getDeviceId).distinct().collect(Collectors.toList());
        }
        return devIds;
    }
}
