package com.njcn.energy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.energy.mapper.LogicAccessMapper;
import com.njcn.energy.pojo.constant.ModelState;
import com.njcn.energy.pojo.po.LogicAccess;
import com.njcn.energy.service.ILogicAccessService;
import com.njcn.energy.service.INetDevService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2022/3/23 21:56
 */
@Service
@AllArgsConstructor
public class LogicAccessServiceImpl extends ServiceImpl<LogicAccessMapper, LogicAccess> implements ILogicAccessService {

    private final INetDevService netDevService;

    @Override
    public List<LogicAccess> findAccessDev(List<String> did) {
        return this.lambdaQuery().in(LogicAccess::getDid,did).ne(LogicAccess::getState, DataStateEnum.DELETED.getCode()).list();
    }

    @Override
    public boolean add(LogicAccess logicAccess) {
        return this.save(logicAccess);
    }

    @Override
    public LogicAccess getAccessDev(String did) {
        List<Integer> list = Stream.of(1,2).collect(Collectors.toList());
        return this.lambdaQuery().eq(LogicAccess::getDid,did).in(LogicAccess::getState,list).one();
    }

    @Override
    public void deleteByDid(List<String> list) {
        this.lambdaUpdate()
                .set(LogicAccess::getState, ModelState.UNREGISTER)
                .in(LogicAccess::getDid, list)
                .update();
    }

    @Override
    public void offlineByDid(List<String> list) {
        this.lambdaUpdate()
                .set(LogicAccess::getState, ModelState.REGISTER)
                .in(LogicAccess::getDid, list)
                .update();
    }

    @Override
    public LogicAccess findCancelAccessDev(String did) {
        return this.lambdaQuery().eq(LogicAccess::getDid,did).eq(LogicAccess::getState,DataStateEnum.DELETED.getCode()).one();
    }

    @Override
    public void accessCancelDev(String did) {
        this.lambdaUpdate()
                .set(LogicAccess::getState, DataStateEnum.ENABLE.getCode())
                .eq(LogicAccess::getDid, did)
                .update();
    }

    @Override
    public void accessDev(LogicAccess logicAccess) {
        this.updateById(logicAccess);
    }

    @Override
    public List<LogicAccess> getLogicAccess(String ndid, List<String> list) {
        String pid = netDevService.findByNdid(ndid).getId();
        return this.lambdaQuery().in(LogicAccess::getDid,list).eq(LogicAccess::getState,ModelState.ACCESS).eq(LogicAccess::getPid,pid).list();
    }

    @Override
    public List<LogicAccess> getAllOnlineDevice(String ndid) {
        String pid = netDevService.findByNdid(ndid).getId();
        return this.lambdaQuery().eq(LogicAccess::getState,ModelState.ACCESS).eq(LogicAccess::getPid,pid).list();
    }

    @Override
    public List<LogicAccess> getAllOfflineDevice(String ndid) {
        String pid = netDevService.findByNdid(ndid).getId();
        return this.lambdaQuery().eq(LogicAccess::getState,ModelState.REGISTER).eq(LogicAccess::getPid,pid).list();
    }

    @Override
    public void offAllDevice(String ndid) {
        String pid = netDevService.findByNdid(ndid).getId();
        this.lambdaUpdate()
                .set(LogicAccess::getState, ModelState.REGISTER)
                .eq(LogicAccess::getState, ModelState.ACCESS)
                .eq(LogicAccess::getPid, pid)
                .update();
    }

    @Override
    public List<LogicAccess> getOnlineByList(List<String> list) {
        return this.lambdaQuery().in(LogicAccess::getDid,list).eq(LogicAccess::getState,ModelState.ACCESS).list();
    }

    @Override
    public List<LogicAccess> getLogicDevByList(List<String> list) {
        return this.lambdaQuery().in(LogicAccess::getPid,list).and(item->{item.eq(LogicAccess::getState,ModelState.REGISTER).or().eq(LogicAccess::getState,ModelState.ACCESS);}).list();
    }

    @Override
    public List<LogicAccess> getAllDev() {
        return this.lambdaQuery().eq(LogicAccess::getState,ModelState.ACCESS).or().eq(LogicAccess::getState,ModelState.REGISTER).list();
    }
}
