package com.njcn.csdevice.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njcn.csdevice.mapper.*;
import com.njcn.csdevice.pojo.po.*;
import com.njcn.csdevice.service.RoleEngineerDevService;
import com.njcn.user.enums.AppRoleEnum;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * Description:
 * Date: 2023/6/27  9:28【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@RequiredArgsConstructor
@Service
public class RoleEngineerDevServiceImpl implements RoleEngineerDevService {

    private final CsEngineeringUserMapper csEngineeringUserMapper;
    private final CsDeviceUserPOMapper csDeviceUserPOMapper;
    private final CsLedgerMapper csLedgerMapper;
    private final CsEngineeringMapper csEngineeringMapper;
    private final CsTouristDataPOMapper csTouristDataPOMapper;
    private final CsMarketDataMapper csMarketDataMapper;
    @Override
    public List<String> getRoleengineer(){
        String role = RequestUtil.getUserRole();
        List<String> strings = JSONArray.parseArray(role, String.class);
        if(CollectionUtils.isEmpty(strings)){
           return new ArrayList<>();
        }
        role=strings.get(0);
        String userIndex = RequestUtil.getUserIndex();
        QueryWrapper<CsEngineeringUserPO> csEngineeringUserPOQueryWrapper = new QueryWrapper<> ();
        QueryWrapper<CsDeviceUserPO> csDeviceUserPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();

        List<String> collect = new ArrayList<>();
        if ( Objects.equals(role,AppRoleEnum.MARKET_USER.getCode())||
                Objects.equals(role, AppRoleEnum.ENGINEERING_USER.getCode())) {
            QueryWrapper<CsMarketData> csMarketDataQueryWrapper = new QueryWrapper<>();
            csMarketDataQueryWrapper.eq("user_id", userIndex);
            List<CsMarketData> csMarketData = csMarketDataMapper.selectList(csMarketDataQueryWrapper);
            collect = csMarketData.stream().map(CsMarketData::getEngineerId).collect(Collectors.toList());

        } else if (Objects.equals(role,AppRoleEnum.TOURIST.getCode())) {
            //todo查询配置的游客工程
            List<CsTouristDataPO> csTouristDataPOS = csTouristDataPOMapper.selectList(null);
            collect = csTouristDataPOS.stream().map(CsTouristDataPO::getEnginerId).distinct().collect(Collectors.toList());

        }
        // ||Objects.equals(role,"bxs_user")
        else if(Objects.equals(role,AppRoleEnum.ROOT.getCode()) || Objects.equals(role,AppRoleEnum.OPERATION_MANAGER.getCode())){
            List<CsEngineeringPO> csEngineeringPOS = csEngineeringMapper.selectList(null);
            collect =csEngineeringPOS.stream().filter(temp->Objects.equals(temp.getStatus(),"1")).map(CsEngineeringPO::getId).collect(Collectors.toList());
        } else {
            csDeviceUserPOQueryWrapper.clear();
            csEngineeringUserPOQueryWrapper.clear();
            csLedgerQueryWrapper.clear();
            csEngineeringUserPOQueryWrapper.lambda().eq(CsEngineeringUserPO::getUserId,userIndex);
            List<CsEngineeringUserPO> csEngineeringUserPOS = csEngineeringUserMapper.selectList(csEngineeringUserPOQueryWrapper);
            collect = csEngineeringUserPOS.stream().map(CsEngineeringUserPO::getEngineeringId).collect(Collectors.toList());
            csDeviceUserPOQueryWrapper.eq("status","1").and(wq -> {
                wq.eq("primary_user_id", userIndex)
                        .or()
                        .eq("sub_user_id",userIndex);
            });
            List<CsDeviceUserPO> csDeviceUserPOS = csDeviceUserPOMapper.selectList(csDeviceUserPOQueryWrapper);
            List<String> collect1 = csDeviceUserPOS.stream().map(CsDeviceUserPO::getDeviceId).distinct().collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(collect1)) {
                csLedgerQueryWrapper.in("id", collect1).eq("level", 2);
                List<CsLedger> csLedgers = csLedgerMapper.selectList(csLedgerQueryWrapper);
                List<String> collect2 = csLedgers.stream().map(CsLedger::getPid).distinct().collect(Collectors.toList());
                csLedgerQueryWrapper.clear();
                if (CollectionUtils.isEmpty(collect2)) {
                    return collect;
                }
                csLedgerQueryWrapper.in("id", collect2).eq("level", 1);
                csLedgers = csLedgerMapper.selectList(csLedgerQueryWrapper);
                List<String> collect3 = csLedgers.stream().map(CsLedger::getPid).distinct().collect(Collectors.toList());
                if (CollectionUtils.isEmpty(collect3)) {
                    return collect;
                }
                collect.addAll(collect3);
                collect = collect.stream().distinct().collect(Collectors.toList());
            }
            return collect;
        }

        return collect;
    }
    @Override
    public List<String> getDevice(){
        String role = RequestUtil.getUserRole();
        List<String> strings = JSONArray.parseArray(role, String.class);
        if(CollectionUtils.isEmpty(strings)){
            return new ArrayList<>();
        }
        role=strings.get(0);
        String userIndex = RequestUtil.getUserIndex();
        QueryWrapper<CsEngineeringUserPO> csEngineeringUserPOQueryWrapper = new QueryWrapper<> ();
        QueryWrapper<CsDeviceUserPO> csDeviceUserPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();

        List<String> collect = new ArrayList<>();
        //fix 新需求 工程用户可以看到关注工程的设备和对应的主用户和子用户的设备
        //note 这边工程用户不查询主设备，只看关注的工程，做下调整，不然会出现工程用户未关注任何工程，但是有一台主设备，导致界面有数据
        if (Objects.equals(role, AppRoleEnum.ENGINEERING_USER.getCode())) {
            List<String> sumDevId = new ArrayList<>();
            csDeviceUserPOQueryWrapper.clear();
            QueryWrapper<CsMarketData> csMarketDataQueryWrapper = new QueryWrapper<>();
            csMarketDataQueryWrapper.eq("user_id", userIndex);
            List<CsMarketData> csMarketData = csMarketDataMapper.selectList(csMarketDataQueryWrapper);
            List<String> collect2 = csMarketData.stream().map(CsMarketData::getEngineerId).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(collect2)){
                return sumDevId;
            }
            csLedgerQueryWrapper.clear();
            csLedgerQueryWrapper.lambda().eq(CsLedger::getLevel, 1).eq(CsLedger::getState, 1).in(CsLedger::getPid, collect2);
            List<CsLedger> csLedgers = csLedgerMapper.selectList(csLedgerQueryWrapper);
            List<String>collect1 = csLedgers.stream().map(CsLedger::getId).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(collect1)){
                return sumDevId;
            }
            csLedgerQueryWrapper.clear();
            csLedgerQueryWrapper.lambda().eq(CsLedger::getLevel, 2).eq(CsLedger::getState, 1).in(CsLedger::getPid, collect1);
            List<CsLedger> csLedgers1 = csLedgerMapper.selectList(csLedgerQueryWrapper);

            collect = csLedgers1.stream().map(CsLedger::getId).collect(Collectors.toList());
            sumDevId.addAll(collect);
            sumDevId = sumDevId.stream().distinct().collect(Collectors.toList());

            return sumDevId;

        } else if ( Objects.equals(role,AppRoleEnum.MARKET_USER.getCode())) {
            QueryWrapper<CsMarketData> csMarketDataQueryWrapper = new QueryWrapper<>();
            csMarketDataQueryWrapper.eq("user_id", userIndex);
            List<CsMarketData> csMarketData = csMarketDataMapper.selectList(csMarketDataQueryWrapper);
            List<String> collect2 = csMarketData.stream().map(CsMarketData::getEngineerId).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(collect2)){
                return collect;
            }
            csLedgerQueryWrapper.clear();
            csLedgerQueryWrapper.lambda().eq(CsLedger::getLevel, 1).eq(CsLedger::getState, 1).in(CsLedger::getPid, collect2);
            List<CsLedger> csLedgers = csLedgerMapper.selectList(csLedgerQueryWrapper);
            List<String>collect1 = csLedgers.stream().map(CsLedger::getId).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(collect1)){
                return collect;
            }
            csLedgerQueryWrapper.clear();
            csLedgerQueryWrapper.lambda().eq(CsLedger::getLevel, 2).eq(CsLedger::getState, 1).in(CsLedger::getPid, collect1);
            List<CsLedger> csLedgers1 = csLedgerMapper.selectList(csLedgerQueryWrapper);

            collect = csLedgers1.stream().map(CsLedger::getId).collect(Collectors.toList());

        }
        else if (Objects.equals(role,AppRoleEnum.TOURIST.getCode())) {

            List<CsTouristDataPO> csTouristDataPOS = csTouristDataPOMapper.selectList(null);
            collect = csTouristDataPOS.stream().map(CsTouristDataPO::getDeviceId).distinct().collect(Collectors.toList());

        }
        else if(Objects.equals(role,AppRoleEnum.ROOT.getCode())||Objects.equals(role,AppRoleEnum.OPERATION_MANAGER.getCode())){
            csLedgerQueryWrapper.clear();
            csLedgerQueryWrapper.eq("level",2).eq("state",1);
            List<CsLedger> csLedgers = csLedgerMapper.selectList(csLedgerQueryWrapper);
            collect = csLedgers.stream().map(CsLedger::getId).distinct().collect(Collectors.toList());
        } else {
            csDeviceUserPOQueryWrapper.clear();
            csDeviceUserPOQueryWrapper.eq("status","1").and(wq -> {
                wq.eq("primary_user_id", userIndex)
                        .or()
                        .eq("sub_user_id",userIndex);
            });
            List<CsDeviceUserPO> csDeviceUserPOS = csDeviceUserPOMapper.selectList(csDeviceUserPOQueryWrapper);
            if(CollectionUtils.isEmpty(csDeviceUserPOS)){
                return new ArrayList<>();
            }
            List<String> collect1 = csDeviceUserPOS.stream().map(CsDeviceUserPO::getDeviceId).distinct().collect(Collectors.toList());
            return collect1;
        }
        return collect;
    }

    @Override
    public List<String> getRoleProject() {
        List<String> roleengineer = this.getRoleengineer();
        if(CollectionUtils.isEmpty(roleengineer)){
            return new ArrayList<>();
        }
        QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();
        csLedgerQueryWrapper.clear();
        csLedgerQueryWrapper.eq("level",1).eq("state",1).in("pid",roleengineer);
        List<CsLedger> csLedgers = csLedgerMapper.selectList(csLedgerQueryWrapper);
        List<String> collect = csLedgers.stream().map(CsLedger::getId).distinct().collect(Collectors.toList());


        return collect;
    }


}
