package com.ltgk.video.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.common.annotation.Anonymous;
import com.ltgk.common.annotation.Log;
import com.ltgk.common.constant.Constants;
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.StringUtils;
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.VideoIdentificationDTO;
import com.ltgk.video.domain.entity.DsVideo;
import com.ltgk.video.domain.entity.VideoIdentification;
import com.ltgk.video.service.IDsVideoService;
import com.ltgk.video.service.IVideoIdentificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 监控识别记录表 (VideoIdentification) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-02-20
 */
@RestController
@RequestMapping("/video/videoIdentification")
@Api(tags = "监控识别记录表")
@Slf4j
public class VideoIdentificationController extends BaseController {

    @Resource
    private IVideoIdentificationService videoIdentificationService;
    @Resource
    private IDsVideoService dsVideoService;

    /**
     * 分页查询监控识别记录表列表
     *
     * @param dto: 监控识别记录表数据传输对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询监控识别记录表列表")
    public TableDataInfo page(@RequestBody VideoIdentificationDTO dto) {
        if (StringUtils.isBlank(dto.getAnalyseType()) || Constants.VIDEO.equals(dto.getAnalyseType())) {
            List<VideoIdentification> list = videoIdentificationService.findVideoIdentificationByPage(dto);
            return getDataTable(list);
        }
        return getDataTable(new ArrayList<>());
    }

    /**
     * 分页查询卡口抓拍记录数据
     *
     * @param dto:
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025/4/2
     */
    @PostMapping("/findBayonetVideoIdentificationList")
    @ApiOperation(value = "分页查询卡口抓拍记录数据")
    public TableDataInfo findBayonetVideoIdentificationList(@RequestBody VideoIdentificationDTO dto) {
        if (StringUtils.isBlank(dto.getAnalyseType()) || Constants.VIDEO.equals(dto.getAnalyseType())) {
            List<VideoIdentification> list = videoIdentificationService.findBayonetVideoIdentificationList(dto);
            return getDataTable(list);
        }
        return getDataTable(new ArrayList<>());
    }

    /**
     * 分页查询烟火事件记录表列表
     *
     * @param dto: 烟火事件记录表数据传输对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author zhouyaoyao
     * @date 2025-02-20
     */
    @PostMapping("/findFirePage")
    @ApiOperation(value = "分页查询烟火事件记录表列表")
//    @Anonymous
    public TableDataInfo findFirePage(@RequestBody VideoIdentificationDTO dto) {
        return getDataTable(videoIdentificationService.findFirePage(dto));
    }

    /**
     * 获取监控识别记录表详细数据
     *
     * @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 VideoIdentificationDTO dto) {
        return AjaxResult.success(videoIdentificationService.getById(dto.getId()));
    }

    /**
     * 新增监控识别记录表数据
     *
     * @param videoIdentification: 监控识别记录表实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-02-20
     */
    @ApiOperation(value = "新增监控识别记录表数据")
    @Anonymous
    @PostMapping("/add")
    public AjaxResult add(VideoIdentification videoIdentification) {
//      TODO::: 此处 番禺 项目中 有 ip 地址替换成域名，此处暂时注释
//        if(org.apache.commons.lang3.StringUtils.isNotBlank(videoIdentification.getVideoUrl())){
//            String videoReplace =  videoIdentification.getVideoUrl().replace("http://120.238.185.186:2001", "https://pyzhyg.panyu.gd.cn:7673");
//            videoIdentification.setVideoUrl(videoReplace.replace("http://120.238.185.186:2002", "https://pyzhyg.panyu.gd.cn:7673"));
//        }
//        if(org.apache.commons.lang3.StringUtils.isNotBlank(videoIdentification.getPictureUrl())){
//            String pictureReplace =  videoIdentification.getPictureUrl().replace("http://120.238.185.186:2001", "https://pyzhyg.panyu.gd.cn:7673");
//            videoIdentification.setPictureUrl(pictureReplace.replace("http://120.238.185.186:2002", "https://pyzhyg.panyu.gd.cn:7673"));
//        }
        log.info("视频算法传递参数：{}", videoIdentification);
        DsVideo dsVideo = dsVideoService.lambdaQuery()
                .eq(DsVideo::getVideoIp, videoIdentification.getVideoIp())
                .last("limit 1")
                .one();

        String userName = "SYS-Admin";
        videoIdentification
                .setPortName(Objects.nonNull(dsVideo) && StringUtils.isNotBlank(dsVideo.getBelongPort()) ? dsVideo.getBelongPort() : null)
                .setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(videoIdentificationService.saveOrUpdate(videoIdentification));
    }

    /**
     * 修改监控识别记录表数据
     *
     * @param videoIdentification: 监控识别记录表实体对象
     * @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 VideoIdentification videoIdentification) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        videoIdentification.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(videoIdentificationService.updateById(videoIdentification));
    }

    /**
     * 删除监控识别记录表数据
     *
     * @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 VideoIdentificationDTO dto) {
        return toAjax(videoIdentificationService.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, VideoIdentificationDTO dto) {
        LambdaQueryWrapper<VideoIdentification> wrapper = getLambdaQueryWrapperByDTO(dto);
        startPage(dto.getPageNum(), dto.getPageSize());
        List<VideoIdentification> list = videoIdentificationService.list(wrapper);
        ExcelUtil<VideoIdentification> util = new ExcelUtil<>(VideoIdentification.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<VideoIdentification> getLambdaQueryWrapperByDTO(VideoIdentificationDTO dto) {
        dto.queryCheck();
        VideoIdentification videoIdentification = new VideoIdentification();
        BeanUtils.copyProperties(dto, videoIdentification);
        return new LambdaQueryWrapper<>(videoIdentification);
    }
}

