package com.skr.mdm.observer.global;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.skr.mdm.api.app.service.impl.AppStrategyServiceImpl;
import com.skr.mdm.dao.AppStrategyDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.AppStrategyDO;
import com.skr.mdm.enums.IssuedTypeEnum;
import com.skr.mdm.enums.StrategyStateEnum;
import com.skr.mdm.event.UserActiveEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

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

/**
 * 用户设备激活全局应用策略下发
 *
 * @author nfq
 * @date 2022/06/09 9:50
 */
@Component
@Slf4j
public class GlobalAppStrategyObserver implements ApplicationListener<UserActiveEvent> {

    @Autowired
    private AppStrategyDao appStrategyDao;
    @Autowired
    private AppStrategyServiceImpl appStrategyService;

    @Override
    public void onApplicationEvent(UserActiveEvent event) {
        try {
            // 获取公司下所有全局策略
            List<AppStrategyDO> appStrategyDOList = new LambdaQueryChainWrapper<>(appStrategyDao)
                    .ne(AppStrategyDO::getGlobalType, IssuedTypeEnum.DEVICE.getIssuedType())
                    .eq(AppStrategyDO::getState, StrategyStateEnum.ISSUED.getState())
                    .eq(AppStrategyDO::getCompanyId, event.getCompanyId())
                    .list();
            if (CollectionUtils.isNotEmpty(appStrategyDOList)) {
                // 筛选非当前设备所在部门的部门全局策略
                List<AppStrategyDO> appStrategyDOS = appStrategyDOList.stream().filter(appStrategyDO ->
                                IssuedTypeEnum.DEPT_GLOBAL.getIssuedType().equals(appStrategyDO.getGlobalType()) &&
                                        !JSONUtil.toList(JSONUtil.parseArray(appStrategyDO.getDepartmentIds()), Integer.class)
                                                .contains(event.getDepartmentId()))
                        .collect(Collectors.toList());
                // 排除筛选出的部门策略
                if (CollectionUtils.isNotEmpty(appStrategyDOS)) {
                    appStrategyDOList.removeAll(appStrategyDOS);
                }
                if (CollectionUtils.isEmpty(appStrategyDOList)) {
                    return;
                }
                List<Integer> strategyIdList = appStrategyDOList.stream().map(AppStrategyDO::getId).collect(Collectors.toList());
                LoginAdminDTO loginAdminDTO = new LoginAdminDTO();
                loginAdminDTO.setCompanyId(event.getCompanyId());
                appStrategyService.saveStrategyUser(strategyIdList, Collections.singletonList(event.getEquipmentId()), loginAdminDTO);
                appStrategyService.doIssuedStrategy(strategyIdList, Collections.singletonList(event.getEquipmentId()), loginAdminDTO);
            }
        } catch (Exception e) {
            log.error("设备激活全局应用策略下发异常：" + event.getEquipmentId(), e);
        }
    }

}