package com.sptg.device.biz.receiver;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.*;
import com.sptg.device.api.stream.DeviceDeleteProduceInterface;
import com.sptg.device.biz.service.*;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.stream.OrgDeleteReceiverInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;

/**
 * title: OrgDeleteConsume
 * description: 机构删除消费者
 *
 *                  
 *            
 */
@Component
@Slf4j
@RequiredArgsConstructor
@EnableBinding(value = { OrgDeleteReceiverInterface.class})
public class OrgDeleteReceiver {

    private final DevicePathServiceInterface devicePathServiceInterface;

    @StreamListener(value = OrgDeleteReceiverInterface.INPUT)
    public void input(Organization org){
        //  删除设备等信息
        this.deleteDeviceByOrg(org);
        //  删除设备位置
        Wrapper<DevicePath> pathWrapper = new EntityWrapper<>();
        pathWrapper.like("s_org_position",org.getPosition(), SqlLike.RIGHT);
        this.devicePathServiceInterface.delete(pathWrapper);
    }

    private final DeviceServiceInterface deviceServiceInterface;
    private final TimeStrategyTaskDeviceDetailsServiceInterface timeStrategyTaskDeviceDetailsServiceInterface;
    private final DeviceDeleteProduceInterface deviceDeleteProduceInterface;

    private void deleteDeviceByOrg(Organization org){
        int current = 1;
        int size = 100;
        //  分页获取设备数量
        DeviceDto dto = new DeviceDto();
        dto.setOrgPosition(org.getPosition());
        PageBean<Device, DeviceDto> pageBean = new PageBean<>();
        pageBean.setDto(dto);
        pageBean.setSize(size);
        while (true){
            pageBean.setCurrent(current);
            Page<Device> resultPage = this.deviceServiceInterface.pagingQuery(pageBean);
            if (resultPage.getRecords().size() > 0) {
                //  得到设备Id列表
                List<Long> idList = resultPage.getRecords().stream().map(Device::getId).collect(Collectors.toList());
                //  通知队列删除这些设备
                Message<List<Long>> build = MessageBuilder.withPayload(idList).build();
                this.deviceDeleteProduceInterface.output().send(build);
                //  删除策略设备的推送记录 time_strategy_task_device_details
                Wrapper<TimeStrategyTaskDeviceDetails> timeStrategyTaskDeviceDetailsWrapper = new EntityWrapper<>();
                timeStrategyTaskDeviceDetailsWrapper.in("l_device_id", idList);
                this.timeStrategyTaskDeviceDetailsServiceInterface.delete(timeStrategyTaskDeviceDetailsWrapper);
            }
            if (resultPage.getRecords().size() < size){
                //  结束循环
                break;
            }
            ++ current;
        }
    }

}
