package com.njcn.cssystem.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.csdevice.api.CsDeviceUserFeignClient;
import com.njcn.csdevice.api.CsLedgerFeignClient;
import com.njcn.csdevice.pojo.dto.DevDetailDTO;
import com.njcn.csdevice.pojo.param.UserDevParam;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.vo.DevUserVO;
import com.njcn.cssystem.pojo.param.WlUserParam;
import com.njcn.cssystem.pojo.vo.WlUserVo;
import com.njcn.cssystem.service.IWlUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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


/**
 * @author xy
 */
@Service
@AllArgsConstructor
public class WlUserServiceImpl implements IWlUserService {

    private final CsDeviceUserFeignClient csDeviceUserFeignClient;
    private final CsLedgerFeignClient csLedgerFeignClient;

    @Override
    public WlUserVo selectDevByUserId(String userId) {
        WlUserVo wlUserVo = new WlUserVo();
        List<WlUserVo.EngineeringVo> list1 = new ArrayList<>();
        List<WlUserVo.portableDevVo> list2 = new ArrayList<>();
        //根据用户信息获取设备信息
        List<String> devList = csDeviceUserFeignClient.findDevByUserId(userId).getData();
        //根据设备查询工程信息
        if (CollectionUtil.isNotEmpty(devList)) {
            List<DevDetailDTO> ledger = csLedgerFeignClient.getInfoByIds(devList).getData();
            ledger.forEach(item->{
                if (Objects.equals(item.getEngineeringid(),"/")) {
                    WlUserVo.portableDevVo portableDevVo = new WlUserVo.portableDevVo();
                    portableDevVo.setId(item.getEquipmentId());
                    portableDevVo.setName(item.getEquipmentName());
                    list2.add(portableDevVo);
                } else {
                    WlUserVo.EngineeringVo engineeringVo = new WlUserVo.EngineeringVo();
                    engineeringVo.setId(item.getEngineeringid());
                    engineeringVo.setName(item.getEngineeringName() + "("+item.getEquipmentName()+")");

                    List<WlUserVo.portableDevVo> devList2 = new ArrayList<>();
                    String[] arr1 = item.getEquipmentId().split(",");
                    String[] arr2 = item.getEquipmentName().split(",");
                    for (int i = 0; i < arr1.length; i++) {
                        WlUserVo.portableDevVo portableDevVo = new WlUserVo.portableDevVo();
                        portableDevVo.setId(arr1[i]);
                        portableDevVo.setName(arr2[i]);
                        devList2.add(portableDevVo);
                    }
                    engineeringVo.setDevList(devList2);
                    list1.add(engineeringVo);
                }
            });
        }
        wlUserVo.setEngineeringList(list1);
        wlUserVo.setPortableDevList(list2);
        return wlUserVo;
    }

    @Override
    public void addUserDev(WlUserParam param) {
        List<String> devList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(param.getEngineeringList())) {
            //根据工程id获取设备id
            List<DevDetailDTO> dto = csLedgerFeignClient.getDevInfoByEngineerIds(param.getEngineeringList()).getData();
            if (CollectionUtil.isNotEmpty(dto)) {
                devList.addAll(dto.stream().map(DevDetailDTO::getEquipmentId).collect(Collectors.toList()));
            }
        }
        if (CollectionUtil.isNotEmpty(param.getPortableDevList())) {
            devList.addAll(param.getPortableDevList());
        }
        //存储用户和设备的关系
        if (CollectionUtil.isNotEmpty(devList)) {
            List<CsDeviceUserPO> list = new ArrayList<>();
            devList.forEach(item->{
                //现根据设备获取主用户
                DevUserVO vo = csDeviceUserFeignClient.queryUserById(item).getData();
                CsDeviceUserPO po = new CsDeviceUserPO();
                if (!Objects.isNull(vo) && !Objects.isNull(vo.getMasterUser())) {
                    po.setPrimaryUserId(vo.getMasterUser().getId());
                } else {
                    po.setPrimaryUserId(param.getUserId());
                }
                po.setSubUserId(param.getUserId());
                po.setDeviceId(item);
                po.setStatus("1");
                list.add(po);
            });
            csDeviceUserFeignClient.add(list);
        }
    }

    @Override
    public void deleteUserDev(WlUserParam param) {
        List<String> devList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(param.getEngineeringList())) {
            //根据工程id获取设备id
            List<DevDetailDTO> dto = csLedgerFeignClient.getDevInfoByEngineerIds(param.getEngineeringList()).getData();
            if (CollectionUtil.isNotEmpty(dto)) {
                devList.addAll(dto.stream().map(DevDetailDTO::getEquipmentId).collect(Collectors.toList()));
            }
        }
        if (CollectionUtil.isNotEmpty(param.getPortableDevList())) {
            devList.addAll(param.getPortableDevList());
        }
        //解绑用户和设备的关系
        if (CollectionUtil.isNotEmpty(devList)) {
            UserDevParam par = new UserDevParam();
            par.setUserId(param.getUserId());
            par.setList(devList);
            csDeviceUserFeignClient.channelDevByUserId(par);
        }
    }



}
