package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.modules.base.dto.PersonDeviceRelationDTO;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.pacs.dto.PacsMsgResult;
import com.jpsoft.shinestar.modules.pacs.service.IGuardService;
import com.jpsoft.shinestar.modules.sys.entity.SysLog;
import com.jpsoft.shinestar.modules.sys.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@RabbitListener(queues = "removeImageQueue",concurrency = "2")
public class RemoveImageListener {
    @Autowired
    private IGuardService guardService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private PersonDeviceRelationService personDeviceRelationService;

    @Autowired
    private ExecuteFailLogService executeFailLogService;

    @RabbitHandler
    public void process(Long personId) {
        SysLog sysLog = new SysLog();

        sysLog.setPointcut("删除用户在设备中的照片");
        sysLog.setUserId(String.valueOf(personId));
        sysLog.setCreateTime(new Date());

        try{
            List<DeviceInfo> deviceInfoList = new ArrayList<>();

            //当前关联设备
            List<PersonDeviceRelation> pdrList = personDeviceRelationService.findByPersonId(personId);
            List<String> idList1 = pdrList.stream().map(pdr->pdr.getDeviceId()).collect(Collectors.toList());

            if (idList1.size()>0) {
                String deviceIds = idList1.stream().distinct().collect(Collectors.joining(","));

                List<PacsMsgResult> list = guardService.deletePersonFromDevice(personId, deviceIds);

                StringBuilder sb = new StringBuilder();

                for (PacsMsgResult pacsMsgResult : list) {
                    sb.append("删除" + pacsMsgResult.getAliasName() + "中照片");
                    sb.append(pacsMsgResult.isSuccess() ? "成功!" : "失败!");
                    sb.append(pacsMsgResult.getMsg() + ";");

                    if (pacsMsgResult.isSuccess()) {
                        personDeviceRelationService.updateIsBound(pacsMsgResult.getPersonId(), pacsMsgResult.getDeviceId(), false);
                    }
                }

                sysLog.setRemark(sb.toString());
            }
            else{
                sysLog.setRemark("没有关联设备!");
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(),ex);
            sysLog.setRemark(ex.getMessage());
        }

        sysLogService.insert(sysLog);
    }


    @RabbitHandler
    public void process(PersonDeviceRelationDTO dto) {
        SysLog sysLog = new SysLog();

        try {
            sysLog.setPointcut("删除用户在单个设备中的照片");
            sysLog.setUserId(String.valueOf(dto.getPersonId()));
            sysLog.setCreateTime(new Date());

            List<PacsMsgResult> list = guardService.deletePersonFromDevice(dto.getPersonId(), dto.getDeviceId());

            StringBuilder sb = new StringBuilder();

            for (PacsMsgResult pacsMsgResult : list) {
                sb.append("删除" + pacsMsgResult.getAliasName() + "中照片");
                sb.append(pacsMsgResult.isSuccess() ? "成功!" : "失败!");
                sb.append(pacsMsgResult.getMsg() + ";");

                if (pacsMsgResult.isSuccess()) {
                    personDeviceRelationService.updateIsBound(pacsMsgResult.getPersonId(), pacsMsgResult.getDeviceId(), false);
                }
            }

            sysLog.setRemark(sb.toString());
        }
        catch (Exception ex) {
            log.error(ex.getMessage(),ex);
            sysLog.setRemark(ex.getMessage());
        }

        sysLogService.insert(sysLog);
    }

    @RabbitHandler
    public void process(ExecuteFailLog efl) {
        SysLog sysLog = new SysLog();

        try {
            DeviceInfo deviceInfo = deviceInfoService.get(efl.getDeviceId());

            if (deviceInfo.getIsOnline()==null || !deviceInfo.getIsOnline()) {
                log.error(deviceInfo.getDeviceNo() + "已离线!");
                return;
            }

            sysLog.setPointcut("删除用户在单个设备中的照片");
            sysLog.setUserId(String.valueOf(efl.getPersonId()));
            sysLog.setCreateTime(new Date());

            List<PacsMsgResult> list = guardService.deletePersonFromDevice(efl.getPersonId(), efl.getDeviceId());

            StringBuilder sb = new StringBuilder();

            for (PacsMsgResult pacsMsgResult : list) {
                sb.append("删除" + pacsMsgResult.getAliasName() + "中照片");
                sb.append(pacsMsgResult.isSuccess() ? "成功!" : "失败!");
                sb.append(pacsMsgResult.getMsg() + ";");

                if (pacsMsgResult.isSuccess()) {
                    personDeviceRelationService.updateIsBound(pacsMsgResult.getPersonId(), pacsMsgResult.getDeviceId(), false);
                }

                efl.setExecuteSuccess(true);
                efl.setExecuteTime(new Date());
                executeFailLogService.update(efl);
            }

            sysLog.setRemark(sb.toString());
        }
        catch (Exception ex) {
            log.error(ex.getMessage(),ex);
            sysLog.setRemark(ex.getMessage());
        }

        sysLogService.insert(sysLog);
    }
}
