package com.zd.bottle.controller;

import com.zd.bottle.domain.AlarmRecord;
import com.zd.bottle.service.AlarmRecordService;
import com.zd.bottle.vo.AlarmRecordVo;
import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
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 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;

/**
 * <p>
 * 报警记录表 前端控制器
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@RestController
@Api(tags = "【报警记录】")
@RequestMapping("/alarmRecord")
public class AlarmRecordController extends BaseController<AlarmRecord> {

    @Resource
    private AlarmRecordService alarmRecordService;

    /**
     * 查询报警记录列表
     * @param alarmRecord 参数
     * @return TableDataInfo
     */
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_ALARMRECORD + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询报警记录列表")
    public TableDataInfo<AlarmRecordVo> list(AlarmRecord alarmRecord) {
        startPage("alarm_time","descending");
        List<AlarmRecord> records = alarmRecordService.getList(alarmRecord);
        TableDataInfo<AlarmRecord> dataTable = getDataTable(records);
        List<AlarmRecordVo> recordVos = TransferUtils.transferList(records, AlarmRecordVo.class);
        if (!recordVos.isEmpty()) {
            alarmRecordService.getRecordInfo(recordVos);
        }
        TableDataInfo<AlarmRecordVo> dataInfo = new TableDataInfo<>();
        BeanUtils.copyProperties(dataTable, dataInfo);
        dataInfo.setRows(recordVos);
        return dataInfo;
    }

    /**
     * 导出报警记录列表
     * @param response 响应值
     * @param alarmRecord 参数
     * @throws IOException 异常
     */
    @ApiOperation(value = "导出报警记录列表")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_ALARMRECORD + PerFun.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<AlarmRecord> list = alarmRecordService.getList(alarmRecord);
        List<AlarmRecordVo> recordVos = TransferUtils.transferList(list, AlarmRecordVo.class);
        if (!recordVos.isEmpty()) {
            alarmRecordService.getRecordInfo(recordVos);
            recordVos.forEach(r -> r.setCurrentPressureValue(r.getCurrentPressure().toPlainString() + "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));
    }
}
