package com.zd.airbottle.controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.airbottle.domain.QpBottleStorage;
import com.zd.airbottle.domain.QpBottleStorageOut;
import com.zd.airbottle.domain.QpTask;
import com.zd.airbottle.domain.vo.*;
import com.zd.airbottle.service.IQpBottleStorageOutService;
import com.zd.airbottle.service.IQpBottleStorageService;
import com.zd.airbottle.service.IQpTaskDetailService;
import com.zd.airbottle.service.IQpTaskService;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.poi.ExcelUtil;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private IQpBottleStorageService qpBottleStorageService;

    @Autowired
    private IQpTaskDetailService qpTaskDetailService;

    @Autowired
    private IQpTaskService qpTaskService;

    /**
     * 查询气瓶出库列表
     */
    //@PreAuthorize(hasPermi = "airbottle:out:list")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_STORAGE_OUT+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询气瓶出库列表")
    public TableDataInfo<QpBottleStorageOutVO> list(QpBottleStorageOutVO qpBottleStorageOut)
    {
        startPage();
        List<QpBottleStorageOutVO> list = qpBottleStorageOutService.selectQpBottleStorageOutList(qpBottleStorageOut);
        if(list.size()>0){
            for (int i=0;i<list.size();i++){
                //入库详情
                QpBottleStorage bottleStorage = qpBottleStorageService.selectQpBottleStorageById(list.get(i).getStorageId());
                //规格详情
                QpTaskDetailVO taskDetail = qpTaskDetailService.selectRelationByDetailsId(bottleStorage.getTaskDetailId());

                if(taskDetail!=null){
                    list.get(i).setLocation(taskDetail.getLocation());
                    list.get(i).setAirName(taskDetail.getAirName());
                    list.get(i).setParent(taskDetail.getParent());
                    list.get(i).setSpecValue(taskDetail.getSpecValue());
                    list.get(i).setCompanyName(taskDetail.getCompanyName());
                    list.get(i).setAirConstituents(taskDetail.getAirConstituents());
                }
                list.get(i).setAirNumber(bottleStorage.getAirNumber());
                JSONObject jsonStr = JSONObject.parseObject(bottleStorage.getSpecificationName());
                if(jsonStr!=null && jsonStr.containsKey("gasLevel")){
                    list.get(i).setConfigName(jsonStr.get("gasLevel").toString());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出气瓶出库列表
     */
    @ApiOperation(value = "导出气瓶出库列表")
    //@PreAuthorize(hasPermi = "airbottle:out:export")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_STORAGE_OUT+ PerFun.EXPORT)
    @Log(title = "气瓶出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,QpBottleStorageOutVO qpBottleStorageOut) throws IOException
    {
        List<QpBottleStorageOutExcelVo> listexcel = new ArrayList<QpBottleStorageOutExcelVo>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<QpBottleStorageOutVO> list = qpBottleStorageOutService.selectQpBottleStorageOutList(qpBottleStorageOut);
        if(list.size()>0){
            for (int i=0;i<list.size();i++){
                QpBottleStorageOutExcelVo  excelOut = new QpBottleStorageOutExcelVo();
                //入库详情
                QpBottleStorageVO bottleStorage = qpBottleStorageService.selectQpBottleStorageById(list.get(i).getStorageId());
                //规格详情
                QpTaskDetailVO taskDetail = qpTaskDetailService.selectRelationByDetailsId(bottleStorage.getTaskDetailId());
                excelOut.setApplyUserName(list.get(i).getApplyUserName());//操作人
                excelOut.setPhone(bottleStorage.getStorageUserPhone());//操作人电话
                //excelOut.setLocation(list.get(i).getRoom()+"-" +taskDetail.getLocation());//实验地点
                excelOut.setLocation(taskDetail.getLocation());//实验地点
                //excelOut.setCompanyName(taskDetail.getCompanyName());//气瓶商家
                excelOut.setAirName(taskDetail.getAirName());//气体名称
                JSONObject jsonStr = JSONObject.parseObject(taskDetail.getApplyDescribe());
                if(taskDetail.getApplyDescribe()!=null && jsonStr!=null && jsonStr.containsKey("gasLevel")){
                    excelOut.setBottleSpecification(jsonStr.get("gasLevel").toString());//气瓶规格
                }
                excelOut.setCreateTime(sdf.format(list.get(i).getCreateTime()));//申请时间

                if(0==list.get(i).getOutStatus()){
                    excelOut.setOutStatus("待回收");
                }else if(1==list.get(i).getOutStatus()){
                    excelOut.setOutStatus("已确认");
                }else if(2==list.get(i).getOutStatus()){
                    excelOut.setOutStatus("已出库");
                }
                listexcel.add(excelOut);
            }
            ExcelUtil<QpBottleStorageOutExcelVo> util = new ExcelUtil<QpBottleStorageOutExcelVo>(QpBottleStorageOutExcelVo.class);
            LocalDate nowDate = LocalDate.now();
            util.exportExcel(response, listexcel, "出库数据", null, "出库信息导出表"+nowDate);
        }
    }

    /**
     * 获取气瓶出库详细信息
     */
    @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")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_STORAGE_OUT+ PerFun.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);
        /*//规格详情
        QpTaskDetailVO taskDetailVO = qpTaskDetailService.selectRelationByDetailsId(bottleStorage.getTaskDetailId());
        map.put("taskDetail",taskDetailVO);*/
        //清单详情
        QpTask qpTask = qpTaskService.selectQpTaskById(bottleStorage.getTaskId());
        map.put("qpTask",qpTask);
        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));
    }
}
