package com.zcxk.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.client.OperationManagerClient;
import com.zcxk.common.StringUtils;
import com.zcxk.dao.AlarmDetailMapper;
import com.zcxk.dao.AlarmSettingMapper;
import com.zcxk.dto.AlarmDetailsDto;
import com.zcxk.dto.AlarmSettingDto;
import com.zcxk.dto.WorkOrderManageByAlarmDto;
import com.zcxk.entity.AlarmSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 报警设置服务接口
 *
 * @author yjy
 * @since 2020-11-17
 */
@Service
public class AlarmSettingService extends ServiceImpl<AlarmSettingMapper, AlarmSetting> {
    @Resource
    private AlarmSettingMapper alarmSettingMapper;

    @Resource
    private AlarmDetailMapper alarmDetailMapper;
    @Autowired
    private OperationManagerClient operationManagerClient;
    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    public boolean deleteByAlarmIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
    /**
     * 修改数据
     *
     * @param
     * @return
     */
    public boolean updateByAlarmId(AlarmSetting alarmSetting) {
        return this.updateById(alarmSetting);
    }
    /**
     * 批量增加数据
     *
     * @param
     * @return
     */
    public boolean insertBatch(List<AlarmSetting> listAlarmSetting) {
        return this.saveBatch(listAlarmSetting);
    }
    /**
     * 根据id查询报警设置数据
     *
     * @param id 所属公司
     * @return 单条数据
     */
    public AlarmSetting selectById(Integer id){
        return this.getById(id);
    }

    /**
     * 根据报警参数、场景查询报警设置数据
     */
     public IPage<AlarmSettingDto> selectPage(IPage<AlarmSettingDto> page, AlarmSettingDto alarmSettingDto){
        return alarmSettingMapper.findPage(page,alarmSettingDto);
    }
    /**
     * 根据设备id查询属性信息
     *
     * @param deviceId
     * @return
     */
    public List<AlarmSettingDto> selectAttributes(Integer deviceId,Integer sceneId){
        return alarmSettingMapper.selectAttributes(deviceId,sceneId);
    }
    /**
     * 修改实时报警数据
     *
     * @param
     * @return
     */
    public void updateRealtimeAlarm(Long[] ids,String updateBy) {
        //系统中已存在的报警
        List<AlarmDetailsDto> alarmDetailsDtos = alarmDetailMapper.selectStateAlarm(null,null,Arrays.asList(ids));

        if(alarmDetailsDtos.size() > 0){
            //标记为历史数据
            List<Long> deviceIds = new ArrayList<>();
            deviceIds.add(alarmDetailsDtos.get(0).getDeviceId().longValue());
            List<WorkOrderManageByAlarmDto> workOrders = operationManagerClient.findWorkOrderByDeviceIds(deviceIds);
            if(workOrders.size()>0){
                WorkOrderManageByAlarmDto orderManageByAlarmDto = workOrders.get(0);
                String taskDesc = orderManageByAlarmDto.getTaskDesc();
                //标记为历史数据 并 修改工单描述
                for(AlarmDetailsDto alarmDetailsDto : alarmDetailsDtos){
                    alarmDetailsDto.setState(0);
                    alarmDetailsDto.setAlarmEndTime(new Date());
                    alarmDetailsDto.setDateUpdate(new Date());
                    alarmDetailsDto.setUpdateBy(updateBy);
                    alarmDetailMapper.update(alarmDetailsDto);

                    taskDesc = taskDesc.replace(";"+alarmDetailsDto.getAlarmContent(),"")
                            .replace(alarmDetailsDto.getAlarmContent(),"");
                }
                orderManageByAlarmDto.setTaskDesc(taskDesc);
                if(orderManageByAlarmDto != null
                        && (orderManageByAlarmDto.getOrderStatus() == null || orderManageByAlarmDto.getOrderStatus() == 0)
                        && StringUtils.isBlank(taskDesc)){
                    //工单未派单且无报警信息，删除工单
                    operationManagerClient.batchDeleteByAlarms(new ArrayList<>(deviceIds));
                }else{
                    //其他情况修改描述
                    operationManagerClient.updateByAlarms(workOrders);
                }
            }else{
                alarmSettingMapper.updateRealtimeAlarm(Arrays.asList(ids),updateBy);
            }
        }
//        return alarmSettingMapper.updateRealtimeAlarm(Arrays.asList(ids),updateBy);
    }
}
