package com.zd.bottle.controller;


import com.zd.bottle.domain.BottleStorage;
import com.zd.bottle.service.BottleStorageService;
import com.zd.bottle.service.UseRecordService;
import com.zd.bottle.vo.BottleStorageUseVo;
import com.zd.bottle.vo.BottleStorageVo;
import com.zd.bottle.vo.UseRecordVo;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
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.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 气瓶入库表 前端控制器
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@RestController
@Api(tags = "【入库】")
@RequestMapping("/bottleStorage")
public class BottleStorageController extends BaseController<BottleStorage> {

    @Autowired
    private BottleStorageService bottleStorageService;

    @Autowired
    private UseRecordService useRecordService;

    /**
     * 查询入库记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_BOTTLESTORAGE + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询报警记录列表")
    public TableDataInfo<BottleStorage> list(BottleStorageVo vo) {
        startPage("create_time","descending");
        List<BottleStorageVo> list = bottleStorageService.getListVo(vo);
        return getDataTable(list);
    }


    /**
     * 新增入库信息
     * @param bottleStorage 参数实体
     * @return Boolean
     */
    @ApiOperation(value = "新增入库信息")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_BOTTLESTORAGE + PerFun.ADD)
    @Log(title = "新增入库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Boolean> add(@RequestBody BottleStorage bottleStorage) {
        return ResultData.result(bottleStorageService.add(bottleStorage));
    }

    /***
     * 入库详情
     * @param id
     * @return
     */
    @ApiOperation(value = "入库详情")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_BOTTLESTORAGE + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Map<String,Object> map = new HashMap<String,Object>();
        //查询使用记录
        List<UseRecordVo> list = useRecordService.getListUseRecord(id);
        map.put("list",list);

        //查询入库详情
        BottleStorage bottleStorage = bottleStorageService.getById(id);
        map.put("bottleStorage",bottleStorage);
        return AjaxResult.success(map);
    }


    /**
     * 查询领用气瓶列表(管理端)
     */
    @GetMapping("/receivingGasList")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_BOTTLEUSEBYADMIN + PerFun.LIST)
    @ApiOperation(value = "查询领用气瓶列表")
    public TableDataInfo<BottleStorage> receivingGasList(BottleStorage bottleStorage) {
        startPage();
        List<BottleStorage> list = bottleStorageService.receivingGasList(bottleStorage);
        return getDataTable(list);
    }


    /**
     * 归还气瓶列表(管理端)
     */
    @GetMapping("/returnGas")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_BOTTLEUSEBYADMIN + PerFun.LIST)
    @ApiOperation(value = "归还气瓶列表")
    public TableDataInfo<BottleStorage> returnGas(BottleStorageUseVo bottleStorageUseVo) {
        startPage();
        bottleStorageUseVo.setUseUserId(SecurityUtils.getUserId());
        List<BottleStorageUseVo> list = bottleStorageService.returnGas(bottleStorageUseVo);
        return getDataTable(list);
    }

    /**
     * 查询领用气瓶列表(学生端)
     */
    @GetMapping("/receivingGasListByStu")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_BOTTLEUSEBYUSER + PerFun.LIST)
    @ApiOperation(value = "查询领用气瓶列表")
    public TableDataInfo<BottleStorage> receivingGasListByStu(BottleStorage bottleStorage) {
        startPage();
        List<BottleStorage> list = bottleStorageService.receivingGasList(bottleStorage);
        return getDataTable(list);
    }

    /**
     * 归还气瓶列表(学生端)
     */
    @GetMapping("/returnGasByStu")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_BOTTLEUSEBYUSER + PerFun.LIST)
    @ApiOperation(value = "归还气瓶列表")
    public TableDataInfo<BottleStorage> returnGasByStu(BottleStorageUseVo bottleStorageUseVo) {
        startPage();
        bottleStorageUseVo.setUseUserId(SecurityUtils.getUserId());
        List<BottleStorageUseVo> list = bottleStorageService.returnGas(bottleStorageUseVo);
        return getDataTable(list);
    }
}
