package com.ltgk.video.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.common.annotation.Log;
import com.ltgk.common.core.controller.BaseController;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.core.page.TableDataInfo;
import com.ltgk.common.enums.BusinessType;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.common.utils.bean.BeanUtils;
import com.ltgk.common.utils.poi.ExcelUtil;
import com.ltgk.common.validation.groups.Detail;
import com.ltgk.video.domain.dto.VideoIdentificationHistoryDTO;
import com.ltgk.video.domain.entity.VideoIdentificationHistory;
import com.ltgk.video.service.IVideoIdentificationHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 监控识别记录历史表 (VideoIdentificationHistory) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-02-20
 */
@RestController
@RequestMapping("/emergency/videoIdentificationHistory")
@Api(tags = "监控识别记录历史表")
public class VideoIdentificationHistoryController extends BaseController {

    @Resource
    private IVideoIdentificationHistoryService videoIdentificationHistoryService;

    /**
     * 分页查询监控识别记录历史表列表
     *
     * @param dto: 监控识别记录历史表数据传输对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询监控识别记录历史表列表")
    public TableDataInfo page(@RequestBody VideoIdentificationHistoryDTO dto) {
        LambdaQueryWrapper<VideoIdentificationHistory> wrapper = getLambdaQueryWrapperByDTO(dto);
        startPage(dto.getPageNum(), dto.getPageSize());
        List<VideoIdentificationHistory> list = videoIdentificationHistoryService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 获取监控识别记录历史表详细数据
     *
     * @param dto: 监控识别记录历史表数据传输对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取监控识别记录历史表详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody VideoIdentificationHistoryDTO dto) {
        return AjaxResult.success(videoIdentificationHistoryService.getById(dto.getId()));
    }

    /**
     * 新增监控识别记录历史表数据
     *
     * @param videoIdentificationHistory: 监控识别记录历史表实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @Log(title = "新增监控识别记录历史表数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增监控识别记录历史表数据")
    public AjaxResult add(@RequestBody VideoIdentificationHistory videoIdentificationHistory) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        videoIdentificationHistory.setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(videoIdentificationHistoryService.save(videoIdentificationHistory));
    }

    /**
     * 修改监控识别记录历史表数据
     *
     * @param videoIdentificationHistory: 监控识别记录历史表实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @Log(title = "修改监控识别记录历史表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改监控识别记录历史表数据")
    public AjaxResult edit(@RequestBody VideoIdentificationHistory videoIdentificationHistory) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        videoIdentificationHistory.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(videoIdentificationHistoryService.updateById(videoIdentificationHistory));
    }

    /**
     * 删除监控识别记录历史表数据
     *
     * @param dto: 监控识别记录历史表数据传输对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @Log(title = "删除监控识别记录历史表数据", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除监控识别记录历史表数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody VideoIdentificationHistoryDTO dto) {
        return toAjax(videoIdentificationHistoryService.removeById(dto.getId()));
    }

    /**
     * 导出监控识别记录历史表数据
     *
     * @param response:
     * @param dto:      监控识别记录历史表数据传输对象
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @Log(title = "导出监控识别记录历史表数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出监控识别记录历史表数据")
    public void export(HttpServletResponse response, VideoIdentificationHistoryDTO dto) {
        LambdaQueryWrapper<VideoIdentificationHistory> wrapper = getLambdaQueryWrapperByDTO(dto);
        startPage(dto.getPageNum(), dto.getPageSize());
        List<VideoIdentificationHistory> list = videoIdentificationHistoryService.list(wrapper);
        ExcelUtil<VideoIdentificationHistory> util = new ExcelUtil<>(VideoIdentificationHistory.class);
        util.exportExcel(response, list, "监控识别记录历史表数据");
    }

    /**
     * 根据 监控识别记录历史表数据传输对象 获取对应的 LambdaQueryWrapper
     *
     * @param dto: 监控识别记录历史表数据传输对象
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.ltgk.emergency.domain.entity.DsBoatCrew>
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    private static LambdaQueryWrapper<VideoIdentificationHistory> getLambdaQueryWrapperByDTO(VideoIdentificationHistoryDTO dto) {
        dto.queryCheck();
        VideoIdentificationHistory videoIdentificationHistory = new VideoIdentificationHistory();
        BeanUtils.copyProperties(dto, videoIdentificationHistory);
        return new LambdaQueryWrapper<>(videoIdentificationHistory);
    }
}

