package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SysLogConstant;
import com.sptg.common.core.entity.PublishTask;
import com.sptg.common.core.enums.Instruct;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.device.api.constant.DeviceSecurityToken;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.InstructPublishLog;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.device.api.feigns.MsgPublishFeign;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.NoticePushDto;
import com.sptg.estate.api.entity.NoticePush;
import com.sptg.estate.api.entity.NoticePushDetails;
import com.sptg.estate.api.entity.NoticePushDeviceDetails;
import com.sptg.estate.api.feigns.NoticePushDeviceDetailsFeign;
import com.sptg.estate.api.feigns.NoticePushFeign;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.estate.api.feigns.UnitFeign;
import com.sptg.permissions.api.dto.RegionAuthorizeDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.RegionAuthorize;
import com.sptg.permissions.api.feigns.AreaFeign;
import com.sptg.permissions.api.feigns.RegionAuthorizeFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;
import java.util.stream.Collectors;

/**
 * title: NoticePushController
 * package: com.sptg.gateway.controller.estate
 * description: 公告发布-关网控制器
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/estate/noticePushDeviceDetails")
@RequiredArgsConstructor
public class NoticePushDeviceDetailsController {
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final DeviceFeign deviceFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final MsgPublishFeign msgPublishFeign;
    private final NoticePushDeviceDetailsFeign noticePushDeviceDetailsFeign;


    /**
     * 功能描述:公告发布设备管理页面
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping("/index")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_PUSH_DEVICE_DETAILS_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("estate/notice-push-device-details-manage");
        return modelAndView;
    }

    /**
     * 功能描述:公告发布设备管理页面
     * @return org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("/findViewByTaskId")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_PUSH_DEVICE_DETAILS_MANAGE)
    public ModelAndView index(NoticePushDeviceDetails noticePushDeviceDetails){
        Assert.notNull(noticePushDeviceDetails.getPushId(),"公告发布ID不能为空");
        ModelAndView modelAndView = new ModelAndView("estate/notice-push-device-details-manage");
        modelAndView.addObject("pushId", noticePushDeviceDetails.getPushId());
        return modelAndView;
    }

    /**
     * 功能描述:展示公告发布管理列表
     * @param noticePushDeviceDetails
     * @param page
     * @return com.sptg.common.core.beans.ResultBean<com.baomidou.mybatisplus.plugins.Page<com.sptg.estate.api.entity.NoticePushDeviceDetails>>
     */
    @GetMapping("/list")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_PUSH_DEVICE_DETAILS_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<NoticePushDeviceDetails>> showNoticePushList(NoticePushDeviceDetails noticePushDeviceDetails, Page<NoticePushDeviceDetails> page){
        //查询当前用户机构位置
//        Account account=securityServiceInterface.getOnLineUser();
        noticePushDeviceDetails.setPage(page);
        return noticePushDeviceDetailsFeign.showNoticePushDeviceDetailsList(noticePushDeviceDetails);
    }

    /**
     * 功能描述:单个或批量删除设备公告（下发删除公告指令）
     * @param noticePushDeviceDetails
     * @return com.sptg.common.core.beans.ResultBean<java.lang.Boolean>
     */
    @RequestMapping("/delete")
    @Security(token = EstateSecurityToken.ESTATE_NOTICE_PUSH_DEVICE_DETAILS_DELETE, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteNoticePushDeviceDetails(NoticePushDeviceDetails noticePushDeviceDetails){
        //日志事件
        int operationEvent = SysLogConstant.DELETE_EVENT;
        List<Long> ids = noticePushDeviceDetails.getIds();
        Assert.notEmpty(ids, "删除设备公告记录IDS不能为空");
        ids.forEach(id -> {
            //下发删除公告到MQ队列上
            deleteNoticePushDevice(id);
        });
        /*---------------------------------------------------记录日志--------------------------------------------------------*/
        //登陆用户
        Account account = this.securityServiceInterface.getOnLineUser();
        SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ESTATE_NOTICE_PUSH_DEVICE_DETAILS_MANAGE,operationEvent,
                WebUtils.getIp(), noticePushDeviceDetails, account);
        sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        return new ResultBean<>(ResultEnum.SUCCESS, true);
    }


    /*----------------------------------------------------内部方法--------------------------------------------------------------*/
    /*-------------------------------------------------------------------------------------------------------------------------*/

    /**
     * 下发删除公告指令给设备
     * @param id
     */
    private void deleteNoticePushDevice(Long id){
        ResultBean<NoticePushDeviceDetails> resultBean = this.noticePushDeviceDetailsFeign.getNoticePushDevice(id);
        if(resultBean != null && resultBean.getData() != null){
            NoticePushDeviceDetails noticePushDeviceDetails = resultBean.getData();
            if(noticePushDeviceDetails != null){
                Long deviceId = noticePushDeviceDetails.getDeviceId();
                Device device = deviceFeign.getEntityById(deviceId);
                if(device != null){
                    List<Long> deviceList = new ArrayList<>();
                    deviceList.add(deviceId);
                    Map map = new HashMap<>();
                    map.put("id", noticePushDeviceDetails.getTaskId());
                    PublishTask publishTask = new PublishTask(noticePushDeviceDetails.getTaskId(), Instruct.delete_notice, map, deviceList);
                    Map<String,String> logMap = new HashMap<>();
                    this.msgPublishFeign.publishTask(publishTask,logMap);
                }
                this.noticePushDeviceDetailsFeign.deleteNoticePushDevice(id); //删除本地记录
            }
        }
    }
}
