package com.sooka.sponest.monitor.dahua.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.sooka.sponest.monitor.dahua.domain.CenternonitorTDahuaOperateLog;
import com.sooka.sponest.monitor.dahua.service.ICenternonitorTDahuaOperateLogService;
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.util.ArrayList;
import java.util.List;

/**
 * 对接大华操作日志接口Controller
 *
 * @author ruoyi
 * @date 2023-12-09
 */
@RestController
@RequestMapping("/dahuaOperateLog")
public class CenternonitorTDahuaOperateLogController extends BaseController {

    @Autowired
    private ICenternonitorTDahuaOperateLogService centernonitorTDahuaOperateLogService;

    /**
     * 查询大华操作日志列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        startPage();
        List<CenternonitorTDahuaOperateLog> list = centernonitorTDahuaOperateLogService.selectCenternonitorTDahuaOperateLogList(centernonitorTDahuaOperateLog);
        return getDataTable(list);
    }

    /**
     * 导出大华云台日志列表
     */
    @Log(title = "导出大华云台日志列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) throws IOException {
        List<CenternonitorTDahuaOperateLog> exportList = new ArrayList<>();
        Integer count = centernonitorTDahuaOperateLogService.selectExportList(centernonitorTDahuaOperateLog);
        for (int i = 1; i <= count; i++) {
            PageHelper.startPage(i, 1000);
            List<CenternonitorTDahuaOperateLog> list = centernonitorTDahuaOperateLogService.selectCenternonitorTDahuaOperateLogList(centernonitorTDahuaOperateLog);
            exportList.addAll(list);
        }
        if (exportList.size() > 65535) {
           throw new IOException("导出数据量不能超过65535条，请分批导出");
        } else {
            ExcelUtil<CenternonitorTDahuaOperateLog> util = new ExcelUtil<CenternonitorTDahuaOperateLog>(CenternonitorTDahuaOperateLog.class);
            util.exportExcel(response, exportList, "云台日志数据");
        }
    }

    /**
     * 获取大华操作日志详细信息
     */
    @GetMapping(value = "/getDetailsById")
    public AjaxResult getInfo(CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        return AjaxResult.success(centernonitorTDahuaOperateLogService.selectCenternonitorTDahuaOperateLogById(centernonitorTDahuaOperateLog));
    }

    /**
     * 新增大华操作日志
     */
    @Log(title = "新增大华操作日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        return toAjax(centernonitorTDahuaOperateLogService.insertCenternonitorTDahuaOperateLog(centernonitorTDahuaOperateLog));
    }

    /**
     * 修改大华操作日志
     */
    @Log(title = "修改大华操作日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        return toAjax(centernonitorTDahuaOperateLogService.updateCenternonitorTDahuaOperateLog(centernonitorTDahuaOperateLog));
    }

    /**
     * 删除大华操作日志
     */
    @Log(title = "删除大华操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(centernonitorTDahuaOperateLogService.deleteCenternonitorTDahuaOperateLogByIds(ids));
    }

    /**
     * 同步大华操作日志
     */
    @Log(title = "同步大华操作日志", businessType = BusinessType.DELETE)
    @GetMapping("/synchronous")
    public R<?> synchronous(@RequestParam(value = "startTime") String startTime,
                            @RequestParam(value = "endTime") String endTime,
                            @RequestParam(value = "operateTypeValue",required = false) String operateTypeValue) throws Exception {
        return centernonitorTDahuaOperateLogService.getAllLogData(startTime, endTime, operateTypeValue);
    }

    /**
     * 获取设备名称
     */
    @Log(title = "获取设备名称", businessType = BusinessType.DELETE)
    @GetMapping("/getDeviceNameList")
    public AjaxResult getDeviceNameList() {
        return AjaxResult.success(centernonitorTDahuaOperateLogService.selectDeviceNameList());
    }

    /**
     * 更新dept_id, parent_id, ancestors, dept_name
     */
    @ApiOperation(value = "清洗摄像头操作日志数据", notes = "清洗摄像头操作日志数据")
    @GetMapping("/updateMonitorFourField")
    public AjaxResult updateFourField(@RequestParam(value = "year") String year) {
        centernonitorTDahuaOperateLogService.updateFourField(year);
        return AjaxResult.success();
    }


    /**
     * 根据部门id查询市本级所有局、三县两区的摄像头总操作数，日均操作数
     */
    @ApiOperation(value = " 根据部门id查询市本级所有局、三县两区的摄像头总操作数，日均操作数", notes = " 根据部门id查询市本级所有局、三县两区的摄像头总操作数，日均操作数")
    @PostMapping("/selectOperateLogGroupByDept")
    public R selectOperateLogCountByDeptId(@RequestBody JSONObject jsonObject) {
        return R.ok(centernonitorTDahuaOperateLogService.selectOperateLogCountByDeptId(jsonObject));
    }

}
