package com.zd.airbottle.controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.airbottle.domain.*;
import com.zd.airbottle.domain.vo.*;
import com.zd.airbottle.service.*;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.StringUtils;
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.service.TokenService;
import com.zd.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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.*;

/**
 * 待办清单表Controller
 */
@RestController
@Api(tags = "【待办清单表】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
//@ApiSupport(author = Knife4jConfiguration.Author.)
@RequestMapping("/task")
public class QpTaskController extends BaseController
{
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");

    @Autowired
    private IQpTaskService qpTaskService;

    @Autowired
    private IQpTaskDetailService qpTaskDetailService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IQpTransportUserService qpTransportUserService;

    @Autowired
    private IQpTransportCarService qpTransportCarService;

    @Autowired
    private IQpSupplierService supplierService;

    @Autowired
    private IQpAirGoodsConfigService qpAirGoodsConfigService;

    @Autowired
    private IQpBottleStorageService qpBottleStorageService;

    @Autowired
    private IQpBottleStorageOutService qpBottleStorageOutService;

    @Autowired
    private IQpSupplierService qpSupplierService;

    /**
     * 查询待办清单表列表
     */
    // @PreAuthorize(hasPermi = "airbottle:task:list")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询待办清单表列表")
    public TableDataInfo<QpTask> list(QpTaskVO qpTask)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        qpTask.setUserId(sysUser.getUserId());//当前登陆人申请数据

        startPage();
        List<QpTaskVO> list = qpTaskService.selectQpTaskList(qpTask);
        return getDataTable(list);
    }

    /****
     * 查询供应商的货物配置信息
     */
    @GetMapping("/listGood")
    @ApiOperation(value = "查询供应商的货物配置信息")
    public TableDataInfo<QpAirGoodsConfigRelationVo> listGood(QpAirGoodsConfigRelationVo relationVo)
    {
        startPage();
        List<QpAirGoodsConfigRelationVo> list = qpAirGoodsConfigService.selectQpAirGoodsConfigRelationList(relationVo);
        return getDataTable(list);
    }

    /**
     * 管理端申请记录列表
     */
    // @PreAuthorize(hasPermi = "airbottle:task:list")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    @GetMapping("/apply/list")
    @ApiOperation(value = "管理端申请记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyName",value = "申请人/联系电话/实验地点"),
            @ApiImplicitParam(name = "applyTime",value = "申请时间")
    })
    public TableDataInfo<QpTask> applyList(String applyName, Date applyTime)
    {
        startPage();
        List<QpTask> list=qpTaskService.getApplyList(applyName,applyTime);
        return getDataTable(list);
    }

    /**
     * 查询待办清单表列表-App
     */
    // @PreAuthorize(hasPermi = "airbottle:task:list")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    @GetMapping("/listApp")
    @ApiOperation(value = "查询待办清单表列表-APP")
    public TableDataInfo<QpTaskVO> listApp(QpTaskVO qpTaskVo)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        QpSupplier supplier = qpSupplierService.selectQpSupplierByAccountId(sysUser.getUserId());
        if(supplier==null){
            throw new ServiceException("未查询到相关供应商信息");
        }
        startPage();
        qpTaskVo.setCompanyId(supplier.getId());
        List<QpTaskVO> listDate = qpTaskService.selectQpTaskListOrderByDate(qpTaskVo);
        if(!listDate.isEmpty()){
            for (QpTaskVO task : listDate) {
                // 1.待办清单集合
                /*QpTaskDetailVO taskDetail = new QpTaskDetailVO();
                taskDetail.setCreateTime(qpTaskVO.getCreateTime());
                taskDetail.setSupplierId(supplier.getId());
                taskDetail.setIsDispose(0L);
                List<QpTaskDetailVO> taskDetailsList = qpTaskDetailService.selectQpTaskDetailListVo(taskDetail);

                for (QpTaskDetailVO td:taskDetailsList) {
                    td.setRemark("gq");
                    QpTaskVO taskVO = qpTaskService.selectQpTaskById(td.getTaskId());
                    td.setCampus(taskVO.getCampus());//校区
                    td.setRoom(taskVO.getRoom());//房间号
                    td.setBuilding(taskVO.getBuilding());//楼栋
                }
                qpTaskVO.setDetailListVO(taskDetailsList);*/

                qpTaskVo.setCreateTime(task.getCreateTimeApp());
                qpTaskVo.setCompanyId(supplier.getId());//商家id
                qpTaskVo.setIsDispose(0);//未处理
                List<QpTaskVO> list =  qpTaskService.selectQpTaskBySubjectRelation(qpTaskVo);

                if (!list.isEmpty()) {
                        //清单详情
                        for (QpTaskVO taskVO : list) {
                            taskVO.setRemark("gq");
                        /*List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(taskVO.getId());
                        taskVO.setDetailListVO(detailList);*/
                            QpTaskDetail taskDetail = new QpTaskDetail();
                            taskDetail.setTaskId(taskVO.getId());
                            taskDetail.setSupplierId(supplier.getId());
                            taskDetail.setIsDispose(0L);
                            List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailListVo(taskDetail);
                            for (QpTaskDetailVO tdvo:detailList) {
                                JSONObject jsonStr = JSONObject.parseObject(tdvo.getApplyDescribe());
                                tdvo.setConfigName(jsonStr.get("gasLevel").toString());
                                tdvo.setAirName(jsonStr.get("airName").toString());
                            }
                            taskVO.setDetailListVO(detailList);
                        }
                    task.setTaskVOList(list);
                }


                //2.入库-问题气瓶
                QpBottleStorageVO bottleStorage = new QpBottleStorageVO();
                bottleStorage.setCreateTime(task.getCreateTimeApp());
                bottleStorage.setAirStatus(1L);//问题气瓶
                bottleStorage.setCompanyId(supplier.getId());//商家id
                bottleStorage.setRemark("0");//未处理
                List<QpBottleStorageVO>  storageList = qpBottleStorageService.selectQpBottleStorageByRelationSubject(bottleStorage);

                if (!storageList.isEmpty()) {
                    //问题气瓶详情
                    for (QpBottleStorageVO bottleStorageVO : storageList) {
                        bottleStorageVO.setRemark("wt");
                        //List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(bottleStorageVO.getTaskDetailId());
                        List<QpTaskDetailVO> detailList = new ArrayList<>();
                        QpTaskDetailVO taskDetailVO = qpTaskDetailService.selectRelationByDetailsId(bottleStorageVO.getTaskDetailId());
                        detailList.add(taskDetailVO);
                        bottleStorageVO.setDetailListVO(detailList);
                    }
                    task.setBottleStorageVOList(storageList);
                }


                //3.出库
                QpBottleStorageOutVO storageOutVO = new QpBottleStorageOutVO();
                storageOutVO.setCreateTime(task.getCreateTimeApp());
                storageOutVO.setCompanyId(supplier.getId());//商家id
                storageOutVO.setRemark("0");
                List<QpBottleStorageOutVO> storageOutList =  qpBottleStorageOutService.selectQpBottleStorageOutRelationSubject(storageOutVO);

                if (!storageOutList.isEmpty()) {
                    //出库气瓶详情
                    for (QpBottleStorageOutVO qpBottleStorageOutVO : storageOutList) {
                        qpBottleStorageOutVO.setRemark("hs");
                        QpBottleStorage bs = qpBottleStorageService.selectQpBottleStorageById(qpBottleStorageOutVO.getStorageId());
                        //List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(bs.getTaskDetailId());
                        List<QpTaskDetailVO> detailList = new ArrayList<>();
                        QpTaskDetailVO taskDetailVO = qpTaskDetailService.selectRelationByDetailsId(bs.getTaskDetailId());
                        detailList.add(taskDetailVO);
                        qpBottleStorageOutVO.setDetailListVO(detailList);
                    }
                    task.setBottleStorageOutVOList(storageOutList);
                }

            }

        }
        return getDataTable(listDate);
    }

    /**
     * 查询供气回收记录列表-App
     */
    // @PreAuthorize(hasPermi = "airbottle:task:list")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    @GetMapping("/listAppReclaim")
    @ApiOperation(value = "查询供气回收记录列表-APP")
    public TableDataInfo<QpTaskVO> listAppReclaim(QpTaskVO qpTaskVo)
    {
        if(qpTaskVo.getRemarkStatus()==null){
            throw new ServiceException("参数缺失");
        }
        //remarkStatus 0 未处理, 1 已处理
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        QpSupplier supplier = qpSupplierService.selectQpSupplierByAccountId(sysUser.getUserId());

        if(supplier==null){
            throw new ServiceException("未查询到相关用户信息");
        }
        startPage();
        qpTaskVo.setCompanyId(supplier.getId());
        List<QpTaskVO> listDate = qpTaskService.selectQpTaskListOrderByDateReclaim(qpTaskVo);
        if(!listDate.isEmpty()){
            for (QpTaskVO qpTaskVO : listDate) {
                // 1.待办清单集合
                qpTaskVo.setCreateTime(qpTaskVO.getCreateTime());
                qpTaskVo.setIsDispose(qpTaskVo.getRemarkStatus());//处理状态
                qpTaskVo.setCompanyId(supplier.getId());//商家id
                List<QpTaskVO> list =  qpTaskService.selectQpTaskBySubjectRelation(qpTaskVo);

                if (!list.isEmpty()) {
                    //清单详情
                    for (QpTaskVO taskVO : list) {
                        taskVO.setRemark("gq");
                        /*List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(taskVO.getId());
                        taskVO.setDetailListVO(detailList);*/
                        QpTaskDetail taskDetail = new QpTaskDetail();
                        taskDetail.setTaskId(taskVO.getId());
                        taskDetail.setSupplierId(supplier.getId());
                        taskDetail.setIsDispose(Long.valueOf(qpTaskVo.getRemarkStatus()));
                        List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailListVo(taskDetail);
                        for (QpTaskDetailVO tdvo:detailList) {
                            JSONObject jsonStr = JSONObject.parseObject(tdvo.getApplyDescribe());
                            tdvo.setConfigName(jsonStr.get("gasLevel").toString());
                            tdvo.setAirName(jsonStr.get("airName").toString());
                        }
                        taskVO.setDetailListVO(detailList);
                    }
                    qpTaskVO.setTaskVOList(list);
                }

                //2.入库-问题气瓶
                QpBottleStorageVO bottleStorage = new QpBottleStorageVO();
                bottleStorage.setCreateTime(qpTaskVO.getCreateTime());
                bottleStorage.setAirStatus(1L);//问题气瓶
                bottleStorage.setCompanyId(supplier.getId());//供应商id
                if(0==qpTaskVo.getRemarkStatus()){
                    bottleStorage.setRemark("0");//待处理
                }else if(1==qpTaskVo.getRemarkStatus()){
                    bottleStorage.setRemark("1");//已处理
                }
                List<QpBottleStorageVO>  storageList = qpBottleStorageService.selectQpBottleStorageByRelationSubject(bottleStorage);

                if (!storageList.isEmpty()) {
                    //问题气瓶详情
                    for (QpBottleStorageVO bottleStorageVO : storageList) {
                        bottleStorageVO.setRemark("wt");
                        //List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(storageList.get(o).getTaskDetailId());
                        QpTaskDetailVO detail = qpTaskDetailService.selectRelationByDetailsId(bottleStorageVO.getTaskDetailId());
                        List<QpTaskDetailVO> detailList = new ArrayList<>();
                        detailList.add(detail);
                        bottleStorageVO.setDetailListVO(detailList);
                    }
                    qpTaskVO.setBottleStorageVOList(storageList);
                }


                //3.出库
                QpBottleStorageOutVO storageOutVO = new QpBottleStorageOutVO();
                storageOutVO.setCreateTime(qpTaskVO.getCreateTime());
                storageOutVO.setRemark(qpTaskVo.getRemarkStatus().toString());
                storageOutVO.setCompanyId(supplier.getId());//供应商id
                if(0==qpTaskVo.getRemarkStatus()){
                    storageOutVO.setRemark("0");//待处理
                }else if(1==qpTaskVo.getRemarkStatus()){
                    storageOutVO.setRemark("1");//已处理
                }
                List<QpBottleStorageOutVO> storageOutList =  qpBottleStorageOutService.selectQpBottleStorageOutRelationSubject(storageOutVO);

                if (!storageOutList.isEmpty()) {
                    //出库气瓶详情
                    for (QpBottleStorageOutVO qpBottleStorageOutVO : storageOutList) {
                        qpBottleStorageOutVO.setRemark("hs");
                        QpBottleStorage bs = qpBottleStorageService.selectQpBottleStorageById(qpBottleStorageOutVO.getStorageId());
                        //List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(bs.getTaskId());
                        QpTaskDetailVO taskDetailVO = qpTaskDetailService.selectRelationByDetailsId(bs.getTaskDetailId());
                        List<QpTaskDetailVO> detailList = new ArrayList<>();
                        detailList.add(taskDetailVO);
                        qpBottleStorageOutVO.setDetailListVO(detailList);
                    }
                    qpTaskVO.setBottleStorageOutVOList(storageOutList);
                }

            }

        }
        return getDataTable(listDate);
    }

    /**
     * 导出待办清单表列表
     */
    @ApiOperation(value = "导出待办清单表列表")
    // @PreAuthorize(hasPermi = "airbottle:task:export")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.EXPORT)
    @Log(title = "待办清单表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpTaskVO qpTask) throws IOException
    {
        if("login".equals(qpTask.getRemark())){
            //获取当前登录用户信息
            SysUser sysUser = tokenService.getLoginUser().getSysUser();
            qpTask.setUserId(sysUser.getUserId());//当前登陆人申请数据
        }

        List<QpTaskVO> list = qpTaskService.selectQpTaskList(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);
    }

    /**
     * 获取待办清单表详细信息-根据待办id
     */
    @ApiOperation(value = "获取待办清单表详细信息-根据待办id")
    // @PreAuthorize(hasPermi = "airbottle:task:query")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Map<String,Object> map = new HashMap<>();
        //待办清单信息
        QpTask qpTask = qpTaskService.selectQpTaskById(id);
        map.put("qpTask",qpTask);

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

        QpTransportCarVo carvo;
        if(StringUtils.isNotNull(qpTask.getCarId())){
            //车辆详情
            carvo = qpTransportCarService.selectQpTransportCarById(qpTask.getCarId());
            map.put("car", carvo);
        }*/

        //待办清单详情信息
        List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailByIdRelation(id);
        map.put("detailList",detailList);
        return AjaxResult.success(map);
    }

    /**
     * 获取待办清单表详细信息根据详细id
     */
    @ApiOperation(value = "获取待办清单表详细信息-根据详情id")
    // @PreAuthorize(hasPermi = "airbottle:task:query")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.QUERY)
    @GetMapping(value = "/getInfoById")
    public AjaxResult getInfoById(Long id)
    {
        Map<String,Object> map = new HashMap<>();

        //代办清单详情
        QpTaskDetail taskDetail = qpTaskDetailService.selectQpTaskDetailById(id);
        map.put("taskDetail",taskDetail);

        //待办清单信息
        QpTask qpTask = qpTaskService.selectQpTaskById(taskDetail.getTaskId());
        map.put("qpTask",qpTask);

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

        QpTransportCarVo carvo;
        if(StringUtils.isNotNull(taskDetail.getCarId())){
            //车辆详情
            carvo = qpTransportCarService.selectQpTransportCarById(taskDetail.getCarId());
            map.put("car", carvo);
        }

        //待办清单详情信息
        QpTaskDetailVO detailVO = new QpTaskDetailVO();
        detailVO.setTaskId(qpTask.getId());
        detailVO.setSupplierId(taskDetail.getSupplierId());
        List<QpTaskDetailVO> detailList = qpTaskDetailService.selectQpTaskDetailListVo(detailVO);
        for (QpTaskDetailVO detavo:detailList) {
            JSONObject jsonStr = JSONObject.parseObject(detavo.getApplyDescribe());
            detavo.setConfigName(jsonStr.get("gasLevel").toString());
            detavo.setAirName(jsonStr.get("airName").toString());
        }
        map.put("detailList",detailList);
        return AjaxResult.success(map);
    }

    /**
     * 新增待办清单表
     */
    @ApiOperation(value = "新增待办清单表")
    // @PreAuthorize(hasPermi = "airbottle:task:add")
    // @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.ADD)
    @Log(title = "待办清单表", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Boolean> add(@RequestBody QpTaskVO qpTaskVO)
    {
        return ResultData.result(qpTaskService.insertQpTask(qpTaskVO));
    }

    /**
     * 修改待办清单表
     */
    @ApiOperation(value = "修改待办清单表")
   // @PreAuthorize(hasPermi = "airbottle:task:edit")
   // @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.EDIT)
    @Log(title = "待办清单表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody QpTaskVO qpTaskVO)
    {
        return  ResultData.result(qpTaskService.updateQpTask(qpTaskVO));
    }

    /**
     * 删除待办清单表
     */
    @ApiOperation(value = "删除待办清单表")
    // @PreAuthorize(hasPermi = "airbottle:task:remove")
    // @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.REMOVE)
    @Log(title = "待办清单表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids)
    {
        return ResultData.result(qpTaskService.deleteQpTaskByIds(ids));
    }

    /***
     * 获取当前登录商家的人员和车辆信息
     */
    @GetMapping(value = "/getUserAndCar")
    public AjaxResult getUserAndCar(){
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        Map<String,Object> map = new HashMap<>();
        //查询供应商信息
        QpSupplier qpSupplier = supplierService.selectQpSupplierByAccountId(sysUser.getUserId());
        //查询注册人员
        QpTransportUser transportUser = new QpTransportUser();
        transportUser.setSupplierId(qpSupplier.getId());
        transportUser.setIsAudit(1);
        List<QpTransportUser> userlist=qpTransportUserService.selectQpTransportUserList(transportUser);
        map.put("userlist",userlist);

        //查询注册车辆
        QpTransportCarVo car = new QpTransportCarVo();
        car.setSupplierId(qpSupplier.getId());
        car.setIsAudit(1);
        List<QpTransportCar>  carlist= qpTransportCarService.selectCarList(car);
        map.put("carlist",carlist);
        return AjaxResult.success(map);
    }

    /**
     * 气瓶信息 按照实验室分组
     */
    @GetMapping("/listGroupBySubjectName")
    @ApiOperation(value = "气瓶信息 按照实验室分组")
    public TableDataInfo<QpTaskVO> listGroupBySubjectName(QpTaskVO qpTaskVo){

        if("login".equals(qpTaskVo.getRemark())){
            //获取当前登录用户信息
            SysUser sysUser = tokenService.getLoginUser().getSysUser();
            /*QpSupplier supplier = qpSupplierService.selectQpSupplierByAccountId(sysUser.getUserId());
            if(supplier==null){
                throw new ServiceException("未获取到相关用户信息");
            }
            qpTaskVo.setCompanyId(supplier.getId());*/
            qpTaskVo.setUserId(sysUser.getUserId());
        }

        startPage();
        List<QpTaskVO> list = qpTaskService.selectUseRecord(qpTaskVo);
        for (QpTaskVO taskVO:list) {
            qpTaskVo.setLocationId(taskVO.getLocationId());
            List<QpAirGoodsConfigRelationVo> relationVoList = qpTaskService.selectUseRecordBySubjectId(qpTaskVo);
            taskVO.setAirGoodsConfigRelationVo(relationVoList);

            for (QpAirGoodsConfigRelationVo gcfVo:relationVoList) {
                gcfVo.setUseTimeApp(gcfVo.getUseTime()!=null?sdf.format(gcfVo.getUseTime()):null);
                gcfVo.setBackTimeApp(gcfVo.getBackTime()!=null?sdf.format(gcfVo.getBackTime()):null);
            }
        }
        return getDataTable(list);
    }

    /**
     *  用气管理首页 资格申请 气瓶信息
     */
    @GetMapping("/listuseRecordByUserId")
    @ApiOperation(value = "用气管理首页 资格申请 气瓶信息")
    public TableDataInfo<QpAirGoodsConfigRelationVo> listuseRecordByUserId(QpTaskVO qpTaskVo){
        SysUser sysUser = tokenService.getLoginUser().getSysUser();

        startPage();
        qpTaskVo.setUserId(sysUser.getUserId());
        List<QpAirGoodsConfigRelationVo> relationVoList = qpTaskService.selectUseRecordBySubjectId(qpTaskVo);
        for (QpAirGoodsConfigRelationVo gcfVo:relationVoList) {
            gcfVo.setUseTimeApp(gcfVo.getUseTime()!=null?sdf.format(gcfVo.getUseTime()):null);
            gcfVo.setBackTimeApp(gcfVo.getBackTime()!=null?sdf.format(gcfVo.getBackTime()):null);
        }
        return getDataTable(relationVoList);
    }

    /**
     * 气瓶信息 -列表
     */
    @GetMapping("/taskListDetail")
    @ApiOperation(value = "气瓶信息-列表")
    public TableDataInfo<QpAirGoodsConfigRelationVo> taskListDetail(QpAirGoodsConfigRelationVo airGoodsConfigRelationVo){
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        startPage();
        airGoodsConfigRelationVo.setUserId(sysUser.getUserId());
        List<QpAirGoodsConfigRelationVo> relationVoList = qpTaskDetailService.selectQpTaskDetailRelationList(airGoodsConfigRelationVo);
        return getDataTable(relationVoList);
    }

    /***
     * 待办清单下 待入库列表查询
     */
    // @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    @GetMapping("/listDrk")
    @ApiOperation(value = "待办清单下 待入库列表查询")
    public AjaxResult  listDrk(Long id)
    {
        //查询该待办清单下的具体申请信息
        QpTaskDetail taskDetail = new QpTaskDetail();
        taskDetail.setTaskId(id);
        taskDetail.setQpStatus(1L);//已派送
        List<QpTaskDetail> detaillist = qpTaskDetailService.selectQpTaskDetailList(taskDetail);
        Map<String,Object> map = new HashMap<>();
        List<QpAirGoodsConfigRelationVo> listRvo = new ArrayList<>();//返回的数据集合

        for (QpTaskDetail detail:detaillist) {
            //查询某个规格气瓶的入库数量
            QpBottleStorageVO bottleStorageVo = new QpBottleStorageVO();
            bottleStorageVo.setTaskId(detail.getTaskId());
            bottleStorageVo.setTaskDetailId(detail.getId());
            List<QpBottleStorageVO> bslist =  qpBottleStorageService.selectQpBottleStorageList(bottleStorageVo);
            detail.setBottleNumber(detail.getBottleNumber()-bslist.size());//未入库数量

            for (int i=0;i<detail.getBottleNumber();i++){
                List<QpAirGoodsConfigRelationVo> cfrlist = qpTaskDetailService.selectQpTaskDetailByDetailsIdRelation(detail.getId());
                if(!cfrlist.isEmpty()){
                    QpAirGoodsConfigRelationVo rvo = new QpAirGoodsConfigRelationVo();//
                    rvo.setId(cfrlist.get(0).getId());//
                    rvo.setAirName(cfrlist.get(0).getAirName());//气体名称
                    //rvo.setSpecValue(cfrlist.get(0).getSpecValue());//规格
                    rvo.setCompanyName(cfrlist.get(0).getCompanyName());//公司名称
                    rvo.setConfigName(cfrlist.get(0).getConfigName());
                    rvo.setAirConstituents(cfrlist.get(0).getAirConstituents());//气体组分
                    rvo.setAirBottleId(cfrlist.get(0).getAirBottleId());
                    rvo.setAirBottleConfigId(cfrlist.get(0).getAirBottleConfigId());
                    rvo.setTaskId(detail.getTaskId());//待办清单id
                    listRvo.add(rvo);
                }
            }
        }
        map.put("list",listRvo);
        return AjaxResult.success(map);
    }

    /***
     * 待办清单下 未派送 列表查询
     */
    // @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TASK+ PerFun.LIST)
    @GetMapping("/listWps")
    @ApiOperation(value = "查询待办清单表列表")
    public AjaxResult listWps(Long id)
    {
        Map<String,Object> map = new HashMap<>();

        //查询该待办清单下的具体申请信息
        QpTaskDetail taskDetail = new QpTaskDetail();
        taskDetail.setTaskId(id);
        taskDetail.setQpStatus(0L);//未派送
        List<QpTaskDetail> detaillist = qpTaskDetailService.selectQpTaskDetailList(taskDetail);
        List<QpAirGoodsConfigRelationVo> listRvo = new ArrayList<>();//返回的数据集合


        for (QpTaskDetail detail:detaillist) {
            for (int i=0;i<detail.getBottleNumber();i++){
                List<QpAirGoodsConfigRelationVo> cfrlist = qpTaskDetailService.selectQpTaskDetailByDetailsIdRelation(detail.getId());
                if(!cfrlist.isEmpty()){
                    QpAirGoodsConfigRelationVo rvo = new QpAirGoodsConfigRelationVo();//
                    rvo.setAirName(cfrlist.get(0).getAirName());//气体名称
                    //rvo.setSpecValue(cfrlist.get(0).getSpecValue());//规格
                    rvo.setCompanyName(cfrlist.get(0).getCompanyName());//公司名称
                    rvo.setConfigName(cfrlist.get(0).getConfigName());
                    rvo.setTaskId(detail.getTaskId());//待办清单id
                    listRvo.add(rvo);
                }
            }
        }
        map.put("list",listRvo);
        return AjaxResult.success(map);
    }

    /**
     *待办清单下 已入库 列表查询
     */
    @GetMapping("/listYrk")
    @ApiOperation(value = "查询待办清单表列表")
    public TableDataInfo<QpAirGoodsConfigRelationVo> listYrk(Long id)
    {
        startPage();
        List<QpAirGoodsConfigRelationVo> list = qpTaskDetailService.selectQpTaskDetailDealWith(id);
        return getDataTable(list);
    }


    /**
     *待办清单下 已拒收 列表查询
     */
    @GetMapping("/listYjs")
    @ApiOperation(value = "查询待办清单表列表")
    public TableDataInfo<QpAirGoodsConfigRelationVo> listYjs(Long id)
    {
        startPage();
        List<QpAirGoodsConfigRelationVo> list = qpTaskDetailService.selectQpTaskDetailRefuse(id);
        return getDataTable(list);
    }

    /***
     * 查询实验室气瓶申请余量和个人余量
     */
    @GetMapping("/listSubjectDetal")
    public AjaxResult listSubjectDetal(Long subjectId)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        /*qpTask.setUserId(sysUser.getUserId());//当前登陆人申请数据*/
        Map<String,Object> map = new HashMap<>();

        //实验室配置的气瓶数量
        List<Map<String, Object>> list = qpTaskService.selectSubjectDetail(subjectId);
        int bottleTotal=0;//实验室总数
        int bottleUserTotal=0;//个人总数
        boolean flgSubject=false;//实验室是否设置数据
        boolean flgUser=false;//实验室个人是否设置数据
        if(!list.isEmpty() && list.get(0).get("bottleTotal")!=null){
            bottleTotal = Integer.parseInt(list.get(0).get("bottleTotal").toString());
            flgSubject=true;
        }
        if(!list.isEmpty() && list.get(0).get("bottleUserTotal")!=null){
            bottleUserTotal = Integer.parseInt(list.get(0).get("bottleUserTotal").toString());
            flgUser=true;
        }

        //查询实验室已经入库的气瓶数量
        QpBottleStorageVO bottleStorageVO = new QpBottleStorageVO();
        bottleStorageVO.setSubjectId(subjectId);
        List<QpBottleStorageVO> intolist = qpBottleStorageService.selectIntoStorageList(bottleStorageVO);
        int surplusSubject=bottleTotal-intolist.size();//实验室余额


        QpBottleStorageVO bs = new QpBottleStorageVO();
        bs.setUserId(sysUser.getUserId());
        List<QpBottleStorageVO> userlist = qpBottleStorageService.selectIntoStorageList(bs);
        int surplusUser = bottleUserTotal-userlist.size();//个人余额


        map.put("subjectNum", Math.max(surplusSubject, 0));
        map.put("userNum", Math.max(surplusUser, 0));
        map.put("flgSubject",flgSubject);
        map.put("flgUser",flgUser);
        return AjaxResult.success(map);
    }

}
