package com.zhgd.xmgl.modules.basicdata.controller.government;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.dto.MonitorTreeDto;
import com.zhgd.xmgl.modules.basicdata.entity.Project;
import com.zhgd.xmgl.modules.basicdata.entity.MonitorDev;
import com.zhgd.xmgl.modules.basicdata.service.IMonitorDevService;
import com.zhgd.xmgl.modules.basicdata.service.IProjectService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Title: Controller
 * @Description: 视频管理表
 * @author： pds
 * @date： 2023-02-27
 * @version： V1.0
 */
@RestController
@RequestMapping("/gov/projectMonitor")
@Slf4j
@Api(tags = "视频管理")
public class GovMonitorDevController {
    @Autowired
    private IMonitorDevService monitorDevService;

    @Autowired
    private IProjectService projectService;

    /**
     * 通过id查询
     * @return
     */
    @OperLog(operModul = "项目视频管理", operType = "查询", operDesc = "通过id查询视频管理表信息")
    @ApiOperation(value = "通过id查询视频管理表信息", notes = "通过id查询视频管理表信息", httpMethod = "POST")
    @ApiImplicitParam(name = "monitorId", value = "视频管理表ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<MonitorDev> queryById(@RequestBody MonitorDev monitorDevEntity) {
        Result<MonitorDev> result = new Result<MonitorDev>();
        MonitorDev monitorDev = monitorDevService.getById(monitorDevEntity.getMonitorId());
        if (monitorDev == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(monitorDev);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 监控点树形列表
     * @return
     */
    @OperLog(operModul = "项目视频管理", operType = "查询", operDesc = "监控点树形列表")
    @ApiOperation(value = "监控点树形列表", notes = "监控点树形列表", httpMethod = "POST")
    @ApiImplicitParam(name = "projectName", value = "项目名称", paramType = "query", dataType = "String")
    @PostMapping(value = "/tree")
    public Result<List<MonitorTreeDto>> tree(@RequestBody Map<String, Object> map) {
        String projectName = MapUtils.getString(map, "projectName");
        SecurityUser user = SecurityUtil.getUser();
        LambdaQueryWrapper<Project> wrapper = Wrappers.<Project>lambdaQuery();
        wrapper.eq(Project::getGovernmentSn, user.getSn());
        if (StringUtils.isNotBlank(projectName)) {
            wrapper.like(Project::getProjectName, projectName);
        }
        List<Project> projectList = projectService.list(wrapper);
        List<MonitorTreeDto> monitorTrees = monitorDevService.getMonitorTreeByProject(projectList);
        return Result.success(monitorTrees);
    }
}
