package com.skr.mdm.manager;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.google.common.collect.Lists;
import com.skr.mdm.constant.BasicNumberConstants;
import com.skr.mdm.dao.AuCompanyDao;
import com.skr.mdm.dao.AuUserDao;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.entity.AuCompanyDO;
import com.skr.mdm.entity.DeviceEquipmentDO;
import com.skr.mdm.enums.DeleteStateEnum;
import com.skr.mdm.enums.IssuedTypeEnum;
import com.skr.mdm.publish.MdmEventPublisher;
import com.skr.mdm.util.GaodeMapAPIUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author nfq
 * @date 2022-06-20 16:49
 */
@Component
public class DeviceManager {

    @Autowired
    private AuUserDao auUserDao;
    @Autowired
    private DeviceEquipmentDao equipmentDao;
    @Autowired
    private AuCompanyDao companyDao;
    @Autowired
    private MdmEventPublisher mdmEventPublisher;

    public List<Integer> getDeviceIdList(Integer isGlobal, List<Integer> userIdList, List<Integer> deptIdList, Integer companyId) {
        List<Integer> deviceIdList = new ArrayList<>();
        if (Objects.nonNull(isGlobal) && isGlobal > BasicNumberConstants.ZERO) {
            if (CollectionUtils.isNotEmpty(deptIdList)) {
                userIdList = auUserDao.selectUserIdListByDeptIdList(deptIdList);
            }
            deviceIdList = isGlobal.equals(BasicNumberConstants.ONE) ? equipmentDao.listEquipmentIdByCompanyId(companyId) : equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        } else if (CollectionUtils.isNotEmpty(userIdList)) {
            deviceIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        }
        return deviceIdList;
    }

    public List<Integer> getDeviceIdList(Integer globalType, List<Integer> candidateIdList, Integer companyId) {
        if (IssuedTypeEnum.GLOBAL.getIssuedType().equals(globalType)) {
            return equipmentDao.listEquipmentIdByCompanyId(companyId);
        }
        if (IssuedTypeEnum.DEPT_GLOBAL.getIssuedType().equals(globalType)) {
            if (CollectionUtils.isNotEmpty(candidateIdList)) {
                return equipmentDao.listEquipmentIdByDepartmentIdList(candidateIdList);
            }
        }
        if (IssuedTypeEnum.DEVICE.getIssuedType().equals(globalType)) {
            if (CollectionUtils.isNotEmpty(candidateIdList)) {
                return candidateIdList;
            }
        }
        return Collections.emptyList();
    }

    /**
     * 高德终端解绑
     *
     * @param deviceIdList
     * @param companyId
     * @return
     */
    public List<Integer> deleteGaodeTerminal(List<Integer> deviceIdList, Integer companyId) {
        if (CollectionUtils.isNotEmpty(deviceIdList)) {
            List<Integer> deleteSuccessDeviceIdList = Lists.newArrayListWithCapacity(deviceIdList.size());
            AuCompanyDO companyDO = companyDao.selectById(companyId);
            for (Integer equipmentId : deviceIdList) {
                Integer tid = equipmentDao.getTidById(equipmentId);
                if (companyDO.getSid() != null && tid != null) {
                    tid = GaodeMapAPIUtil.listService(companyDO.getSid(), tid);
                    if (null != tid) {
                        if (GaodeMapAPIUtil.deleteTerminal(companyDO.getSid(), tid)) {
                            // 删除高德数据成功
                            deleteSuccessDeviceIdList.add(equipmentId);
                        }
                        // 删除失败则不进行删除
                    } else {
                        deleteSuccessDeviceIdList.add(equipmentId);
                    }
                } else {
                    // 无高德设备数据
                    deleteSuccessDeviceIdList.add(equipmentId);
                }
            }
            return deleteSuccessDeviceIdList;
        }
        return Collections.emptyList();
    }

    /**
     * 设备删除
     *
     * @param deviceIdList
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteDevice(List<Integer> deviceIdList) {
        if (CollectionUtils.isEmpty(deviceIdList)) {
            return;
        }
        // deviceIdList需要保留
        List<Integer> equipmentIdList = Lists.newCopyOnWriteArrayList(deviceIdList);
        // 物理删除未激活设备
        List<Integer> inactiveDeviceIdList = equipmentDao.selectInactiveDeviceIdList(equipmentIdList, 0);
        if (CollectionUtils.isNotEmpty(inactiveDeviceIdList)) {
            equipmentDao.deleteBatchIds(inactiveDeviceIdList);
        }
        // 逻辑删除已激活设备
        new LambdaUpdateChainWrapper<>(equipmentDao)
                .set(DeviceEquipmentDO::getIsDelete, DeleteStateEnum.DELETED.getState())
                .in(DeviceEquipmentDO::getId, equipmentIdList)
                .update();
        // 删除设备所有相关数据
        mdmEventPublisher.publishEquipmentDeleteEvent(equipmentIdList);
    }
}