package com.zd.laboratory.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.enums.HazardTypeEnum;
import com.zd.common.core.valid.ValidGroup;
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 com.zd.laboratory.domain.LabHazard;
import com.zd.laboratory.domain.LabQpSubjectRelation;
import com.zd.laboratory.domain.dto.LabHazardDTO;
import com.zd.laboratory.domain.dto.LabHazardJoinSubDTO;
import com.zd.laboratory.domain.dto.LabHazardNewVo;
import com.zd.laboratory.domain.vo.LabHazardColumnarVO;
import com.zd.laboratory.domain.vo.LabHazardSubVO;
import com.zd.laboratory.domain.vo.LabHazardVO;
import com.zd.laboratory.service.ILabContainerService;
import com.zd.laboratory.service.ILabHazardService;
import com.zd.laboratory.service.ILabHazardSubjectRelationService;
import com.zd.laboratory.service.ILabQpSubjectRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 危险源Controller
 *
 * @author zd
 * @date 2021-09-01
 */
@RestController
@RequestMapping("/hazard")
@Api(tags = {"危险源管理"})
public class LabHazardController extends BaseController {
    @Autowired
    private ILabHazardService labHazardService;
    @Autowired
    private ILabContainerService labContainerService;
    @Autowired
    private ILabHazardSubjectRelationService labHazardSubjectRelationService;
    @Autowired
    private ILabQpSubjectRelationService labQpSubjectRelationService;
    /**
     * 查询危险源列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询危险源列表")
    public TableDataInfo<LabHazardDTO> list(LabHazardDTO labHazard) {
        startPage();
        List<LabHazardDTO> list = labHazardService.selectLabHazardAndContainerList(labHazard);
        return getDataTable(list);
    }

    /**
     * 查询实验室已有危险源关系
     */
    @GetMapping("/{subId}/list")
    @ApiOperation(value = "查询实验室已有危险源关系")
    public ResultData<List<LabHazardSubVO>> queryLabById(@PathVariable("subId") Long subId) {
        return ResultData.success(labHazardSubjectRelationService.queryHazardSubListVO(subId));
    }

//    /**
//     * 将危险源关联到多个实验室
//     */
//    @PostMapping("/addRelation")
//    @ApiOperation(value = "将危险源关联到多个实验室")
//    public AjaxResult addRelation(@RequestBody @Validated(ValidGroup.Crud.Update.class) LabHazardDTO labHazard) {
//
//        labHazardSubjectRelationService.addRelation(labHazard);
//        return AjaxResult.success();
//    }

    @GetMapping(value = "/getQpHazard")
    @ApiOperation(value = "获取实验室气瓶存储数量")
    public ResultData<LabQpSubjectRelation> getQpHazard(Long subId) {
        LabQpSubjectRelation LabQpSubjectRelation = new LabQpSubjectRelation();
        LabQpSubjectRelation.setSubjectId(subId);
        List<LabQpSubjectRelation> list = labQpSubjectRelationService.selectLabQpSubjectRelationList(LabQpSubjectRelation);
        if(list.size()>0){
            LabQpSubjectRelation = list.get(0);
        }
        return ResultData.success(LabQpSubjectRelation);
    }

    /**
     * 维护危险源与实验室关系
     */
    @PostMapping("/addRelation")
    @ApiOperation(value = "维护危险源与实验室关系")
    public AjaxResult addRelation(@RequestBody @Validated() LabHazardJoinSubDTO hazardJoinSubDTO) {

        labHazardSubjectRelationService.updateSubHazardsRelation(hazardJoinSubDTO, HazardTypeEnum.HAZARD);
        return AjaxResult.success();
    }


//    /**
//     * 将危险源关联到多个实验室
//     */
//    @PostMapping("/addRelation")
//    @ApiOperation(value = "将危险源关联到多个实验室")
//    public AjaxResult addRelation(@RequestBody LabHazardDTO labHazard) {
//        if(labHazard.getHazardId() == null && labHazard.getContainerId() == null){
//            return error("参数异常");
//        }
//        Long id = null;
//        if(labHazard.getHazardId() != null){
//            LabHazard labHazard1 = labHazardService.selectLabHazardById(labHazard.getHazardId());
//            id = labHazard1 != null ? labHazard1.getId() : null;
//        }else {
//            LabContainer labContainer = labContainerService.selectLabContainerById(labHazard.getContainerId());
//            id = labContainer != null ? labContainer.getId() : null;
//        }
//        if(id == null){
//            return error("参数无效！");
//        }
//        labHazardSubjectRelationService.addRelation(labHazard);
//        return AjaxResult.success();
//    }

    /**
     * 查询危化品下拉框
     */
    @PostMapping("/queryOption")
    @ApiOperation(value = "查询危化品下拉框")
    public AjaxResult queryOption(@RequestBody LabHazard labHazard) {
        List<LabHazardNewVo> list = labHazardService.selectLabHazardList(labHazard);
        return AjaxResult.success(list);
    }


    /**
     * 查询危化品下拉框
     */
    @GetMapping("/queryHazardOption")
    @ApiOperation(value = "查询危化品下拉框")
    public AjaxResult queryHazardOption(LabHazard labHazard) {
        List<LabHazardNewVo> list = labHazardService.selectLabHazardList(labHazard);
        return AjaxResult.success(list);
    }

//    /**
//     * 导出危险源列表
//     */
//    @PreAuthorize(hasPermi = "laboratory:hazard:export")
//    @Log(title = "危险源", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ApiOperation(value = "导出危险源列表")
//    public void export(HttpServletResponse response, LabHazard labHazard) throws IOException {
//        List<LabHazard> list = labHazardService.selectLabHazardList(labHazard);
//        ExcelUtil<LabHazard> util = new ExcelUtil<LabHazard>(LabHazard.class);
//        util.exportExcel(response, list, "危险源数据");
//    }

    /**
     * 获取危险源详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取危险源详细信息")
    public ResultData<LabHazardNewVo> getInfo(@PathVariable("id") Long id) {
        LabHazard labHazard = labHazardService.selectLabHazardById(id);
        LabHazardNewVo labHazardNewVo = new LabHazardNewVo();
        if(labHazard != null){
            BeanUtils.copyProperties(labHazard, labHazardNewVo);
        }
        return ResultData.success(labHazardNewVo);
    }

//    /**
//     * 新增危险源(压力容器)
//     */
//    @PreAuthorize(hasPermi = "laboratory:hazard:add")
//    @Log(title = "危险源", businessType = BusinessType.INSERT)
//    @PostMapping("/addContainer")
//    @ApiOperation(value = "新增危险源(压力容器)")
//    public AjaxResult addContainer(@RequestBody @Validated(ValidGroup.Crud.Create.class) LabContainer labContainer) {
//        return toAjax(labContainerService.insertLabContainer(labContainer));
//    }

    /**
     * 小程序二维码 查询数据详情 并记录count
     */
    @ApiOperation(value = "二维码 查询数据详情 并记录count")
    @GetMapping(value = "/lookUp/{id}")
    public ResultData<LabHazardNewVo> lookUp(@PathVariable("id") Long id) {
        LabHazardNewVo labHazardNewVo = new LabHazardNewVo();
        LabHazard labHazard = labHazardService.selectLabHazardById(id);
        if(labHazard != null){
            labHazard.setScanCount(labHazard.getScanCount() + 1);
            labHazardService.updateLabHazardLook(labHazard);
            BeanUtils.copyProperties(labHazard, labHazardNewVo);
        }
        return ResultData.success(labHazardNewVo);
    }

    /**
     * 新增危险源
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD + PerFun.ADD)
    @Log(title = "危险源", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增危险源")
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Crud.Create.class) LabHazard labHazard) {
        return toAjax(labHazardService.insertLabHazard(labHazard));
    }

    /**
     * 修改危险源
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD + PerFun.EDIT)
    @Log(title = "危险源", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改危险源")
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Crud.Update.class) LabHazard labHazard) {
        if(labHazard.getId() == null){
            return error("ID不能为空！");
        }
        return toAjax(labHazardService.updateLabHazard(labHazard));
    }

    /**
     * 删除危险源
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD + PerFun.REMOVE)
    @Log(title = "危险源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除危险源")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labHazardService.deleteLabHazardByIds(ids));
    }


    /**
     * 查询危险源下拉框列表（安全分级排除已有的）
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD + PerFun.LIST)
    @GetMapping("/listByHazard")
    public AjaxResult listByHazard(LabHazard labHazard) {
        return AjaxResult.success(labHazardService.listByHazard(labHazard));
    }

    /**
     * 危险源排名 （根据分配实验室统计）
     */
    @PostMapping("/queryRanking")
    @ApiOperation(value = "危险源排名")
    public AjaxResult queryRanking(@RequestBody Map<String, Object> params) {
        return AjaxResult.success(labHazardService.queryRanking(params));
    }


    /**
     * 危险源排名占比(首页)
     */
    @GetMapping("/queryHazardColumnar")
    @ApiOperation(value = "危险源排名占比")
    public AjaxResult queryHazardColumnar(LabHazard labHazard) {
        return AjaxResult.success(labHazardService.queryHazardColumnar(labHazard));
    }

    /**
     * 危险源排名占比(列表柱状图)
     */
    @GetMapping("/queryHazardColumnarTop")
    @ApiOperation(value = "危险源排名占比")
    public AjaxResult queryHazardColumnarTop(LabHazard labHazard) {
        return AjaxResult.success(labHazardService.queryHazardColumnarTop(labHazard));
    }

    /**
     * 危险源统计列表
     */
    @GetMapping("/queryHazardList")
    @ApiOperation(value = "危险源统计列表")
    public TableDataInfo queryHazardList(LabHazardVO labHazardVO) {
        startPage();
        List<LabHazardColumnarVO> list = labHazardService.queryHazardList(labHazardVO);
        return getDataTable(list);
    }



    /**
     * 危险源统计列表(大屏用)
     */
    @GetMapping("/queryBigViewHazardList")
    @ApiOperation(value = "危险源统计列表(大屏用)")
    public AjaxResult queryBigViewHazardList(LabHazardVO labHazardVO) {
        return AjaxResult.success(labHazardService.queryBigViewHazardList(labHazardVO));
    }
}
