package com.zd.airbottle.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zd.airbottle.domain.*;
import com.zd.airbottle.domain.vo.QpBottleStorageOutVO;
import com.zd.airbottle.domain.vo.QpTransportUserVo;
import com.zd.airbottle.service.*;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.web.domain.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.core.web.page.TableDataInfo;

/**
 * 气瓶出库Controller
 *
 * @author zd
 * @date 2022-05-23
 */
@RestController
@Api(tags = "【气瓶出库】")
@RequestMapping("/storageOut")
public class QpBottleStorageOutController extends BaseController
{
    @Autowired
    private IQpBottleStorageOutService qpBottleStorageOutService;

    @Autowired
    private IQpBottleStorageService qpBottleStorageService;

    @Autowired
    private IQpTaskDetailService qpTaskDetailService;

    @Autowired
    private IQpTaskService qpTaskService;

    @Autowired
    private IQpTransportUserService qpTransportUserService;

    /**
     * 查询气瓶出库列表
     */
    //@PreAuthorize(hasPermi = "airbottle:out:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询气瓶出库列表")
    public TableDataInfo<QpBottleStorageOutVO> list(QpBottleStorageOutVO qpBottleStorageOut)
    {
        startPage();
        List<QpBottleStorageOutVO> list = qpBottleStorageOutService.selectQpBottleStorageOutList(qpBottleStorageOut);
        return getDataTable(list);
    }

    /**
     * 导出气瓶出库列表
     */
    @ApiOperation(value = "导出气瓶出库列表")
    //@PreAuthorize(hasPermi = "airbottle:out:export")
    @Log(title = "气瓶出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpBottleStorageOutVO qpBottleStorageOut) throws IOException
    {
        List<QpBottleStorageOutVO> list = qpBottleStorageOutService.selectQpBottleStorageOutList(qpBottleStorageOut);
        ExcelUtil<QpBottleStorageOutVO> util = new ExcelUtil<QpBottleStorageOutVO>(QpBottleStorageOutVO.class);
        util.exportExcel(response, list, "气瓶出库数据");
    }

    /**
     * 获取气瓶出库详细信息
     */
    @ApiOperation(value = "获取气瓶出库详细信息")
    //@PreAuthorize(hasPermi = "airbottle:out:query")
    @GetMapping(value = "/{id}")
    public ResultData<QpBottleStorageOut> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(qpBottleStorageOutService.selectQpBottleStorageOutById(id));
    }

    /**
     * 获取气瓶出库详细信息
     */
    @ApiOperation(value = "获取气瓶出库详细信息")
    //@PreAuthorize(hasPermi = "airbottle:out:query")
    @GetMapping(value = "/getInfoById")
    public AjaxResult getInfoById(Long id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        //出库详情
        QpBottleStorageOut bottleStorageOut = qpBottleStorageOutService.selectQpBottleStorageOutById(id);
        QpBottleStorage bottleStorage = qpBottleStorageService.selectQpBottleStorageById(bottleStorageOut.getStorageId());
        map.put("bottleStorageOut",bottleStorageOut);

        //规格详情
        QpTaskDetail detail = new QpTaskDetail();
        detail.setId(bottleStorage.getTaskDetailId());
        detail.setTaskId(bottleStorage.getTaskId());
        List<QpTaskDetail> detailList = qpTaskDetailService.selectQpTaskDetailList(detail);
        map.put("taskDetail",detailList.size()>0?detailList.get(0):detail);

        //清单详情
        QpTask qpTask = qpTaskService.selectQpTaskById(bottleStorage.getTaskId());
        map.put("qpTask",qpTask);

        //人员详情
        if(StringUtils.isNotNull(bottleStorageOut.getTransportId()) ){
            QpTransportUserVo transportUser =qpTransportUserService.selectQpTransportUserById(bottleStorageOut.getTransportId());
            map.put("transportUser",transportUser);
        }

        return AjaxResult.success(map);
    }

    /**
     * 新增气瓶出库
     */
    @ApiOperation(value = "新增气瓶出库")
    //@PreAuthorize(hasPermi = "airbottle:out:add")
    @Log(title = "气瓶出库", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody QpBottleStorageOut qpBottleStorageOut)
    {
        return ResultData.result(qpBottleStorageOutService.insertQpBottleStorageOut(qpBottleStorageOut));
    }

    /**
     * 修改气瓶出库
     */
    @ApiOperation(value = "修改气瓶出库")
    //@PreAuthorize(hasPermi = "airbottle:out:edit")
    @Log(title = "气瓶出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody QpBottleStorageOut qpBottleStorageOut)
    {
        return  ResultData.result(qpBottleStorageOutService.updateQpBottleStorageOut(qpBottleStorageOut));
    }

    /**
     * 删除气瓶出库
     */
    @ApiOperation(value = "删除气瓶出库")
    //@PreAuthorize(hasPermi = "airbottle:out:remove")
    @Log(title = "气瓶出库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(qpBottleStorageOutService.deleteQpBottleStorageOutByIds(ids));
    }
}
