package com.zd.bottle.controller;

import com.zd.bottle.domain.BottleStorageError;
import com.zd.bottle.service.BottleStorageErrorService;
import com.zd.bottle.vo.BottleStorageErrorVo;
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.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.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("/bottleStorageError")
public class BottleStorageErrorController extends BaseController<BottleStorageError> {

    @Resource
    private BottleStorageErrorService storageErrorService;

    /**
     * 查询气瓶异常记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_STORAGEERROR + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询气瓶异常记录列表")
    public TableDataInfo<BottleStorageErrorVo> list(BottleStorageError bottleStorageError) {
        startPage("op_time","descending");
        List<BottleStorageError> list = storageErrorService.list(bottleStorageError);
        TableDataInfo<BottleStorageError> dataTable = getDataTable(list);
        List<BottleStorageErrorVo> storageErrorVos = TransferUtils.transferList(list, BottleStorageErrorVo.class);
        if (!storageErrorVos.isEmpty()){
            storageErrorService.getBottleInfo(storageErrorVos);
        }
        TableDataInfo<BottleStorageErrorVo> errorVoTableDataInfo=new TableDataInfo<>();
        BeanUtils.copyProperties(dataTable,errorVoTableDataInfo);
        errorVoTableDataInfo.setRows(storageErrorVos);
        return errorVoTableDataInfo;
    }

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

    /**
     * 获取气瓶异常记录详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_STORAGEERROR + PerFun.QUERY)
    @ApiOperation(value = "获取气瓶异常记录详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<BottleStorageErrorVo> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(storageErrorService.getStorageErrorInfo(id));
    }

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