package com.zd.airbottle.controller;

import com.zd.airbottle.domain.QpBottleStorageError;
import com.zd.airbottle.domain.vo.QpBottleStorageErrorVo;
import com.zd.airbottle.service.IQpBottleStorageErrorService;
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.domain.ResultData;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 气瓶异常记录Controller
 */
@RestController
@Api(tags = "【气瓶异常记录】")
@RequestMapping("/bottle/error")
public class QpBottleStorageErrorController extends BaseController<QpBottleStorageError> {
    @Autowired
    private IQpBottleStorageErrorService qpBottleStorageErrorService;

    /**
     * 查询气瓶异常记录列表
     */
    @PreAuthorize(hasPermi = "airbottle:bottle:error:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询气瓶异常记录列表")
    public TableDataInfo<QpBottleStorageErrorVo> list(QpBottleStorageError qpBottleStorageError) {
        startPage("op_time","descending");
        List<QpBottleStorageError> list = qpBottleStorageErrorService.selectQpBottleStorageErrorList(qpBottleStorageError);
        TableDataInfo<QpBottleStorageError> dataTable = getDataTable(list);
        List<QpBottleStorageErrorVo> storageErrorVos = TransferUtils.transferList(list, QpBottleStorageErrorVo.class);
        if (!storageErrorVos.isEmpty()){
            qpBottleStorageErrorService.getBottleInfo(storageErrorVos);
        }
        TableDataInfo<QpBottleStorageErrorVo> errorVoTableDataInfo=new TableDataInfo<>();
        BeanUtils.copyProperties(dataTable,errorVoTableDataInfo);
        errorVoTableDataInfo.setRows(storageErrorVos);
        return errorVoTableDataInfo;
    }

    /**
     * 导出气瓶异常记录列表
     */
    @ApiOperation(value = "导出气瓶异常记录列表")
    @Log(title = "气瓶异常记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpBottleStorageError qpBottleStorageError) throws IOException {
        List<QpBottleStorageError> list = qpBottleStorageErrorService.selectQpBottleStorageErrorList(qpBottleStorageError);
        List<QpBottleStorageErrorVo> storageErrorVos = TransferUtils.transferList(list, QpBottleStorageErrorVo.class);
        if (!storageErrorVos.isEmpty()){
            qpBottleStorageErrorService.getBottleInfo(storageErrorVos);
            storageErrorVos.forEach(i-> i.setBeforeEditValue(i.getBeforeEdit().stripTrailingZeros().toPlainString() +"MPa").setAfterEditValue(i.getAfterEdit().stripTrailingZeros().toPlainString() +"MPa"));
        }
        ExcelUtil<QpBottleStorageErrorVo> util = new ExcelUtil<>(QpBottleStorageErrorVo.class);
        util.exportExcel(response, storageErrorVos, "气瓶异常记录数据");
    }

    /**
     * 获取气瓶异常记录详细信息
     */
    @ApiOperation(value = "获取气瓶异常记录详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<QpBottleStorageErrorVo> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpBottleStorageErrorService.getStorageErrorInfo(id));
    }

    /**
     * 新增气瓶异常记录
     */
    @ApiOperation(value = "新增气瓶异常记录")
    @Log(title = "气瓶异常记录", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Boolean> add(@RequestBody QpBottleStorageError qpBottleStorageError) {
        return ResultData.result(qpBottleStorageErrorService.insertQpBottleStorageError(qpBottleStorageError));
    }


    /**
     * 查询气瓶异常记录列表-小程序端
     */
    @GetMapping("/listApp")
    @ApiOperation(value = "查询气瓶异常记录列表")
    public TableDataInfo<QpBottleStorageError> listApp(QpBottleStorageError qpBottleStorageError) {
        startPage("op_time","descending");
        List<QpBottleStorageError> list = qpBottleStorageErrorService.selectQpBottleStorageErrorList(qpBottleStorageError);
        return getDataTable(list);
    }
}
