package com.zd.airbottle.controller;

import cn.hutool.core.collection.CollUtil;
import com.zd.airbottle.domain.QpAirBottle;
import com.zd.airbottle.domain.dto.QpAirBottleDto;
import com.zd.airbottle.domain.vo.QpAirBottleRVo;
import com.zd.airbottle.service.IQpAirBottleService;
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 java.util.List;

/**
 * 气瓶Controller
 */
@RestController
@Api(tags = "【气瓶】")
@RequestMapping("/bottle")
public class QpAirBottleController extends BaseController<QpAirBottle> {
    @Resource
    private IQpAirBottleService qpAirBottleService;

    /**
     * 查询气瓶列表
     */
    @GetMapping("/list")
    @PreAuthorize(hasPermi = "airbottle:bottle:list")
    @ApiOperation(value = "查询气瓶列表")
    public TableDataInfo<QpAirBottleRVo> list(QpAirBottle qpAirBottle) {
        startPage("create_time", "descending");
        List<QpAirBottle> list = qpAirBottleService.getList(qpAirBottle);
        return getVoTableDataInfo(list);
    }

    private TableDataInfo<QpAirBottleRVo> getVoTableDataInfo(List<QpAirBottle> list) {
        TableDataInfo<QpAirBottleRVo> info = new TableDataInfo<>();
        if (CollUtil.isNotEmpty(list)) {
            TableDataInfo<QpAirBottle> dataInfo = getDataTable(list);
            BeanUtils.copyProperties(dataInfo, info);
            List<QpAirBottleRVo> recordVos = qpAirBottleService.selectQpAirBottleList(list);
            info.setRows(recordVos);
        }
        return info;
    }

    /**
     * 供应商查询气瓶列表
     */
    @GetMapping("/supplier/list")
    @ApiOperation(value = "查询气瓶列表")
    public TableDataInfo<QpAirBottleRVo> supplierList(QpAirBottle qpAirBottle) {
        qpAirBottleService.setSupplier(qpAirBottle);
        startPage("create_time", "descending");
        List<QpAirBottle> list = qpAirBottleService.getSupplierList(qpAirBottle);
        return getVoTableDataInfo(list);
    }

    /**
     * 获取气瓶详细信息
     */
    @ApiOperation(value = "获取气瓶详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<QpAirBottleRVo> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpAirBottleService.getInfo(id));
    }

    /**
     * 新增气瓶
     */
    @ApiOperation(value = "新增气瓶")
    @Log(title = "气瓶", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public ResultData<Boolean> add(@RequestBody QpAirBottleDto bottleDto) {
        return ResultData.result(qpAirBottleService.insertQpAirBottle(bottleDto));
    }

    /**
     * 修改气瓶
     */
    @ApiOperation(value = "修改气瓶")
    @Log(title = "气瓶", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody QpAirBottleDto bottleDto) {
        return ResultData.result(qpAirBottleService.update(bottleDto));
    }

    /**
     * 删除气瓶
     */
    @ApiOperation(value = "删除气瓶")
    @PreAuthorize(hasPermi = "airbottle:bottle:remove")
    @Log(title = "气瓶", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids) {
        return ResultData.result(qpAirBottleService.deleteQpAirBottleByIds(ids));
    }
}