package com.zcxk.charge.web.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.charge.api.vo.AlarmDetailsEntityVo;
import com.zcxk.charge.api.vo.AlarmSettingVo;
import com.zcxk.charge.core.entity.AlarmDetailsEntity;
import com.zcxk.charge.web.client.OperationManagerClient;
import com.zcxk.charge.core.dao.AlarmDetailMapper;
import com.zcxk.charge.core.dao.AlarmSettingMapper;
import com.zcxk.charge.api.dto.AlarmSettingDto;
import com.zcxk.charge.api.dto.WorkOrderManageByAlarmDto;
import com.zcxk.charge.core.entity.AlarmSetting;
import com.zcxk.core.common.util.BeanCopyUtils;
import org.apache.commons.lang3.StringUtils;
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<AlarmSettingVo> selectPage(IPage<AlarmSettingDto> page, AlarmSettingDto alarmSettingDto){
        return alarmSettingMapper.findPage(page,alarmSettingDto);
    }
    /**
     * 根据设备id查询属性信息
     *
     * @param deviceId
     * @return
     */
    public List<AlarmSettingVo> selectAttributes(Integer deviceId,Integer sceneId){
        return alarmSettingMapper.selectAttributes(deviceId,sceneId);
    }
    /**
     * 修改实时报警数据
     *
     * @param
     * @return
     */
    public void updateRealtimeAlarm(Long[] ids,String updateBy) {
        //系统中已存在的报警
        List<AlarmDetailsEntityVo> voList = alarmDetailMapper.selectStateAlarm(null,null,Arrays.asList(ids));

        if(voList.size() > 0){
            //标记为历史数据
            List<Long> deviceIds = new ArrayList<>();
            deviceIds.add(voList.get(0).getDeviceId().longValue());
            List<WorkOrderManageByAlarmDto> workOrders = operationManagerClient.findWorkOrderByDeviceIds(deviceIds);
            if(workOrders.size()>0){
                WorkOrderManageByAlarmDto orderManageByAlarmDto = workOrders.get(0);
                String taskDesc = orderManageByAlarmDto.getTaskDesc();
                //标记为历史数据 并 修改工单描述
                for(AlarmDetailsEntityVo alarmDetailsDto : voList){
                    alarmDetailsDto.setState(0);
                    alarmDetailsDto.setAlarmEndTime(new Date());
                    alarmDetailsDto.setDateUpdate(new Date());
                    alarmDetailsDto.setUpdateBy(updateBy);
                    AlarmDetailsEntity entity = new AlarmDetailsEntity();
                    BeanCopyUtils.copyProperties(alarmDetailsDto, entity, AlarmDetailsEntity.class);
                    alarmDetailMapper.update(entity);
                    taskDesc = taskDesc.replace(";"+alarmDetailsDto.getAlarmContent(),"")
                            .replace(alarmDetailsDto.getAlarmContent(),"");
                }
                orderManageByAlarmDto.setTaskDesc(taskDesc);
                if(orderManageByAlarmDto != null && isEmpty(orderManageByAlarmDto.getOrderStatus(), taskDesc)){
                    //工单未派单且无报警信息，删除工单
                    operationManagerClient.batchDeleteByAlarms(new ArrayList<>(deviceIds));
                } else {
                    //其他情况修改描述
                    operationManagerClient.updateByAlarms(workOrders);
                }
            }else{
                alarmSettingMapper.updateRealtimeAlarm(Arrays.asList(ids),updateBy);
            }
        }
    }

    private boolean isEmpty (Integer orderStatus, String taskDesc){
         return  (orderStatus == null || orderStatus == 0  ) && StringUtils.isBlank(taskDesc);
    }
}
