package com.zd.airbottle.controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.airbottle.domain.QpBottleStorage;
import com.zd.airbottle.domain.QpTask;
import com.zd.airbottle.domain.vo.*;
import com.zd.airbottle.service.*;
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-19
 */
@RestController
@Api(tags = "【气瓶入库-管理端】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
// @ApiSupport(author = Knife4jConfiguration.Author.)
@RequestMapping("/storageManage")
public class QpBottleStorageManageController extends BaseController
{
    @Autowired
    private IQpBottleStorageService qpBottleStorageService;

    @Autowired
    private IQpTaskService qpTaskService;

    @Autowired
    private   IQpTaskDetailService qpTaskDetailService;

    @Autowired
    private IQpTransportUserService qpTransportUserService;

    @Autowired
    private IQpTransportCarService qpTransportCarService;

    /**
     * 查询气瓶入库列表
     */
    //@PreAuthorize(hasPermi = "airbottle:storage:list")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_STORAGE+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询气瓶入库列表")
    public TableDataInfo<QpBottleStorageVO> list(QpBottleStorageVO qpBottleStorage)
    {
        startPage();
        List<QpBottleStorageVO> list = qpBottleStorageService.selectQpBottleStorageList(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 = "airbottle:storage:export")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_STORAGE+ PerFun.EXPORT)
    @Log(title = "气瓶入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    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.selectQpBottleStorageList(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);
    }

    /**
     * 获取气瓶入库详细信息
     */
    @ApiOperation(value = "获取气瓶入库详细信息")
    @PreAuthorize(hasPermi = "airbottle:storage:query")
    @GetMapping(value = "/{id}")
    public ResultData<QpBottleStorage> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(qpBottleStorageService.selectQpBottleStorageById(id));
    }

    /***
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取气瓶入库详细信息")
   // @PreAuthorize(hasPermi = "airbottle:storage:query")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_STORAGE+ PerFun.QUERY)
    @GetMapping(value = "/getInfoById")
    public AjaxResult getInfoById(Long id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        //入库详情
        QpBottleStorage bottleStorage = qpBottleStorageService.selectQpBottleStorageById(id);
        map.put("bottleStorage",bottleStorage);
        //规格详情
        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:storage:add")
    @Log(title = "气瓶入库", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody QpBottleStorageVO qpBottleStorage)
    {
        return ResultData.result(qpBottleStorageService.insertQpBottleStorage(qpBottleStorage));
    }

    /**
     * 修改气瓶入库
     */
    @ApiOperation(value = "修改气瓶入库")
    @PreAuthorize(hasPermi = "airbottle:storage:edit")
    @Log(title = "气瓶入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody QpBottleStorage qpBottleStorage)
    {
        return  ResultData.result(qpBottleStorageService.updateQpBottleStorage(qpBottleStorage));
    }

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

    /**
     * 入库点击事件  根据待办清单id查询
     */
    @ApiOperation(value = "入库点击事件 数据集合查询")
    @GetMapping(value = "/getTaskInfo")
    public AjaxResult getTaskInfo(Long id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        //待办清单详情
        /*QpTask task = qpTaskService.selectQpTaskById(id);

        //人员详情
        QpTransportUserVo user = qpTransportUserService.selectQpTransportUserById(task.getTransportId());
        map.put("user",user);

        //车辆详情
        QpTransportCarVo carvo = qpTransportCarService.selectQpTransportCarById(task.getCarId());
        map.put("car",carvo);*/
        return AjaxResult.success(map);
    }

}
