package com.zd.airbottle.controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.airbottle.domain.QpBottleStorage;
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.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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liujh
 * @Date: 2022/08/12/10:24
 * @Description:
 */
@RestController
@Api(tags = "【信息记录】")
@RequestMapping("/informationRecord")
public class QpInformationRecordController extends BaseController {

    @Autowired
    private IQpTaskService qpTaskService;

    @Autowired
    private IQpTaskDetailService qpTaskDetailService;

    @Autowired
    private IQpBottleStorageService qpBottleStorageService;

    @Autowired
    private IQpBottleStorageOutService qpBottleStorageOutService;

    /**
     * 申领记录
     */
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "taskList")
    @GetMapping("/taskList")
    @ApiOperation(value = "查询申领记录列表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "taskList")
    public TableDataInfo<QpTaskVO> list(QpTaskVO qpTask)
    {
        startPage();
        List<QpTaskVO> list = qpTaskService.selectQpTaskRecordList(qpTask);
        if(!list.isEmpty()){
            for (QpTaskVO qpTaskVO : list) {
                List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(qpTaskVO.getId());
                qpTaskVO.setDetailListVO(detailList);
            }
        }
        return getDataTable(list);
    }

    /**
     * 申领记录导出
     */
    @ApiOperation(value = "申领记录导出",notes = "权限字符："+ PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "taskExport")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "taskExport")
    @Log(title = "申领记录导出", businessType = BusinessType.EXPORT)
    @PostMapping("/taskExport")
    public void export(HttpServletResponse response, QpTaskVO qpTask) throws IOException
    {
        List<QpTaskVO> list = qpTaskService.selectQpTaskRecordList(qpTask);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<QpTaskExcelVo> listExcel = new ArrayList<>();
        for (QpTaskVO taskVO:list) {
            QpTaskExcelVo excelVo = new QpTaskExcelVo();
            excelVo.setUserName(taskVO.getUserName());//申领人
            excelVo.setPhone(taskVO.getPhone());//联系方式
            excelVo.setLocation(taskVO.getLocation());//实验地点
            excelVo.setStrTime(sdf.format(taskVO.getCreateTime()));//申领时间
            listExcel.add(excelVo);
        }
        ExcelUtil<QpTaskExcelVo> util = new ExcelUtil<>(QpTaskExcelVo.class);
        //本地日期,不包括时分秒
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, listExcel, "申领记录数据", null, "申领记录信息数据导出表"+nowDate);
    }

    /**
     * 信息记录-入库记录
     */
    @ApiOperation(value = "信息记录-入库记录",notes = "权限字符："+ PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "storageList")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "storageList")
    @GetMapping("/storageList")
    public TableDataInfo<QpBottleStorageVO> list(QpBottleStorageVO qpBottleStorage)
    {
        startPage();
        List<QpBottleStorageVO> list = qpBottleStorageService.selectQpBottleStorageRecordList(qpBottleStorage);
        if(list.size()>0){
            for (int i=0;i<list.size();i++){
                QpTaskDetailVO detailVO = qpTaskDetailService.selectRelationByDetailsId(list.get(i).getTaskDetailId());
                if(detailVO!=null){
                    list.get(i).setLocation(detailVO.getLocation());
                    list.get(i).setAirName(detailVO.getAirName());
                    list.get(i).setParent(detailVO.getParent());
                    list.get(i).setSpecValue(detailVO.getSpecValue());
                    list.get(i).setCompanyName(detailVO.getCompanyName());//
                    list.get(i).setAirConstituents(detailVO.getAirConstituents());
                }
                JSONObject jsonStr = JSONObject.parseObject(list.get(i).getSpecificationName());
                if(list.get(i).getSpecificationName()!=null && jsonStr!=null && jsonStr.containsKey("gasLevel")){
                    list.get(i).setConfigName(jsonStr.get("gasLevel").toString());
                }

            }
        }
        return getDataTable(list);
    }

    /**
     * 信息记录-入库记录导出
     */
    @ApiOperation(value = "信息记录-入库记录导出")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "storageExport")
    @Log(title = "气瓶入库", businessType = BusinessType.EXPORT)
    @PostMapping("/storageExport")
    public void export(HttpServletResponse response,QpBottleStorageVO qpBottleStorage) throws IOException
    {
        List<QpBottleStorageExcelVo> listVo = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<QpBottleStorageVO> list = qpBottleStorageService.selectQpBottleStorageRecordList(qpBottleStorage);
        if(list.size()>0){
            for (int i=0;i<list.size();i++){
                QpTaskDetailVO detailVO = qpTaskDetailService.selectRelationByDetailsId(list.get(i).getTaskDetailId());
                list.get(i).setQpTaskDetailVO(detailVO);
                QpBottleStorageExcelVo vo = new QpBottleStorageExcelVo();
                vo.setStorageUser(list.get(i).getStorageUser());//操作人
                vo.setPhone(list.get(i).getStorageUserPhone());//操作人电话
                //vo.setLocation(list.get(i).getRoom()+"-"+list.get(i).getQpTaskDetailVO().getLocation());//实验地点
                vo.setLocation(list.get(i).getQpTaskDetailVO().getLocation());//实验地点
                vo.setAirName(list.get(i).getQpTaskDetailVO().getAirName());//气体名称
                //vo.setBottleSpecification(list.get(i).getQpTaskDetailVO().getSpecValue());//气瓶规格.
                JSONObject jsonStr = JSONObject.parseObject(list.get(i).getSpecificationName());
                if(list.get(i).getSpecificationName()!=null && jsonStr!=null && jsonStr.containsKey("gasLevel")){
                    vo.setBottleSpecification(jsonStr.get("gasLevel").toString());
                }

                vo.setCompanyName(list.get(i).getQpTaskDetailVO().getCompanyName());//气瓶商家
                vo.setCreateTime(sdf.format(list.get(i).getCreateTime()) );//入库时间
                listVo.add(vo);
            }
        }
        ExcelUtil<QpBottleStorageExcelVo> util = new ExcelUtil<QpBottleStorageExcelVo>(QpBottleStorageExcelVo.class);
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, listVo, "入库数据", null, "入库信息导出表"+nowDate);
    }

    /**
     * 查询气瓶出库列表
     */
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "storageOutlist")
    @GetMapping("/storageOutlist")
    @ApiOperation(value = "查询气瓶出库列表")
    public TableDataInfo<QpBottleStorageOutVO> list(QpBottleStorageOutVO qpBottleStorageOut)
    {
        startPage();
        List<QpBottleStorageOutVO> list = qpBottleStorageOutService.selectQpBottleStorageOutRecordList(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 = PerPrefix.AIRBOTTLE_INFORMATIONRECORD+ "storageOutexport")
    @Log(title = "气瓶出库", businessType = BusinessType.EXPORT)
    @PostMapping("/storageOutexport")
    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.selectQpBottleStorageOutRecordList(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);
        }
    }
}
