package com.zd.chemical.controller;

import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.zd.chemical.domain.vo.HxpCabinetInclusionChemicalVo;
import com.zd.chemical.domain.vo.HxpCabinetSearch;
import com.zd.chemical.domain.vo.HxpCabinetVo;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.web.domain.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.chemical.domain.HxpCabinet;
import com.zd.chemical.service.IHxpCabinetService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;

import com.zd.common.core.web.page.TableDataInfo;

/**
 * 化学品机柜管理信息Controller
 *
 * @author liubo
 * @date 2022-06-16
 */
@RestController
@Api(tags = "【化学品机柜管理信息】")
@RequestMapping("/hxpCabinet")
public class HxpCabinetController extends BaseController {
    @Autowired
    private IHxpCabinetService hxpCabinetService;

    /**
     * 查询化学品机柜管理信息列表
     */
    @PreAuthorize(hasPermi = PerPrefix.HXP_CABINET+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询化学品机柜管理信息列表")
    public TableDataInfo<HxpCabinetVo> list(HxpCabinetSearch hxpCabinetSearch) {
        startPage();
        List<HxpCabinetVo> list = hxpCabinetService.selectHxpCabinetList(hxpCabinetSearch);
        return getDataTable(list);
    }

    /**
     * 导出化学品机柜管理信息列表
     */
    @ApiOperation(value = "导出化学品机柜管理信息列表")
    @PreAuthorize(hasPermi = PerPrefix.HXP_CABINET+ PerFun.EXPORT)
    @Log(title = "化学品机柜管理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HxpCabinetSearch hxpCabinetSearch) throws IOException {
        List<HxpCabinetVo> list = hxpCabinetService.selectHxpCabinetList(hxpCabinetSearch);
        ExcelUtil<HxpCabinetVo> util = new ExcelUtil<HxpCabinetVo>(HxpCabinetVo.class);
        util.exportExcel(response, list, "化学品机柜管理信息数据");
    }

    /**
     * 获取化学品机柜管理信息详细信息
     */
    @ApiOperation(value = "获取化学品机柜管理信息详细信息")
    @PreAuthorize(hasPermi = PerPrefix.HXP_CABINET+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<HxpCabinet> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(hxpCabinetService.selectHxpCabinetById(id));
    }

    /**
     * 新增化学品机柜管理信息
     */
    @ApiOperation(value = "新增化学品机柜管理信息")
    @PreAuthorize(hasPermi = PerPrefix.HXP_CABINET+ PerFun.ADD)
    @Log(title = "化学品机柜管理信息", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody HxpCabinet hxpCabinet) {
        return ResultData.result(hxpCabinetService.insertHxpCabinet(hxpCabinet));
    }

    /**
     * 修改化学品机柜管理信息
     */
    @ApiOperation(value = "修改化学品机柜管理信息")
    @PreAuthorize(hasPermi = PerPrefix.HXP_CABINET+ PerFun.EDIT)
    @Log(title = "化学品机柜管理信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody HxpCabinet hxpCabinet) {
        return ResultData.result(hxpCabinetService.updateHxpCabinet(hxpCabinet));
    }

    /**
     * 删除化学品机柜管理信息
     */
    @ApiOperation(value = "删除化学品机柜管理信息")
    @PreAuthorize(hasPermi = PerPrefix.HXP_CABINET+ PerFun.REMOVE)
    @Log(title = "化学品机柜管理信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        HxpCabinetSearch hxpCabinetSearch = new HxpCabinetSearch();
        hxpCabinetSearch.setId(ids[0]);
        List<HxpCabinetInclusionChemicalVo> list = hxpCabinetService.getCabinetByChemical(hxpCabinetSearch);
        if(list.size()>0){
            throw new ServiceException("删除失败，机柜已经产生化学品相关信息");
        }
        return ResultData.result(hxpCabinetService.deleteHxpCabinetByIds(ids));
    }


    /**
     * 查询化学品机柜管理信息列表
     */
    @GetMapping("/getCabinetBySubId")
    @ApiOperation(value = "查询化学品机柜管理信息列表")
    public ResultData getCabinetBySubId(HxpCabinetSearch hxpCabinetSearch) {
        List<HxpCabinetVo> list = hxpCabinetService.selectHxpCabinetList(hxpCabinetSearch);
        return ResultData.success(list);
    }


    /**
     * 查询机柜关联化学品数据统计列表
     */
    @GetMapping("/getCabinetByChemical")
    @PreAuthorize(hasPermi = PerPrefix.HXP_CABINET+ PerFun.JOINLIST)
    @ApiOperation(value = "查询化学品机柜管理信息列表")
    public TableDataInfo<HxpCabinetInclusionChemicalVo> getCabinetByChemical(HxpCabinetSearch hxpCabinetSearch) {
        startPage();
        List<HxpCabinetInclusionChemicalVo> list = hxpCabinetService.getCabinetByChemical(hxpCabinetSearch);
        return getDataTable(list);
    }


    /**
     * 查询化学品机柜管理信息列表
     */
    @RequestMapping("/getCabinetList")
    @ApiOperation(value = "查询化学品机柜管理信息列表")
    public ResultData<List<LinkedHashMap<String,String>>> getCabinetList(@RequestBody HxpCabinetSearch hxpCabinetSearch) {
        List<HxpCabinetVo> list = hxpCabinetService.selectHxpCabinetList(hxpCabinetSearch);
        List<LinkedHashMap<String,String>> cabinetList = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream()
                .map(a->{
                    LinkedHashMap<String,String> toMap = new LinkedHashMap();
                    toMap.put("id",a.getId()+"");
                    toMap.put("cabinetName",a.getCabinetName());
                    return toMap;
                })
                .collect(Collectors.toList());
        return ResultData.success(cabinetList);
    }


    /**
     * 查询化学品机柜管理信息列表(没权限的)
     */
    @GetMapping("/getNoauthList")
    @ApiOperation(value = "查询化学品机柜管理信息列表")
    public TableDataInfo<HxpCabinetVo> getNoauthList(HxpCabinetSearch hxpCabinetSearch) {
        startPage();
        List<HxpCabinetVo> list = hxpCabinetService.selectHxpCabinetList(hxpCabinetSearch);
        return getDataTable(list);
    }
}
