package com.ruoyi.web.controller.warehouse.basicData;

import com.ruoyi.basicData.domain.TWarehouseWebcam;
import com.ruoyi.basicData.service.ITWarehouseWebcamService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 仓库摄像头Controller
 *
 * @author ruoyi
 * @date 2021-12-21
 */
@RestController
@RequestMapping("/warehouse/webcam")
public class TWarehouseWebcamController extends BaseController {
    @Autowired
    private ITWarehouseWebcamService tWarehouseWebcamService;

    /**
     * 查询仓库摄像头列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:webcam:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseWebcam tWarehouseWebcam) {
        startPage();
        List<TWarehouseWebcam> list = tWarehouseWebcamService.selectTWarehouseWebcamList(tWarehouseWebcam);
        return getDataTable(list);
    }

    /**
     * APP查询仓库摄像头列表
     */
    @GetMapping("/appList")
    public TableDataInfo appList(TWarehouseWebcam tWarehouseWebcam) {
        startPage();
        List<TWarehouseWebcam> list = tWarehouseWebcamService.selectAppTWarehouseWebcamList(tWarehouseWebcam);
        Map<Long, List<TWarehouseWebcam>> grouping = list.stream().collect(Collectors.groupingBy(TWarehouseWebcam::getfWarehouseId));

        List<Map<String, Object>> data = new ArrayList<>();
        grouping.forEach((k, v) -> {
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put("warehouseName", v.get(0).getfWarehouseName());
            tempMap.put("createTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, v.get(0).getCreateTime()));
            tempMap.put("webcam", v);
            data.add(tempMap);
        });

        return getDataTable(data);
    }

    /**
     * 导出仓库摄像头列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:webcam:export')")
    @Log(title = "仓库摄像头", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWarehouseWebcam tWarehouseWebcam) {
        List<TWarehouseWebcam> list = tWarehouseWebcamService.selectTWarehouseWebcamList(tWarehouseWebcam);
        ExcelUtil<TWarehouseWebcam> util = new ExcelUtil<TWarehouseWebcam>(TWarehouseWebcam.class);
        return util.exportExcel(list, "webcam");
    }

    /**
     * 获取仓库摄像头详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:webcam:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tWarehouseWebcamService.selectTWarehouseWebcamById(fId));
    }

    /**
     * 新增仓库摄像头
     */
    @Log(title = "仓库摄像头", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody TWarehouseWebcam tWarehouseWebcam) {
        if (StringUtils.isNotNull(tWarehouseWebcamService.selectTWarehouseWebcamByDeviceSerial(tWarehouseWebcam.getDeviceSerial()))) {
            return AjaxResult.error("设备序列号已存在");
        }

        return toAjax(tWarehouseWebcamService.insertTWarehouseWebcam(tWarehouseWebcam));
    }

    /**
     * 修改仓库摄像头
     */
    @PreAuthorize("@ss.hasPermi('warehouse:webcam:edit')")
    @Log(title = "仓库摄像头", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody TWarehouseWebcam tWarehouseWebcam) {
        return toAjax(tWarehouseWebcamService.updateTWarehouseWebcam(tWarehouseWebcam));
    }

    /**
     * 删除仓库摄像头
     */
    @PreAuthorize("@ss.hasPermi('warehouse:webcam:remove')")
    @Log(title = "仓库摄像头", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return toAjax(tWarehouseWebcamService.deleteTWarehouseWebcamByIds(fIds));
    }
}
