package com.zd.airbottle.controller;

import com.zd.airbottle.domain.AlarmRecord;
import com.zd.airbottle.domain.vo.AlarmRecordVo;
import com.zd.airbottle.domain.vo.BottleStorageInfoVo;
import com.zd.airbottle.service.IAlarmRecordService;
import com.zd.common.core.domain.R;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.utils.bean.TransferUtils;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.system.api.domain.InventoryTag;
import com.zd.system.api.laboratory.domain.RemoteLabHardware;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报警记录Controller
 * @author Administrator
 */
@RestController
@Api(tags = "【报警记录】")
@RequestMapping("/alarm/record")
public class AlarmRecordController extends BaseController<AlarmRecord> {
    @Resource
    private IAlarmRecordService alarmRecordService;

    /**
     * 查询报警记录列表
     */
    @PreAuthorize(hasPermi = "airbottle:alarm:record:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询报警记录列表")
    public TableDataInfo<AlarmRecordVo> list(AlarmRecord alarmRecord) {
        List<BottleStorageInfoVo> storageInfoVos = alarmRecordService.setMasterIds(alarmRecord);
        if (!storageInfoVos.isEmpty()) {
            List<Long> storageIds = storageInfoVos.stream().map(BottleStorageInfoVo::getId).distinct().collect(Collectors.toList());
            alarmRecord.setMasterIds(storageIds);
        }else if (!StringUtils.isEmpty(alarmRecord.getSearchValue()) && storageInfoVos.isEmpty()){
            return new TableDataInfo<>();
        }
        startPage("alarm_time","descending");
        List<AlarmRecord> list = alarmRecordService.selectAlarmRecordList(alarmRecord);
        TableDataInfo<AlarmRecord> dataTable = getDataTable(list);
        List<AlarmRecordVo> recordVos = TransferUtils.transferList(list, AlarmRecordVo.class);
        if (!recordVos.isEmpty()) {
            alarmRecordService.getRecordInfo(recordVos);
        }
        TableDataInfo<AlarmRecordVo> dataInfo = new TableDataInfo<>();
        BeanUtils.copyProperties(dataTable, dataInfo);
        dataInfo.setRows(recordVos);
        return dataInfo;
    }

    /**
     * 导出报警记录列表
     */
    @ApiOperation(value = "导出报警记录列表")
    @PreAuthorize(hasPermi = "airbottle:alarm:record:export")
    @Log(title = "报警记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlarmRecord alarmRecord) throws IOException {
        ExcelUtil<AlarmRecordVo> util = new ExcelUtil<>(AlarmRecordVo.class);
        List<BottleStorageInfoVo> storageInfoVos = alarmRecordService.setMasterIds(alarmRecord);
        if (!storageInfoVos.isEmpty()) {
            List<Long> storageIds = storageInfoVos.stream().map(BottleStorageInfoVo::getId).distinct().collect(Collectors.toList());
            alarmRecord.setMasterIds(storageIds);
        }else if (!StringUtils.isEmpty(alarmRecord.getSearchValue()) && storageInfoVos.isEmpty()){
            throw new ServiceException("暂无相关数据");
        }
        List<AlarmRecord> list = alarmRecordService.selectAlarmRecordList(alarmRecord);
        List<AlarmRecordVo> recordVos = TransferUtils.transferList(list, AlarmRecordVo.class);
        if (!recordVos.isEmpty()) {
            alarmRecordService.getRecordInfo(recordVos);
            recordVos.forEach(r -> r.setCurrentPressureValue(StringUtils.convertDoubleToString(r.getCurrentPressure()) + "MPa"));
        }

        util.exportExcel(response, recordVos, "报警记录数据");
    }

    /**
     * 识别器新增报警记录
     */
    @ApiOperation(value = "识别器新增报警记录")
    @Log(title = "识别器新增报警记录", businessType = BusinessType.INSERT)
    @PostMapping("remote/add")
    public R<Boolean> remoteAdd(@RequestBody InventoryTag tag) {
        return R.ok(alarmRecordService.remoteAdd(tag));
    }
}
