package com.zd.airbottle.controller;

import com.zd.airbottle.domain.QpAirBottle;
import com.zd.airbottle.domain.QpAirGoogs;
import com.zd.airbottle.domain.dto.QpAirGoogsDto;
import com.zd.airbottle.domain.vo.QpAirGoogsVo;
import com.zd.airbottle.service.IQpAirBottleService;
import com.zd.airbottle.service.IQpAirGoogsService;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
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.annotation.Resource;
import java.util.List;

/**
 * 气瓶货物Controller
 */
@RestController
@Api(tags = "【气瓶货物】")
@RequestMapping("/googs")
public class QpAirGoogsController extends BaseController<QpAirGoogs> {
    @Autowired
    private IQpAirGoogsService qpAirGoogsService;
    @Resource
    private IQpAirBottleService bottleService;

    /**
     * 查询气瓶货物列表
     */
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_GOOGS+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询气瓶货物列表")
    public TableDataInfo<QpAirGoogsVo> list(QpAirGoogs qpAirGoogs) {
        startPage("create_time","descending");
        List<QpAirGoogs> googs = qpAirGoogsService.selectQpAirGoogsList(qpAirGoogs);
        return getVoTableDataInfo(googs);
    }

    /**
     * 获取气瓶货物详细信息
     */
    @ApiOperation(value = "获取气瓶货物详细信息")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_GOOGS+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<QpAirGoogsVo> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpAirGoogsService.getById(id));
    }

    /**
     * 小程序端获取货品配置信息
     */
    @ApiOperation(value = "小程序端获取货品配置信息")
    @GetMapping(value = "/info/{id}")
    public ResultData<QpAirGoogsVo> getConfigInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpAirGoogsService.getConfigInfo(id));
    }

    /**
     * 新增气瓶货物
     */
    @ApiOperation(value = "新增气瓶货物")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_GOOGS+ PerFun.ADD)
    @Log(title = "气瓶货物", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Boolean> add(@RequestBody QpAirGoogsDto googsDto) {
        return ResultData.result(qpAirGoogsService.insertQpAirGoogs(googsDto));
    }

    /**
     * 修改气瓶货物
     */
    @ApiOperation(value = "修改气瓶货物")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_GOOGS+ PerFun.EDIT)
    @Log(title = "气瓶货物", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody QpAirGoogsDto googsDto) {
        return ResultData.result(qpAirGoogsService.update(googsDto));
    }

    /**
     * 删除气瓶货物
     */
    @ApiOperation(value = "删除气瓶货物")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_GOOGS+ PerFun.REMOVE)
    @Log(title = "气瓶货物", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids) {
        for (Long id :ids) {
            QpAirBottle bottle=new QpAirBottle();
            bottle.setConfigId(id);
            List<QpAirBottle> list = bottleService.getList(bottle);
            if (!list.isEmpty()){
                return ResultData.fail("已有供应商使用该货品，无法删除。如删除该货品可能会出现异常");
            }
        }
        return ResultData.result(qpAirGoogsService.deleteQpAirGoogsByIds(ids));
    }


    /**
     * 查询气瓶货物列表-无权限
     */
    @GetMapping("/listGoogs")
    @ApiOperation(value = "查询气瓶货物列表-无权限")
    public TableDataInfo<QpAirGoogsVo> listGoogs(QpAirGoogs qpAirGoogs) {
        List<QpAirGoogs> googs = qpAirGoogsService.selectQpAirGoogsList(qpAirGoogs);
        return getVoTableDataInfo(googs);
    }

    private TableDataInfo<QpAirGoogsVo> getVoTableDataInfo(List<QpAirGoogs> googs) {
        TableDataInfo<QpAirGoogs> dataTable = getDataTable(googs);
        TableDataInfo<QpAirGoogsVo> dataInfo = new TableDataInfo<>();
        BeanUtils.copyProperties(dataTable, dataInfo);
        if (!googs.isEmpty()) {
            List<QpAirGoogsVo> list = qpAirGoogsService.list(googs);
            dataInfo.setRows(list);
        }
        return dataInfo;
    }

    /**
     * 查询气瓶货物列表-小程序
     */
    @GetMapping("/listApp")
    @ApiOperation(value = "查询气瓶货物列表-小程序无权限")
    public TableDataInfo<QpAirGoogsVo> listApp(QpAirGoogs qpAirGoogs) {
        startPage();
        List<QpAirGoogs> googs = qpAirGoogsService.selectQpAirGoogsList(qpAirGoogs);
        return getVoTableDataInfo(googs);
    }
}
